e1d9a8456d42762587ba2b41a4c4318b2a7b8369
[oweals/opkg-lede.git] / libopkg / release_parse.c
1 /* release_parse.c - the opkg package management system
2
3    Copyright (C) 2010,2011 Javier Palacios
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2, or (at
8    your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14 */
15
16 #include "config.h"
17
18 #include <stdio.h>
19
20 #include "release.h"
21 #include "release_parse.h"
22 #include "libbb/libbb.h"
23 #include "parse_util.h"
24
25 static int
26 release_parse_line(release_t *release, const char *line)
27 {
28         int ret = 0;
29         unsigned int count = 0;
30         char **list = 0;
31         static int reading_md5sums = 0;
32 #ifdef HAVE_SHA256
33         static int reading_sha256sums = 0;
34 #endif
35
36         switch (*line) {
37         case 'A':
38                 if (is_field("Architectures", line)) {
39                         release->architectures = parse_list(line, &release->architectures_count, ' ', 0);
40                 }
41                 break;
42
43         case 'C':
44                 if (is_field("Codename", line)) {
45                         release->name = parse_simple("Codename", line);
46                 }
47                 else if (is_field("Components", line)) {
48                         release->components = parse_list(line, &release->components_count, ' ', 0);
49                 }
50                 break;
51
52         case 'D':
53                 if (is_field("Date", line)) {
54                         release->datestring = parse_simple("Date", line);
55                 }
56                 break;
57
58         case 'M':
59                 if (is_field("MD5sum", line)) {
60                         reading_md5sums = 1;
61                         if (release->md5sums == NULL) {
62                              release->md5sums = xcalloc(1, sizeof(cksum_list_t));
63                              cksum_list_init(release->md5sums);
64                         }
65                         goto dont_reset_flags;
66                 }
67                 break;
68
69 #ifdef HAVE_SHA256
70         case 'S':
71                 if (is_field("SHA256", line)) {
72                         reading_sha256sums = 1;
73                         if (release->sha256sums == NULL) {
74                              release->sha256sums = xcalloc(1, sizeof(cksum_list_t));
75                              cksum_list_init(release->sha256sums);
76                         }
77                         goto dont_reset_flags;
78                 }
79                 break;
80 #endif
81
82         case ' ':
83                 if (reading_md5sums) {
84                         list = parse_list(line, &count, ' ', 1);
85                         cksum_list_append(release->md5sums, list);
86                         goto dont_reset_flags;
87                 }
88 #ifdef HAVE_SHA256
89                 else if (reading_sha256sums) {
90                         list = parse_list(line, &count, ' ', 1);
91                         cksum_list_append(release->sha256sums, list);
92                         goto dont_reset_flags;
93                 }
94 #endif
95                 break;
96
97         default:
98                 ret = 1;
99         }
100
101         reading_md5sums = 0;
102 #ifdef HAVE_SHA256
103         reading_sha256sums = 0;
104 #endif
105
106 dont_reset_flags:
107
108         return ret;
109 }
110
111 int
112 release_parse_from_stream(release_t *release, FILE *fp)
113 {
114         int ret = 0;
115         char *buf = NULL;
116         size_t buflen, nread;
117
118         nread = getline(&buf, &buflen, fp);
119         while ( nread != -1 ) {
120                 if (buf[nread-1] == '\n') buf[nread-1] = '\0';
121                 if (release_parse_line(release, buf))
122                         opkg_msg(DEBUG, "Failed to parse release line for %s:\n\t%s\n",
123                                         release->name, buf);
124                 nread = getline(&buf, &buflen, fp);
125         }
126
127         if (!feof(fp)) {
128                 opkg_perror(ERROR, "Problems reading Release file for %sd\n", release->name);
129                 ret = -1;
130         }
131
132         free(buf);
133         return ret;
134 }
135