solv example: always internalize the "installed" repo, support '-i' option
[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, "pubkeys");
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_REUSE_REPODATA | 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_REUSE_REPODATA | 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_NO_INTERNALIZE | 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_NO_INTERNALIZE | REPO_USE_ROOTDIR))
1783         {
1784           fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1785           exit(1);
1786         }
1787 #endif
1788       repo_internalize(repo);
1789       writecachedrepo(repo, 0, 0, installedcookie);
1790     }
1791   pool_set_installed(pool, repo);
1792
1793   for (i = 0; i < nrepoinfos; i++)
1794     {
1795       cinfo = repoinfos + i;
1796       if (!cinfo->enabled)
1797         continue;
1798
1799       repo = repo_create(pool, cinfo->alias);
1800       cinfo->repo = repo;
1801       repo->appdata = cinfo;
1802       repo->priority = 99 - cinfo->priority;
1803
1804       dorefresh = cinfo->autorefresh;
1805       if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0, 0), &stb) == 0)
1806         {
1807           if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1808             dorefresh = 0;
1809         }
1810       if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1811         {
1812           printf("repo '%s':", cinfo->alias);
1813           printf(" cached\n");
1814           continue;
1815         }
1816       switch (cinfo->type)
1817         {
1818 #ifdef ENABLE_RPMMD
1819         case TYPE_RPMMD:
1820           printf("rpmmd repo '%s':", cinfo->alias);
1821           fflush(stdout);
1822           if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1823             {
1824               printf(" no repomd.xml file, skipped\n");
1825               repo_free(repo, 1);
1826               cinfo->repo = 0;
1827               break;
1828             }
1829           calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1830           if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1831             {
1832               printf(" cached\n");
1833               fclose(fp);
1834               break;
1835             }
1836           if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "repodata/repomd.xml.asc", &sigpool))
1837             {
1838               fclose(fp);
1839               break;
1840             }
1841           if (repo_add_repomdxml(repo, fp, 0))
1842             {
1843               printf("repomd.xml: %s\n", pool_errstr(pool));
1844               fclose(fp);
1845               break;    /* hopeless */
1846             }
1847           fclose(fp);
1848           printf(" fetching\n");
1849           filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1850           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
1851             {
1852               if (repo_add_rpmmd(repo, fp, 0, 0))
1853                 {
1854                   printf("primary: %s\n", pool_errstr(pool));
1855                   cinfo->incomplete = 1;
1856                 }
1857               fclose(fp);
1858             }
1859           if (cinfo->incomplete)
1860             break;      /* hopeless */
1861
1862           filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1863           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
1864             {
1865               if (repo_add_updateinfoxml(repo, fp, 0))
1866                 {
1867                   printf("updateinfo: %s\n", pool_errstr(pool));
1868                   cinfo->incomplete = 1;
1869                 }
1870               fclose(fp);
1871             }
1872
1873 #ifdef ENABLE_APPDATA
1874           filename = repomd_find(repo, "appdata", &filechksum, &filechksumtype);
1875           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
1876             {
1877               if (repo_add_appdata(repo, fp, 0))
1878                 {
1879                   printf("appdata: %s\n", pool_errstr(pool));
1880                   cinfo->incomplete = 1;
1881                 }
1882               fclose(fp);
1883             }
1884 #endif
1885           data = repo_add_repodata(repo, 0);
1886           if (!repomd_add_ext(repo, data, "deltainfo"))
1887             repomd_add_ext(repo, data, "prestodelta");
1888           repomd_add_ext(repo, data, "filelists");
1889           repodata_internalize(data);
1890           if (!cinfo->incomplete)
1891             writecachedrepo(repo, 0, 0, cinfo->cookie);
1892           repodata_create_stubs(repo_last_repodata(repo));
1893           break;
1894 #endif
1895
1896 #ifdef ENABLE_SUSEREPO
1897         case TYPE_SUSETAGS:
1898           printf("susetags repo '%s':", cinfo->alias);
1899           fflush(stdout);
1900           descrdir = 0;
1901           defvendor = 0;
1902           if ((fp = curlfopen(cinfo, "content", 0, 0, 0, 0)) == 0)
1903             {
1904               printf(" no content file, skipped\n");
1905               repo_free(repo, 1);
1906               cinfo->repo = 0;
1907               break;
1908             }
1909           calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1910           if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1911             {
1912               printf(" cached\n");
1913               fclose(fp);
1914               break;
1915             }
1916           if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "content.asc", &sigpool))
1917             {
1918               fclose(fp);
1919               break;
1920             }
1921           if (repo_add_content(repo, fp, 0))
1922             {
1923               printf("content: %s\n", pool_errstr(pool));
1924               fclose(fp);
1925               break;    /* hopeless */
1926             }
1927           fclose(fp);
1928           defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1929           descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1930           if (!descrdir)
1931             descrdir = "suse/setup/descr";
1932           filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1933           if (!filename)
1934             filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1935           if (!filename)
1936             {
1937               printf(" no packages file entry, skipped\n");
1938               break;
1939             }
1940           printf(" fetching\n");
1941           if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 1)) == 0)
1942             break;      /* hopeless */
1943           if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|SUSETAGS_RECORD_SHARES))
1944             {
1945               printf("packages: %s\n", pool_errstr(pool));
1946               fclose(fp);
1947               cinfo->incomplete = 1;
1948               break;    /* hopeless */
1949             }
1950           fclose(fp);
1951           /* add default language */
1952           filename = susetags_find(repo, "packages.en.gz", &filechksum, &filechksumtype);
1953           if (!filename)
1954             filename = susetags_find(repo, "packages.en", &filechksum, &filechksumtype);
1955           if (filename)
1956             {
1957               if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
1958                 {
1959                   if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|REPO_REUSE_REPODATA|REPO_EXTEND_SOLVABLES))
1960                     {
1961                       printf("packages.en: %s\n", pool_errstr(pool));
1962                       cinfo->incomplete = 1;
1963                     }
1964                   fclose(fp);
1965                 }
1966             }
1967           filename = susetags_find(repo, "patterns", &filechksum, &filechksumtype);
1968           if (filename)
1969             {
1970               if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
1971                 {
1972                   char pbuf[256];
1973                   while (fgets(pbuf, sizeof(pbuf), fp))
1974                     {
1975                       int l = strlen(pbuf);
1976                       FILE *fp2;
1977                       if (l && pbuf[l - 1] == '\n')
1978                         pbuf[--l] = 0;
1979                       if (!*pbuf || *pbuf == '.' || strchr(pbuf, '/') != 0)
1980                         continue;
1981                       filename = susetags_find(repo, pbuf, &filechksum, &filechksumtype);
1982                       if (filename && (fp2 = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
1983                         {
1984                           if (repo_add_susetags(repo, fp2, defvendor, 0, REPO_NO_INTERNALIZE))
1985                             {
1986                               printf("%s: %s\n", pbuf, pool_errstr(pool));
1987                               cinfo->incomplete = 1;
1988                             }
1989                           fclose(fp2);
1990                         }
1991                     }
1992                   fclose(fp);
1993                 }
1994             }
1995 #ifdef ENABLE_APPDATA
1996           filename = susetags_find(repo, "appdata.xml.gz", &filechksum, &filechksumtype);
1997           if (!filename)
1998             filename = susetags_find(repo, "appdata.xml", &filechksum, &filechksumtype);
1999           if (filename && (fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
2000             {
2001               if (repo_add_appdata(repo, fp, 0))
2002                 {
2003                   printf("appdata: %s\n", pool_errstr(pool));
2004                   cinfo->incomplete = 1;
2005                 }
2006               fclose(fp);
2007             }
2008 #endif
2009           repo_internalize(repo);
2010           data = repo_add_repodata(repo, 0);
2011           susetags_add_ext(repo, data);
2012           repodata_internalize(data);
2013           if (!cinfo->incomplete)
2014             writecachedrepo(repo, 0, 0, cinfo->cookie);
2015           repodata_create_stubs(repo_last_repodata(repo));
2016           break;
2017 #endif
2018
2019 #if defined(ENABLE_DEBIAN)
2020         case TYPE_DEBIAN:
2021           printf("debian repo '%s':", cinfo->alias);
2022           fflush(stdout);
2023           filename = solv_dupjoin("dists/", cinfo->name, "/Release");
2024           if ((fpr = curlfopen(cinfo, filename, 0, 0, 0, 0)) == 0)
2025             {
2026               printf(" no Release file, skipped\n");
2027               repo_free(repo, 1);
2028               cinfo->repo = 0;
2029               free((char *)filename);
2030               break;
2031             }
2032           solv_free((char *)filename);
2033           if (cinfo->repo_gpgcheck)
2034             {
2035               filename = solv_dupjoin("dists/", cinfo->name, "/Release.gpg");
2036               if (!downloadchecksig(cinfo, fpr, filename, &sigpool))
2037                 {
2038                   fclose(fpr);
2039                   solv_free((char *)filename);
2040                   break;
2041                 }
2042               solv_free((char *)filename);
2043             }
2044           calc_checksum_fp(fpr, REPOKEY_TYPE_SHA256, cinfo->cookie);
2045           if (usecachedrepo(repo, 0, cinfo->cookie, 1))
2046             {
2047               printf(" cached\n");
2048               fclose(fpr);
2049               break;
2050             }
2051           printf(" fetching\n");
2052           for (j = 0; j < cinfo->ncomponents; j++)
2053             {
2054               if (!(filename = debian_find_component(cinfo, fpr, cinfo->components[j], &filechksum, &filechksumtype)))
2055                 {
2056                   printf("[component %s not found]\n", cinfo->components[j]);
2057                   continue;
2058                 }
2059               if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
2060                 {
2061                   if (repo_add_debpackages(repo, fp, 0))
2062                     {
2063                       printf("component %s: %s\n", cinfo->components[j], pool_errstr(pool));
2064                       cinfo->incomplete = 1;
2065                     }
2066                   fclose(fp);
2067                 }
2068               solv_free((char *)filechksum);
2069               solv_free((char *)filename);
2070             }
2071           fclose(fpr);
2072           if (!cinfo->incomplete)
2073             writecachedrepo(repo, 0, 0, cinfo->cookie);
2074           break;
2075 #endif
2076
2077         default:
2078           printf("unsupported repo '%s': skipped\n", cinfo->alias);
2079           repo_free(repo, 1);
2080           cinfo->repo = 0;
2081           break;
2082         }
2083     }
2084   if (sigpool)
2085     pool_free(sigpool);
2086 }
2087
2088 int
2089 yesno(const char *str)
2090 {
2091   char inbuf[128], *ip;
2092
2093   for (;;)
2094     {
2095       printf("%s", str);
2096       fflush(stdout);
2097       *inbuf = 0;
2098       if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2099         {
2100           printf("Abort.\n");
2101           exit(1);
2102         }
2103       while (*ip == ' ' || *ip == '\t')
2104         ip++;
2105       if (*ip == 'q')
2106         {
2107           printf("Abort.\n");
2108           exit(1);
2109         }
2110       if (*ip == 'y' || *ip == 'n')
2111         return *ip == 'y' ? 1 : 0;
2112     }
2113 }
2114
2115 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2116
2117 struct fcstate {
2118   FILE **newpkgsfps;
2119   Queue *checkq;
2120   int newpkgscnt;
2121   void *rpmstate;
2122 };
2123
2124 static void *
2125 fileconflict_cb(Pool *pool, Id p, void *cbdata)
2126 {
2127   struct fcstate *fcstate = cbdata;
2128   Solvable *s;
2129   Id rpmdbid;
2130   int i;
2131   FILE *fp;
2132
2133   s = pool_id2solvable(pool, p);
2134   if (pool->installed && s->repo == pool->installed)
2135     {
2136       if (!s->repo->rpmdbid)
2137         return 0;
2138       rpmdbid = s->repo->rpmdbid[p - s->repo->start];
2139       if (!rpmdbid)
2140         return 0;
2141       return rpm_byrpmdbid(fcstate->rpmstate, rpmdbid);
2142     }
2143   for (i = 0; i < fcstate->newpkgscnt; i++)
2144     if (fcstate->checkq->elements[i] == p)
2145       break;
2146   if (i == fcstate->newpkgscnt)
2147     return 0;
2148   fp = fcstate->newpkgsfps[i];
2149   if (!fp)
2150     return 0;
2151   rewind(fp);
2152   return rpm_byfp(fcstate->rpmstate, fp, pool_solvable2str(pool, s));
2153 }
2154
2155
2156 void
2157 runrpm(const char *arg, const char *name, int dupfd3, const char *rootdir)
2158 {
2159   pid_t pid;
2160   int status;
2161
2162   if ((pid = fork()) == (pid_t)-1)
2163     {
2164       perror("fork");
2165       exit(1);
2166     }
2167   if (pid == 0)
2168     {
2169       if (!rootdir)
2170         rootdir = "/";
2171       if (dupfd3 != -1 && dupfd3 != 3)
2172         {
2173           dup2(dupfd3, 3);
2174           close(dupfd3);
2175         }
2176       if (dupfd3 != -1)
2177         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
2178       if (strcmp(arg, "-e") == 0)
2179         execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2180       else
2181         execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2182       perror("rpm");
2183       _exit(0);
2184     }
2185   while (waitpid(pid, &status, 0) != pid)
2186     ;
2187   if (status)
2188     {
2189       printf("rpm failed\n");
2190       exit(1);
2191     }
2192 }
2193
2194 #endif
2195
2196 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2197
2198 void
2199 rundpkg(const char *arg, const char *name, int dupfd3, const char *rootdir)
2200 {
2201   pid_t pid;
2202   int status;
2203
2204   if ((pid = fork()) == (pid_t)-1)
2205     {
2206       perror("fork");
2207       exit(1);
2208     }
2209   if (pid == 0)
2210     {
2211       if (!rootdir)
2212         rootdir = "/";
2213       if (dupfd3 != -1 && dupfd3 != 3)
2214         {
2215           dup2(dupfd3, 3);
2216           close(dupfd3);
2217         }
2218       if (dupfd3 != -1)
2219         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
2220       if (strcmp(arg, "--install") == 0)
2221         execlp("dpkg", "dpkg", "--install", "--root", rootdir, "--force", "all", name, (char *)0);
2222       else
2223         execlp("dpkg", "dpkg", "--remove", "--root", rootdir, "--force", "all", name, (char *)0);
2224       perror("dpkg");
2225       _exit(0);
2226     }
2227   while (waitpid(pid, &status, 0) != pid)
2228     ;
2229   if (status)
2230     {
2231       printf("dpkg failed\n");
2232       exit(1);
2233     }
2234 }
2235
2236 #endif
2237
2238 #ifdef SUSE
2239 static Id
2240 nscallback(Pool *pool, void *data, Id name, Id evr)
2241 {
2242 #if 0
2243   if (name == NAMESPACE_LANGUAGE)
2244     {
2245       if (!strcmp(pool_id2str(pool, evr), "ja"))
2246         return 1;
2247       if (!strcmp(pool_id2str(pool, evr), "de"))
2248         return 1;
2249       if (!strcmp(pool_id2str(pool, evr), "en"))
2250         return 1;
2251       if (!strcmp(pool_id2str(pool, evr), "en_US"))
2252         return 1;
2253     }
2254 #endif
2255   return 0;
2256 }
2257 #endif
2258
2259 #ifdef SOFTLOCKS_PATH
2260 void
2261 addsoftlocks(Pool *pool, Queue *job)
2262 {
2263   FILE *fp;
2264   Id type, id, p, pp;
2265   char *bp, *ep, buf[4096];
2266
2267   if ((fp = fopen(SOFTLOCKS_PATH, "r")) == 0)
2268     return;
2269   while((bp = fgets(buf, sizeof(buf), fp)) != 0)
2270     {
2271       while (*bp == ' ' || *bp == '\t')
2272         bp++;
2273       if (!*bp || *bp == '#')
2274         continue;
2275       for (ep = bp; *ep; ep++)
2276         if (*ep == ' ' || *ep == '\t' || *ep == '\n')
2277           break;
2278       *ep = 0;
2279       type = SOLVER_SOLVABLE_NAME;
2280       if (!strncmp(bp, "provides:", 9) && bp[9])
2281         {
2282           type = SOLVER_SOLVABLE_PROVIDES;
2283           bp += 9;
2284         }
2285       id = pool_str2id(pool, bp, 1);
2286       if (pool->installed)
2287         {
2288           FOR_JOB_SELECT(p, pp, type, id)
2289             if (pool->solvables[p].repo == pool->installed)
2290               break;
2291           if (p)
2292             continue;   /* ignore, as it is already installed */
2293         }
2294       queue_push2(job, SOLVER_LOCK|SOLVER_WEAK|type, id);
2295     }
2296   fclose(fp);
2297 }
2298 #endif
2299
2300
2301 #if defined(ENABLE_RPMDB)
2302
2303 static void
2304 rewrite_repos(Pool *pool, Queue *addedfileprovides, Queue *addedfileprovides_inst)
2305 {
2306   Repo *repo;
2307   Repodata *data;
2308   Map providedids;
2309   Queue fileprovidesq;
2310   int i, j, n;
2311   struct repoinfo *cinfo;
2312
2313   map_init(&providedids, pool->ss.nstrings);
2314   queue_init(&fileprovidesq);
2315   for (i = 0; i < addedfileprovides->count; i++)
2316     MAPSET(&providedids, addedfileprovides->elements[i]);
2317   FOR_REPOS(i, repo)
2318     {
2319       /* make sure all repodatas but the first are extensions */
2320       if (repo->nrepodata < 2)
2321         continue;
2322       cinfo = repo->appdata;
2323       if (cinfo && cinfo->incomplete)
2324         continue;
2325       data = repo_id2repodata(repo, 1);
2326       if (data->loadcallback)
2327         continue;
2328       for (j = 2; j < repo->nrepodata; j++)
2329         {
2330           Repodata *edata = repo_id2repodata(repo, j);
2331           if (!edata->loadcallback)
2332             break;
2333         }
2334       if (j < repo->nrepodata)
2335         continue;       /* found a non-externsion repodata, can't rewrite  */
2336       if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
2337         {
2338           if (repo == pool->installed && addedfileprovides_inst)
2339             {
2340               for (j = 0; j < addedfileprovides->count; j++)
2341                 MAPCLR(&providedids, addedfileprovides->elements[j]);
2342               for (j = 0; j < addedfileprovides_inst->count; j++)
2343                 MAPSET(&providedids, addedfileprovides_inst->elements[j]);
2344             }
2345           n = 0;
2346           for (j = 0; j < fileprovidesq.count; j++)
2347             if (MAPTST(&providedids, fileprovidesq.elements[j]))
2348               n++;
2349           if (repo == pool->installed && addedfileprovides_inst)
2350             {
2351               for (j = 0; j < addedfileprovides_inst->count; j++)
2352                 MAPCLR(&providedids, addedfileprovides_inst->elements[j]);
2353               for (j = 0; j < addedfileprovides->count; j++)
2354                 MAPSET(&providedids, addedfileprovides->elements[j]);
2355               if (n == addedfileprovides_inst->count)
2356                 continue;       /* nothing new added */
2357             }
2358           else if (n == addedfileprovides->count)
2359             continue;   /* nothing new added */
2360         }
2361       repodata_set_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, repo == pool->installed && addedfileprovides_inst ? addedfileprovides_inst : addedfileprovides);
2362       repodata_internalize(data);
2363       writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
2364     }
2365   queue_free(&fileprovidesq);
2366   map_free(&providedids);
2367 }
2368
2369 static void
2370 addfileprovides(Pool *pool)
2371 {
2372   Queue addedfileprovides;
2373   Queue addedfileprovides_inst;
2374
2375   queue_init(&addedfileprovides);
2376   queue_init(&addedfileprovides_inst);
2377   pool_addfileprovides_queue(pool, &addedfileprovides, &addedfileprovides_inst);
2378   if (addedfileprovides.count || addedfileprovides_inst.count)
2379     rewrite_repos(pool, &addedfileprovides, &addedfileprovides_inst);
2380   queue_free(&addedfileprovides);
2381   queue_free(&addedfileprovides_inst);
2382 }
2383
2384 #endif
2385
2386 #if defined(SUSE) || defined(FEDORA)
2387 static void
2388 add_patchjobs(Pool *pool, Queue *job)
2389 {
2390   Id p, pp;
2391   int pruneyou = 0;
2392   Map installedmap, multiversionmap;
2393   Solvable *s;
2394
2395   map_init(&multiversionmap, 0);
2396   map_init(&installedmap, pool->nsolvables);
2397   solver_calculate_multiversionmap(pool, job, &multiversionmap);
2398   if (pool->installed)
2399     FOR_REPO_SOLVABLES(pool->installed, p, s)
2400       MAPSET(&installedmap, p);
2401
2402   /* install all patches */
2403   for (p = 1; p < pool->nsolvables; p++)
2404     {
2405       const char *type;
2406       int r;
2407       Id p2;
2408
2409       s = pool->solvables + p;
2410       if (strncmp(pool_id2str(pool, s->name), "patch:", 6) != 0)
2411         continue;
2412       FOR_PROVIDES(p2, pp, s->name)
2413         {
2414           Solvable *s2 = pool->solvables + p2;
2415           if (s2->name != s->name)
2416             continue;
2417           r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2418           if (r < 0 || (r == 0 && p > p2))
2419             break;
2420         }
2421       if (p2)
2422         continue;
2423       type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2424       if (type && !strcmp(type, "optional"))
2425         continue;
2426       r = solvable_trivial_installable_map(s, &installedmap, 0, &multiversionmap);
2427       if (r == -1)
2428         continue;
2429       if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2430         {
2431           if (!pruneyou++)
2432             queue_empty(job);
2433         }
2434       else if (pruneyou)
2435         continue;
2436       queue_push2(job, SOLVER_SOLVABLE, p);
2437     }
2438   map_free(&installedmap);
2439   map_free(&multiversionmap);
2440 }
2441 #endif
2442
2443 #ifdef SUSE
2444 static void
2445 showdiskusagechanges(Transaction *trans)
2446 {
2447   DUChanges duc[4];
2448   int i;
2449
2450   /* XXX: use mountpoints here */
2451   duc[0].path = "/";
2452   duc[1].path = "/usr/share/man";
2453   duc[2].path = "/sbin";
2454   duc[3].path = "/etc";
2455   transaction_calc_duchanges(trans, duc, 4);
2456   for (i = 0; i < 4; i++)
2457     printf("duchanges %s: %d K  %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
2458 }
2459 #endif
2460
2461 #if defined(ENABLE_RPMDB)
2462 static FILE *
2463 trydeltadownload(Solvable *s, struct repoinfo *cinfo, const char *loc)
2464 {
2465   Pool *pool = s->repo->pool;
2466   Dataiterator di;
2467   Id pp;
2468   const unsigned char *chksum;
2469   Id chksumtype;
2470   FILE *retfp = 0;
2471   char *matchname = strdup(pool_id2str(pool, s->name));
2472
2473   dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
2474   dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
2475   while (dataiterator_step(&di))
2476     {
2477       Id baseevr, op;
2478
2479       dataiterator_setpos_parent(&di);
2480       if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
2481           pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
2482         continue;
2483       baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
2484       FOR_PROVIDES(op, pp, s->name)
2485         {
2486           Solvable *os = pool->solvables + op;
2487           if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
2488             break;
2489         }
2490       if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
2491         {
2492           /* base is installed, run sequence check */
2493           const char *seq;
2494           const char *dloc;
2495           const char *archstr;
2496           FILE *fp;
2497           char cmd[128];
2498           int newfd;
2499
2500           archstr = pool_id2str(pool, s->arch);
2501           if (strlen(archstr) > 10 || strchr(archstr, '\'') != 0)
2502             continue;
2503
2504           seq = pool_tmpjoin(pool, pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME), "-", pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR));
2505           seq = pool_tmpappend(pool, seq, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM));
2506           if (strchr(seq, '\'') != 0)
2507             continue;
2508 #ifdef FEDORA
2509           sprintf(cmd, "/usr/bin/applydeltarpm -a '%s' -c -s '", archstr);
2510 #else
2511           sprintf(cmd, "/usr/bin/applydeltarpm -c -s '");
2512 #endif
2513           if (system(pool_tmpjoin(pool, cmd, seq, "'")) != 0)
2514             continue;   /* didn't match */
2515           /* looks good, download delta */
2516           chksumtype = 0;
2517           chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
2518           if (!chksumtype)
2519             continue;   /* no way! */
2520           dloc = pool_lookup_deltalocation(pool, SOLVID_POS, 0);
2521           if (!dloc)
2522             continue;
2523 #ifdef ENABLE_SUSEREPO
2524           if (cinfo->type == TYPE_SUSETAGS)
2525             {
2526               const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
2527               dloc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", dloc);
2528             }
2529 #endif
2530           if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
2531             continue;
2532           /* got it, now reconstruct */
2533           newfd = opentmpfile();
2534 #ifdef FEDORA
2535           sprintf(cmd, "applydeltarpm -a '%s' /dev/fd/%d /dev/fd/%d", archstr, fileno(fp), newfd);
2536 #else
2537           sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
2538 #endif
2539           fcntl(fileno(fp), F_SETFD, 0);
2540           if (system(cmd))
2541             {
2542               close(newfd);
2543               fclose(fp);
2544               continue;
2545             }
2546           lseek(newfd, 0, SEEK_SET);
2547           chksumtype = 0;
2548           chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2549           if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
2550             {
2551               close(newfd);
2552               fclose(fp);
2553               continue;
2554             }
2555           retfp = fdopen(newfd, "r");
2556           fclose(fp);
2557           break;
2558         }
2559     }
2560   dataiterator_free(&di);
2561   solv_free(matchname);
2562   return retfp;
2563 }
2564 #endif
2565
2566
2567 #define MODE_LIST        0
2568 #define MODE_INSTALL     1
2569 #define MODE_ERASE       2
2570 #define MODE_UPDATE      3
2571 #define MODE_DISTUPGRADE 4
2572 #define MODE_VERIFY      5
2573 #define MODE_PATCH       6
2574 #define MODE_INFO        7
2575 #define MODE_REPOLIST    8
2576 #define MODE_SEARCH      9
2577
2578 void
2579 usage(int r)
2580 {
2581   fprintf(stderr, "Usage: solv COMMAND <select>\n");
2582   fprintf(stderr, "\n");
2583   fprintf(stderr, "    dist-upgrade: replace installed packages with\n");
2584   fprintf(stderr, "                  versions from the repositories\n");
2585   fprintf(stderr, "    erase:        erase installed packages\n");
2586   fprintf(stderr, "    info:         display package information\n");
2587   fprintf(stderr, "    install:      install packages\n");
2588   fprintf(stderr, "    list:         list packages\n");
2589   fprintf(stderr, "    repos:        list enabled repositories\n");
2590   fprintf(stderr, "    search:       search name/summary/description\n");
2591   fprintf(stderr, "    update:       update installed packages\n");
2592   fprintf(stderr, "    verify:       check dependencies of installed packages\n");
2593 #if defined(SUSE) || defined(FEDORA)
2594   fprintf(stderr, "    patch:        install newest patches\n");
2595 #endif
2596   fprintf(stderr, "\n");
2597   exit(r);
2598 }
2599
2600 int
2601 main(int argc, char **argv)
2602 {
2603   Pool *pool;
2604   Repo *commandlinerepo = 0;
2605   Id *commandlinepkgs = 0;
2606   Id p;
2607   struct repoinfo *repoinfos;
2608   int nrepoinfos = 0;
2609   int mainmode = 0, mode = 0;
2610   int i, newpkgs;
2611   Queue job, checkq;
2612   Solver *solv = 0;
2613   Transaction *trans;
2614   FILE **newpkgsfps;
2615   Queue repofilter;
2616   Queue kindfilter;
2617   Queue archfilter;
2618   int archfilter_src = 0;
2619   int cleandeps = 0;
2620   int forcebest = 0;
2621   char *rootdir = 0;
2622   char *keyname = 0;
2623   int debuglevel = 0;
2624
2625   argc--;
2626   argv++;
2627   userhome = getenv("HOME");
2628   if (userhome && userhome[0] != '/')
2629     userhome = 0;
2630   while (argc && !strcmp(argv[0], "-d"))
2631     {
2632       debuglevel++;
2633       argc--;
2634       argv++;
2635     }
2636   if (!argv[0])
2637     usage(1);
2638   if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2639     {
2640       mainmode = MODE_INSTALL;
2641       mode = SOLVER_INSTALL;
2642     }
2643 #if defined(SUSE) || defined(FEDORA)
2644   else if (!strcmp(argv[0], "patch"))
2645     {
2646       mainmode = MODE_PATCH;
2647       mode = SOLVER_INSTALL;
2648     }
2649 #endif
2650   else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2651     {
2652       mainmode = MODE_ERASE;
2653       mode = SOLVER_ERASE;
2654     }
2655   else if (!strcmp(argv[0], "list") || !strcmp(argv[0], "ls"))
2656     {
2657       mainmode = MODE_LIST;
2658       mode = 0;
2659     }
2660   else if (!strcmp(argv[0], "info"))
2661     {
2662       mainmode = MODE_INFO;
2663       mode = 0;
2664     }
2665   else if (!strcmp(argv[0], "search") || !strcmp(argv[0], "se"))
2666     {
2667       mainmode = MODE_SEARCH;
2668       mode = 0;
2669     }
2670   else if (!strcmp(argv[0], "verify"))
2671     {
2672       mainmode = MODE_VERIFY;
2673       mode = SOLVER_VERIFY;
2674     }
2675   else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2676     {
2677       mainmode = MODE_UPDATE;
2678       mode = SOLVER_UPDATE;
2679     }
2680   else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2681     {
2682       mainmode = MODE_DISTUPGRADE;
2683       mode = SOLVER_DISTUPGRADE;
2684     }
2685   else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2686     {
2687       mainmode = MODE_REPOLIST;
2688       mode = 0;
2689     }
2690   else
2691     usage(1);
2692
2693   for (;;)
2694     {
2695       if (argc > 2 && !strcmp(argv[1], "--root"))
2696         {
2697           rootdir = argv[2];
2698           argc -= 2;
2699           argv += 2;
2700         }
2701       else if (argc > 1 && !strcmp(argv[1], "--clean"))
2702         {
2703           cleandeps = 1;
2704           argc--;
2705           argv++;
2706         }
2707       else if (argc > 1 && !strcmp(argv[1], "--best"))
2708         {
2709           forcebest = 1;
2710           argc--;
2711           argv++;
2712         }
2713       if (argc > 2 && !strcmp(argv[1], "--keyname"))
2714         {
2715           keyname = argv[2];
2716           argc -= 2;
2717           argv += 2;
2718         }
2719       else
2720         break;
2721     }
2722
2723   pool = pool_create();
2724   pool_set_rootdir(pool, rootdir);
2725
2726 #if 0
2727   {
2728     const char *langs[] = {"de_DE", "de", "en"};
2729     pool_set_languages(pool, langs, sizeof(langs)/sizeof(*langs));
2730   }
2731 #endif
2732
2733   pool_setloadcallback(pool, load_stub, 0);
2734 #ifdef SUSE
2735   pool->nscallback = nscallback;
2736 #endif
2737   if (debuglevel)
2738     pool_setdebuglevel(pool, debuglevel);
2739   setarch(pool);
2740   pool_set_flag(pool, POOL_FLAG_ADDFILEPROVIDESFILTERED, 1);
2741   repoinfos = read_repoinfos(pool, &nrepoinfos);
2742
2743   if (mainmode == MODE_REPOLIST)
2744     {
2745       int j = 1;
2746       for (i = 0; i < nrepoinfos; i++)
2747         {
2748           struct repoinfo *cinfo = repoinfos + i;
2749           if (!cinfo->enabled)
2750             continue;
2751           printf("%d: %-20s %s (prio %d)\n", j++, cinfo->alias, cinfo->name, cinfo->priority);
2752         }
2753       exit(0);
2754     }
2755
2756   read_repos(pool, repoinfos, nrepoinfos);
2757
2758   /* setup filters */
2759   queue_init(&repofilter);
2760   queue_init(&kindfilter);
2761   queue_init(&archfilter);
2762   while (argc > 1)
2763     {
2764       if (!strcmp(argv[1], "-i"))
2765         {
2766           queue_push2(&repofilter, SOLVER_SOLVABLE_REPO | SOLVER_SETREPO, pool->installed->repoid);
2767           argc--;
2768           argv++;
2769         }
2770       else if (argc > 2 && (!strcmp(argv[1], "-r") || !strcmp(argv[1], "--repo")))
2771         {
2772           const char *rname = argv[2], *rp;
2773           Id repoid = 0;
2774           for (rp = rname; *rp; rp++)
2775             if (*rp <= '0' || *rp >= '9')
2776               break;
2777           if (!*rp)
2778             {
2779               /* repo specified by number */
2780               int rnum = atoi(rname);
2781               for (i = 0; i < nrepoinfos; i++)
2782                 {
2783                   struct repoinfo *cinfo = repoinfos + i;
2784                   if (!cinfo->enabled)
2785                     continue;
2786                   if (--rnum == 0)
2787                     repoid = cinfo->repo->repoid;
2788                 }
2789             }
2790           else
2791             {
2792               /* repo specified by alias */
2793               Repo *repo;
2794               FOR_REPOS(i, repo)
2795                 {
2796                   if (!strcasecmp(rname, repo->name))
2797                     repoid = repo->repoid;
2798                 }
2799             }
2800           if (!repoid)
2801             {
2802               fprintf(stderr, "%s: no such repo\n", rname);
2803               exit(1);
2804             }
2805           /* SETVENDOR is actually wrong but useful */
2806           queue_push2(&repofilter, SOLVER_SOLVABLE_REPO | SOLVER_SETREPO | SOLVER_SETVENDOR, repoid);
2807           argc -= 2;
2808           argv += 2;
2809         }
2810       else if (argc > 2 && !strcmp(argv[1], "--arch"))
2811         {
2812           if (!strcmp(argv[2], "src") || !strcmp(argv[2], "nosrc"))
2813             archfilter_src = 1;
2814           queue_push2(&archfilter, SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, 0, pool_str2id(pool, argv[2], 1), REL_ARCH, 1));
2815           argc -= 2;
2816           argv += 2;
2817         }
2818       else if (argc > 2 && (!strcmp(argv[1], "-t") || !strcmp(argv[1], "--type")))
2819         {
2820           const char *kind = argv[2];
2821           if (!strcmp(kind, "srcpackage"))
2822             {
2823               /* hey! should use --arch! */
2824               queue_push2(&archfilter, SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, 0, ARCH_SRC, REL_ARCH, 1));
2825               archfilter_src = 1;
2826               argc -= 2;
2827               argv += 2;
2828               continue;
2829             }
2830           if (!strcmp(kind, "package"))
2831             kind = "";
2832           if (!strcmp(kind, "all"))
2833             queue_push2(&kindfilter, SOLVER_SOLVABLE_ALL, 0);
2834           else
2835             queue_push2(&kindfilter, SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, 0, pool_str2id(pool, kind, 1), REL_KIND, 1));
2836           argc -= 2;
2837           argv += 2;
2838         }
2839       else
2840         break;
2841     }
2842
2843   if (mainmode == MODE_SEARCH)
2844     {
2845       Queue sel, q;
2846       Dataiterator di;
2847       if (argc != 2)
2848         usage(1);
2849       pool_createwhatprovides(pool);
2850       queue_init(&sel);
2851       dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2852       dataiterator_set_keyname(&di, SOLVABLE_NAME);
2853       dataiterator_set_search(&di, 0, 0);
2854       while (dataiterator_step(&di))
2855         queue_push2(&sel, SOLVER_SOLVABLE, di.solvid);
2856       dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2857       dataiterator_set_search(&di, 0, 0);
2858       while (dataiterator_step(&di))
2859         queue_push2(&sel, SOLVER_SOLVABLE, di.solvid);
2860       dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2861       dataiterator_set_search(&di, 0, 0);
2862       while (dataiterator_step(&di))
2863         queue_push2(&sel, SOLVER_SOLVABLE, di.solvid);
2864       dataiterator_free(&di);
2865       if (repofilter.count)
2866         selection_filter(pool, &sel, &repofilter);
2867         
2868       queue_init(&q);
2869       selection_solvables(pool, &sel, &q);
2870       queue_free(&sel);
2871       for (i = 0; i < q.count; i++)
2872         {
2873           Solvable *s = pool_id2solvable(pool, q.elements[i]);
2874           printf("  - %s [%s]: %s\n", pool_solvable2str(pool, s), s->repo->name, solvable_lookup_str(s, SOLVABLE_SUMMARY));
2875         }
2876       queue_free(&q);
2877       exit(0);
2878     }
2879
2880   /* process command line packages */
2881   if (mainmode == MODE_LIST || mainmode == MODE_INFO || mainmode == MODE_INSTALL)
2882     {
2883       for (i = 1; i < argc; i++)
2884         {
2885           int l;
2886           l = strlen(argv[i]);
2887 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2888           if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2889             continue;
2890 #endif
2891 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2892           if (l <= 4 || strcmp(argv[i] + l - 4, ".deb"))
2893             continue;
2894 #endif
2895           if (access(argv[i], R_OK))
2896             {
2897               perror(argv[i]);
2898               exit(1);
2899             }
2900           if (!commandlinepkgs)
2901             commandlinepkgs = solv_calloc(argc, sizeof(Id));
2902           if (!commandlinerepo)
2903             commandlinerepo = repo_create(pool, "@commandline");
2904           p = 0;
2905 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2906           p = repo_add_rpm(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2907 #endif
2908 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2909           p = repo_add_deb(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2910 #endif
2911           if (!p)
2912             {
2913               fprintf(stderr, "could not add '%s'\n", argv[i]);
2914               exit(1);
2915             }
2916           commandlinepkgs[i] = p;
2917         }
2918       if (commandlinerepo)
2919         repo_internalize(commandlinerepo);
2920     }
2921
2922   // FOR_REPOS(i, repo)
2923   //   printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2924
2925 #if defined(ENABLE_RPMDB)
2926   if (pool->disttype == DISTTYPE_RPM)
2927     addfileprovides(pool);
2928 #endif
2929   pool_createwhatprovides(pool);
2930
2931   if (keyname)
2932     keyname = solv_dupjoin("solvable:", keyname, 0);
2933   queue_init(&job);
2934   for (i = 1; i < argc; i++)
2935     {
2936       Queue job2;
2937       int j, flags, rflags;
2938
2939       if (commandlinepkgs && commandlinepkgs[i])
2940         {
2941           queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
2942           continue;
2943         }
2944       queue_init(&job2);
2945       flags = SELECTION_NAME|SELECTION_PROVIDES|SELECTION_GLOB;
2946       flags |= SELECTION_CANON|SELECTION_DOTARCH|SELECTION_REL;
2947       if (kindfilter.count)
2948         flags |= SELECTION_SKIP_KIND;
2949       if (mode == MODE_LIST || archfilter_src)
2950         flags |= SELECTION_WITH_SOURCE;
2951       if (argv[i][0] == '/')
2952         flags |= SELECTION_FILELIST | (mode == MODE_ERASE ? SELECTION_INSTALLED_ONLY : 0);
2953       if (!keyname)
2954         rflags = selection_make(pool, &job2, argv[i], flags);
2955       else
2956         rflags = selection_make_matchdeps(pool, &job2, argv[i], flags, pool_str2id(pool, keyname, 1), 0);
2957       if (repofilter.count)
2958         selection_filter(pool, &job2, &repofilter);
2959       if (archfilter.count)
2960         selection_filter(pool, &job2, &archfilter);
2961       if (kindfilter.count)
2962         selection_filter(pool, &job2, &kindfilter);
2963       if (!job2.count)
2964         {
2965           flags |= SELECTION_NOCASE;
2966           if (!keyname)
2967             rflags = selection_make(pool, &job2, argv[i], flags);
2968           else
2969             rflags = selection_make_matchdeps(pool, &job2, argv[i], flags, pool_str2id(pool, keyname, 1), 0);
2970           if (repofilter.count)
2971             selection_filter(pool, &job2, &repofilter);
2972           if (archfilter.count)
2973             selection_filter(pool, &job2, &archfilter);
2974           if (kindfilter.count)
2975             selection_filter(pool, &job2, &kindfilter);
2976           if (job2.count)
2977             printf("[ignoring case for '%s']\n", argv[i]);
2978         }
2979       if (!job2.count)
2980         {
2981           fprintf(stderr, "nothing matches '%s'\n", argv[i]);
2982           exit(1);
2983         }
2984       if (rflags & SELECTION_FILELIST)
2985         printf("[using file list match for '%s']\n", argv[i]);
2986       if (rflags & SELECTION_PROVIDES)
2987         printf("[using capability match for '%s']\n", argv[i]);
2988       for (j = 0; j < job2.count; j++)
2989         queue_push(&job, job2.elements[j]);
2990       queue_free(&job2);
2991     }
2992   keyname = solv_free(keyname);
2993
2994   if (!job.count && (mainmode == MODE_UPDATE || mainmode == MODE_DISTUPGRADE || mainmode == MODE_VERIFY || repofilter.count || archfilter.count || kindfilter.count))
2995     {
2996       queue_push2(&job, SOLVER_SOLVABLE_ALL, 0);
2997       if (repofilter.count)
2998         selection_filter(pool, &job, &repofilter);
2999       if (archfilter.count)
3000         selection_filter(pool, &job, &archfilter);
3001       if (kindfilter.count)
3002         selection_filter(pool, &job, &kindfilter);
3003     }
3004   queue_free(&repofilter);
3005   queue_free(&archfilter);
3006   queue_free(&kindfilter);
3007
3008   if (!job.count && mainmode != MODE_PATCH)
3009     {
3010       printf("no package matched\n");
3011       exit(1);
3012     }
3013
3014   if (mainmode == MODE_LIST || mainmode == MODE_INFO)
3015     {
3016       /* list mode, no solver needed */
3017       Queue q;
3018       queue_init(&q);
3019       for (i = 0; i < job.count; i += 2)
3020         {
3021           int j;
3022           queue_empty(&q);
3023           pool_job2solvables(pool, &q, job.elements[i], job.elements[i + 1]);
3024           for (j = 0; j < q.count; j++)
3025             {
3026               Solvable *s = pool_id2solvable(pool, q.elements[j]);
3027               if (mainmode == MODE_INFO)
3028                 {
3029                   const char *str;
3030                   printf("Name:        %s\n", pool_solvable2str(pool, s));
3031                   printf("Repo:        %s\n", s->repo->name);
3032                   printf("Summary:     %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
3033                   str = solvable_lookup_str(s, SOLVABLE_URL);
3034                   if (str)
3035                     printf("Url:         %s\n", str);
3036                   str = solvable_lookup_str(s, SOLVABLE_LICENSE);
3037                   if (str)
3038                     printf("License:     %s\n", str);
3039 #if 0
3040                   str = solvable_lookup_sourcepkg(s);
3041                   if (str)
3042                     printf("Source:      %s\n", str);
3043 #endif
3044                   printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
3045                   printf("\n");
3046                 }
3047               else
3048                 {
3049 #if 1
3050                   const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de", 1);
3051 #else
3052                   const char *sum = solvable_lookup_str_poollang(s, SOLVABLE_SUMMARY);
3053 #endif
3054                   printf("  - %s [%s]\n", pool_solvable2str(pool, s), s->repo->name);
3055                   if (sum)
3056                     printf("    %s\n", sum);
3057                 }
3058             }
3059         }
3060       queue_free(&q);
3061       queue_free(&job);
3062       pool_free(pool);
3063       free_repoinfos(repoinfos, nrepoinfos);
3064       solv_free(commandlinepkgs);
3065 #ifdef FEDORA
3066       yum_substitute(pool, 0);
3067 #endif
3068       exit(0);
3069     }
3070
3071 #if defined(SUSE) || defined(FEDORA)
3072   if (mainmode == MODE_PATCH)
3073     add_patchjobs(pool, &job);
3074 #endif
3075
3076   // add mode
3077   for (i = 0; i < job.count; i += 2)
3078     {
3079       job.elements[i] |= mode;
3080       if (mode == SOLVER_UPDATE && pool_isemptyupdatejob(pool, job.elements[i], job.elements[i + 1]))
3081         job.elements[i] ^= SOLVER_UPDATE ^ SOLVER_INSTALL;
3082       if (cleandeps)
3083         job.elements[i] |= SOLVER_CLEANDEPS;
3084       if (forcebest)
3085         job.elements[i] |= SOLVER_FORCEBEST;
3086     }
3087
3088   // multiversion test
3089   // queue_push2(&job, SOLVER_MULTIVERSION|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae", 1));
3090   // queue_push2(&job, SOLVER_MULTIVERSION|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-base", 1));
3091   // queue_push2(&job, SOLVER_MULTIVERSION|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-extra", 1));
3092 #if 0
3093   queue_push2(&job, SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, NAMESPACE_LANGUAGE, 0, REL_NAMESPACE, 1));
3094   queue_push2(&job, SOLVER_ERASE|SOLVER_CLEANDEPS|SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, NAMESPACE_LANGUAGE, 0, REL_NAMESPACE, 1));
3095 #endif
3096
3097 #ifdef SOFTLOCKS_PATH
3098   addsoftlocks(pool, &job);
3099 #endif
3100
3101 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3102 rerunsolver:
3103 #endif
3104   solv = solver_create(pool);
3105   solver_set_flag(solv, SOLVER_FLAG_SPLITPROVIDES, 1);
3106 #ifdef FEDORA
3107   solver_set_flag(solv, SOLVER_FLAG_ALLOW_VENDORCHANGE, 1);
3108 #endif
3109   if (mainmode == MODE_ERASE)
3110     solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1);      /* don't nag */
3111   solver_set_flag(solv, SOLVER_FLAG_BEST_OBEY_POLICY, 1);
3112
3113   for (;;)
3114     {
3115       Id problem, solution;
3116       int pcnt, scnt;
3117
3118       if (!solver_solve(solv, &job))
3119         break;
3120       pcnt = solver_problem_count(solv);
3121       printf("Found %d problems:\n", pcnt);
3122       for (problem = 1; problem <= pcnt; problem++)
3123         {
3124           int take = 0;
3125           printf("Problem %d/%d:\n", problem, pcnt);
3126           solver_printprobleminfo(solv, problem);
3127           printf("\n");
3128           scnt = solver_solution_count(solv, problem);
3129           for (solution = 1; solution <= scnt; solution++)
3130             {
3131               printf("Solution %d:\n", solution);
3132               solver_printsolution(solv, problem, solution);
3133               printf("\n");
3134             }
3135           for (;;)
3136             {
3137               char inbuf[128], *ip;
3138               printf("Please choose a solution: ");
3139               fflush(stdout);
3140               *inbuf = 0;
3141               if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
3142                 {
3143                   printf("Abort.\n");
3144                   exit(1);
3145                 }
3146               while (*ip == ' ' || *ip == '\t')
3147                 ip++;
3148               if (*ip >= '0' && *ip <= '9')
3149                 {
3150                   take = atoi(ip);
3151                   if (take >= 1 && take <= scnt)
3152                     break;
3153                 }
3154               if (*ip == 's')
3155                 {
3156                   take = 0;
3157                   break;
3158                 }
3159               if (*ip == 'q')
3160                 {
3161                   printf("Abort.\n");
3162                   exit(1);
3163                 }
3164             }
3165           if (!take)
3166             continue;
3167           solver_take_solution(solv, problem, take, &job);
3168         }
3169     }
3170
3171   trans = solver_create_transaction(solv);
3172   if (!trans->steps.count)
3173     {
3174       printf("Nothing to do.\n");
3175       transaction_free(trans);
3176       solver_free(solv);
3177       queue_free(&job);
3178       pool_free(pool);
3179       free_repoinfos(repoinfos, nrepoinfos);
3180       solv_free(commandlinepkgs);
3181 #ifdef FEDORA
3182       yum_substitute(pool, 0);
3183 #endif
3184       exit(1);
3185     }
3186
3187   /* display transaction to the user and ask for confirmation */
3188   printf("\n");
3189   printf("Transaction summary:\n\n");
3190   transaction_print(trans);
3191 #if defined(SUSE)
3192   showdiskusagechanges(trans);
3193 #endif
3194   printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
3195   printf("\n");
3196
3197   if (!yesno("OK to continue (y/n)? "))
3198     {
3199       printf("Abort.\n");
3200       transaction_free(trans);
3201       solver_free(solv);
3202       queue_free(&job);
3203       pool_free(pool);
3204       free_repoinfos(repoinfos, nrepoinfos);
3205       solv_free(commandlinepkgs);
3206 #ifdef FEDORA
3207       yum_substitute(pool, 0);
3208 #endif
3209       exit(1);
3210     }
3211
3212   /* download all new packages */
3213   queue_init(&checkq);
3214   newpkgs = transaction_installedresult(trans, &checkq);
3215   newpkgsfps = 0;
3216   if (newpkgs)
3217     {
3218       int downloadsize = 0;
3219       for (i = 0; i < newpkgs; i++)
3220         {
3221           Solvable *s;
3222
3223           p = checkq.elements[i];
3224           s = pool_id2solvable(pool, p);
3225           downloadsize += solvable_lookup_sizek(s, SOLVABLE_DOWNLOADSIZE, 0);
3226         }
3227       printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
3228       newpkgsfps = solv_calloc(newpkgs, sizeof(*newpkgsfps));
3229       for (i = 0; i < newpkgs; i++)
3230         {
3231           unsigned int medianr;
3232           const char *loc;
3233           Solvable *s;
3234           struct repoinfo *cinfo;
3235           const unsigned char *chksum;
3236           Id chksumtype;
3237
3238           p = checkq.elements[i];
3239           s = pool_id2solvable(pool, p);
3240           if (s->repo == commandlinerepo)
3241             {
3242               loc = solvable_lookup_location(s, &medianr);
3243               if (!loc)
3244                 continue;
3245               if (!(newpkgsfps[i] = fopen(loc, "r")))
3246                 {
3247                   perror(loc);
3248                   exit(1);
3249                 }
3250               putchar('.');
3251               continue;
3252             }
3253           cinfo = s->repo->appdata;
3254           if (!cinfo)
3255             {
3256               printf("%s: no repository information\n", s->repo->name);
3257               exit(1);
3258             }
3259           loc = solvable_lookup_location(s, &medianr);
3260           if (!loc)
3261              continue;
3262 #if defined(ENABLE_RPMDB)
3263           if (pool->installed && pool->installed->nsolvables)
3264             {
3265               if ((newpkgsfps[i] = trydeltadownload(s, cinfo, loc)) != 0)
3266                 {
3267                   putchar('d');
3268                   fflush(stdout);
3269                   continue;             /* delta worked! */
3270                 }
3271             }
3272 #endif
3273 #ifdef ENABLE_SUSEREPO
3274           if (cinfo->type == TYPE_SUSETAGS)
3275             {
3276               const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
3277               loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
3278             }
3279 #endif
3280           chksumtype = 0;
3281           chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3282           if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
3283             {
3284               printf("\n%s: %s not found in repository\n", s->repo->name, loc);
3285               exit(1);
3286             }
3287           putchar('.');
3288           fflush(stdout);
3289         }
3290       putchar('\n');
3291     }
3292
3293 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3294   /* check for file conflicts */
3295   if (newpkgs)
3296     {
3297       Queue conflicts;
3298       struct fcstate fcstate;
3299
3300       printf("Searching for file conflicts\n");
3301       queue_init(&conflicts);
3302       fcstate.rpmstate = rpm_state_create(pool, rootdir);
3303       fcstate.newpkgscnt = newpkgs;
3304       fcstate.checkq = &checkq;
3305       fcstate.newpkgsfps = newpkgsfps;
3306       pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, FINDFILECONFLICTS_USE_SOLVABLEFILELIST | FINDFILECONFLICTS_CHECK_DIRALIASING | FINDFILECONFLICTS_USE_ROOTDIR, &fileconflict_cb, &fcstate);
3307       fcstate.rpmstate = rpm_state_free(fcstate.rpmstate);
3308       if (conflicts.count)
3309         {
3310           printf("\n");
3311           for (i = 0; i < conflicts.count; i += 6)
3312             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]));
3313           printf("\n");
3314           if (yesno("Re-run solver (y/n/q)? "))
3315             {
3316               for (i = 0; i < newpkgs; i++)
3317                 if (newpkgsfps[i])
3318                   fclose(newpkgsfps[i]);
3319               newpkgsfps = solv_free(newpkgsfps);
3320               solver_free(solv);
3321               solv = 0;
3322               pool_add_fileconflicts_deps(pool, &conflicts);
3323               goto rerunsolver;
3324             }
3325         }
3326       queue_free(&conflicts);
3327     }
3328 #endif
3329
3330   /* and finally commit the transaction */
3331   printf("Committing transaction:\n\n");
3332   transaction_order(trans, 0);
3333   for (i = 0; i < trans->steps.count; i++)
3334     {
3335 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3336       const char *evr, *evrp, *nvra;
3337 #endif
3338       Solvable *s;
3339       int j;
3340       FILE *fp;
3341       Id type;
3342
3343       p = trans->steps.elements[i];
3344       s = pool_id2solvable(pool, p);
3345       type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
3346       switch(type)
3347         {
3348         case SOLVER_TRANSACTION_ERASE:
3349           printf("erase %s\n", pool_solvid2str(pool, p));
3350 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3351           if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
3352             continue;
3353           /* strip epoch from evr */
3354           evr = evrp = pool_id2str(pool, s->evr);
3355           while (*evrp >= '0' && *evrp <= '9')
3356             evrp++;
3357           if (evrp > evr && evrp[0] == ':' && evrp[1])
3358             evr = evrp + 1;
3359           nvra = pool_tmpjoin(pool, pool_id2str(pool, s->name), "-", evr);
3360           nvra = pool_tmpappend(pool, nvra, ".", pool_id2str(pool, s->arch));
3361           runrpm("-e", nvra, -1, rootdir);      /* too bad that --querybynumber doesn't work */
3362 #endif
3363 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3364           rundpkg("--remove", pool_id2str(pool, s->name), 0, rootdir);
3365 #endif
3366           break;
3367         case SOLVER_TRANSACTION_INSTALL:
3368         case SOLVER_TRANSACTION_MULTIINSTALL:
3369           printf("install %s\n", pool_solvid2str(pool, p));
3370           for (j = 0; j < newpkgs; j++)
3371             if (checkq.elements[j] == p)
3372               break;
3373           fp = j < newpkgs ? newpkgsfps[j] : 0;
3374           if (!fp)
3375             continue;
3376           rewind(fp);
3377           lseek(fileno(fp), 0, SEEK_SET);
3378 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3379           runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp), rootdir);
3380 #endif
3381 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3382           rundpkg("--install", "/dev/fd/3", fileno(fp), rootdir);
3383 #endif
3384           fclose(fp);
3385           newpkgsfps[j] = 0;
3386           break;
3387         default:
3388           break;
3389         }
3390     }
3391
3392   for (i = 0; i < newpkgs; i++)
3393     if (newpkgsfps[i])
3394       fclose(newpkgsfps[i]);
3395   solv_free(newpkgsfps);
3396   queue_free(&checkq);
3397   transaction_free(trans);
3398   solver_free(solv);
3399   queue_free(&job);
3400   pool_free(pool);
3401   free_repoinfos(repoinfos, nrepoinfos);
3402   solv_free(commandlinepkgs);
3403 #ifdef FEDORA
3404   yum_substitute(pool, 0);
3405 #endif
3406   exit(0);
3407 }