libopkg: store checksums in binary form, use integer index for architecture
[oweals/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                         size_t len;
191                         char *cksum = checksum_hex2bin(line + strlen("MD5sum") + 1, &len);
192                         if (cksum && len == 16)
193                                 pkg_set_raw(pkg, PKG_MD5SUM, cksum, len);
194                 }
195                 else if ((mask & PFM_MAINTAINER)
196                          && is_field("Maintainer", line))
197                         pkg_set_string(pkg, PKG_MAINTAINER, line + strlen("Maintainer") + 1);
198                 break;
199
200         case 'P':
201                 if ((mask & PFM_PACKAGE) && is_field("Package", line))
202                         pkg->name = parse_simple("Package", line);
203                 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
204                         pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1);
205                 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
206                         parse_providelist(pkg, line + strlen("Provides") + 1);
207                 else if ((mask & PFM_PRE_DEPENDS)
208                          && is_field("Pre-Depends", line))
209                         parse_deplist(pkg, PREDEPEND, line + strlen("Pre-Depends") + 1);
210                 break;
211
212         case 'R':
213                 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
214                         parse_deplist(pkg, RECOMMEND, line + strlen("Recommends") + 1);
215                 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
216                         parse_replacelist(pkg, line + strlen("Replaces") + 1);
217                 break;
218
219         case 'S':
220                 if ((mask & PFM_SECTION) && is_field("Section", line))
221                         pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1);
222 #ifdef HAVE_SHA256
223                 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line)) {
224                         size_t len;
225                         char *cksum = checksum_hex2bin(line + strlen("SHA256sum") + 1, &len);
226                         if (cksum && len == 32)
227                                 pkg_set_raw(pkg, PKG_SHA256SUM, cksum, len);
228                 }
229 #endif
230                 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
231                         pkg_set_int(pkg, PKG_SIZE, strtoul(line + strlen("Size") + 1, NULL, 0));
232                 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
233                         pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1);
234                 else if ((mask & PFM_STATUS) && is_field("Status", line))
235                         parse_status(pkg, line);
236                 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
237                         parse_deplist(pkg, SUGGEST, line + strlen("Suggests") + 1);
238                 break;
239
240         case 'T':
241                 if ((mask & PFM_TAGS) && is_field("Tags", line))
242                         pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1);
243                 break;
244
245         case 'V':
246                 if ((mask & PFM_VERSION) && is_field("Version", line))
247                         parse_version(pkg, line);
248                 break;
249
250         case ' ':
251                 if ((mask & PFM_DESCRIPTION) && reading_description) {
252                         if (isatty(1)) {
253                                 description = xrealloc(description,
254                                                             strlen(description)
255                                                             + 1 + strlen(line) +
256                                                             1);
257                                 strcat(description, "\n");
258                         } else {
259                                 description = xrealloc(description,
260                                                             strlen(description)
261                                                             + 1 + strlen(line));
262                         }
263                         strcat(description, (line));
264                         goto dont_reset_flags;
265                 } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
266                         parse_conffiles(pkg, line);
267                         goto dont_reset_flags;
268                 }
269
270                 /* FALLTHROUGH */
271         default:
272                 /* For package lists, signifies end of package. */
273                 if (line_is_blank(line)) {
274                         ret = 1;
275                         break;
276                 }
277         }
278
279         if (reading_description && description) {
280                 pkg_set_string(pkg, PKG_DESCRIPTION, description);
281                 free(description);
282                 reading_description = 0;
283                 description = NULL;
284         }
285
286         reading_conffiles = 0;
287
288 dont_reset_flags:
289
290         return ret;
291 }
292
293 int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask)
294 {
295         int ret;
296         char *buf;
297         const size_t len = 4096;
298
299         buf = xmalloc(len);
300         ret =
301             parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf,
302                                        len);
303         free(buf);
304
305         if (pkg->name == NULL) {
306                 /* probably just a blank line */
307                 ret = 1;
308         }
309
310         return ret;
311 }