fd5ada5534eaf14a7457979f7bb22453a3b28978
[project/opkg-lede.git] / libopkg / pkg_parse.c
1 /* pkg_parse.c - the opkg package management system
2
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4
5 Steven M. Ayer
6 Copyright (C) 2002 Compaq Computer Corporation
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17 */
18
19 #include "includes.h"
20 #include <errno.h>
21 #include <ctype.h>
22
23 #include "pkg.h"
24 #include "opkg_utils.h"
25 #include "pkg_parse.h"
26 #include "libbb/libbb.h"
27
28 static int
29 is_field(const char *type, const char *line)
30 {
31 if (!strncmp(line, type, strlen(type)))
32 return 1;
33 return 0;
34 }
35
36 static char *
37 parse_simple(const char *type, const char *line)
38 {
39 return trim_xstrdup(line + strlen(type) + 1);
40 }
41
42 /*
43 * Parse a comma separated string into an array.
44 */
45 static char **
46 parse_comma_separated(const char *raw, unsigned int *count)
47 {
48 char **depends = NULL;
49 const char *start, *end;
50 int line_count = 0;
51
52 /* skip past the "Field:" marker */
53 while (*raw && *raw != ':')
54 raw++;
55 raw++;
56
57 if (line_is_blank(raw)) {
58 *count = line_count;
59 return NULL;
60 }
61
62 while (*raw) {
63 depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
64
65 while (isspace(*raw))
66 raw++;
67
68 start = raw;
69 while (*raw != ',' && *raw)
70 raw++;
71 end = raw;
72
73 while (end > start && isspace(*end))
74 end--;
75
76 depends[line_count] = xstrndup(start, end-start);
77
78 line_count++;
79 if (*raw == ',')
80 raw++;
81 }
82
83 *count = line_count;
84 return depends;
85 }
86
87 static void
88 parse_status(pkg_t *pkg, const char *sstr)
89 {
90 char sw_str[64], sf_str[64], ss_str[64];
91
92 if (sscanf(sstr, "Status: %63s %63s %63s",
93 sw_str, sf_str, ss_str) != 3) {
94 opkg_msg(ERROR, "Failed to parse Status line for %s\n",
95 pkg->name);
96 return;
97 }
98
99 pkg->state_want = pkg_state_want_from_str(sw_str);
100 pkg->state_flag = pkg_state_flag_from_str(sf_str);
101 pkg->state_status = pkg_state_status_from_str(ss_str);
102 }
103
104 static void
105 parse_conffiles(pkg_t *pkg, const char *cstr)
106 {
107 char file_name[1024], md5sum[35];
108
109 if (sscanf(cstr, "%1023s %34s", file_name, md5sum) != 2) {
110 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
111 pkg->name);
112 return;
113 }
114
115 conffile_list_append(&pkg->conffiles, file_name, md5sum);
116 }
117
118 int
119 parse_version(pkg_t *pkg, const char *vstr)
120 {
121 char *colon;
122
123 if (strncmp(vstr, "Version:", 8) == 0)
124 vstr += 8;
125
126 while (*vstr && isspace(*vstr))
127 vstr++;
128
129 colon = strchr(vstr, ':');
130 if (colon) {
131 errno = 0;
132 pkg->epoch = strtoul(vstr, NULL, 10);
133 if (errno) {
134 opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
135 }
136 vstr = ++colon;
137 } else {
138 pkg->epoch= 0;
139 }
140
141 pkg->version= xstrdup(vstr);
142 pkg->revision = strrchr(pkg->version,'-');
143
144 if (pkg->revision)
145 *pkg->revision++ = '\0';
146
147 return 0;
148 }
149
150 static int
151 pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
152 {
153 /* these flags are a bit hackish... */
154 static int reading_conffiles = 0, reading_description = 0;
155 int ret = 0;
156
157 /* Exclude globally masked fields. */
158 mask |= conf->pfm;
159
160 /* Flip the semantics of the mask. */
161 mask ^= PFM_ALL;
162
163 switch (*line) {
164 case 'A':
165 if ((mask & PFM_ARCHITECTURE ) && is_field("Architecture", line))
166 pkg->architecture = parse_simple("Architecture", line);
167 else if ((mask & PFM_AUTO_INSTALLED) && is_field("Auto-Installed", line)) {
168 char *tmp = parse_simple("Auto-Installed", line);
169 if (strcmp(tmp, "yes") == 0)
170 pkg->auto_installed = 1;
171 free(tmp);
172 }
173 break;
174
175 case 'C':
176 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
177 reading_conffiles = 1;
178 reading_description = 0;
179 goto dont_reset_flags;
180 }
181 else if ((mask & PFM_CONFLICTS) && is_field("Conflicts", line))
182 pkg->conflicts_str = parse_comma_separated(line, &pkg->conflicts_count);
183 break;
184
185 case 'D':
186 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
187 pkg->description = parse_simple("Description", line);
188 reading_conffiles = 0;
189 reading_description = 1;
190 goto dont_reset_flags;
191 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
192 pkg->depends_str = parse_comma_separated(line, &pkg->depends_count);
193 break;
194
195 case 'E':
196 if((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
197 char *tmp = parse_simple("Essential", line);
198 if (strcmp(tmp, "yes") == 0)
199 pkg->essential = 1;
200 free(tmp);
201 }
202 break;
203
204 case 'F':
205 if((mask & PFM_FILENAME) && is_field("Filename", line))
206 pkg->filename = parse_simple("Filename", line);
207 break;
208
209 case 'I':
210 if ((mask && PFM_INSTALLED_SIZE) && is_field("Installed-Size", line)) {
211 char *tmp = parse_simple("Installed-Size", line);
212 pkg->installed_size = strtoul(tmp, NULL, 0);
213 free (tmp);
214 } else if ((mask && PFM_INSTALLED_TIME) && is_field("Installed-Time", line)) {
215 char *tmp = parse_simple("Installed-Time", line);
216 pkg->installed_time = strtoul(tmp, NULL, 0);
217 free (tmp);
218 }
219 break;
220
221 case 'M':
222 if (mask && PFM_MD5SUM) {
223 if (is_field("MD5sum:", line))
224 pkg->md5sum = parse_simple("MD5sum", line);
225 /* The old opkg wrote out status files with the wrong
226 * case for MD5sum, let's parse it either way */
227 else if (is_field("MD5Sum:", line))
228 pkg->md5sum = parse_simple("MD5Sum", line);
229 } else if((mask & PFM_MAINTAINER) && is_field("Maintainer", line))
230 pkg->maintainer = parse_simple("Maintainer", line);
231 break;
232
233 case 'P':
234 if ((mask & PFM_PACKAGE) && is_field("Package", line))
235 pkg->name = parse_simple("Package", line);
236 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
237 pkg->priority = parse_simple("Priority", line);
238 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
239 pkg->provides_str = parse_comma_separated(line, &pkg->provides_count);
240 else if ((mask & PFM_PRE_DEPENDS) && is_field("Pre-Depends", line))
241 pkg->pre_depends_str = parse_comma_separated(line, &pkg->pre_depends_count);
242 break;
243
244 case 'R':
245 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
246 pkg->recommends_str = parse_comma_separated(line, &pkg->recommends_count);
247 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
248 pkg->replaces_str = parse_comma_separated(line, &pkg->replaces_count);
249
250 break;
251
252 case 'S':
253 if ((mask & PFM_SECTION) && is_field("Section", line))
254 pkg->section = parse_simple("Section", line);
255 #ifdef HAVE_SHA256
256 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
257 pkg->sha256sum = parse_simple("SHA256sum", line);
258 #endif
259 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
260 char *tmp = parse_simple("Size", line);
261 pkg->size = strtoul(tmp, NULL, 0);
262 free (tmp);
263 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
264 pkg->source = parse_simple("Source", line);
265 else if ((mask & PFM_STATUS) && is_field("Status", line))
266 parse_status(pkg, line);
267 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
268 pkg->suggests_str = parse_comma_separated(line, &pkg->suggests_count);
269 break;
270
271 case 'T':
272 if ((mask & PFM_TAGS) && is_field("Tags", line))
273 pkg->tags = parse_simple("Tags", line);
274 break;
275
276 case 'V':
277 if ((mask & PFM_VERSION) && is_field("Version", line))
278 parse_version(pkg, line);
279 break;
280
281 case ' ':
282 if ((mask & PFM_DESCRIPTION) && reading_description) {
283 pkg->description = xrealloc(pkg->description,
284 strlen(pkg->description)
285 + 1 + strlen(line) + 1);
286 strcat(pkg->description, "\n");
287 strcat(pkg->description, (line));
288 goto dont_reset_flags;
289 } else if ((mask && PFM_CONFFILES) && reading_conffiles) {
290 parse_conffiles(pkg, line);
291 goto dont_reset_flags;
292 }
293
294 /* FALLTHROUGH */
295 default:
296 /* For package lists, signifies end of package. */
297 if(line_is_blank(line)) {
298 ret = 1;
299 break;
300 }
301 }
302
303 reading_description = 0;
304 reading_conffiles = 0;
305
306 dont_reset_flags:
307
308 return ret;
309 }
310
311 int
312 pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
313 char **buf0, size_t buf0len)
314 {
315 int ret, lineno;
316 char *buf, *nl;
317 size_t buflen;
318
319 lineno = 1;
320 ret = 0;
321
322 buflen = buf0len;
323 buf = *buf0;
324 buf[0] = '\0';
325
326 while (1) {
327 if (fgets(buf, (int)buflen, fp) == NULL) {
328 if (ferror(fp)) {
329 opkg_perror(ERROR, "fgets");
330 ret = -1;
331 } else if (strlen(*buf0) == buf0len-1) {
332 opkg_msg(ERROR, "Missing new line character"
333 " at end of file!\n");
334 pkg_parse_line(pkg, *buf0, mask);
335 }
336 break;
337 }
338
339 nl = strchr(buf, '\n');
340 if (nl == NULL) {
341 if (strlen(buf) < buflen-1) {
342 /*
343 * Line could be exactly buflen-1 long and
344 * missing a newline, but we won't know until
345 * fgets fails to read more data.
346 */
347 opkg_msg(ERROR, "Missing new line character"
348 " at end of file!\n");
349 pkg_parse_line(pkg, *buf0, mask);
350 break;
351 }
352 if (buf0len >= EXCESSIVE_LINE_LEN) {
353 opkg_msg(ERROR, "Excessively long line at "
354 "%d. Corrupt file?\n",
355 lineno);
356 ret = -1;
357 break;
358 }
359
360 /*
361 * Realloc and point buf past the data already read,
362 * at the NULL terminator inserted by fgets.
363 * |<--------------- buf0len ----------------->|
364 * | |<------- buflen ---->|
365 * |---------------------|---------------------|
366 * buf0 buf
367 */
368 buflen = buf0len +1;
369 buf0len *= 2;
370 *buf0 = xrealloc(*buf0, buf0len);
371 buf = *buf0 + buflen -2;
372
373 continue;
374 }
375
376 *nl = '\0';
377
378 lineno++;
379
380 if (pkg_parse_line(pkg, *buf0, mask))
381 break;
382
383 buf = *buf0;
384 buflen = buf0len;
385 buf[0] = '\0';
386 };
387
388 if (pkg->name == NULL) {
389 /* probably just a blank line */
390 ret = 1;
391 }
392
393 return ret;
394 }
395
396 int
397 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
398 {
399 int ret;
400 char *buf;
401 const size_t len = 4096;
402
403 buf = xmalloc(len);
404 ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);
405 free(buf);
406
407 return ret;
408 }