libopkg: propagate SF_NEED_DETAIL from abstract to discrete package
[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 "config.h"
20
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <unistd.h>
24
25 #include "pkg.h"
26 #include "opkg_utils.h"
27 #include "pkg_parse.h"
28 #include "pkg_depends.h"
29 #include "libbb/libbb.h"
30
31 #include "file_util.h"
32 #include "parse_util.h"
33
34 static void parse_status(pkg_t * pkg, const char *sstr)
35 {
36 char sw_str[64], sf_str[64], ss_str[64];
37
38 if (sscanf(sstr, "Status: %63s %63s %63s", sw_str, sf_str, ss_str) != 3) {
39 opkg_msg(ERROR, "Failed to parse Status line for %s\n",
40 pkg->name);
41 return;
42 }
43
44 pkg->state_want = pkg_state_want_from_str(sw_str);
45 pkg->state_flag |= pkg_state_flag_from_str(sf_str);
46 pkg->state_status = pkg_state_status_from_str(ss_str);
47 }
48
49 static void parse_conffiles(pkg_t * pkg, const char *cstr)
50 {
51 conffile_list_t *cl;
52 char file_name[1024], md5sum[85];
53
54 if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
55 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
56 pkg->name);
57 return;
58 }
59
60 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
61
62 if (cl)
63 conffile_list_append(cl, file_name, md5sum);
64 }
65
66 int parse_version(pkg_t * pkg, const char *vstr)
67 {
68 char *colon, *rev;
69
70 if (strncmp(vstr, "Version:", 8) == 0)
71 vstr += 8;
72
73 while (*vstr && isspace(*vstr))
74 vstr++;
75
76 colon = strchr(vstr, ':');
77 if (colon) {
78 errno = 0;
79 pkg_set_int(pkg, PKG_EPOCH, strtoul(vstr, NULL, 10));
80 if (errno) {
81 opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
82 }
83 vstr = ++colon;
84 }
85
86 rev = strrchr(vstr, '-');
87
88 if (rev) {
89 *rev++ = '\0';
90 pkg_set_string(pkg, PKG_REVISION, rev);
91 }
92
93 pkg_set_string(pkg, PKG_VERSION, vstr);
94
95 return 0;
96 }
97
98 static char *parse_architecture(pkg_t *pkg, const char *str)
99 {
100 const char *s = str;
101 const char *e;
102
103 while (isspace(*s))
104 s++;
105
106 e = s + strlen(s);
107
108 while (e > s && isspace(*e))
109 e--;
110
111 return pkg_set_architecture(pkg, s, e - s);
112 }
113
114 int pkg_parse_line(void *ptr, const char *line, uint mask)
115 {
116 pkg_t *pkg = (pkg_t *) ptr;
117 abstract_pkg_t *ab_pkg = NULL;
118
119 /* these flags are a bit hackish... */
120 static int reading_conffiles = 0, reading_description = 0;
121 static char *description = NULL;
122 char *s;
123 int ret = 0;
124
125 /* Exclude globally masked fields. */
126 mask |= conf->pfm;
127
128 /* Flip the semantics of the mask. */
129 mask ^= PFM_ALL;
130
131 switch (*line) {
132 case 'A':
133 if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line))
134 parse_architecture(pkg, line + strlen("Architecture") + 1);
135 else if ((mask & PFM_AUTO_INSTALLED)
136 && is_field("Auto-Installed", line)) {
137 char *tmp = parse_simple("Auto-Installed", line);
138 if (strcmp(tmp, "yes") == 0)
139 pkg->auto_installed = 1;
140 free(tmp);
141 }
142 break;
143
144 case 'C':
145 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
146 reading_conffiles = 1;
147 reading_description = 0;
148 goto dont_reset_flags;
149 } else if ((mask & PFM_CONFLICTS)
150 && is_field("Conflicts", line))
151 parse_deplist(pkg, CONFLICTS, line + strlen("Conflicts") + 1);
152 break;
153
154 case 'D':
155 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
156 description = parse_simple("Description", line);
157 reading_conffiles = 0;
158 reading_description = 1;
159 goto dont_reset_flags;
160 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
161 parse_deplist(pkg, DEPEND, line + strlen("Depends") + 1);
162 break;
163
164 case 'E':
165 if ((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
166 char *tmp = parse_simple("Essential", line);
167 if (strcmp(tmp, "yes") == 0)
168 pkg->essential = 1;
169 free(tmp);
170 }
171 break;
172
173 case 'F':
174 if ((mask & PFM_FILENAME) && is_field("Filename", line))
175 pkg_set_string(pkg, PKG_FILENAME, line + strlen("Filename") + 1);
176 break;
177
178 case 'I':
179 if ((mask & PFM_INSTALLED_SIZE)
180 && is_field("Installed-Size", line)) {
181 pkg_set_int(pkg, PKG_INSTALLED_SIZE, strtoul(line + strlen("Installed-Size") + 1, NULL, 0));
182 } else if ((mask & PFM_INSTALLED_TIME)
183 && is_field("Installed-Time", line)) {
184 pkg_set_int(pkg, PKG_INSTALLED_TIME, strtoul(line + strlen("Installed-Time") + 1, NULL, 0));
185 }
186 break;
187
188 case 'M':
189 if ((mask & PFM_MD5SUM) && (is_field("MD5sum:", line) || is_field("MD5Sum:", line)))
190 pkg_set_md5(pkg, line + strlen("MD5sum") + 1);
191 else if ((mask & PFM_MAINTAINER)
192 && is_field("Maintainer", line))
193 pkg_set_string(pkg, PKG_MAINTAINER, line + strlen("Maintainer") + 1);
194 break;
195
196 case 'P':
197 if ((mask & PFM_PACKAGE) && is_field("Package", line)) {
198 pkg->name = parse_simple("Package", line);
199 ab_pkg = abstract_pkg_fetch_by_name(pkg->name);
200
201 if (ab_pkg && (ab_pkg->state_flag & SF_NEED_DETAIL)) {
202 if (!(pkg->state_flag & SF_NEED_DETAIL)) {
203 opkg_msg(DEPEND, "propagating abpkg flag to pkg %s\n", pkg->name);
204 pkg->state_flag |= SF_NEED_DETAIL;
205 }
206 }
207 }
208 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
209 pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1);
210 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
211 parse_providelist(pkg, line + strlen("Provides") + 1);
212 else if ((mask & PFM_PRE_DEPENDS)
213 && is_field("Pre-Depends", line))
214 parse_deplist(pkg, PREDEPEND, line + strlen("Pre-Depends") + 1);
215 break;
216
217 case 'R':
218 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
219 parse_deplist(pkg, RECOMMEND, line + strlen("Recommends") + 1);
220 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
221 parse_replacelist(pkg, line + strlen("Replaces") + 1);
222 break;
223
224 case 'S':
225 if ((mask & PFM_SECTION) && is_field("Section", line))
226 pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1);
227 #ifdef HAVE_SHA256
228 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
229 pkg_set_sha256(pkg, line + strlen("SHA256sum") + 1);
230 #endif
231 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
232 pkg_set_int(pkg, PKG_SIZE, strtoul(line + strlen("Size") + 1, NULL, 0));
233 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
234 pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1);
235 else if ((mask & PFM_STATUS) && is_field("Status", line))
236 parse_status(pkg, line);
237 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
238 parse_deplist(pkg, SUGGEST, line + strlen("Suggests") + 1);
239 break;
240
241 case 'T':
242 if ((mask & PFM_TAGS) && is_field("Tags", line))
243 pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1);
244 break;
245
246 case 'V':
247 if ((mask & PFM_VERSION) && is_field("Version", line))
248 parse_version(pkg, line);
249 break;
250
251 case ' ':
252 if ((mask & PFM_DESCRIPTION) && reading_description) {
253 if (isatty(1)) {
254 description = xrealloc(description,
255 strlen(description)
256 + 1 + strlen(line) +
257 1);
258 strcat(description, "\n");
259 } else {
260 description = xrealloc(description,
261 strlen(description)
262 + 1 + strlen(line));
263 }
264 strcat(description, (line));
265 goto dont_reset_flags;
266 } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
267 parse_conffiles(pkg, line);
268 goto dont_reset_flags;
269 }
270
271 /* FALLTHROUGH */
272 default:
273 /* For package lists, signifies end of package. */
274 if (line_is_blank(line)) {
275 ret = 1;
276 break;
277 }
278 }
279
280 if (reading_description && description) {
281 pkg_set_string(pkg, PKG_DESCRIPTION, description);
282 free(description);
283 reading_description = 0;
284 description = NULL;
285 }
286
287 reading_conffiles = 0;
288
289 dont_reset_flags:
290
291 return ret;
292 }
293
294 int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask)
295 {
296 int ret;
297 char *buf;
298 const size_t len = 4096;
299
300 buf = xmalloc(len);
301 ret =
302 parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf,
303 len);
304 free(buf);
305
306 if (pkg->name == NULL) {
307 /* probably just a blank line */
308 ret = 1;
309 }
310
311 return ret;
312 }