4eb340f4573d189fc92b10552fda256cc343db2e
[platform/upstream/libsolv.git] / ext / repo_deltainfoxml.c
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_deltainfoxml.h"
19
20 /*
21  * <deltainfo>
22  *   <newpackage name="libtool" epoch="0" version="1.5.24" release="6.fc9" arch="i386">
23  *     <delta oldepoch="0" oldversion="1.5.24" oldrelease="3.fc8">
24  *       <filename>DRPMS/libtool-1.5.24-3.fc8_1.5.24-6.fc9.i386.drpm</filename>
25  *       <sequence>libtool-1.5.24-3.fc8-d3571f98b048b1a870e40241bb46c67ab4</sequence>
26  *       <size>22452</size>
27  *       <checksum type="sha">8f05394695dee9399c204614e21e5f6848990ab7</checksum>
28  *     </delta>
29  *     <delta oldepoch="0" oldversion="1.5.22" oldrelease="11.fc7">
30  *       <filename>DRPMS/libtool-1.5.22-11.fc7_1.5.24-6.fc9.i386.drpm</filename>
31  *        <sequence>libtool-1.5.22-11.fc7-e82691677eee1e83b4812572c5c9ce8eb</sequence>
32  *        <size>110362</size>
33  *        <checksum type="sha">326658fee45c0baec1e70231046dbaf560f941ce</checksum>
34  *      </delta>
35  *    </newpackage>
36  *  </deltainfo>
37  */
38
39 enum state {
40   STATE_START,
41   STATE_NEWPACKAGE,     /* 1 */
42   STATE_DELTA,          /* 2 */
43   STATE_FILENAME,       /* 3 */
44   STATE_SEQUENCE,       /* 4 */
45   STATE_SIZE,           /* 5 */
46   STATE_CHECKSUM,       /* 6 */
47   STATE_LOCATION,       /* 7 */
48   NUMSTATES
49 };
50
51 static struct solv_xmlparser_element stateswitches[] = {
52   /* compatibility with old yum-presto */
53   { STATE_START,       "prestodelta",     STATE_START, 0 },
54   { STATE_START,       "deltainfo",       STATE_START, 0 },
55   { STATE_START,       "newpackage",      STATE_NEWPACKAGE,  0 },
56   { STATE_NEWPACKAGE,  "delta",           STATE_DELTA,       0 },
57   /* compatibility with yum-presto */
58   { STATE_DELTA,       "filename",        STATE_FILENAME,    1 },
59   { STATE_DELTA,       "location",        STATE_LOCATION,    0 },
60   { STATE_DELTA,       "sequence",        STATE_SEQUENCE,    1 },
61   { STATE_DELTA,       "size",            STATE_SIZE,        1 },
62   { STATE_DELTA,       "checksum",        STATE_CHECKSUM,    1 },
63   { NUMSTATES }
64 };
65
66 /* Cumulated info about the current deltarpm or patchrpm */
67 struct deltarpm {
68   char *location;
69   char *locbase;
70   unsigned int buildtime;
71   unsigned long long downloadsize;
72   char *filechecksum;
73   int filechecksumtype;
74   /* Baseversion.  deltarpm only has one. */
75   Id *bevr;
76   unsigned nbevr;
77   Id seqname;
78   Id seqevr;
79   char *seqnum;
80 };
81
82 struct parsedata {
83   int ret;
84   Pool *pool;
85   Repo *repo;
86   Repodata *data;
87
88   struct deltarpm delta;
89   Id newpkgevr;
90   Id newpkgname;
91   Id newpkgarch;
92
93   Id *handles;
94   int nhandles;
95
96   struct solv_xmlparser xmlp;
97 };
98
99
100 /*
101  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
102  */
103
104 static Id
105 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
106 {
107   const char *e, *v, *r, *v2;
108   char *c, *space;
109   int l;
110
111   e = v = r = 0;
112   for (; *atts; atts += 2)
113     {
114       if (!strcmp(*atts, "oldepoch"))
115         e = atts[1];
116       else if (!strcmp(*atts, "epoch"))
117         e = atts[1];
118       else if (!strcmp(*atts, "version"))
119         v = atts[1];
120       else if (!strcmp(*atts, "oldversion"))
121         v = atts[1];
122       else if (!strcmp(*atts, "release"))
123         r = atts[1];
124       else if (!strcmp(*atts, "oldrelease"))
125         r = atts[1];
126     }
127   if (e && (!*e || !strcmp(e, "0")))
128     e = 0;
129   if (v && !e)
130     {
131       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
132         ;
133       if (v2 > v && *v2 == ':')
134         e = "0";
135     }
136   l = 1;
137   if (e)
138     l += strlen(e) + 1;
139   if (v)
140     l += strlen(v);
141   if (r)
142     l += strlen(r) + 1;
143   c = space = solv_xmlparser_contentspace(&pd->xmlp, l);
144   if (e)
145     {
146       strcpy(c, e);
147       c += strlen(c);
148       *c++ = ':';
149     }
150   if (v)
151     {
152       strcpy(c, v);
153       c += strlen(c);
154     }
155   if (r)
156     {
157       *c++ = '-';
158       strcpy(c, r);
159       c += strlen(c);
160     }
161   *c = 0;
162   if (!*space)
163     return 0;
164 #if 0
165   fprintf(stderr, "evr: %s\n", space);
166 #endif
167   return pool_str2id(pool, space, 1);
168 }
169
170 static void
171 startElement(struct solv_xmlparser *xmlp, int state, const char *name, const char **atts)
172 {
173   struct parsedata *pd = xmlp->userdata;
174   Pool *pool = pd->pool;
175   const char *str;
176
177   switch(state)
178     {
179     case STATE_NEWPACKAGE:
180       if ((str = solv_xmlparser_find_attr("name", atts)) != 0)
181         pd->newpkgname = pool_str2id(pool, str, 1);
182       pd->newpkgevr = makeevr_atts(pool, pd, atts);
183       if ((str = solv_xmlparser_find_attr("arch", atts)) != 0)
184         pd->newpkgarch = pool_str2id(pool, str, 1);
185       break;
186
187     case STATE_DELTA:
188       memset(&pd->delta, 0, sizeof(pd->delta));
189       pd->delta.bevr = solv_extend(pd->delta.bevr, pd->delta.nbevr, 1, sizeof(Id), 7);
190       pd->delta.bevr[pd->delta.nbevr++] = makeevr_atts(pool, pd, atts);
191       break;
192
193     case STATE_FILENAME:
194       if ((str = solv_xmlparser_find_attr("xml:base", atts)))
195         pd->delta.locbase = solv_strdup(str);
196       break;
197
198     case STATE_LOCATION:
199       pd->delta.location = solv_strdup(solv_xmlparser_find_attr("href", atts));
200       if ((str = solv_xmlparser_find_attr("xml:base", atts)))
201         pd->delta.locbase = solv_strdup(str);
202       break;
203
204     case STATE_CHECKSUM:
205       pd->delta.filechecksum = 0;
206       pd->delta.filechecksumtype = REPOKEY_TYPE_SHA1;
207       if ((str = solv_xmlparser_find_attr("type", atts)) != 0)
208         {
209           pd->delta.filechecksumtype = solv_chksum_str2type(str);
210           if (!pd->delta.filechecksumtype)
211             pool_debug(pool, SOLV_ERROR, "unknown checksum type: '%s'\n", str);
212         }
213       break;
214
215     default:
216       break;
217     }
218 }
219
220
221 static void
222 endElement(struct solv_xmlparser *xmlp, int state, char *content)
223 {
224   struct parsedata *pd = xmlp->userdata;
225   Pool *pool = pd->pool;
226   const char *str;
227
228   switch (state)
229     {
230     case STATE_DELTA:
231       {
232         /* read all data for a deltarpm. commit into attributes */
233         Id handle;
234         struct deltarpm *d = &pd->delta;
235
236         handle = repodata_new_handle(pd->data);
237         /* we commit all handles later on in one go so that the
238          * repodata code doesn't need to realloc every time */
239         pd->handles = solv_extend(pd->handles, pd->nhandles, 1, sizeof(Id), 63);
240         pd->handles[pd->nhandles++] = handle;
241         repodata_set_id(pd->data, handle, DELTA_PACKAGE_NAME, pd->newpkgname);
242         repodata_set_id(pd->data, handle, DELTA_PACKAGE_EVR, pd->newpkgevr);
243         repodata_set_id(pd->data, handle, DELTA_PACKAGE_ARCH, pd->newpkgarch);
244         if (d->location)
245           {
246             repodata_set_deltalocation(pd->data, handle, 0, 0, d->location);
247             if (d->locbase)
248               repodata_set_poolstr(pd->data, handle, DELTA_LOCATION_BASE, d->locbase);
249           }
250         if (d->downloadsize)
251           repodata_set_num(pd->data, handle, DELTA_DOWNLOADSIZE, d->downloadsize);
252         if (d->filechecksum)
253           repodata_set_checksum(pd->data, handle, DELTA_CHECKSUM, d->filechecksumtype, d->filechecksum);
254         if (d->seqnum)
255           {
256             repodata_set_id(pd->data, handle, DELTA_BASE_EVR, d->bevr[0]);
257             repodata_set_id(pd->data, handle, DELTA_SEQ_NAME, d->seqname);
258             repodata_set_id(pd->data, handle, DELTA_SEQ_EVR, d->seqevr);
259             /* should store as binary blob! */
260             repodata_set_str(pd->data, handle, DELTA_SEQ_NUM, d->seqnum);
261           }
262       }
263       pd->delta.filechecksum = solv_free(pd->delta.filechecksum);
264       pd->delta.bevr = solv_free(pd->delta.bevr);
265       pd->delta.nbevr = 0;
266       pd->delta.seqnum = solv_free(pd->delta.seqnum);
267       pd->delta.location = solv_free(pd->delta.location);
268       pd->delta.locbase = solv_free(pd->delta.locbase);
269       break;
270     case STATE_FILENAME:
271       pd->delta.location = solv_strdup(content);
272       break;
273     case STATE_CHECKSUM:
274       pd->delta.filechecksum = solv_strdup(content);
275       break;
276     case STATE_SIZE:
277       pd->delta.downloadsize = strtoull(content, 0, 10);
278       break;
279     case STATE_SEQUENCE:
280       if ((str = content) != 0)
281         {
282           const char *s1, *s2;
283           s1 = strrchr(str, '-');
284           if (s1)
285             {
286               for (s2 = s1 - 1; s2 > str; s2--)
287                 if (*s2 == '-')
288                   break;
289               if (*s2 == '-')
290                 {
291                   for (s2 = s2 - 1; s2 > str; s2--)
292                     if (*s2 == '-')
293                       break;
294                   if (*s2 == '-')
295                     {
296                       pd->delta.seqevr = pool_strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
297                       pd->delta.seqname = pool_strn2id(pool, str, s2 - str, 1);
298                       str = s1 + 1;
299                     }
300                 }
301             }
302           pd->delta.seqnum = solv_strdup(str);
303       }
304     default:
305       break;
306     }
307 }
308
309 void
310 errorCallback(struct solv_xmlparser *xmlp, const char *errstr, unsigned int line, unsigned int column)
311 {
312   struct parsedata *pd = xmlp->userdata;
313   pd->ret = pool_error(pd->pool, -1, "repo_deltainfoxml: %s at line %u:%u", errstr, line, column);
314 }
315
316 int
317 repo_add_deltainfoxml(Repo *repo, FILE *fp, int flags)
318 {
319   Pool *pool = repo->pool;
320   Repodata *data;
321   struct parsedata pd;
322   int i;
323
324   data = repo_add_repodata(repo, flags);
325
326   memset(&pd, 0, sizeof(pd));
327   pd.pool = pool;
328   pd.repo = repo;
329   pd.data = data;
330   solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement, errorCallback);
331   solv_xmlparser_parse(&pd.xmlp, fp);
332   solv_xmlparser_free(&pd.xmlp);
333
334   /* now commit all handles */
335   if (!pd.ret)
336     for (i = 0; i < pd.nhandles; i++)
337       repodata_add_flexarray(pd.data, SOLVID_META, REPOSITORY_DELTAINFO, pd.handles[i]);
338   solv_free(pd.handles);
339
340   if (!(flags & REPO_NO_INTERNALIZE))
341     repodata_internalize(data);
342   return pd.ret;
343 }
344
345 /* EOF */