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