1 /*
2 * Copyright (c) 2007, Novell Inc.
3 *
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
6 */
7
8 #define _GNU_SOURCE
9 #include <sys/types.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "pool.h"
15 #include "repo.h"
16 #include "chksum.h"
17 #include "solv_xmlparser.h"
18 #include "repo_repomdxml.h"
19
20 /*
21 <repomd>
22
23 <!-- these tags are available in create repo > 0.9.6 -->
24 <revision>timestamp_or_arbitrary_user_supplied_string</revision>
25 <tags>
26 <content>opensuse</content>
27 <content>i386</content>
28 <content>other string</content>
29 <distro cpeid="cpe://o:opensuse_project:opensuse:11">openSUSE 11.0</distro>
30 </tags>
31 <!-- end -->
32
33 <data type="primary">
34 <location href="repodata/primary.xml.gz"/>
35 <checksum type="sha">e9162516fa25fec8d60caaf4682d2e49967786cc</checksum>
36 <timestamp>1215708444</timestamp>
37 <open-checksum type="sha">c796c48184cd5abc260e4ba929bdf01be14778a7</open-checksum>
38 </data>
39 <data type="filelists">
40 <location href="repodata/filelists.xml.gz"/>
41 <checksum type="sha">1c638295c49e9707c22810004ebb0799791fcf45</checksum>
42 <timestamp>1215708445</timestamp>
43 <open-checksum type="sha">54a40d5db3df0813b8acbe58cea616987eb9dc16</open-checksum>
44 </data>
45 <data type="other">
46 <location href="repodata/other.xml.gz"/>
47 <checksum type="sha">a81ef39eaa70e56048f8351055119d8c82af2491</checksum>
48 <timestamp>1215708447</timestamp>
49 <open-checksum type="sha">4d1ee867c8864025575a2fb8fde3b85371d51978</open-checksum>
50 </data>
51 <data type="deltainfo">
52 <location href="repodata/deltainfo.xml.gz"/>
53 <checksum type="sha">5880cfa5187026a24a552d3c0650904a44908c28</checksum>
54 <timestamp>1215708447</timestamp>
55 <open-checksum type="sha">7c964a2c3b17df5bfdd962c3be952c9ca6978d8b</open-checksum>
56 </data>
57 <data type="updateinfo">
58 <location href="repodata/updateinfo.xml.gz"/>
59 <checksum type="sha">4097f7e25c7bb0770ae31b2471a9c8c077ee904b</checksum>
60 <timestamp>1215708447</timestamp>
61 <open-checksum type="sha">24f8252f3dd041e37e7c3feb2d57e02b4422d316</open-checksum>
62 </data>
63 <data type="diskusage">
64 <location href="repodata/diskusage.xml.gz"/>
65 <checksum type="sha">4097f7e25c7bb0770ae31b2471a9c8c077ee904b</checksum>
66 <timestamp>1215708447</timestamp>
67 <open-checksum type="sha">24f8252f3dd041e37e7c3feb2d57e02b4422d316</open-checksum>
68 </data>
69 </repomd>
70
71 support also extension suseinfo format
72 <suseinfo>
73 <expire>timestamp</expire>
74 <products>
75 <id>...</id>
76 </products>
77 <kewwords>
78 <k>...</k>
79 </keywords>
80 </suseinfo>
81
82 */
83
84 enum state {
85 STATE_START,
86 /* extension tags */
87 STATE_SUSEINFO,
88 STATE_EXPIRE,
89 STATE_KEYWORDS,
90 STATE_KEYWORD,
91
92 /* normal repomd.xml */
93 STATE_REPOMD,
94 STATE_REVISION,
95 STATE_TAGS,
96 STATE_REPO,
97 STATE_CONTENT,
98 STATE_DISTRO,
99 STATE_UPDATES,
100 STATE_DATA,
101 STATE_LOCATION,
102 STATE_CHECKSUM,
103 STATE_TIMESTAMP,
104 STATE_OPENCHECKSUM,
105 STATE_SIZE,
106 NUMSTATES
107 };
108
109 static struct solv_xmlparser_element stateswitches[] = {
110 /* suseinfo tags */
111 { STATE_START, "repomd", STATE_REPOMD, 0 },
112 { STATE_START, "suseinfo", STATE_SUSEINFO, 0 },
113 /* we support the tags element in suseinfo in case
114 createrepo version does not support it yet */
115 { STATE_SUSEINFO, "tags", STATE_TAGS, 0 },
116 { STATE_SUSEINFO, "expire", STATE_EXPIRE, 1 },
117 { STATE_SUSEINFO, "keywords", STATE_KEYWORDS, 0 },
118 /* keywords is the suse extension equivalent of
119 tags/content when this one was not yet available.
120 therefore we parse both */
121 { STATE_KEYWORDS, "k", STATE_KEYWORD, 1 },
122 /* standard tags */
123 { STATE_REPOMD, "revision", STATE_REVISION, 1 },
124 { STATE_REPOMD, "tags", STATE_TAGS, 0 },
125 { STATE_REPOMD, "data", STATE_DATA, 0 },
126
127 { STATE_TAGS, "repo", STATE_REPO, 1 },
128 { STATE_TAGS, "content", STATE_CONTENT, 1 },
129 { STATE_TAGS, "distro", STATE_DISTRO, 1 },
130 /* this tag is only valid in suseinfo.xml for now */
131 { STATE_TAGS, "updates", STATE_UPDATES, 1 },
132
133 { STATE_DATA, "location", STATE_LOCATION, 0 },
134 { STATE_DATA, "checksum", STATE_CHECKSUM, 1 },
135 { STATE_DATA, "timestamp", STATE_TIMESTAMP, 1 },
136 { STATE_DATA, "open-checksum", STATE_OPENCHECKSUM, 1 },
137 { STATE_DATA, "size", STATE_SIZE, 1 },
138 { NUMSTATES }
139 };
140
141
142 struct parsedata {
143 int ret;
144 Pool *pool;
145 Repo *repo;
146 Repodata *data;
147
148 struct solv_xmlparser xmlp;
149
150 int timestamp;
151 /* handles for collection
152 structures */
153 /* repo updates */
154 Id ruhandle;
155 /* repo products */
156 Id rphandle;
157 /* repo data handle */
158 Id rdhandle;
159
160 Id chksumtype;
161 };
162
163
164 static void
startElement(struct solv_xmlparser * xmlp,int state,const char * name,const char ** atts)165 startElement(struct solv_xmlparser *xmlp, int state, const char *name, const char **atts)
166 {
167 struct parsedata *pd = xmlp->userdata;
168
169 switch(state)
170 {
171 case STATE_REPOMD:
172 {
173 const char *updstr;
174
175 /* this should be OBSOLETE soon */
176 updstr = solv_xmlparser_find_attr("updates", atts);
177 if (updstr)
178 {
179 char *value = solv_strdup(updstr);
180 char *fvalue = value; /* save the first */
181 while (value)
182 {
183 char *p = strchr(value, ',');
184 if (p)
185 *p++ = 0;
186 if (*value)
187 repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_UPDATES, value);
188 value = p;
189 }
190 solv_free(fvalue);
191 }
192 break;
193 }
194 case STATE_DISTRO:
195 {
196 /* this is extra metadata about the product this repository
197 was designed for */
198 const char *cpeid = solv_xmlparser_find_attr("cpeid", atts);
199 pd->rphandle = repodata_new_handle(pd->data);
200 /* set the cpeid for the product
201 the label is set in the content of the tag */
202 if (cpeid)
203 repodata_set_poolstr(pd->data, pd->rphandle, REPOSITORY_PRODUCT_CPEID, cpeid);
204 break;
205 }
206 case STATE_UPDATES:
207 {
208 /* this is extra metadata about the product this repository
209 was designed for */
210 const char *cpeid = solv_xmlparser_find_attr("cpeid", atts);
211 pd->ruhandle = repodata_new_handle(pd->data);
212 /* set the cpeid for the product
213 the label is set in the content of the tag */
214 if (cpeid)
215 repodata_set_poolstr(pd->data, pd->ruhandle, REPOSITORY_PRODUCT_CPEID, cpeid);
216 break;
217 }
218 case STATE_DATA:
219 {
220 const char *type= solv_xmlparser_find_attr("type", atts);
221 pd->rdhandle = repodata_new_handle(pd->data);
222 if (type)
223 repodata_set_poolstr(pd->data, pd->rdhandle, REPOSITORY_REPOMD_TYPE, type);
224 break;
225 }
226 case STATE_LOCATION:
227 {
228 const char *href = solv_xmlparser_find_attr("href", atts);
229 if (href)
230 repodata_set_str(pd->data, pd->rdhandle, REPOSITORY_REPOMD_LOCATION, href);
231 break;
232 }
233 case STATE_CHECKSUM:
234 case STATE_OPENCHECKSUM:
235 {
236 const char *type= solv_xmlparser_find_attr("type", atts);
237 pd->chksumtype = type && *type ? solv_chksum_str2type(type) : 0;
238 if (!pd->chksumtype)
239 pd->ret = pool_error(pd->pool, -1, "line %d: unknown checksum type: %s", solv_xmlparser_lineno(xmlp), type ? type : "NULL");
240 break;
241 }
242 default:
243 break;
244 }
245 return;
246 }
247
248 static void
endElement(struct solv_xmlparser * xmlp,int state,char * content)249 endElement(struct solv_xmlparser *xmlp, int state, char *content)
250 {
251 struct parsedata *pd = xmlp->userdata;
252 switch (state)
253 {
254 case STATE_REPOMD:
255 if (pd->timestamp > 0)
256 repodata_set_num(pd->data, SOLVID_META, REPOSITORY_TIMESTAMP, pd->timestamp);
257 break;
258 case STATE_DATA:
259 if (pd->rdhandle)
260 repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_REPOMD, pd->rdhandle);
261 pd->rdhandle = 0;
262 break;
263
264 case STATE_CHECKSUM:
265 case STATE_OPENCHECKSUM:
266 if (!pd->chksumtype)
267 break;
268 if (strlen(content) != 2 * solv_chksum_len(pd->chksumtype))
269 pd->ret = pool_error(pd->pool, -1, "line %d: invalid checksum length for %s", solv_xmlparser_lineno(xmlp), solv_chksum_type2str(pd->chksumtype));
270 else
271 repodata_set_checksum(pd->data, pd->rdhandle, state == STATE_CHECKSUM ? REPOSITORY_REPOMD_CHECKSUM : REPOSITORY_REPOMD_OPENCHECKSUM, pd->chksumtype, content);
272 break;
273
274 case STATE_TIMESTAMP:
275 {
276 /**
277 * we want to look for the newest timestamp
278 * of all resources to save it as the time
279 * the metadata was generated
280 */
281 int timestamp = atoi(content);
282 if (timestamp)
283 repodata_set_num(pd->data, pd->rdhandle, REPOSITORY_REPOMD_TIMESTAMP, timestamp);
284 if (timestamp > pd->timestamp)
285 pd->timestamp = timestamp;
286 break;
287 }
288 case STATE_EXPIRE:
289 {
290 int expire = atoi(content);
291 if (expire > 0)
292 repodata_set_num(pd->data, SOLVID_META, REPOSITORY_EXPIRE, expire);
293 break;
294 }
295 /* repomd.xml content and suseinfo.xml keywords are equivalent */
296 case STATE_CONTENT:
297 case STATE_KEYWORD:
298 if (*content)
299 repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_KEYWORDS, content);
300 break;
301 case STATE_REVISION:
302 if (*content)
303 repodata_set_str(pd->data, SOLVID_META, REPOSITORY_REVISION, content);
304 break;
305 case STATE_DISTRO:
306 /* distro tag is used in repomd.xml to say the product this repo is
307 made for */
308 if (*content)
309 repodata_set_str(pd->data, pd->rphandle, REPOSITORY_PRODUCT_LABEL, content);
310 repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_DISTROS, pd->rphandle);
311 break;
312 case STATE_UPDATES:
313 /* updates tag is used in suseinfo.xml to say the repo updates a product
314 however it s not yet a tag standarized for repomd.xml */
315 if (*content)
316 repodata_set_str(pd->data, pd->ruhandle, REPOSITORY_PRODUCT_LABEL, content);
317 repodata_add_flexarray(pd->data, SOLVID_META, REPOSITORY_UPDATES, pd->ruhandle);
318 break;
319 case STATE_REPO:
320 if (*content)
321 repodata_add_poolstr_array(pd->data, SOLVID_META, REPOSITORY_REPOID, content);
322 break;
323 case STATE_SIZE:
324 if (*content)
325 repodata_set_num(pd->data, pd->rdhandle, REPOSITORY_REPOMD_SIZE, strtoull(content, 0, 10));
326 break;
327 default:
328 break;
329 }
330 }
331
332 int
repo_add_repomdxml(Repo * repo,FILE * fp,int flags)333 repo_add_repomdxml(Repo *repo, FILE *fp, int flags)
334 {
335 Pool *pool = repo->pool;
336 struct parsedata pd;
337 Repodata *data;
338
339 data = repo_add_repodata(repo, flags);
340
341 memset(&pd, 0, sizeof(pd));
342 pd.timestamp = 0;
343 pd.pool = pool;
344 pd.repo = repo;
345 pd.data = data;
346 solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement);
347 if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK)
348 pd.ret = pool_error(pd.pool, -1, "repo_repomdxml: %s at line %u:%u", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column);
349 solv_xmlparser_free(&pd.xmlp);
350
351 if (!(flags & REPO_NO_INTERNALIZE))
352 repodata_internalize(data);
353
354 return pd.ret;
355 }
356
357 /* EOF */
358