implement update to specific update targets
[platform/upstream/libsolv.git] / examples / solv.c
1 /*
2  * Copyright (c) 2009, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /* solv, a little software installer demoing the sat solver library */
9
10 /* things it does:
11  * - understands globs for package names / dependencies
12  * - understands .arch suffix
13  * - installation of commandline packages
14  * - repository data caching
15  * - on demand loading of secondary repository data
16  * - gpg and checksum verification
17  * - file conflicts
18  * - deltarpm support
19  * - fastestmirror implementation
20  *
21  * things available in the library but missing from solv:
22  * - vendor policy loading
23  * - soft locks file handling
24  * - multi version handling
25  */
26
27 #define _GNU_SOURCE
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <dirent.h>
32 #include <fnmatch.h>
33 #include <unistd.h>
34 #include <zlib.h>
35 #include <fcntl.h>
36 #include <assert.h>
37 #include <sys/utsname.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <time.h>
41 #include <sys/time.h>
42 #include <sys/dir.h>
43 #include <sys/stat.h>
44
45 #include <sys/socket.h>
46 #include <netdb.h>
47 #include <poll.h>
48 #include <errno.h>
49
50 #include "pool.h"
51 #include "poolarch.h"
52 #include "repo.h"
53 #include "evr.h"
54 #include "policy.h"
55 #include "util.h"
56 #include "solver.h"
57 #include "solverdebug.h"
58 #include "chksum.h"
59 #include "repo_solv.h"
60 #include "selection.h"
61
62 #include "repo_write.h"
63 #ifdef ENABLE_RPMDB
64 #include "repo_rpmdb.h"
65 #include "pool_fileconflicts.h"
66 #endif
67 #ifdef ENABLE_DEBIAN
68 #include "repo_deb.h"
69 #endif
70 #ifdef ENABLE_RPMMD
71 #include "repo_rpmmd.h"
72 #include "repo_repomdxml.h"
73 #include "repo_updateinfoxml.h"
74 #include "repo_deltainfoxml.h"
75 #endif
76 #ifdef ENABLE_SUSEREPO
77 #include "repo_products.h"
78 #include "repo_susetags.h"
79 #include "repo_content.h"
80 #endif
81 #include "solv_xfopen.h"
82
83 #ifdef FEDORA
84 # define REPOINFO_PATH "/etc/yum.repos.d"
85 #endif
86 #ifdef SUSE
87 # define REPOINFO_PATH "/etc/zypp/repos.d"
88 # define PRODUCTS_PATH "/etc/products.d"
89 # define SOFTLOCKS_PATH "/var/lib/zypp/SoftLocks"
90 #endif
91
92 #define SOLVCACHE_PATH "/var/cache/solv"
93
94 #define METADATA_EXPIRE (60 * 15)
95
96 struct repoinfo {
97   Repo *repo;
98
99   char *alias;
100   char *name;
101   int enabled;
102   int autorefresh;
103   char *baseurl;
104   char *metalink;
105   char *mirrorlist;
106   char *path;
107   int type;
108   int pkgs_gpgcheck;
109   int repo_gpgcheck;
110   int priority;
111   int keeppackages;
112   int metadata_expire;
113   char **components;
114   int ncomponents;
115
116   unsigned char cookie[32];
117   unsigned char extcookie[32];
118 };
119
120 #ifdef FEDORA
121 char *
122 yum_substitute(Pool *pool, char *line)
123 {
124   char *p, *p2;
125   static char *releaseevr;
126   static char *basearch;
127
128   if (!line)
129     {
130       solv_free(releaseevr);
131       releaseevr = 0;
132       solv_free(basearch);
133       basearch = 0;
134       return 0;
135     }
136   p = line;
137   while ((p2 = strchr(p, '$')) != 0)
138     {
139       if (!strncmp(p2, "$releasever", 11))
140         {
141           if (!releaseevr)
142             {
143               Queue q;
144               const char *rootdir = pool_get_rootdir(pool);
145         
146               queue_init(&q);
147               rpm_installedrpmdbids(rootdir, "Providename", "redhat-release", &q);
148               if (q.count)
149                 {
150                   void *handle, *state = 0;
151                   char *p;
152                   handle = rpm_byrpmdbid(q.elements[0], rootdir, &state);
153                   releaseevr = rpm_query(handle, SOLVABLE_EVR);
154                   rpm_byrpmdbid(0, 0, &state);
155                   if ((p = strchr(releaseevr, '-')) != 0)
156                     *p = 0;
157                 }
158               queue_free(&q);
159               if (!releaseevr)
160                 {
161                   fprintf(stderr, "no installed package provides 'redhat-release', cannot determine $releasever\n");
162                   exit(1);
163                 }
164             }
165           *p2 = 0;
166           p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
167           p2 = p + (p2 - line);
168           line = p;
169           p = p2 + strlen(releaseevr);
170           continue;
171         }
172       if (!strncmp(p2, "$basearch", 9))
173         {
174           if (!basearch)
175             {
176               struct utsname un;
177               if (uname(&un))
178                 {
179                   perror("uname");
180                   exit(1);
181                 }
182               basearch = strdup(un.machine);
183               if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
184                 basearch[1] = '3';
185             }
186           *p2 = 0;
187           p = pool_tmpjoin(pool, line, basearch, p2 + 9);
188           p2 = p + (p2 - line);
189           line = p;
190           p = p2 + strlen(basearch);
191           continue;
192         }
193       p = p2 + 1;
194     }
195   return line;
196 }
197 #endif
198
199 #define TYPE_UNKNOWN    0
200 #define TYPE_SUSETAGS   1
201 #define TYPE_RPMMD      2
202 #define TYPE_PLAINDIR   3
203 #define TYPE_DEBIAN     4
204
205 #ifndef NOSYSTEM
206 static int
207 read_repoinfos_sort(const void *ap, const void *bp)
208 {
209   const struct repoinfo *a = ap;
210   const struct repoinfo *b = bp;
211   return strcmp(a->alias, b->alias);
212 }
213 #endif
214
215 #if defined(SUSE) || defined(FEDORA)
216
217 struct repoinfo *
218 read_repoinfos(Pool *pool, int *nrepoinfosp)
219 {
220   const char *reposdir = REPOINFO_PATH;
221   char buf[4096];
222   char buf2[4096], *kp, *vp, *kpe;
223   DIR *dir;
224   FILE *fp;
225   struct dirent *ent;
226   int l, rdlen;
227   struct repoinfo *repoinfos = 0, *cinfo;
228   int nrepoinfos = 0;
229
230   rdlen = strlen(reposdir);
231   dir = opendir(reposdir);
232   if (!dir)
233     {
234       *nrepoinfosp = 0;
235       return 0;
236     }
237   while ((ent = readdir(dir)) != 0)
238     {
239       if (ent->d_name[0] == '.')
240         continue;
241       l = strlen(ent->d_name);
242       if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
243         continue;
244       snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
245       if ((fp = fopen(buf, "r")) == 0)
246         {
247           perror(buf);
248           continue;
249         }
250       cinfo = 0;
251       while(fgets(buf2, sizeof(buf2), fp))
252         {
253           l = strlen(buf2);
254           if (l == 0)
255             continue;
256           while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
257             buf2[--l] = 0;
258           kp = buf2;
259           while (*kp == ' ' || *kp == '\t')
260             kp++;
261           if (!*kp || *kp == '#')
262             continue;
263 #ifdef FEDORA
264           if (strchr(kp, '$'))
265             kp = yum_substitute(pool, kp);
266 #endif
267           if (*kp == '[')
268             {
269               vp = strrchr(kp, ']');
270               if (!vp)
271                 continue;
272               *vp = 0;
273               repoinfos = solv_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
274               cinfo = repoinfos + nrepoinfos++;
275               memset(cinfo, 0, sizeof(*cinfo));
276               cinfo->alias = strdup(kp + 1);
277               cinfo->type = TYPE_RPMMD;
278               cinfo->autorefresh = 1;
279               cinfo->priority = 99;
280 #ifndef FEDORA
281               cinfo->repo_gpgcheck = 1;
282 #endif
283               cinfo->metadata_expire = METADATA_EXPIRE;
284               continue;
285             }
286           if (!cinfo)
287             continue;
288           vp = strchr(kp, '=');
289           if (!vp)
290             continue;
291           for (kpe = vp - 1; kpe >= kp; kpe--)
292             if (*kpe != ' ' && *kpe != '\t')
293               break;
294           if (kpe == kp)
295             continue;
296           vp++;
297           while (*vp == ' ' || *vp == '\t')
298             vp++;
299           kpe[1] = 0;
300           if (!strcmp(kp, "name"))
301             cinfo->name = strdup(vp);
302           else if (!strcmp(kp, "enabled"))
303             cinfo->enabled = *vp == '0' ? 0 : 1;
304           else if (!strcmp(kp, "autorefresh"))
305             cinfo->autorefresh = *vp == '0' ? 0 : 1;
306           else if (!strcmp(kp, "gpgcheck"))
307             cinfo->pkgs_gpgcheck = *vp == '0' ? 0 : 1;
308           else if (!strcmp(kp, "repo_gpgcheck"))
309             cinfo->repo_gpgcheck = *vp == '0' ? 0 : 1;
310           else if (!strcmp(kp, "baseurl"))
311             cinfo->baseurl = strdup(vp);
312           else if (!strcmp(kp, "mirrorlist"))
313             {
314               if (strstr(vp, "metalink"))
315                 cinfo->metalink = strdup(vp);
316               else
317                 cinfo->mirrorlist = strdup(vp);
318             }
319           else if (!strcmp(kp, "path"))
320             {
321               if (vp && strcmp(vp, "/") != 0)
322                 cinfo->path = strdup(vp);
323             }
324           else if (!strcmp(kp, "type"))
325             {
326               if (!strcmp(vp, "yast2"))
327                 cinfo->type = TYPE_SUSETAGS;
328               else if (!strcmp(vp, "rpm-md"))
329                 cinfo->type = TYPE_RPMMD;
330               else if (!strcmp(vp, "plaindir"))
331                 cinfo->type = TYPE_PLAINDIR;
332               else
333                 cinfo->type = TYPE_UNKNOWN;
334             }
335           else if (!strcmp(kp, "priority"))
336             cinfo->priority = atoi(vp);
337           else if (!strcmp(kp, "keeppackages"))
338             cinfo->keeppackages = *vp == '0' ? 0 : 1;
339         }
340       fclose(fp);
341       cinfo = 0;
342     }
343   closedir(dir);
344   qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
345   *nrepoinfosp = nrepoinfos;
346   return repoinfos;
347 }
348
349 #endif
350
351 #ifdef DEBIAN
352
353 struct repoinfo *
354 read_repoinfos(Pool *pool, int *nrepoinfosp)
355 {
356   FILE *fp;
357   char buf[4096];
358   char buf2[4096];
359   int l;
360   char *kp, *url, *distro;
361   struct repoinfo *repoinfos = 0, *cinfo;
362   int nrepoinfos = 0;
363   DIR *dir = 0;
364   struct dirent *ent;
365
366   fp = fopen("/etc/apt/sources.list", "r");
367   while (1)
368     {
369       if (!fp)
370         {
371           if (!dir)
372             {
373               dir = opendir("/etc/apt/sources.list.d");
374               if (!dir)
375                 break;
376             }
377           if ((ent = readdir(dir)) == 0)
378             {
379               closedir(dir);
380               break;
381             }
382           if (ent->d_name[0] == '.')
383             continue;
384           l = strlen(ent->d_name);
385           if (l < 5 || strcmp(ent->d_name + l - 5, ".list") != 0)
386             continue;
387           snprintf(buf, sizeof(buf), "%s/%s", "/etc/apt/sources.list.d", ent->d_name);
388           if (!(fp = fopen(buf, "r")))
389             continue;
390         }
391       while(fgets(buf2, sizeof(buf2), fp))
392         {
393           l = strlen(buf2);
394           if (l == 0)
395             continue;
396           while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
397             buf2[--l] = 0;
398           kp = buf2;
399           while (*kp == ' ' || *kp == '\t')
400             kp++;
401           if (!*kp || *kp == '#')
402             continue;
403           if (strncmp(kp, "deb", 3) != 0)
404             continue;
405           kp += 3;
406           if (*kp != ' ' && *kp != '\t')
407             continue;
408           while (*kp == ' ' || *kp == '\t')
409             kp++;
410           if (!*kp)
411             continue;
412           url = kp;
413           while (*kp && *kp != ' ' && *kp != '\t')
414             kp++;
415           if (*kp)
416             *kp++ = 0;
417           while (*kp == ' ' || *kp == '\t')
418             kp++;
419           if (!*kp)
420             continue;
421           distro = kp;
422           while (*kp && *kp != ' ' && *kp != '\t')
423             kp++;
424           if (*kp)
425             *kp++ = 0;
426           while (*kp == ' ' || *kp == '\t')
427             kp++;
428           if (!*kp)
429             continue;
430           repoinfos = solv_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
431           cinfo = repoinfos + nrepoinfos++;
432           memset(cinfo, 0, sizeof(*cinfo));
433           cinfo->baseurl = strdup(url);
434           cinfo->alias = solv_dupjoin(url, "/", distro);
435           cinfo->name = strdup(distro);
436           cinfo->type = TYPE_DEBIAN;
437           cinfo->enabled = 1;
438           cinfo->autorefresh = 1;
439           cinfo->repo_gpgcheck = 1;
440           cinfo->metadata_expire = METADATA_EXPIRE;
441           while (*kp)
442             {
443               char *compo;
444               while (*kp == ' ' || *kp == '\t')
445                 kp++;
446               if (!*kp)
447                 break;
448               compo = kp;
449               while (*kp && *kp != ' ' && *kp != '\t')
450                 kp++;
451               if (*kp)
452                 *kp++ = 0;
453               cinfo->components = solv_extend(cinfo->components, cinfo->ncomponents, 1, sizeof(*cinfo->components), 15);
454               cinfo->components[cinfo->ncomponents++] = strdup(compo);
455             }
456         }
457       fclose(fp);
458       fp = 0;
459     }
460   qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
461   *nrepoinfosp = nrepoinfos;
462   return repoinfos;
463 }
464
465 #endif
466
467 #ifdef NOSYSTEM
468 struct repoinfo *
469 read_repoinfos(Pool *pool, int *nrepoinfosp)
470 {
471   *nrepoinfosp = 0;
472   return 0;
473 }
474 #endif
475
476
477 void
478 free_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
479 {
480   int i, j;
481   for (i = 0; i < nrepoinfos; i++)
482     {
483       struct repoinfo *cinfo = repoinfos + i;
484       solv_free(cinfo->name);
485       solv_free(cinfo->alias);
486       solv_free(cinfo->path);
487       solv_free(cinfo->metalink);
488       solv_free(cinfo->mirrorlist);
489       solv_free(cinfo->baseurl);
490       for (j = 0; j < cinfo->ncomponents; j++)
491         solv_free(cinfo->components[j]);
492       solv_free(cinfo->components);
493     }
494   solv_free(repoinfos);
495 }
496
497 static inline int
498 opentmpfile()
499 {
500   char tmpl[100];
501   int fd;
502
503   strcpy(tmpl, "/var/tmp/solvXXXXXX");
504   fd = mkstemp(tmpl);
505   if (fd < 0)
506     {
507       perror("mkstemp");
508       exit(1);
509     }
510   unlink(tmpl);
511   return fd;
512 }
513
514 static int
515 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
516 {
517   char buf[1024];
518   const unsigned char *sum;
519   void *h;
520   int l;
521
522   h = solv_chksum_create(chksumtype);
523   if (!h)
524     {
525       printf("%s: unknown checksum type\n", file);
526       return 0;
527     }
528   while ((l = read(fd, buf, sizeof(buf))) > 0)
529     solv_chksum_add(h, buf, l);
530   lseek(fd, 0, SEEK_SET);
531   l = 0;
532   sum = solv_chksum_get(h, &l);
533   if (memcmp(sum, chksum, l))
534     {
535       printf("%s: checksum mismatch\n", file);
536       solv_chksum_free(h, 0);
537       return 0;
538     }
539   solv_chksum_free(h, 0);
540   return 1;
541 }
542
543 void
544 findfastest(char **urls, int nurls)
545 {
546   int i, j, port;
547   int *socks, qc;
548   struct pollfd *fds;
549   char *p, *p2, *q;
550   char portstr[16];
551   struct addrinfo hints, *result;;
552
553   fds = solv_calloc(nurls, sizeof(*fds));
554   socks = solv_calloc(nurls, sizeof(*socks));
555   for (i = 0; i < nurls; i++)
556     {
557       socks[i] = -1;
558       p = strchr(urls[i], '/');
559       if (!p)
560         continue;
561       if (p[1] != '/')
562         continue;
563       p += 2;
564       q = strchr(p, '/');
565       qc = 0;
566       if (q)
567         {
568           qc = *q;
569           *q = 0;
570         }
571       if ((p2 = strchr(p, '@')) != 0)
572         p = p2 + 1;
573       port = 80;
574       if (!strncmp("https:", urls[i], 6))
575         port = 443;
576       else if (!strncmp("ftp:", urls[i], 4))
577         port = 21;
578       if ((p2 = strrchr(p, ':')) != 0)
579         {
580           port = atoi(p2 + 1);
581           if (q)
582             *q = qc;
583           q = p2;
584           qc = *q;
585           *q = 0;
586         }
587       sprintf(portstr, "%d", port);
588       memset(&hints, 0, sizeof(struct addrinfo));
589       hints.ai_family = AF_UNSPEC;
590       hints.ai_socktype = SOCK_STREAM;
591       hints.ai_flags = AI_NUMERICSERV;
592       result = 0;
593       if (!getaddrinfo(p, portstr, &hints, &result))
594         {
595           socks[i] = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
596           if (socks[i] >= 0)
597             {
598               fcntl(socks[i], F_SETFL, O_NONBLOCK);
599               if (connect(socks[i], result->ai_addr, result->ai_addrlen) == -1)
600                 {
601                   if (errno != EINPROGRESS)
602                     {
603                       close(socks[i]);
604                       socks[i] = -1;
605                     }
606                 }
607             }
608           freeaddrinfo(result);
609         }
610       if (q)
611         *q = qc;
612     }
613   for (;;)
614     {
615       for (i = j = 0; i < nurls; i++)
616         {
617           if (socks[i] < 0)
618             continue;
619           fds[j].fd = socks[i];
620           fds[j].events = POLLOUT;
621           j++;
622         }
623       if (j < 2)
624         {
625           i = j - 1;
626           break;
627         }
628       if (poll(fds, j, 10000) <= 0)
629         {
630           i = -1;       /* something is wrong */
631           break;
632         }
633       for (i = 0; i < j; i++)
634         if ((fds[i].revents & POLLOUT) != 0)
635           {
636             int soe = 0;
637             socklen_t soel = sizeof(int);
638             if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, &soe, &soel) == -1 || soe != 0)
639               {
640                 /* connect failed, kill socket */
641                 for (j = 0; j < nurls; j++)
642                   if (socks[j] == fds[i].fd)
643                     {
644                       close(socks[j]);
645                       socks[j] = -1;
646                     }
647                 i = j + 1;
648                 break;
649               }
650             break;      /* horray! */
651           }
652       if (i == j + 1)
653         continue;
654       if (i == j)
655         i = -1;         /* something is wrong, no bit was set */
656       break;
657     }
658   /* now i contains the fastest fd index */
659   if (i >= 0)
660     {
661       for (j = 0; j < nurls; j++)
662         if (socks[j] == fds[i].fd)
663           break;
664       if (j != 0)
665         {
666           char *url0 = urls[0];
667           urls[0] = urls[j];
668           urls[j] = url0;
669         }
670     }
671   for (i = j = 0; i < nurls; i++)
672     if (socks[i] >= 0)
673       close(socks[i]);
674   free(socks);
675   free(fds);
676 }
677
678 char *
679 findmetalinkurl(FILE *fp, unsigned char *chksump, Id *chksumtypep)
680 {
681   char buf[4096], *bp, *ep;
682   char **urls = 0;
683   int nurls = 0;
684   int i;
685
686   if (chksumtypep)
687     *chksumtypep = 0;
688   while((bp = fgets(buf, sizeof(buf), fp)) != 0)
689     {
690       while (*bp == ' ' || *bp == '\t')
691         bp++;
692       if (chksumtypep && !*chksumtypep && !strncmp(bp, "<hash type=\"sha256\">", 20))
693         {
694           bp += 20;
695           if (solv_hex2bin((const char **)&bp, chksump, 32) == 32)
696             *chksumtypep = REPOKEY_TYPE_SHA256;
697           continue;
698         }
699       if (strncmp(bp, "<url", 4))
700         continue;
701       bp = strchr(bp, '>');
702       if (!bp)
703         continue;
704       bp++;
705       ep = strstr(bp, "repodata/repomd.xml</url>");
706       if (!ep)
707         continue;
708       *ep = 0;
709       if (strncmp(bp, "http", 4))
710         continue;
711       urls = solv_extend(urls, nurls, 1, sizeof(*urls), 15);
712       urls[nurls++] = strdup(bp);
713     }
714   if (nurls)
715     {
716       if (nurls > 1)
717         findfastest(urls, nurls > 5 ? 5 : nurls);
718       bp = urls[0];
719       urls[0] = 0;
720       for (i = 0; i < nurls; i++)
721         solv_free(urls[i]);
722       solv_free(urls);
723       ep = strchr(bp, '/');
724       if ((ep = strchr(ep + 2, '/')) != 0)
725         {
726           *ep = 0;
727           printf("[using mirror %s]\n", bp);
728           *ep = '/';
729         }
730       return bp;
731     }
732   return 0;
733 }
734
735 char *
736 findmirrorlisturl(FILE *fp)
737 {
738   char buf[4096], *bp, *ep;
739   int i, l;
740   char **urls = 0;
741   int nurls = 0;
742
743   while((bp = fgets(buf, sizeof(buf), fp)) != 0)
744     {
745       while (*bp == ' ' || *bp == '\t')
746         bp++;
747       if (!*bp || *bp == '#')
748         continue;
749       l = strlen(bp);
750       while (l > 0 && (bp[l - 1] == ' ' || bp[l - 1] == '\t' || bp[l - 1] == '\n'))
751         bp[--l] = 0;
752       urls = solv_extend(urls, nurls, 1, sizeof(*urls), 15);
753       urls[nurls++] = strdup(bp);
754     }
755   if (nurls)
756     {
757       if (nurls > 1)
758         findfastest(urls, nurls > 5 ? 5 : nurls);
759       bp = urls[0];
760       urls[0] = 0;
761       for (i = 0; i < nurls; i++)
762         solv_free(urls[i]);
763       solv_free(urls);
764       ep = strchr(bp, '/');
765       if ((ep = strchr(ep + 2, '/')) != 0)
766         {
767           *ep = 0;
768           printf("[using mirror %s]\n", bp);
769           *ep = '/';
770         }
771       return bp;
772     }
773   return 0;
774 }
775
776 static inline int
777 iscompressed(const char *name)
778 {
779   return solv_xfopen_iscompressed(name) != 0;
780 }
781
782 FILE *
783 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype, int *badchecksump)
784 {
785   FILE *fp;
786   pid_t pid;
787   int fd, l;
788   int status;
789   char url[4096];
790   const char *baseurl = cinfo->baseurl;
791
792   if (!baseurl)
793     {
794       if (!cinfo->metalink && !cinfo->mirrorlist)
795         return 0;
796       if (file != cinfo->metalink && file != cinfo->mirrorlist)
797         {
798           unsigned char mlchksum[32];
799           fp = curlfopen(cinfo, cinfo->metalink ? cinfo->metalink : cinfo->mirrorlist, 0, 0, 0, 0);
800           Id mlchksumtype = 0;
801           if (!fp)
802             return 0;
803           if (cinfo->metalink)
804             cinfo->baseurl = findmetalinkurl(fp, mlchksum, &mlchksumtype);
805           else
806             cinfo->baseurl = findmirrorlisturl(fp);
807           fclose(fp);
808           if (!cinfo->baseurl)
809             return 0;
810 #ifdef FEDORA
811           if (strchr(cinfo->baseurl, '$'))
812             {
813               char *b = yum_substitute(cinfo->repo->pool, cinfo->baseurl);
814               free(cinfo->baseurl);
815               cinfo->baseurl = strdup(b);
816             }
817 #endif
818           if (!chksumtype && mlchksumtype && !strcmp(file, "repodata/repomd.xml"))
819             {
820               chksumtype = mlchksumtype;
821               chksum = mlchksum;
822             }
823           return curlfopen(cinfo, file, uncompress, chksum, chksumtype, badchecksump);
824         }
825       snprintf(url, sizeof(url), "%s", file);
826     }
827   else
828     {
829       l = strlen(baseurl);
830       if (l && baseurl[l - 1] == '/')
831         snprintf(url, sizeof(url), "%s%s", baseurl, file);
832       else
833         snprintf(url, sizeof(url), "%s/%s", baseurl, file);
834     }
835   fd = opentmpfile();
836   // printf("url: %s\n", url);
837   if ((pid = fork()) == (pid_t)-1)
838     {
839       perror("fork");
840       exit(1);
841     }
842   if (pid == 0)
843     {
844       if (fd != 1)
845         {
846           dup2(fd, 1);
847           close(fd);
848         }
849       execlp("curl", "curl", "-f", "-s", "-L", url, (char *)0);
850       perror("curl");
851       _exit(0);
852     }
853   status = 0;
854   while (waitpid(pid, &status, 0) != pid)
855     ;
856   if (lseek(fd, 0, SEEK_END) == 0 && (!status || !chksumtype))
857     {
858       /* empty file */
859       close(fd);
860       return 0;
861     }
862   lseek(fd, 0, SEEK_SET);
863   if (status)
864     {
865       printf("%s: download error %d\n", file, status >> 8 ? status >> 8 : status);
866       if (badchecksump)
867         *badchecksump = 1;
868       close(fd);
869       return 0;
870     }
871   if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
872     {
873       if (badchecksump)
874         *badchecksump = 1;
875       close(fd);
876       return 0;
877     }
878   fcntl(fd, F_SETFD, FD_CLOEXEC);
879   if (uncompress)
880     {
881       if (solv_xfopen_iscompressed(file) < 0)
882         {
883           printf("%s: unsupported compression\n", file);
884           if (badchecksump)
885             *badchecksump = 1;
886           close(fd);
887           return 0;
888         }
889       fp = solv_xfopen_fd(file, fd, "r");
890     }
891   else
892     fp = fdopen(fd, "r");
893   if (!fp)
894     close(fd);
895   return fp;
896 }
897
898 #ifndef DEBIAN
899
900 static void
901 cleanupgpg(char *gpgdir)
902 {
903   char cmd[256];
904   snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
905   unlink(cmd);
906   snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
907   unlink(cmd);
908   snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
909   unlink(cmd);
910   snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
911   unlink(cmd);
912   snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
913   unlink(cmd);
914   rmdir(gpgdir);
915 }
916
917 int
918 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
919 {
920   char *gpgdir;
921   char *keysfile;
922   const char *pubkey;
923   char cmd[256];
924   FILE *kfp;
925   Solvable *s;
926   Id p;
927   off_t posfp, possigfp;
928   int r, nkeys;
929
930   gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
931   if (!gpgdir)
932     return 0;
933   keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
934   if (!(kfp = fopen(keysfile, "w")) )
935     {
936       cleanupgpg(gpgdir);
937       return 0;
938     }
939   nkeys = 0;
940   for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
941     {
942       if (!s->repo)
943         continue;
944       pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
945       if (!pubkey || !*pubkey)
946         continue;
947       if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
948         break;
949       if (fputc('\n', kfp) == EOF)      /* Just in case... */
950         break;
951       nkeys++;
952     }
953   if (fclose(kfp) || !nkeys)
954     {
955       cleanupgpg(gpgdir);
956       return 0;
957     }
958   snprintf(cmd, sizeof(cmd), "gpg2 -q --homedir %s --import %s", gpgdir, keysfile);
959   if (system(cmd))
960     {
961       fprintf(stderr, "key import error\n");
962       cleanupgpg(gpgdir);
963       return 0;
964     }
965   unlink(keysfile);
966   posfp = lseek(fileno(fp), 0, SEEK_CUR);
967   lseek(fileno(fp), 0, SEEK_SET);
968   possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
969   lseek(fileno(sigfp), 0, SEEK_SET);
970   snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
971   fcntl(fileno(fp), F_SETFD, 0);        /* clear CLOEXEC */
972   fcntl(fileno(sigfp), F_SETFD, 0);     /* clear CLOEXEC */
973   r = system(cmd);
974   lseek(fileno(sigfp), possigfp, SEEK_SET);
975   lseek(fileno(fp), posfp, SEEK_SET);
976   fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
977   fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
978   cleanupgpg(gpgdir);
979   return r == 0 ? 1 : 0;
980 }
981
982 #else
983
984 static int
985 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
986 {
987   char cmd[256];
988   int r;
989
990   snprintf(cmd, sizeof(cmd), "gpgv -q --keyring /etc/apt/trusted.gpg /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", fileno(sigfp), fileno(fp));
991   fcntl(fileno(fp), F_SETFD, 0);        /* clear CLOEXEC */
992   fcntl(fileno(sigfp), F_SETFD, 0);     /* clear CLOEXEC */
993   r = system(cmd);
994   fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
995   fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
996   return r == 0 ? 1 : 0;
997 }
998
999 #endif
1000
1001 static Pool *
1002 read_sigs()
1003 {
1004   Pool *sigpool = pool_create();
1005 #if defined(ENABLE_RPMDB_PUBKEY)
1006   Repo *repo = repo_create(sigpool, "rpmdbkeys");
1007   repo_add_rpmdb_pubkeys(repo, 0);
1008 #endif
1009   return sigpool;
1010 }
1011
1012 static int
1013 downloadchecksig(struct repoinfo *cinfo, FILE *fp, const char *sigurl, Pool **sigpool)
1014 {
1015   FILE *sigfp;
1016   sigfp = curlfopen(cinfo, sigurl, 0, 0, 0, 0);
1017   if (!sigfp)
1018     {
1019       printf(" unsigned, skipped\n");
1020       return 0;
1021     }
1022   if (!*sigpool)
1023     *sigpool = read_sigs();
1024   if (!checksig(*sigpool, fp, sigfp))
1025     {
1026       printf(" checksig failed, skipped\n");
1027       fclose(sigfp);
1028       return 0;
1029     }
1030   fclose(sigfp);
1031   return 1;
1032 }
1033
1034 #define CHKSUM_IDENT "1.1"
1035
1036 void
1037 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
1038 {
1039   char buf[4096];
1040   void *h = solv_chksum_create(chktype);
1041   int l;
1042
1043   solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
1044   while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
1045     solv_chksum_add(h, buf, l);
1046   rewind(fp);
1047   solv_chksum_free(h, out);
1048 }
1049
1050 void
1051 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *cookie, unsigned char *out)
1052 {
1053   void *h = solv_chksum_create(chktype);
1054   solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
1055   if (cookie)
1056     solv_chksum_add(h, cookie, 32);
1057   solv_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
1058   solv_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
1059   solv_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
1060   solv_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
1061   solv_chksum_free(h, out);
1062 }
1063
1064 void
1065 setarch(Pool *pool)
1066 {
1067   struct utsname un;
1068   if (uname(&un))
1069     {
1070       perror("uname");
1071       exit(1);
1072     }
1073   pool_setarch(pool, un.machine);
1074 }
1075
1076 char *calccachepath(Repo *repo, const char *repoext)
1077 {
1078   char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
1079   if (repoext)
1080     {
1081       p = pool_tmpappend(repo->pool, p, "_", repoext);
1082       p = pool_tmpappend(repo->pool, p, ".solvx", 0);
1083     }
1084   else
1085     p = pool_tmpappend(repo->pool, p, ".solv", 0);
1086   q = p + strlen(SOLVCACHE_PATH) + 1;
1087   if (*q == '.')
1088     *q = '_';
1089   for (; *q; q++)
1090     if (*q == '/')
1091       *q = '_';
1092   return p;
1093 }
1094
1095 int
1096 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie, int mark)
1097 {
1098   FILE *fp;
1099   unsigned char mycookie[32];
1100   unsigned char myextcookie[32];
1101   struct repoinfo *cinfo;
1102   int flags;
1103
1104   cinfo = repo->appdata;
1105   if (!(fp = fopen(calccachepath(repo, repoext), "r")))
1106     return 0;
1107   if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
1108     {
1109       fclose(fp);
1110       return 0;
1111     }
1112   if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
1113     {
1114       fclose(fp);
1115       return 0;
1116     }
1117   if (cinfo && !repoext)
1118     {
1119       if (fseek(fp, -sizeof(mycookie) * 2, SEEK_END) || fread(myextcookie, sizeof(myextcookie), 1, fp) != 1)
1120         {
1121           fclose(fp);
1122           return 0;
1123         }
1124     }
1125   rewind(fp);
1126
1127   flags = 0;
1128   if (repoext)
1129     {
1130       flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
1131       if (strcmp(repoext, "DL") != 0)
1132         flags |= REPO_LOCALPOOL;        /* no local pool for DL so that we can compare IDs */
1133     }
1134
1135   if (repo_add_solv(repo, fp, flags))
1136     {
1137       fclose(fp);
1138       return 0;
1139     }
1140   if (cinfo && !repoext)
1141     {
1142       memcpy(cinfo->cookie, mycookie, sizeof(mycookie));
1143       memcpy(cinfo->extcookie, myextcookie, sizeof(myextcookie));
1144     }
1145   if (mark)
1146     futimes(fileno(fp), 0);     /* try to set modification time */
1147   fclose(fp);
1148   return 1;
1149 }
1150
1151 void
1152 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
1153 {
1154   FILE *fp;
1155   int i, fd;
1156   char *tmpl;
1157   struct repoinfo *cinfo;
1158   int onepiece;
1159
1160   cinfo = repo->appdata;
1161   mkdir(SOLVCACHE_PATH, 0755);
1162   /* use dupjoin instead of tmpjoin because tmpl must survive repo_write */
1163   tmpl = solv_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
1164   fd = mkstemp(tmpl);
1165   if (fd < 0)
1166     {
1167       free(tmpl);
1168       return;
1169     }
1170   fchmod(fd, 0444);
1171   if (!(fp = fdopen(fd, "w")))
1172     {
1173       close(fd);
1174       unlink(tmpl);
1175       free(tmpl);
1176       return;
1177     }
1178
1179   onepiece = 1;
1180   for (i = repo->start; i < repo->end; i++)
1181    if (repo->pool->solvables[i].repo != repo)
1182      break;
1183   if (i < repo->end)
1184     onepiece = 0;
1185
1186   if (!info)
1187     repo_write(repo, fp);
1188   else if (repoext)
1189     repodata_write(info, fp);
1190   else
1191     {
1192       int oldnrepodata = repo->nrepodata;
1193       repo->nrepodata = oldnrepodata > 2 ? 2 : oldnrepodata;    /* XXX: do this right */
1194       repo_write(repo, fp);
1195       repo->nrepodata = oldnrepodata;
1196       onepiece = 0;
1197     }
1198
1199   if (!repoext && cinfo)
1200     {
1201       if (!cinfo->extcookie[0])
1202         {
1203           /* create the ext cookie and append it */
1204           /* we just need some unique ID */
1205           struct stat stb;
1206           if (!fstat(fileno(fp), &stb))
1207             memset(&stb, 0, sizeof(stb));
1208           calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cookie, cinfo->extcookie);
1209           if (cinfo->extcookie[0] == 0)
1210             cinfo->extcookie[0] = 1;
1211         }
1212       if (fwrite(cinfo->extcookie, 32, 1, fp) != 1)
1213         {
1214           fclose(fp);
1215           unlink(tmpl);
1216           free(tmpl);
1217           return;
1218         }
1219     }
1220   /* append our cookie describing the metadata state */
1221   if (fwrite(cookie, 32, 1, fp) != 1)
1222     {
1223       fclose(fp);
1224       unlink(tmpl);
1225       free(tmpl);
1226       return;
1227     }
1228   if (fclose(fp))
1229     {
1230       unlink(tmpl);
1231       free(tmpl);
1232       return;
1233     }
1234   if (onepiece)
1235     {
1236       /* switch to just saved repo to activate paging and save memory */
1237       FILE *fp = fopen(tmpl, "r");
1238       if (fp)
1239         {
1240           if (!repoext)
1241             {
1242               /* main repo */
1243               repo_empty(repo, 1);
1244               if (repo_add_solv(repo, fp, SOLV_ADD_NO_STUBS))
1245                 {
1246                   /* oops, no way to recover from here */
1247                   fprintf(stderr, "internal error\n");
1248                   exit(1);
1249                 }
1250             }
1251           else
1252             {
1253               /* make sure repodata contains complete repo */
1254               /* (this is how repodata_write saves it) */
1255               repodata_extend_block(info, repo->start, repo->end - repo->start);
1256               info->state = REPODATA_LOADING;
1257               /* no need for LOCALPOOL as pool already contains ids */
1258               repo_add_solv(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1259               info->state = REPODATA_AVAILABLE; /* in case the load failed */
1260             }
1261           fclose(fp);
1262         }
1263     }
1264   if (!rename(tmpl, calccachepath(repo, repoext)))
1265     unlink(tmpl);
1266   free(tmpl);
1267 }
1268
1269
1270 #ifdef ENABLE_RPMMD
1271 /* repomd helpers */
1272
1273 static inline const char *
1274 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1275 {
1276   Pool *pool = repo->pool;
1277   Dataiterator di;
1278   const char *filename;
1279
1280   filename = 0;
1281   *chksump = 0;
1282   *chksumtypep = 0;
1283   dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1284   dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1285   if (dataiterator_step(&di))
1286     {
1287       dataiterator_setpos_parent(&di);
1288       filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1289       *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
1290     }
1291   dataiterator_free(&di);
1292   if (filename && !*chksumtypep)
1293     {
1294       printf("no %s file checksum!\n", what);
1295       filename = 0;
1296     }
1297   return filename;
1298 }
1299
1300 int
1301 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
1302 {
1303   Id chksumtype, handle;
1304   const unsigned char *chksum;
1305   const char *filename;
1306
1307   filename = repomd_find(repo, what, &chksum, &chksumtype);
1308   if (!filename)
1309     return 0;
1310   if (!strcmp(what, "prestodelta"))
1311     what = "deltainfo";
1312   handle = repodata_new_handle(data);
1313   repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
1314   repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
1315   repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
1316   if (!strcmp(what, "deltainfo"))
1317     {
1318       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
1319       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
1320     }
1321   if (!strcmp(what, "filelists"))
1322     {
1323       repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1324       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1325     }
1326   repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1327   return 1;
1328 }
1329
1330 int
1331 repomd_load_ext(Repo *repo, Repodata *data)
1332 {
1333   const char *filename, *repomdtype;
1334   char ext[3];
1335   FILE *fp;
1336   struct repoinfo *cinfo;
1337   const unsigned char *filechksum;
1338   Id filechksumtype;
1339   int r = 0;
1340
1341   cinfo = repo->appdata;
1342   repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
1343   if (!repomdtype)
1344     return 0;
1345   if (!strcmp(repomdtype, "filelists"))
1346     strcpy(ext, "FL");
1347   else if (!strcmp(repomdtype, "deltainfo"))
1348     strcpy(ext, "DL");
1349   else
1350     return 0;
1351 #if 1
1352   printf("[%s:%s", repo->name, ext);
1353 #endif
1354   if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1355     {
1356       printf(" cached]\n");fflush(stdout);
1357       return 1;
1358     }
1359   printf(" fetching]\n"); fflush(stdout);
1360   filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
1361   filechksumtype = 0;
1362   filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, REPOSITORY_REPOMD_CHECKSUM, &filechksumtype);
1363   if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1364     return 0;
1365   if (!strcmp(ext, "FL"))
1366     r = repo_add_rpmmd(repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1367   else if (!strcmp(ext, "DL"))
1368     r = repo_add_deltainfoxml(repo, fp, REPO_USE_LOADING);
1369   fclose(fp);
1370   if (r)
1371     {
1372       printf("%s\n", pool_errstr(repo->pool));
1373       return 0;
1374     }
1375   writecachedrepo(repo, data, ext, cinfo->extcookie);
1376   return 1;
1377 }
1378
1379 #endif
1380
1381
1382 #ifdef ENABLE_SUSEREPO
1383 /* susetags helpers */
1384
1385 static inline const char *
1386 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1387 {
1388   Pool *pool = repo->pool;
1389   Dataiterator di;
1390   const char *filename;
1391
1392   filename = 0;
1393   *chksump = 0;
1394   *chksumtypep = 0;
1395   dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
1396   dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1397   if (dataiterator_step(&di))
1398     {
1399       dataiterator_setpos_parent(&di);
1400       *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
1401       filename = what;
1402     }
1403   dataiterator_free(&di);
1404   if (filename && !*chksumtypep)
1405     {
1406       printf("no %s file checksum!\n", what);
1407       filename = 0;
1408     }
1409   return filename;
1410 }
1411
1412 static Id susetags_langtags[] = {
1413   SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
1414   SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
1415   SOLVABLE_EULA, REPOKEY_TYPE_STR,
1416   SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
1417   SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
1418   SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
1419   0, 0
1420 };
1421
1422 void
1423 susetags_add_ext(Repo *repo, Repodata *data)
1424 {
1425   Pool *pool = repo->pool;
1426   Dataiterator di;
1427   char ext[3];
1428   Id handle, filechksumtype;
1429   const unsigned char *filechksum;
1430   int i;
1431
1432   dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
1433   dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1434   while (dataiterator_step(&di))
1435     {
1436       if (strncmp(di.kv.str, "packages.", 9) != 0)
1437         continue;
1438       if (!strcmp(di.kv.str + 9, "gz"))
1439         continue;
1440       if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
1441         continue;
1442       ext[0] = di.kv.str[9];
1443       ext[1] = di.kv.str[10];
1444       ext[2] = 0;
1445       if (!strcmp(ext, "en"))
1446         continue;
1447       if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
1448         continue;
1449       handle = repodata_new_handle(data);
1450       repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
1451       if (filechksumtype)
1452         repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
1453       if (!strcmp(ext, "DU"))
1454         {
1455           repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
1456           repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
1457         }
1458       else if (!strcmp(ext, "FL"))
1459         {
1460           repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1461           repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1462         }
1463       else
1464         {
1465           for (i = 0; susetags_langtags[i]; i += 2)
1466             {
1467               repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
1468               repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
1469             }
1470         }
1471       repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1472     }
1473   dataiterator_free(&di);
1474 }
1475
1476 int
1477 susetags_load_ext(Repo *repo, Repodata *data)
1478 {
1479   const char *filename, *descrdir;
1480   Id defvendor;
1481   char ext[3];
1482   FILE *fp;
1483   struct repoinfo *cinfo;
1484   const unsigned char *filechksum;
1485   Id filechksumtype;
1486
1487   cinfo = repo->appdata;
1488   filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
1489   if (!filename)
1490     return 0;
1491   /* susetags load */
1492   ext[0] = filename[9];
1493   ext[1] = filename[10];
1494   ext[2] = 0;
1495 #if 1
1496   printf("[%s:%s", repo->name, ext);
1497 #endif
1498   if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1499     {
1500       printf(" cached]\n"); fflush(stdout);
1501       return 1;
1502     }
1503 #if 1
1504   printf(" fetching]\n"); fflush(stdout);
1505 #endif
1506   defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1507   descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1508   if (!descrdir)
1509     descrdir = "suse/setup/descr";
1510   filechksumtype = 0;
1511   filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
1512   if ((fp = curlfopen(cinfo, pool_tmpjoin(repo->pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1513     return 0;
1514   if (repo_add_susetags(repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES))
1515     {
1516       fclose(fp);
1517       printf("%s\n", pool_errstr(repo->pool));
1518       return 0;
1519     }
1520   fclose(fp);
1521   writecachedrepo(repo, data, ext, cinfo->extcookie);
1522   return 1;
1523 }
1524 #endif
1525
1526
1527
1528 /* load callback */
1529
1530 int
1531 load_stub(Pool *pool, Repodata *data, void *dp)
1532 {
1533   struct repoinfo *cinfo = data->repo->appdata;
1534   switch (cinfo->type)
1535     {
1536 #ifdef ENABLE_SUSEREPO
1537     case TYPE_SUSETAGS:
1538       return susetags_load_ext(data->repo, data);
1539 #endif
1540 #ifdef ENABLE_RPMMD
1541     case TYPE_RPMMD:
1542       return repomd_load_ext(data->repo, data);
1543 #endif
1544     default:
1545       return 0;
1546     }
1547 }
1548
1549 static unsigned char installedcookie[32];
1550
1551 #ifdef ENABLE_DEBIAN
1552
1553 const char *
1554 debian_find_component(struct repoinfo *cinfo, FILE *fp, char *comp, const unsigned char **chksump, Id *chksumtypep)
1555 {
1556   char buf[4096];
1557   Id chksumtype;
1558   unsigned char *chksum;
1559   Id curchksumtype;
1560   int l, compl;
1561   char *ch, *fn, *bp;
1562   char *filename;
1563   static char *basearch;
1564   char *binarydir;
1565   int lbinarydir;
1566
1567   if (!basearch)
1568     {
1569       struct utsname un;
1570       if (uname(&un))
1571         {
1572           perror("uname");
1573           exit(1);
1574         }
1575       basearch = strdup(un.machine);
1576       if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
1577         basearch[1] = '3';
1578     }
1579   binarydir = solv_dupjoin("binary-", basearch, "/");
1580   lbinarydir = strlen(binarydir);
1581   compl = strlen(comp);
1582   rewind(fp);
1583   curchksumtype = 0;
1584   filename = 0;
1585   chksum = solv_malloc(32);
1586   chksumtype = 0;
1587   while(fgets(buf, sizeof(buf), fp))
1588     {
1589       l = strlen(buf);
1590       if (l == 0)
1591         continue;
1592       while (l && (buf[l - 1] == '\n' || buf[l - 1] == ' ' || buf[l - 1] == '\t'))
1593         buf[--l] = 0;
1594       if (!strncasecmp(buf, "MD5Sum:", 7))
1595         {
1596           curchksumtype = REPOKEY_TYPE_MD5;
1597           continue;
1598         }
1599       if (!strncasecmp(buf, "SHA1:", 5))
1600         {
1601           curchksumtype = REPOKEY_TYPE_SHA1;
1602           continue;
1603         }
1604       if (!strncasecmp(buf, "SHA256:", 7))
1605         {
1606           curchksumtype = REPOKEY_TYPE_SHA256;
1607           continue;
1608         }
1609       if (!curchksumtype)
1610         continue;
1611       bp = buf;
1612       if (*bp++ != ' ')
1613         {
1614           curchksumtype = 0;
1615           continue;
1616         }
1617       ch = bp;
1618       while (*bp && *bp != ' ' && *bp != '\t')
1619         bp++;
1620       if (!*bp)
1621         continue;
1622       *bp++ = 0;
1623       while (*bp == ' ' || *bp == '\t')
1624         bp++;
1625       while (*bp && *bp != ' ' && *bp != '\t')
1626         bp++;
1627       if (!*bp)
1628         continue;
1629       while (*bp == ' ' || *bp == '\t')
1630         bp++;
1631       fn = bp;
1632       if (strncmp(fn, comp, compl) != 0 || fn[compl] != '/')
1633         continue;
1634       bp += compl + 1;
1635       if (strncmp(bp, binarydir, lbinarydir))
1636         continue;
1637       bp += lbinarydir;
1638       if (!strcmp(bp, "Packages") || !strcmp(bp, "Packages.gz"))
1639         {
1640           unsigned char curchksum[32];
1641           int curl;
1642           if (filename && !strcmp(bp, "Packages"))
1643             continue;
1644           curl = solv_chksum_len(curchksumtype);
1645           if (!curl || (chksumtype && solv_chksum_len(chksumtype) > curl))
1646             continue;
1647           if (solv_hex2bin((const char **)&ch, curchksum, sizeof(curchksum)) != curl)
1648             continue;
1649           solv_free(filename);
1650           filename = strdup(fn);
1651           chksumtype = curchksumtype;
1652           memcpy(chksum, curchksum, curl);
1653         }
1654     }
1655   free(binarydir);
1656   if (filename)
1657     {
1658       fn = solv_dupjoin("/", filename, 0);
1659       solv_free(filename);
1660       filename = solv_dupjoin("dists/", cinfo->name, fn);
1661       solv_free(fn);
1662     }
1663   if (!chksumtype)
1664     chksum = solv_free(chksum);
1665   *chksump = chksum;
1666   *chksumtypep = chksumtype;
1667   return filename;
1668 }
1669 #endif
1670
1671 void
1672 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
1673 {
1674   Repo *repo;
1675   struct repoinfo *cinfo;
1676   int i;
1677   FILE *fp;
1678   const char *filename;
1679   const unsigned char *filechksum;
1680   Id filechksumtype;
1681 #ifdef ENABLE_SUSEREPO
1682   const char *descrdir;
1683   int defvendor;
1684 #endif
1685   struct stat stb;
1686   Pool *sigpool = 0;
1687 #if defined(ENABLE_SUSEREPO) || defined(ENABLE_RPMMD)
1688   Repodata *data;
1689 #endif
1690   int badchecksum;
1691   int dorefresh;
1692 #if defined(ENABLE_DEBIAN)
1693   FILE *fpr;
1694   int j;
1695 #endif
1696
1697   repo = repo_create(pool, "@System");
1698 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1699   printf("rpm database:");
1700   if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/rpm/Packages"), &stb))
1701     memset(&stb, 0, sizeof(&stb));
1702 #endif
1703 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1704   printf("dpgk database:");
1705   if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/dpkg/status"), &stb))
1706     memset(&stb, 0, sizeof(&stb));
1707 #endif
1708 #ifdef NOSYSTEM
1709   printf("no installed database:");
1710   memset(&stb, 0, sizeof(&stb));
1711 #endif
1712   calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, 0, installedcookie);
1713   if (usecachedrepo(repo, 0, installedcookie, 0))
1714     printf(" cached\n");
1715   else
1716     {
1717 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1718       FILE *ofp;
1719       Repo *ref = 0;
1720 #endif
1721       printf(" reading\n");
1722
1723 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1724 # if defined(ENABLE_SUSEREPO) && defined(PRODUCTS_PATH)
1725       if (repo_add_products(repo, PRODUCTS_PATH, REPO_NO_INTERNALIZE | REPO_USE_ROOTDIR))
1726         {
1727           fprintf(stderr, "product reading failed: %s\n", pool_errstr(pool));
1728           exit(1);
1729         }
1730 # endif
1731       if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1732         {
1733           ref = repo_create(pool, "@System.old");
1734           if (repo_add_solv(ref, ofp, 0))
1735             {
1736               repo_free(ref, 1);
1737               ref = 0;
1738             }
1739           fclose(ofp);
1740         }
1741       if (repo_add_rpmdb(repo, ref, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
1742         {
1743           fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1744           exit(1);
1745         }
1746       if (ref)
1747         repo_free(ref, 1);
1748 #endif
1749 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1750       if (repo_add_debdb(repo, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
1751         {
1752           fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1753           exit(1);
1754         }
1755 #endif
1756       writecachedrepo(repo, 0, 0, installedcookie);
1757     }
1758   pool_set_installed(pool, repo);
1759
1760   for (i = 0; i < nrepoinfos; i++)
1761     {
1762       cinfo = repoinfos + i;
1763       if (!cinfo->enabled)
1764         continue;
1765
1766       repo = repo_create(pool, cinfo->alias);
1767       cinfo->repo = repo;
1768       repo->appdata = cinfo;
1769       repo->priority = 99 - cinfo->priority;
1770
1771       dorefresh = cinfo->autorefresh;
1772       if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0), &stb) == 0)
1773         {
1774           if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1775             dorefresh = 0;
1776         }
1777       if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1778         {
1779           printf("repo '%s':", cinfo->alias);
1780           printf(" cached\n");
1781           continue;
1782         }
1783       badchecksum = 0;
1784       switch (cinfo->type)
1785         {
1786 #ifdef ENABLE_RPMMD
1787         case TYPE_RPMMD:
1788           printf("rpmmd repo '%s':", cinfo->alias);
1789           fflush(stdout);
1790           if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1791             {
1792               printf(" no repomd.xml file, skipped\n");
1793               repo_free(repo, 1);
1794               cinfo->repo = 0;
1795               break;
1796             }
1797           calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1798           if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1799             {
1800               printf(" cached\n");
1801               fclose(fp);
1802               break;
1803             }
1804           if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "repodata/repomd.xml.asc", &sigpool))
1805             {
1806               fclose(fp);
1807               break;
1808             }
1809           if (repo_add_repomdxml(repo, fp, 0))
1810             {
1811               printf("repomd.xml: %s\n", pool_errstr(pool));
1812               fclose(fp);
1813               break;    /* hopeless */
1814             }
1815           fclose(fp);
1816           printf(" fetching\n");
1817           filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1818           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1819             {
1820               if (repo_add_rpmmd(repo, fp, 0, 0))
1821                 {
1822                   printf("primary: %s\n", pool_errstr(pool));
1823                   badchecksum = 1;
1824                 }
1825               fclose(fp);
1826             }
1827           if (badchecksum)
1828             break;      /* hopeless */
1829
1830           filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1831           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1832             {
1833               if (repo_add_updateinfoxml(repo, fp, 0))
1834                 {
1835                   printf("updateinfo: %s\n", pool_errstr(pool));
1836                   badchecksum = 1;
1837                 }
1838               fclose(fp);
1839             }
1840
1841           data = repo_add_repodata(repo, 0);
1842           if (!repomd_add_ext(repo, data, "deltainfo"))
1843             repomd_add_ext(repo, data, "prestodelta");
1844           repomd_add_ext(repo, data, "filelists");
1845           repodata_internalize(data);
1846           if (!badchecksum)
1847             writecachedrepo(repo, 0, 0, cinfo->cookie);
1848           repodata_create_stubs(repo_last_repodata(repo));
1849           break;
1850 #endif
1851
1852 #ifdef ENABLE_SUSEREPO
1853         case TYPE_SUSETAGS:
1854           printf("susetags repo '%s':", cinfo->alias);
1855           fflush(stdout);
1856           descrdir = 0;
1857           defvendor = 0;
1858           if ((fp = curlfopen(cinfo, "content", 0, 0, 0, 0)) == 0)
1859             {
1860               printf(" no content file, skipped\n");
1861               repo_free(repo, 1);
1862               cinfo->repo = 0;
1863               break;
1864             }
1865           calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1866           if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1867             {
1868               printf(" cached\n");
1869               fclose(fp);
1870               break;
1871             }
1872           if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "content.asc", &sigpool))
1873             {
1874               fclose(fp);
1875               break;
1876             }
1877           if (repo_add_content(repo, fp, 0))
1878             {
1879               printf("content: %s\n", pool_errstr(pool));
1880               fclose(fp);
1881               break;    /* hopeless */
1882             }
1883           fclose(fp);
1884           defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1885           descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1886           if (!descrdir)
1887             descrdir = "suse/setup/descr";
1888           filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1889           if (!filename)
1890             filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1891           if (!filename)
1892             {
1893               printf(" no packages file entry, skipped\n");
1894               break;
1895             }
1896           printf(" fetching\n");
1897           if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
1898             break;      /* hopeless */
1899           if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|SUSETAGS_RECORD_SHARES))
1900             {
1901               printf("packages: %s\n", pool_errstr(pool));
1902               fclose(fp);
1903               break;    /* hopeless */
1904             }
1905           fclose(fp);
1906           /* add default language */
1907           filename = susetags_find(repo, "packages.en.gz", &filechksum, &filechksumtype);
1908           if (!filename)
1909             filename = susetags_find(repo, "packages.en", &filechksum, &filechksumtype);
1910           if (filename)
1911             {
1912               if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1913                 {
1914                   if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|REPO_REUSE_REPODATA|REPO_EXTEND_SOLVABLES))
1915                     {
1916                       printf("packages.en: %s\n", pool_errstr(pool));
1917                       badchecksum = 1;
1918                     }
1919                   fclose(fp);
1920                 }
1921             }
1922           filename = susetags_find(repo, "patterns", &filechksum, &filechksumtype);
1923           if (filename)
1924             {
1925               if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1926                 {
1927                   char pbuf[256];
1928                   while (fgets(pbuf, sizeof(pbuf), fp))
1929                     {
1930                       int l = strlen(pbuf);
1931                       FILE *fp2;
1932                       if (l && pbuf[l - 1] == '\n')
1933                         pbuf[--l] = 0;
1934                       if (!*pbuf || *pbuf == '.' || strchr(pbuf, '/') != 0)
1935                         continue;
1936                       filename = susetags_find(repo, pbuf, &filechksum, &filechksumtype);
1937                       if (filename && (fp2 = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1938                         {
1939                           if (repo_add_susetags(repo, fp2, defvendor, 0, REPO_NO_INTERNALIZE))
1940                             {
1941                               printf("%s: %s\n", pbuf, pool_errstr(pool));
1942                               badchecksum = 1;
1943                             }
1944                           fclose(fp2);
1945                         }
1946                     }
1947                   fclose(fp);
1948                 }
1949             }
1950           repo_internalize(repo);
1951           data = repo_add_repodata(repo, 0);
1952           susetags_add_ext(repo, data);
1953           repodata_internalize(data);
1954           if (!badchecksum)
1955             writecachedrepo(repo, 0, 0, cinfo->cookie);
1956           repodata_create_stubs(repo_last_repodata(repo));
1957           break;
1958 #endif
1959
1960 #if defined(ENABLE_DEBIAN)
1961         case TYPE_DEBIAN:
1962           printf("debian repo '%s':", cinfo->alias);
1963           fflush(stdout);
1964           filename = solv_dupjoin("dists/", cinfo->name, "/Release");
1965           if ((fpr = curlfopen(cinfo, filename, 0, 0, 0, 0)) == 0)
1966             {
1967               printf(" no Release file, skipped\n");
1968               repo_free(repo, 1);
1969               cinfo->repo = 0;
1970               free((char *)filename);
1971               break;
1972             }
1973           solv_free((char *)filename);
1974           if (cinfo->repo_gpgcheck)
1975             {
1976               filename = solv_dupjoin("dists/", cinfo->name, "/Release.gpg");
1977               if (!downloadchecksig(cinfo, fp, filename, &sigpool))
1978                 {
1979                   fclose(fpr);
1980                   solv_free((char *)filename);
1981                   break;
1982                 }
1983               solv_free((char *)filename);
1984             }
1985           calc_checksum_fp(fpr, REPOKEY_TYPE_SHA256, cinfo->cookie);
1986           if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1987             {
1988               printf(" cached\n");
1989               fclose(fpr);
1990               break;
1991             }
1992           printf(" fetching\n");
1993           for (j = 0; j < cinfo->ncomponents; j++)
1994             {
1995               if (!(filename = debian_find_component(cinfo, fpr, cinfo->components[j], &filechksum, &filechksumtype)))
1996                 {
1997                   printf("[component %s not found]\n", cinfo->components[j]);
1998                   continue;
1999                 }
2000               if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
2001                 {
2002                   if (repo_add_debpackages(repo, fp, 0))
2003                     {
2004                       printf("component %s: %s\n", cinfo->components[j], pool_errstr(pool));
2005                       badchecksum = 1;
2006                     }
2007                   fclose(fp);
2008                 }
2009               solv_free((char *)filechksum);
2010               solv_free((char *)filename);
2011             }
2012           fclose(fpr);
2013           if (!badchecksum)
2014             writecachedrepo(repo, 0, 0, cinfo->cookie);
2015           break;
2016 #endif
2017
2018         default:
2019           printf("unsupported repo '%s': skipped\n", cinfo->alias);
2020           repo_free(repo, 1);
2021           cinfo->repo = 0;
2022           break;
2023         }
2024     }
2025   if (sigpool)
2026     pool_free(sigpool);
2027 }
2028
2029 int
2030 yesno(const char *str)
2031 {
2032   char inbuf[128], *ip;
2033
2034   for (;;)
2035     {
2036       printf("%s", str);
2037       fflush(stdout);
2038       *inbuf = 0;
2039       if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2040         {
2041           printf("Abort.\n");
2042           exit(1);
2043         }
2044       while (*ip == ' ' || *ip == '\t')
2045         ip++;
2046       if (*ip == 'q')
2047         {
2048           printf("Abort.\n");
2049           exit(1);
2050         }
2051       if (*ip == 'y' || *ip == 'n')
2052         return *ip == 'y' ? 1 : 0;
2053     }
2054 }
2055
2056 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2057
2058 struct fcstate {
2059   FILE **newpkgsfps;
2060   Queue *checkq;
2061   int newpkgscnt;
2062   void *rpmdbstate;
2063 };
2064
2065 static void *
2066 fileconflict_cb(Pool *pool, Id p, void *cbdata)
2067 {
2068   struct fcstate *fcstate = cbdata;
2069   Solvable *s;
2070   Id rpmdbid;
2071   int i;
2072   FILE *fp;
2073
2074   if (!p)
2075     {
2076       rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
2077       return 0;
2078     }
2079   s = pool_id2solvable(pool, p);
2080   if (pool->installed && s->repo == pool->installed)
2081     {
2082       if (!s->repo->rpmdbid)
2083         return 0;
2084       rpmdbid = s->repo->rpmdbid[p - s->repo->start];
2085       if (!rpmdbid)
2086         return 0;
2087        return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
2088     }
2089   for (i = 0; i < fcstate->newpkgscnt; i++)
2090     if (fcstate->checkq->elements[i] == p)
2091       break;
2092   if (i == fcstate->newpkgscnt)
2093     return 0;
2094   fp = fcstate->newpkgsfps[i];
2095   if (!fp)
2096     return 0;
2097   rewind(fp);
2098   return rpm_byfp(fp, pool_solvable2str(pool, s), &fcstate->rpmdbstate);
2099 }
2100
2101
2102 void
2103 runrpm(const char *arg, const char *name, int dupfd3, const char *rootdir)
2104 {
2105   pid_t pid;
2106   int status;
2107
2108   if ((pid = fork()) == (pid_t)-1)
2109     {
2110       perror("fork");
2111       exit(1);
2112     }
2113   if (pid == 0)
2114     {
2115       if (!rootdir)
2116         rootdir = "/";
2117       if (dupfd3 != -1 && dupfd3 != 3)
2118         {
2119           dup2(dupfd3, 3);
2120           close(dupfd3);
2121         }
2122       if (dupfd3 != -1)
2123         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
2124       if (strcmp(arg, "-e") == 0)
2125         execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2126       else
2127         execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2128       perror("rpm");
2129       _exit(0);
2130     }
2131   while (waitpid(pid, &status, 0) != pid)
2132     ;
2133   if (status)
2134     {
2135       printf("rpm failed\n");
2136       exit(1);
2137     }
2138 }
2139
2140 #endif
2141
2142 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2143
2144 void
2145 rundpkg(const char *arg, const char *name, int dupfd3, const char *rootdir)
2146 {
2147   pid_t pid;
2148   int status;
2149
2150   if ((pid = fork()) == (pid_t)-1)
2151     {
2152       perror("fork");
2153       exit(1);
2154     }
2155   if (pid == 0)
2156     {
2157       if (!rootdir)
2158         rootdir = "/";
2159       if (dupfd3 != -1 && dupfd3 != 3)
2160         {
2161           dup2(dupfd3, 3);
2162           close(dupfd3);
2163         }
2164       if (dupfd3 != -1)
2165         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
2166       if (strcmp(arg, "--install") == 0)
2167         execlp("dpkg", "dpkg", "--install", "--root", rootdir, "--force", "all", name, (char *)0);
2168       else
2169         execlp("dpkg", "dpkg", "--remove", "--root", rootdir, "--force", "all", name, (char *)0);
2170       perror("dpkg");
2171       _exit(0);
2172     }
2173   while (waitpid(pid, &status, 0) != pid)
2174     ;
2175   if (status)
2176     {
2177       printf("dpkg failed\n");
2178       exit(1);
2179     }
2180 }
2181
2182 #endif
2183
2184 static Id
2185 nscallback(Pool *pool, void *data, Id name, Id evr)
2186 {
2187   if (name == NAMESPACE_PRODUCTBUDDY)
2188     {
2189       /* SUSE specific hack: each product has an associated rpm */
2190       Solvable *s = pool->solvables + evr;
2191       Id p, pp, cap;
2192       Id bestp = 0;
2193
2194       cap = pool_str2id(pool, pool_tmpjoin(pool, "product(", pool_id2str(pool, s->name) + 8, ")"), 0);
2195       if (!cap)
2196         return 0;
2197       cap = pool_rel2id(pool, cap, s->evr, REL_EQ, 0);
2198       if (!cap)
2199         return 0;
2200       FOR_PROVIDES(p, pp, cap)
2201         {
2202           Solvable *ps = pool->solvables + p;
2203           if (ps->repo == s->repo && ps->arch == s->arch)
2204             if (!bestp || pool_evrcmp(pool, pool->solvables[bestp].evr, ps->evr, EVRCMP_COMPARE) < 0)
2205               bestp = p;
2206         }
2207       return bestp;
2208     }
2209   return 0;
2210 }
2211
2212 #ifdef SOFTLOCKS_PATH
2213
2214 void
2215 addsoftlocks(Pool *pool, Queue *job)
2216 {
2217   FILE *fp;
2218   Id type, id, p, pp;
2219   char *bp, *ep, buf[4096];
2220
2221   if ((fp = fopen(SOFTLOCKS_PATH, "r")) == 0)
2222     return;
2223   while((bp = fgets(buf, sizeof(buf), fp)) != 0)
2224     {
2225       while (*bp == ' ' || *bp == '\t')
2226         bp++;
2227       if (!*bp || *bp == '#')
2228         continue;
2229       for (ep = bp; *ep; ep++)
2230         if (*ep == ' ' || *ep == '\t' || *ep == '\n')
2231           break;
2232       *ep = 0;
2233       type = SOLVER_SOLVABLE_NAME;
2234       if (!strncmp(bp, "provides:", 9) && bp[9])
2235         {
2236           type = SOLVER_SOLVABLE_PROVIDES;
2237           bp += 9;
2238         }
2239       id = pool_str2id(pool, bp, 1);
2240       if (pool->installed)
2241         {
2242           FOR_JOB_SELECT(p, pp, type, id)
2243             if (pool->solvables[p].repo == pool->installed)
2244               break;
2245           if (p)
2246             continue;   /* ignore, as it is already installed */
2247         }
2248       queue_push2(job, SOLVER_LOCK|SOLVER_WEAK|type, id);
2249     }
2250   fclose(fp);
2251 }
2252
2253 #endif
2254
2255
2256 void
2257 rewrite_repos(Pool *pool, Queue *addedfileprovides, Queue *addedfileprovides_inst)
2258 {
2259   Repo *repo;
2260   Repodata *data;
2261   Map providedids;
2262   Queue fileprovidesq;
2263   int i, j, n;
2264   struct repoinfo *cinfo;
2265
2266   map_init(&providedids, pool->ss.nstrings);
2267   queue_init(&fileprovidesq);
2268   for (i = 0; i < addedfileprovides->count; i++)
2269     MAPSET(&providedids, addedfileprovides->elements[i]);
2270   FOR_REPOS(i, repo)
2271     {
2272       /* make sure all repodatas but the first are extensions */
2273       if (repo->nrepodata < 2)
2274         continue;
2275       data = repo_id2repodata(repo, 1);
2276       if (data->loadcallback)
2277         continue;
2278       for (j = 2; j < repo->nrepodata; j++)
2279         {
2280           Repodata *edata = repo_id2repodata(repo, j);
2281           if (!edata->loadcallback)
2282             break;
2283         }
2284       if (j < repo->nrepodata)
2285         continue;       /* found a non-externsion repodata, can't rewrite  */
2286       if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
2287         {
2288           if (repo == pool->installed && addedfileprovides_inst)
2289             {
2290               for (j = 0; j < addedfileprovides->count; j++)
2291                 MAPCLR(&providedids, addedfileprovides->elements[j]);
2292               for (j = 0; j < addedfileprovides_inst->count; j++)
2293                 MAPSET(&providedids, addedfileprovides_inst->elements[j]);
2294             }
2295           n = 0;
2296           for (j = 0; j < fileprovidesq.count; j++)
2297             if (MAPTST(&providedids, fileprovidesq.elements[j]))
2298               n++;
2299           if (repo == pool->installed && addedfileprovides_inst)
2300             {
2301               for (j = 0; j < addedfileprovides_inst->count; j++)
2302                 MAPCLR(&providedids, addedfileprovides_inst->elements[j]);
2303               for (j = 0; j < addedfileprovides->count; j++)
2304                 MAPSET(&providedids, addedfileprovides->elements[j]);
2305               if (n == addedfileprovides_inst->count)
2306                 continue;       /* nothing new added */
2307             }
2308           else if (n == addedfileprovides->count)
2309             continue;   /* nothing new added */
2310         }
2311       repodata_set_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, repo == pool->installed && addedfileprovides_inst ? addedfileprovides_inst : addedfileprovides);
2312       repodata_internalize(data);
2313       cinfo = repo->appdata;
2314       writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
2315     }
2316   queue_free(&fileprovidesq);
2317   map_free(&providedids);
2318 }
2319
2320 static void
2321 select_patches(Pool *pool, Queue *job)
2322 {
2323   Id p, pp;
2324   int pruneyou = 0;
2325   Map installedmap, noobsmap;
2326   Solvable *s;
2327
2328   map_init(&noobsmap, 0);
2329   map_init(&installedmap, pool->nsolvables);
2330   solver_calculate_noobsmap(pool, job, &noobsmap);
2331   if (pool->installed)
2332     FOR_REPO_SOLVABLES(pool->installed, p, s)
2333       MAPSET(&installedmap, p);
2334
2335   /* install all patches */
2336   for (p = 1; p < pool->nsolvables; p++)
2337     {
2338       const char *type;
2339       int r;
2340       Id p2;
2341
2342       s = pool->solvables + p;
2343       if (strncmp(pool_id2str(pool, s->name), "patch:", 6) != 0)
2344         continue;
2345       FOR_PROVIDES(p2, pp, s->name)
2346         {
2347           Solvable *s2 = pool->solvables + p2;
2348           if (s2->name != s->name)
2349             continue;
2350           r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2351           if (r < 0 || (r == 0 && p > p2))
2352             break;
2353         }
2354       if (p2)
2355         continue;
2356       type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2357       if (type && !strcmp(type, "optional"))
2358         continue;
2359       r = solvable_trivial_installable_map(s, &installedmap, 0, &noobsmap);
2360       if (r == -1)
2361         continue;
2362       if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2363         {
2364           if (!pruneyou++)
2365             queue_empty(job);
2366         }
2367       else if (pruneyou)
2368         continue;
2369       queue_push2(job, SOLVER_SOLVABLE, p);
2370     }
2371   map_free(&installedmap);
2372   map_free(&noobsmap);
2373 }
2374
2375 #define MODE_LIST        0
2376 #define MODE_INSTALL     1
2377 #define MODE_ERASE       2
2378 #define MODE_UPDATE      3
2379 #define MODE_DISTUPGRADE 4
2380 #define MODE_VERIFY      5
2381 #define MODE_PATCH       6
2382 #define MODE_INFO        7
2383 #define MODE_REPOLIST    8
2384 #define MODE_SEARCH      9
2385
2386 void
2387 usage(int r)
2388 {
2389   fprintf(stderr, "Usage: solv COMMAND <select>\n");
2390   fprintf(stderr, "\n");
2391   fprintf(stderr, "    dist-upgrade: replace installed packages with\n");
2392   fprintf(stderr, "                  versions from the repositories\n");
2393   fprintf(stderr, "    erase:        erase installed packages\n");
2394   fprintf(stderr, "    info:         display package information\n");
2395   fprintf(stderr, "    install:      install packages\n");
2396   fprintf(stderr, "    list:         list packages\n");
2397   fprintf(stderr, "    repos:        list enabled repositories\n");
2398   fprintf(stderr, "    search:       search name/summary/description\n");
2399   fprintf(stderr, "    update:       update installed packages\n");
2400   fprintf(stderr, "    verify:       check dependencies of installed packages\n");
2401   fprintf(stderr, "\n");
2402   exit(r);
2403 }
2404
2405 int
2406 main(int argc, char **argv)
2407 {
2408   Pool *pool;
2409   Repo *commandlinerepo = 0;
2410   Id *commandlinepkgs = 0;
2411   Id p, pp;
2412   struct repoinfo *repoinfos;
2413   int nrepoinfos = 0;
2414   int mainmode = 0, mode = 0;
2415   int i, newpkgs;
2416   Queue job, checkq;
2417   Solver *solv = 0;
2418   Transaction *trans;
2419   char inbuf[128], *ip;
2420   FILE **newpkgsfps;
2421   Queue addedfileprovides;
2422   Queue addedfileprovides_inst;
2423   Queue repofilter;
2424   int cleandeps = 0;
2425   char *rootdir = 0;
2426
2427   argc--;
2428   argv++;
2429   if (!argv[0])
2430     usage(1);
2431   if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2432     {
2433       mainmode = MODE_INSTALL;
2434       mode = SOLVER_INSTALL;
2435     }
2436   else if (!strcmp(argv[0], "patch"))
2437     {
2438       mainmode = MODE_PATCH;
2439       mode = SOLVER_INSTALL;
2440     }
2441   else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2442     {
2443       mainmode = MODE_ERASE;
2444       mode = SOLVER_ERASE;
2445     }
2446   else if (!strcmp(argv[0], "list"))
2447     {
2448       mainmode = MODE_LIST;
2449       mode = 0;
2450     }
2451   else if (!strcmp(argv[0], "info"))
2452     {
2453       mainmode = MODE_INFO;
2454       mode = 0;
2455     }
2456   else if (!strcmp(argv[0], "search"))
2457     {
2458       mainmode = MODE_SEARCH;
2459       mode = 0;
2460     }
2461   else if (!strcmp(argv[0], "verify"))
2462     {
2463       mainmode = MODE_VERIFY;
2464       mode = SOLVER_VERIFY;
2465     }
2466   else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2467     {
2468       mainmode = MODE_UPDATE;
2469       mode = SOLVER_UPDATE;
2470     }
2471   else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2472     {
2473       mainmode = MODE_DISTUPGRADE;
2474       mode = SOLVER_DISTUPGRADE;
2475     }
2476   else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2477     {
2478       mainmode = MODE_REPOLIST;
2479       mode = 0;
2480     }
2481   else
2482     usage(1);
2483
2484   for (;;)
2485     {
2486       if (argc > 2 && !strcmp(argv[1], "--root"))
2487         {
2488           rootdir = argv[2];
2489           argc -= 2;
2490           argv += 2;
2491         }
2492
2493       else if (argc > 1 && !strcmp(argv[1], "--clean"))
2494         {
2495           cleandeps = 1;
2496           argc--;
2497           argv++;
2498         }
2499       else
2500         break;
2501     }
2502
2503   pool = pool_create();
2504   pool_set_rootdir(pool, rootdir);
2505
2506 #if 0
2507   {
2508     const char *langs[] = {"de_DE", "de", "en"};
2509     pool_set_languages(pool, langs, sizeof(langs)/sizeof(*langs));
2510   }
2511 #endif
2512
2513   pool_setloadcallback(pool, load_stub, 0);
2514   pool->nscallback = nscallback;
2515   // pool_setdebuglevel(pool, 2);
2516   setarch(pool);
2517   repoinfos = read_repoinfos(pool, &nrepoinfos);
2518
2519   if (mainmode == MODE_REPOLIST)
2520     {
2521       int j = 1;
2522       for (i = 0; i < nrepoinfos; i++)
2523         {
2524           struct repoinfo *cinfo = repoinfos + i;
2525           if (!cinfo->enabled)
2526             continue;
2527           printf("%d: %-20s %s (prio %d)\n", j++, cinfo->alias, cinfo->name, cinfo->priority);
2528         }
2529       exit(0);
2530     }
2531
2532   read_repos(pool, repoinfos, nrepoinfos);
2533
2534   queue_init(&repofilter);
2535   while (argc > 2 && !strcmp(argv[1], "-r"))
2536     {
2537       const char *rname = argv[2], *rp;
2538       Id repoid = 0;
2539       for (rp = rname; *rp; rp++)
2540         if (*rp <= '0' || *rp >= '9')
2541           break;
2542       if (!*rp)
2543         {
2544           /* repo specified by number */
2545           int rnum = atoi(rname);
2546           for (i = 0; i < nrepoinfos; i++)
2547             {
2548               struct repoinfo *cinfo = repoinfos + i;
2549               if (!cinfo->enabled)
2550                 continue;
2551               if (--rnum == 0)
2552                 repoid = cinfo->repo->repoid;
2553             }
2554         }
2555       else
2556         {
2557           /* repo specified by alias */
2558           Repo *repo;
2559           FOR_REPOS(i, repo)
2560             {
2561               if (!strcasecmp(rname, repo->name))
2562                 repoid = repo->repoid;
2563             }
2564         }
2565       if (!repoid)
2566         {
2567           fprintf(stderr, "%s: no such repo\n", rname);
2568           exit(1);
2569         }
2570       /* SETVENDOR is actually wrong but useful */
2571       queue_push2(&repofilter, SOLVER_SOLVABLE_REPO | SOLVER_SETREPO | SOLVER_SETVENDOR, repoid);
2572       argc -= 2;
2573       argv += 2;
2574     }
2575   if (mainmode == MODE_SEARCH)
2576     {
2577       Dataiterator di;
2578       Map m;
2579       if (argc != 2)
2580         usage(1);
2581       map_init(&m, pool->nsolvables);
2582       dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2583       dataiterator_set_keyname(&di, SOLVABLE_NAME);
2584       dataiterator_set_search(&di, 0, 0);
2585       while (dataiterator_step(&di))
2586         MAPSET(&m, di.solvid);
2587       dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2588       dataiterator_set_search(&di, 0, 0);
2589       while (dataiterator_step(&di))
2590         MAPSET(&m, di.solvid);
2591       dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2592       dataiterator_set_search(&di, 0, 0);
2593       while (dataiterator_step(&di))
2594         MAPSET(&m, di.solvid);
2595       dataiterator_free(&di);
2596
2597       for (p = 1; p < pool->nsolvables; p++)
2598         {
2599           Solvable *s = pool_id2solvable(pool, p);
2600           if (!MAPTST(&m, p))
2601             continue;
2602           printf("  - %s: %s\n", pool_solvable2str(pool, s), solvable_lookup_str(s, SOLVABLE_SUMMARY));
2603         }
2604       map_free(&m);
2605       exit(0);
2606     }
2607
2608
2609   if (mainmode == MODE_LIST || mainmode == MODE_INSTALL)
2610     {
2611       for (i = 1; i < argc; i++)
2612         {
2613           int l;
2614           l = strlen(argv[i]);
2615 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2616           if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2617             continue;
2618 #endif
2619 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2620           if (l <= 4 || strcmp(argv[i] + l - 4, ".deb"))
2621             continue;
2622 #endif
2623           if (access(argv[i], R_OK))
2624             {
2625               perror(argv[i]);
2626               exit(1);
2627             }
2628           if (!commandlinepkgs)
2629             commandlinepkgs = solv_calloc(argc, sizeof(Id));
2630           if (!commandlinerepo)
2631             commandlinerepo = repo_create(pool, "@commandline");
2632           p = 0;
2633 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2634           p = repo_add_rpm(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2635 #endif
2636 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2637           p = repo_add_deb(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2638 #endif
2639           if (!p)
2640             {
2641               fprintf(stderr, "could not add '%s'\n", argv[i]);
2642               exit(1);
2643             }
2644           commandlinepkgs[i] = p;
2645         }
2646       if (commandlinerepo)
2647         repo_internalize(commandlinerepo);
2648     }
2649
2650   // FOR_REPOS(i, repo)
2651   //   printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2652   queue_init(&addedfileprovides);
2653   queue_init(&addedfileprovides_inst);
2654   pool_addfileprovides_queue(pool, &addedfileprovides, &addedfileprovides_inst);
2655   if (addedfileprovides.count || addedfileprovides_inst.count)
2656     rewrite_repos(pool, &addedfileprovides, &addedfileprovides_inst);
2657   queue_free(&addedfileprovides);
2658   queue_free(&addedfileprovides_inst);
2659   pool_createwhatprovides(pool);
2660
2661   queue_init(&job);
2662   for (i = 1; i < argc; i++)
2663     {
2664       Queue job2;
2665       int j, flags, rflags;
2666
2667       if (commandlinepkgs && commandlinepkgs[i])
2668         {
2669           queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
2670           continue;
2671         }
2672       queue_init(&job2);
2673       flags = SELECTION_NAME|SELECTION_PROVIDES|SELECTION_GLOB;
2674       if (mode == MODE_LIST)
2675         flags |= SELECTION_WITH_SOURCE;
2676       if (argv[i][0] == '/')
2677         flags |= SELECTION_FILELIST | (mode == MODE_ERASE ? SELECTION_INSTALLED_ONLY : 0);
2678       rflags = selection_make(pool, &job2, argv[i], flags);
2679       if (repofilter.count)
2680         selection_limit(pool, &job2, &repofilter);
2681       if (!job2.count)
2682         {
2683           flags |= SELECTION_NOCASE;
2684           rflags = selection_make(pool, &job2, argv[i], flags);
2685           if (repofilter.count)
2686             selection_limit(pool, &job2, &repofilter);
2687           if (job2.count)
2688             printf("[ignoring case for '%s']\n", argv[i]);
2689         }
2690       if (!job2.count)
2691         {
2692           fprintf(stderr, "nothing matches '%s'\n", argv[i]);
2693           exit(1);
2694         }
2695       if (rflags & SELECTION_FILELIST)
2696         printf("[using file list match for '%s']\n", argv[i]);
2697       if (rflags & SELECTION_PROVIDES)
2698         printf("[using capability match for '%s']\n", argv[i]);
2699       for (j = 0; j < job2.count; j++)
2700         queue_push(&job, job2.elements[j]);
2701       queue_free(&job2);
2702     }
2703
2704   if (!job.count && (mainmode == MODE_UPDATE || mainmode == MODE_DISTUPGRADE || mainmode == MODE_VERIFY || repofilter.count))
2705     {
2706       queue_push2(&job, SOLVER_SOLVABLE_ALL, 0);
2707       if (repofilter.count)
2708         selection_limit(pool, &job, &repofilter);
2709     }
2710   queue_free(&repofilter);
2711
2712   if (!job.count && MODE_PATCH)
2713     {
2714       printf("no package matched\n");
2715       exit(1);
2716     }
2717
2718   if (mainmode == MODE_LIST || mainmode == MODE_INFO)
2719     {
2720       /* list mode, no solver needed */
2721       Queue q;
2722       queue_init(&q);
2723       for (i = 0; i < job.count; i += 2)
2724         {
2725           int j;
2726           queue_empty(&q);
2727           pool_job2solvables(pool, &q, job.elements[i], job.elements[i + 1]);
2728           for (j = 0; j < q.count; j++)
2729             {
2730               Solvable *s = pool_id2solvable(pool, q.elements[j]);
2731               if (mainmode == MODE_INFO)
2732                 {
2733                   const char *str;
2734                   printf("Name:        %s\n", pool_solvable2str(pool, s));
2735                   printf("Repo:        %s\n", s->repo->name);
2736                   printf("Summary:     %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
2737                   str = solvable_lookup_str(s, SOLVABLE_URL);
2738                   if (str)
2739                     printf("Url:         %s\n", str);
2740                   str = solvable_lookup_str(s, SOLVABLE_LICENSE);
2741                   if (str)
2742                     printf("License:     %s\n", str);
2743                   printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
2744                   printf("\n");
2745                 }
2746               else
2747                 {
2748 #if 1
2749                   const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de", 1);
2750 #else
2751                   const char *sum = solvable_lookup_str_poollang(s, SOLVABLE_SUMMARY);
2752 #endif
2753                   printf("  - %s [%s]\n", pool_solvable2str(pool, s), s->repo->name);
2754                   if (sum)
2755                     printf("    %s\n", sum);
2756                 }
2757             }
2758         }
2759       queue_free(&q);
2760       queue_free(&job);
2761       pool_free(pool);
2762       free_repoinfos(repoinfos, nrepoinfos);
2763       solv_free(commandlinepkgs);
2764 #ifdef FEDORA
2765       yum_substitute(pool, 0);
2766 #endif
2767       exit(0);
2768     }
2769
2770   if (mainmode == MODE_PATCH)
2771     select_patches(pool, &job);
2772
2773   // add mode
2774   for (i = 0; i < job.count; i += 2)
2775     {
2776       job.elements[i] |= mode;
2777       if (mode == SOLVER_UPDATE && pool_isemptyupdatejob(pool, job.elements[i], job.elements[i + 1]))
2778         job.elements[i] ^= SOLVER_UPDATE ^ SOLVER_INSTALL;
2779       if (cleandeps)
2780         job.elements[i] |= SOLVER_CLEANDEPS;
2781     }
2782
2783   // multiversion test
2784   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae", 1));
2785   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-base", 1));
2786   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-extra", 1));
2787
2788 #ifdef SOFTLOCKS_PATH
2789   addsoftlocks(pool, &job);
2790 #endif
2791
2792 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2793 rerunsolver:
2794 #endif
2795   for (;;)
2796     {
2797       Id problem, solution;
2798       int pcnt, scnt;
2799
2800       solv = solver_create(pool);
2801       solver_set_flag(solv, SOLVER_FLAG_SPLITPROVIDES, 1);
2802       if (mainmode == MODE_ERASE)
2803         solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1);  /* don't nag */
2804
2805       if (!solver_solve(solv, &job))
2806         break;
2807       pcnt = solver_problem_count(solv);
2808       printf("Found %d problems:\n", pcnt);
2809       for (problem = 1; problem <= pcnt; problem++)
2810         {
2811           int take = 0;
2812           printf("Problem %d:\n", problem);
2813           solver_printprobleminfo(solv, problem);
2814           printf("\n");
2815           scnt = solver_solution_count(solv, problem);
2816           for (solution = 1; solution <= scnt; solution++)
2817             {
2818               printf("Solution %d:\n", solution);
2819               solver_printsolution(solv, problem, solution);
2820               printf("\n");
2821             }
2822           for (;;)
2823             {
2824               printf("Please choose a solution: ");
2825               fflush(stdout);
2826               *inbuf = 0;
2827               if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2828                 {
2829                   printf("Abort.\n");
2830                   exit(1);
2831                 }
2832               while (*ip == ' ' || *ip == '\t')
2833                 ip++;
2834               if (*ip >= '0' && *ip <= '9')
2835                 {
2836                   take = atoi(ip);
2837                   if (take >= 1 && take <= scnt)
2838                     break;
2839                 }
2840               if (*ip == 's')
2841                 {
2842                   take = 0;
2843                   break;
2844                 }
2845               if (*ip == 'q')
2846                 {
2847                   printf("Abort.\n");
2848                   exit(1);
2849                 }
2850             }
2851           if (!take)
2852             continue;
2853           solver_take_solution(solv, problem, take, &job);
2854         }
2855       solver_free(solv);
2856       solv = 0;
2857     }
2858
2859   trans = solver_create_transaction(solv);
2860   if (!trans->steps.count)
2861     {
2862       printf("Nothing to do.\n");
2863       transaction_free(trans);
2864       solver_free(solv);
2865       queue_free(&job);
2866       pool_free(pool);
2867       free_repoinfos(repoinfos, nrepoinfos);
2868       solv_free(commandlinepkgs);
2869 #ifdef FEDORA
2870       yum_substitute(pool, 0);
2871 #endif
2872       exit(1);
2873     }
2874   printf("\n");
2875   printf("Transaction summary:\n\n");
2876   transaction_print(trans);
2877
2878 #if defined(SUSE)
2879   if (1)
2880     {
2881       DUChanges duc[4];
2882       int i;
2883
2884       duc[0].path = "/";
2885       duc[1].path = "/usr/share/man";
2886       duc[2].path = "/sbin";
2887       duc[3].path = "/etc";
2888       transaction_calc_duchanges(trans, duc, 4);
2889       for (i = 0; i < 4; i++)
2890         printf("duchanges %s: %d K  %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
2891     }
2892 #endif
2893   printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
2894   printf("\n");
2895
2896   if (!yesno("OK to continue (y/n)? "))
2897     {
2898       printf("Abort.\n");
2899       transaction_free(trans);
2900       solver_free(solv);
2901       queue_free(&job);
2902       pool_free(pool);
2903       free_repoinfos(repoinfos, nrepoinfos);
2904       solv_free(commandlinepkgs);
2905 #ifdef FEDORA
2906       yum_substitute(pool, 0);
2907 #endif
2908       exit(1);
2909     }
2910
2911   queue_init(&checkq);
2912   newpkgs = transaction_installedresult(trans, &checkq);
2913   newpkgsfps = 0;
2914
2915   if (newpkgs)
2916     {
2917       int downloadsize = 0;
2918       for (i = 0; i < newpkgs; i++)
2919         {
2920           Solvable *s;
2921
2922           p = checkq.elements[i];
2923           s = pool_id2solvable(pool, p);
2924           downloadsize += solvable_lookup_sizek(s, SOLVABLE_DOWNLOADSIZE, 0);
2925         }
2926       printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
2927       newpkgsfps = solv_calloc(newpkgs, sizeof(*newpkgsfps));
2928       for (i = 0; i < newpkgs; i++)
2929         {
2930           unsigned int medianr;
2931           const char *loc;
2932           Solvable *s;
2933           struct repoinfo *cinfo;
2934           const unsigned char *chksum;
2935           Id chksumtype;
2936           Dataiterator di;
2937
2938           p = checkq.elements[i];
2939           s = pool_id2solvable(pool, p);
2940           if (s->repo == commandlinerepo)
2941             {
2942               loc = solvable_lookup_location(s, &medianr);
2943               if (!(newpkgsfps[i] = fopen(loc, "r")))
2944                 {
2945                   perror(loc);
2946                   exit(1);
2947                 }
2948               putchar('.');
2949               continue;
2950             }
2951           cinfo = s->repo->appdata;
2952           if (!cinfo)
2953             {
2954               printf("%s: no repository information\n", s->repo->name);
2955               exit(1);
2956             }
2957           loc = solvable_lookup_location(s, &medianr);
2958           if (!loc)
2959              continue;
2960
2961           if (pool->installed && pool->installed->nsolvables)
2962             {
2963               /* try a delta first */
2964               char *matchname = strdup(pool_id2str(pool, s->name));
2965               dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
2966               dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
2967               while (dataiterator_step(&di))
2968                 {
2969                   Id baseevr, op;
2970
2971                   dataiterator_setpos_parent(&di);
2972                   if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
2973                       pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
2974                     continue;
2975                   baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
2976                   FOR_PROVIDES(op, pp, s->name)
2977                     {
2978                       Solvable *os = pool->solvables + op;
2979                       if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
2980                         break;
2981                     }
2982                   if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
2983                     {
2984                       /* base is installed, run sequence check */
2985                       const char *seqname;
2986                       const char *seqevr;
2987                       const char *seqnum;
2988                       const char *seq;
2989                       const char *dloc;
2990                       const char *archstr;
2991                       FILE *fp;
2992                       char cmd[128];
2993                       int newfd;
2994
2995                       archstr = pool_id2str(pool, s->arch);
2996                       if (strlen(archstr) > 10 || strchr(archstr, '\'') != 0)
2997                         continue;
2998
2999                       seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
3000                       seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
3001                       seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
3002                       seq = pool_tmpjoin(pool, seqname, "-", seqevr);
3003                       seq = pool_tmpappend(pool, seq, "-", seqnum);
3004                       if (strchr(seq, '\'') != 0)
3005                         continue;
3006 #ifdef FEDORA
3007                       sprintf(cmd, "/usr/bin/applydeltarpm -a '%s' -c -s '", archstr);
3008 #else
3009                       sprintf(cmd, "/usr/bin/applydeltarpm -c -s '");
3010 #endif
3011                       if (system(pool_tmpjoin(pool, cmd, seq, "'")) != 0)
3012                         continue;       /* didn't match */
3013                       /* looks good, download delta */
3014                       chksumtype = 0;
3015                       chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
3016                       if (!chksumtype)
3017                         continue;       /* no way! */
3018                       dloc = pool_lookup_deltalocation(pool, SOLVID_POS);
3019                       if (!dloc)
3020                         continue;
3021                       if (cinfo->type == TYPE_SUSETAGS)
3022                         {
3023                           const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
3024                           dloc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", dloc);
3025                         }
3026                       if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
3027                         continue;
3028                       /* got it, now reconstruct */
3029                       newfd = opentmpfile();
3030 #ifdef FEDORA
3031                       sprintf(cmd, "applydeltarpm -a '%s' /dev/fd/%d /dev/fd/%d", archstr, fileno(fp), newfd);
3032 #else
3033                       sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
3034 #endif
3035                       fcntl(fileno(fp), F_SETFD, 0);
3036                       if (system(cmd))
3037                         {
3038                           close(newfd);
3039                           fclose(fp);
3040                           continue;
3041                         }
3042                       lseek(newfd, 0, SEEK_SET);
3043                       chksumtype = 0;
3044                       chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3045                       if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
3046                         {
3047                           close(newfd);
3048                           fclose(fp);
3049                           continue;
3050                         }
3051                       newpkgsfps[i] = fdopen(newfd, "r");
3052                       fclose(fp);
3053                       break;
3054                     }
3055                 }
3056               dataiterator_free(&di);
3057               solv_free(matchname);
3058             }
3059
3060           if (newpkgsfps[i])
3061             {
3062               putchar('d');
3063               fflush(stdout);
3064               continue;         /* delta worked! */
3065             }
3066           if (cinfo->type == TYPE_SUSETAGS)
3067             {
3068               const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
3069               loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
3070             }
3071           chksumtype = 0;
3072           chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3073           if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
3074             {
3075               printf("\n%s: %s not found in repository\n", s->repo->name, loc);
3076               exit(1);
3077             }
3078           putchar('.');
3079           fflush(stdout);
3080         }
3081       putchar('\n');
3082     }
3083
3084 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3085   if (newpkgs)
3086     {
3087       Queue conflicts;
3088       struct fcstate fcstate;
3089
3090       printf("Searching for file conflicts\n");
3091       queue_init(&conflicts);
3092       fcstate.rpmdbstate = 0;
3093       fcstate.newpkgscnt = newpkgs;
3094       fcstate.checkq = &checkq;
3095       fcstate.newpkgsfps = newpkgsfps;
3096       pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fileconflict_cb, &fcstate);
3097       if (conflicts.count)
3098         {
3099           printf("\n");
3100           for (i = 0; i < conflicts.count; i += 5)
3101             printf("file %s of package %s conflicts with package %s\n", pool_id2str(pool, conflicts.elements[i]), pool_solvid2str(pool, conflicts.elements[i + 1]), pool_solvid2str(pool, conflicts.elements[i + 3]));
3102           printf("\n");
3103           if (yesno("Re-run solver (y/n/q)? "))
3104             {
3105               for (i = 0; i < newpkgs; i++)
3106                 if (newpkgsfps[i])
3107                   fclose(newpkgsfps[i]);
3108               newpkgsfps = solv_free(newpkgsfps);
3109               solver_free(solv);
3110               pool_add_fileconflicts_deps(pool, &conflicts);
3111               pool_createwhatprovides(pool);    /* Hmm... */
3112               goto rerunsolver;
3113             }
3114         }
3115       queue_free(&conflicts);
3116     }
3117 #endif
3118
3119   printf("Committing transaction:\n\n");
3120   transaction_order(trans, 0);
3121   for (i = 0; i < trans->steps.count; i++)
3122     {
3123 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3124       const char *evr, *evrp, *nvra;
3125 #endif
3126       Solvable *s;
3127       int j;
3128       FILE *fp;
3129
3130       p = trans->steps.elements[i];
3131       s = pool_id2solvable(pool, p);
3132       Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
3133       switch(type)
3134         {
3135         case SOLVER_TRANSACTION_ERASE:
3136           printf("erase %s\n", pool_solvid2str(pool, p));
3137 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3138           if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
3139             continue;
3140           /* strip epoch from evr */
3141           evr = evrp = pool_id2str(pool, s->evr);
3142           while (*evrp >= '0' && *evrp <= '9')
3143             evrp++;
3144           if (evrp > evr && evrp[0] == ':' && evrp[1])
3145             evr = evrp + 1;
3146           nvra = pool_tmpjoin(pool, pool_id2str(pool, s->name), "-", evr);
3147           nvra = pool_tmpappend(pool, nvra, ".", pool_id2str(pool, s->arch));
3148           runrpm("-e", nvra, -1, rootdir);      /* too bad that --querybynumber doesn't work */
3149 #endif
3150 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3151           rundpkg("--remove", pool_id2str(pool, s->name), 0, rootdir);
3152 #endif
3153           break;
3154         case SOLVER_TRANSACTION_INSTALL:
3155         case SOLVER_TRANSACTION_MULTIINSTALL:
3156           printf("install %s\n", pool_solvid2str(pool, p));
3157           for (j = 0; j < newpkgs; j++)
3158             if (checkq.elements[j] == p)
3159               break;
3160           fp = j < newpkgs ? newpkgsfps[j] : 0;
3161           if (!fp)
3162             continue;
3163           rewind(fp);
3164           lseek(fileno(fp), 0, SEEK_SET);
3165 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3166           runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp), rootdir);
3167 #endif
3168 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3169           rundpkg("--install", "/dev/fd/3", fileno(fp), rootdir);
3170 #endif
3171           fclose(fp);
3172           newpkgsfps[j] = 0;
3173           break;
3174         default:
3175           break;
3176         }
3177     }
3178
3179   for (i = 0; i < newpkgs; i++)
3180     if (newpkgsfps[i])
3181       fclose(newpkgsfps[i]);
3182   solv_free(newpkgsfps);
3183   queue_free(&checkq);
3184   transaction_free(trans);
3185   solver_free(solv);
3186   queue_free(&job);
3187   pool_free(pool);
3188   free_repoinfos(repoinfos, nrepoinfos);
3189   solv_free(commandlinepkgs);
3190 #ifdef FEDORA
3191   yum_substitute(pool, 0);
3192 #endif
3193   exit(0);
3194 }