do not rewrite the command line repo
[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 (repo != pool->installed && !cinfo)
2324         continue;
2325       if (cinfo && cinfo->incomplete)
2326         continue;
2327       data = repo_id2repodata(repo, 1);
2328       if (data->loadcallback)
2329         continue;
2330       for (j = 2; j < repo->nrepodata; j++)
2331         {
2332           Repodata *edata = repo_id2repodata(repo, j);
2333           if (!edata->loadcallback)
2334             break;
2335         }
2336       if (j < repo->nrepodata)
2337         continue;       /* found a non-externsion repodata, can't rewrite  */
2338       if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
2339         {
2340           if (repo == pool->installed && addedfileprovides_inst)
2341             {
2342               for (j = 0; j < addedfileprovides->count; j++)
2343                 MAPCLR(&providedids, addedfileprovides->elements[j]);
2344               for (j = 0; j < addedfileprovides_inst->count; j++)
2345                 MAPSET(&providedids, addedfileprovides_inst->elements[j]);
2346             }
2347           n = 0;
2348           for (j = 0; j < fileprovidesq.count; j++)
2349             if (MAPTST(&providedids, fileprovidesq.elements[j]))
2350               n++;
2351           if (repo == pool->installed && addedfileprovides_inst)
2352             {
2353               for (j = 0; j < addedfileprovides_inst->count; j++)
2354                 MAPCLR(&providedids, addedfileprovides_inst->elements[j]);
2355               for (j = 0; j < addedfileprovides->count; j++)
2356                 MAPSET(&providedids, addedfileprovides->elements[j]);
2357               if (n == addedfileprovides_inst->count)
2358                 continue;       /* nothing new added */
2359             }
2360           else if (n == addedfileprovides->count)
2361             continue;   /* nothing new added */
2362         }
2363       repodata_set_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, repo == pool->installed && addedfileprovides_inst ? addedfileprovides_inst : addedfileprovides);
2364       repodata_internalize(data);
2365       writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
2366     }
2367   queue_free(&fileprovidesq);
2368   map_free(&providedids);
2369 }
2370
2371 static void
2372 addfileprovides(Pool *pool)
2373 {
2374   Queue addedfileprovides;
2375   Queue addedfileprovides_inst;
2376
2377   queue_init(&addedfileprovides);
2378   queue_init(&addedfileprovides_inst);
2379   pool_addfileprovides_queue(pool, &addedfileprovides, &addedfileprovides_inst);
2380   if (addedfileprovides.count || addedfileprovides_inst.count)
2381     rewrite_repos(pool, &addedfileprovides, &addedfileprovides_inst);
2382   queue_free(&addedfileprovides);
2383   queue_free(&addedfileprovides_inst);
2384 }
2385
2386 #endif
2387
2388 #if defined(SUSE) || defined(FEDORA)
2389 static void
2390 add_patchjobs(Pool *pool, Queue *job)
2391 {
2392   Id p, pp;
2393   int pruneyou = 0;
2394   Map installedmap, multiversionmap;
2395   Solvable *s;
2396
2397   map_init(&multiversionmap, 0);
2398   map_init(&installedmap, pool->nsolvables);
2399   solver_calculate_multiversionmap(pool, job, &multiversionmap);
2400   if (pool->installed)
2401     FOR_REPO_SOLVABLES(pool->installed, p, s)
2402       MAPSET(&installedmap, p);
2403
2404   /* install all patches */
2405   for (p = 1; p < pool->nsolvables; p++)
2406     {
2407       const char *type;
2408       int r;
2409       Id p2;
2410
2411       s = pool->solvables + p;
2412       if (strncmp(pool_id2str(pool, s->name), "patch:", 6) != 0)
2413         continue;
2414       FOR_PROVIDES(p2, pp, s->name)
2415         {
2416           Solvable *s2 = pool->solvables + p2;
2417           if (s2->name != s->name)
2418             continue;
2419           r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2420           if (r < 0 || (r == 0 && p > p2))
2421             break;
2422         }
2423       if (p2)
2424         continue;
2425       type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2426       if (type && !strcmp(type, "optional"))
2427         continue;
2428       r = solvable_trivial_installable_map(s, &installedmap, 0, &multiversionmap);
2429       if (r == -1)
2430         continue;
2431       if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2432         {
2433           if (!pruneyou++)
2434             queue_empty(job);
2435         }
2436       else if (pruneyou)
2437         continue;
2438       queue_push2(job, SOLVER_SOLVABLE, p);
2439     }
2440   map_free(&installedmap);
2441   map_free(&multiversionmap);
2442 }
2443 #endif
2444
2445 #ifdef SUSE
2446 static void
2447 showdiskusagechanges(Transaction *trans)
2448 {
2449   DUChanges duc[4];
2450   int i;
2451
2452   /* XXX: use mountpoints here */
2453   duc[0].path = "/";
2454   duc[1].path = "/usr/share/man";
2455   duc[2].path = "/sbin";
2456   duc[3].path = "/etc";
2457   transaction_calc_duchanges(trans, duc, 4);
2458   for (i = 0; i < 4; i++)
2459     printf("duchanges %s: %d K  %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
2460 }
2461 #endif
2462
2463 #if defined(ENABLE_RPMDB)
2464 static FILE *
2465 trydeltadownload(Solvable *s, struct repoinfo *cinfo, const char *loc)
2466 {
2467   Pool *pool = s->repo->pool;
2468   Dataiterator di;
2469   Id pp;
2470   const unsigned char *chksum;
2471   Id chksumtype;
2472   FILE *retfp = 0;
2473   char *matchname = strdup(pool_id2str(pool, s->name));
2474
2475   dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
2476   dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
2477   while (dataiterator_step(&di))
2478     {
2479       Id baseevr, op;
2480
2481       dataiterator_setpos_parent(&di);
2482       if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
2483           pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
2484         continue;
2485       baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
2486       FOR_PROVIDES(op, pp, s->name)
2487         {
2488           Solvable *os = pool->solvables + op;
2489           if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
2490             break;
2491         }
2492       if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
2493         {
2494           /* base is installed, run sequence check */
2495           const char *seq;
2496           const char *dloc;
2497           const char *archstr;
2498           FILE *fp;
2499           char cmd[128];
2500           int newfd;
2501
2502           archstr = pool_id2str(pool, s->arch);
2503           if (strlen(archstr) > 10 || strchr(archstr, '\'') != 0)
2504             continue;
2505
2506           seq = pool_tmpjoin(pool, pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME), "-", pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR));
2507           seq = pool_tmpappend(pool, seq, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM));
2508           if (strchr(seq, '\'') != 0)
2509             continue;
2510 #ifdef FEDORA
2511           sprintf(cmd, "/usr/bin/applydeltarpm -a '%s' -c -s '", archstr);
2512 #else
2513           sprintf(cmd, "/usr/bin/applydeltarpm -c -s '");
2514 #endif
2515           if (system(pool_tmpjoin(pool, cmd, seq, "'")) != 0)
2516             continue;   /* didn't match */
2517           /* looks good, download delta */
2518           chksumtype = 0;
2519           chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
2520           if (!chksumtype)
2521             continue;   /* no way! */
2522           dloc = pool_lookup_deltalocation(pool, SOLVID_POS, 0);
2523           if (!dloc)
2524             continue;
2525 #ifdef ENABLE_SUSEREPO
2526           if (cinfo->type == TYPE_SUSETAGS)
2527             {
2528               const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
2529               dloc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", dloc);
2530             }
2531 #endif
2532           if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
2533             continue;
2534           /* got it, now reconstruct */
2535           newfd = opentmpfile();
2536 #ifdef FEDORA
2537           sprintf(cmd, "applydeltarpm -a '%s' /dev/fd/%d /dev/fd/%d", archstr, fileno(fp), newfd);
2538 #else
2539           sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
2540 #endif
2541           fcntl(fileno(fp), F_SETFD, 0);
2542           if (system(cmd))
2543             {
2544               close(newfd);
2545               fclose(fp);
2546               continue;
2547             }
2548           lseek(newfd, 0, SEEK_SET);
2549           chksumtype = 0;
2550           chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2551           if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
2552             {
2553               close(newfd);
2554               fclose(fp);
2555               continue;
2556             }
2557           retfp = fdopen(newfd, "r");
2558           fclose(fp);
2559           break;
2560         }
2561     }
2562   dataiterator_free(&di);
2563   solv_free(matchname);
2564   return retfp;
2565 }
2566 #endif
2567
2568
2569 #define MODE_LIST        0
2570 #define MODE_INSTALL     1
2571 #define MODE_ERASE       2
2572 #define MODE_UPDATE      3
2573 #define MODE_DISTUPGRADE 4
2574 #define MODE_VERIFY      5
2575 #define MODE_PATCH       6
2576 #define MODE_INFO        7
2577 #define MODE_REPOLIST    8
2578 #define MODE_SEARCH      9
2579
2580 void
2581 usage(int r)
2582 {
2583   fprintf(stderr, "Usage: solv COMMAND <select>\n");
2584   fprintf(stderr, "\n");
2585   fprintf(stderr, "    dist-upgrade: replace installed packages with\n");
2586   fprintf(stderr, "                  versions from the repositories\n");
2587   fprintf(stderr, "    erase:        erase installed packages\n");
2588   fprintf(stderr, "    info:         display package information\n");
2589   fprintf(stderr, "    install:      install packages\n");
2590   fprintf(stderr, "    list:         list packages\n");
2591   fprintf(stderr, "    repos:        list enabled repositories\n");
2592   fprintf(stderr, "    search:       search name/summary/description\n");
2593   fprintf(stderr, "    update:       update installed packages\n");
2594   fprintf(stderr, "    verify:       check dependencies of installed packages\n");
2595 #if defined(SUSE) || defined(FEDORA)
2596   fprintf(stderr, "    patch:        install newest patches\n");
2597 #endif
2598   fprintf(stderr, "\n");
2599   exit(r);
2600 }
2601
2602 int
2603 main(int argc, char **argv)
2604 {
2605   Pool *pool;
2606   Repo *commandlinerepo = 0;
2607   Id *commandlinepkgs = 0;
2608   Id p;
2609   struct repoinfo *repoinfos;
2610   int nrepoinfos = 0;
2611   int mainmode = 0, mode = 0;
2612   int i, newpkgs;
2613   Queue job, checkq;
2614   Solver *solv = 0;
2615   Transaction *trans;
2616   FILE **newpkgsfps;
2617   Queue repofilter;
2618   Queue kindfilter;
2619   Queue archfilter;
2620   int archfilter_src = 0;
2621   int cleandeps = 0;
2622   int forcebest = 0;
2623   char *rootdir = 0;
2624   char *keyname = 0;
2625   int debuglevel = 0;
2626
2627   argc--;
2628   argv++;
2629   userhome = getenv("HOME");
2630   if (userhome && userhome[0] != '/')
2631     userhome = 0;
2632   while (argc && !strcmp(argv[0], "-d"))
2633     {
2634       debuglevel++;
2635       argc--;
2636       argv++;
2637     }
2638   if (!argv[0])
2639     usage(1);
2640   if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2641     {
2642       mainmode = MODE_INSTALL;
2643       mode = SOLVER_INSTALL;
2644     }
2645 #if defined(SUSE) || defined(FEDORA)
2646   else if (!strcmp(argv[0], "patch"))
2647     {
2648       mainmode = MODE_PATCH;
2649       mode = SOLVER_INSTALL;
2650     }
2651 #endif
2652   else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2653     {
2654       mainmode = MODE_ERASE;
2655       mode = SOLVER_ERASE;
2656     }
2657   else if (!strcmp(argv[0], "list") || !strcmp(argv[0], "ls"))
2658     {
2659       mainmode = MODE_LIST;
2660       mode = 0;
2661     }
2662   else if (!strcmp(argv[0], "info"))
2663     {
2664       mainmode = MODE_INFO;
2665       mode = 0;
2666     }
2667   else if (!strcmp(argv[0], "search") || !strcmp(argv[0], "se"))
2668     {
2669       mainmode = MODE_SEARCH;
2670       mode = 0;
2671     }
2672   else if (!strcmp(argv[0], "verify"))
2673     {
2674       mainmode = MODE_VERIFY;
2675       mode = SOLVER_VERIFY;
2676     }
2677   else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2678     {
2679       mainmode = MODE_UPDATE;
2680       mode = SOLVER_UPDATE;
2681     }
2682   else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2683     {
2684       mainmode = MODE_DISTUPGRADE;
2685       mode = SOLVER_DISTUPGRADE;
2686     }
2687   else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2688     {
2689       mainmode = MODE_REPOLIST;
2690       mode = 0;
2691     }
2692   else
2693     usage(1);
2694
2695   for (;;)
2696     {
2697       if (argc > 2 && !strcmp(argv[1], "--root"))
2698         {
2699           rootdir = argv[2];
2700           argc -= 2;
2701           argv += 2;
2702         }
2703       else if (argc > 1 && !strcmp(argv[1], "--clean"))
2704         {
2705           cleandeps = 1;
2706           argc--;
2707           argv++;
2708         }
2709       else if (argc > 1 && !strcmp(argv[1], "--best"))
2710         {
2711           forcebest = 1;
2712           argc--;
2713           argv++;
2714         }
2715       if (argc > 2 && !strcmp(argv[1], "--keyname"))
2716         {
2717           keyname = argv[2];
2718           argc -= 2;
2719           argv += 2;
2720         }
2721       else
2722         break;
2723     }
2724
2725   pool = pool_create();
2726   pool_set_rootdir(pool, rootdir);
2727
2728 #if 0
2729   {
2730     const char *langs[] = {"de_DE", "de", "en"};
2731     pool_set_languages(pool, langs, sizeof(langs)/sizeof(*langs));
2732   }
2733 #endif
2734
2735   pool_setloadcallback(pool, load_stub, 0);
2736 #ifdef SUSE
2737   pool->nscallback = nscallback;
2738 #endif
2739   if (debuglevel)
2740     pool_setdebuglevel(pool, debuglevel);
2741   setarch(pool);
2742   pool_set_flag(pool, POOL_FLAG_ADDFILEPROVIDESFILTERED, 1);
2743   repoinfos = read_repoinfos(pool, &nrepoinfos);
2744
2745   if (mainmode == MODE_REPOLIST)
2746     {
2747       int j = 1;
2748       for (i = 0; i < nrepoinfos; i++)
2749         {
2750           struct repoinfo *cinfo = repoinfos + i;
2751           if (!cinfo->enabled)
2752             continue;
2753           printf("%d: %-20s %s (prio %d)\n", j++, cinfo->alias, cinfo->name, cinfo->priority);
2754         }
2755       exit(0);
2756     }
2757
2758   read_repos(pool, repoinfos, nrepoinfos);
2759
2760   /* setup filters */
2761   queue_init(&repofilter);
2762   queue_init(&kindfilter);
2763   queue_init(&archfilter);
2764   while (argc > 1)
2765     {
2766       if (!strcmp(argv[1], "-i"))
2767         {
2768           queue_push2(&repofilter, SOLVER_SOLVABLE_REPO | SOLVER_SETREPO, pool->installed->repoid);
2769           argc--;
2770           argv++;
2771         }
2772       else if (argc > 2 && (!strcmp(argv[1], "-r") || !strcmp(argv[1], "--repo")))
2773         {
2774           const char *rname = argv[2], *rp;
2775           Id repoid = 0;
2776           for (rp = rname; *rp; rp++)
2777             if (*rp <= '0' || *rp >= '9')
2778               break;
2779           if (!*rp)
2780             {
2781               /* repo specified by number */
2782               int rnum = atoi(rname);
2783               for (i = 0; i < nrepoinfos; i++)
2784                 {
2785                   struct repoinfo *cinfo = repoinfos + i;
2786                   if (!cinfo->enabled)
2787                     continue;
2788                   if (--rnum == 0)
2789                     repoid = cinfo->repo->repoid;
2790                 }
2791             }
2792           else
2793             {
2794               /* repo specified by alias */
2795               Repo *repo;
2796               FOR_REPOS(i, repo)
2797                 {
2798                   if (!strcasecmp(rname, repo->name))
2799                     repoid = repo->repoid;
2800                 }
2801             }
2802           if (!repoid)
2803             {
2804               fprintf(stderr, "%s: no such repo\n", rname);
2805               exit(1);
2806             }
2807           /* SETVENDOR is actually wrong but useful */
2808           queue_push2(&repofilter, SOLVER_SOLVABLE_REPO | SOLVER_SETREPO | SOLVER_SETVENDOR, repoid);
2809           argc -= 2;
2810           argv += 2;
2811         }
2812       else if (argc > 2 && !strcmp(argv[1], "--arch"))
2813         {
2814           if (!strcmp(argv[2], "src") || !strcmp(argv[2], "nosrc"))
2815             archfilter_src = 1;
2816           queue_push2(&archfilter, SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, 0, pool_str2id(pool, argv[2], 1), REL_ARCH, 1));
2817           argc -= 2;
2818           argv += 2;
2819         }
2820       else if (argc > 2 && (!strcmp(argv[1], "-t") || !strcmp(argv[1], "--type")))
2821         {
2822           const char *kind = argv[2];
2823           if (!strcmp(kind, "srcpackage"))
2824             {
2825               /* hey! should use --arch! */
2826               queue_push2(&archfilter, SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, 0, ARCH_SRC, REL_ARCH, 1));
2827               archfilter_src = 1;
2828               argc -= 2;
2829               argv += 2;
2830               continue;
2831             }
2832           if (!strcmp(kind, "package"))
2833             kind = "";
2834           if (!strcmp(kind, "all"))
2835             queue_push2(&kindfilter, SOLVER_SOLVABLE_ALL, 0);
2836           else
2837             queue_push2(&kindfilter, SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, 0, pool_str2id(pool, kind, 1), REL_KIND, 1));
2838           argc -= 2;
2839           argv += 2;
2840         }
2841       else
2842         break;
2843     }
2844
2845   if (mainmode == MODE_SEARCH)
2846     {
2847       Queue sel, q;
2848       Dataiterator di;
2849       if (argc != 2)
2850         usage(1);
2851       pool_createwhatprovides(pool);
2852       queue_init(&sel);
2853       dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2854       dataiterator_set_keyname(&di, SOLVABLE_NAME);
2855       dataiterator_set_search(&di, 0, 0);
2856       while (dataiterator_step(&di))
2857         queue_push2(&sel, SOLVER_SOLVABLE, di.solvid);
2858       dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2859       dataiterator_set_search(&di, 0, 0);
2860       while (dataiterator_step(&di))
2861         queue_push2(&sel, SOLVER_SOLVABLE, di.solvid);
2862       dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2863       dataiterator_set_search(&di, 0, 0);
2864       while (dataiterator_step(&di))
2865         queue_push2(&sel, SOLVER_SOLVABLE, di.solvid);
2866       dataiterator_free(&di);
2867       if (repofilter.count)
2868         selection_filter(pool, &sel, &repofilter);
2869         
2870       queue_init(&q);
2871       selection_solvables(pool, &sel, &q);
2872       queue_free(&sel);
2873       for (i = 0; i < q.count; i++)
2874         {
2875           Solvable *s = pool_id2solvable(pool, q.elements[i]);
2876           printf("  - %s [%s]: %s\n", pool_solvable2str(pool, s), s->repo->name, solvable_lookup_str(s, SOLVABLE_SUMMARY));
2877         }
2878       queue_free(&q);
2879       exit(0);
2880     }
2881
2882   /* process command line packages */
2883   if (mainmode == MODE_LIST || mainmode == MODE_INFO || mainmode == MODE_INSTALL)
2884     {
2885       for (i = 1; i < argc; i++)
2886         {
2887           int l;
2888           l = strlen(argv[i]);
2889 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2890           if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2891             continue;
2892 #endif
2893 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2894           if (l <= 4 || strcmp(argv[i] + l - 4, ".deb"))
2895             continue;
2896 #endif
2897           if (access(argv[i], R_OK))
2898             {
2899               perror(argv[i]);
2900               exit(1);
2901             }
2902           if (!commandlinepkgs)
2903             commandlinepkgs = solv_calloc(argc, sizeof(Id));
2904           if (!commandlinerepo)
2905             commandlinerepo = repo_create(pool, "@commandline");
2906           p = 0;
2907 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2908           p = repo_add_rpm(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2909 #endif
2910 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2911           p = repo_add_deb(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2912 #endif
2913           if (!p)
2914             {
2915               fprintf(stderr, "could not add '%s'\n", argv[i]);
2916               exit(1);
2917             }
2918           commandlinepkgs[i] = p;
2919         }
2920       if (commandlinerepo)
2921         repo_internalize(commandlinerepo);
2922     }
2923
2924   // FOR_REPOS(i, repo)
2925   //   printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2926
2927 #if defined(ENABLE_RPMDB)
2928   if (pool->disttype == DISTTYPE_RPM)
2929     addfileprovides(pool);
2930 #endif
2931   pool_createwhatprovides(pool);
2932
2933   if (keyname)
2934     keyname = solv_dupjoin("solvable:", keyname, 0);
2935   queue_init(&job);
2936   for (i = 1; i < argc; i++)
2937     {
2938       Queue job2;
2939       int j, flags, rflags;
2940
2941       if (commandlinepkgs && commandlinepkgs[i])
2942         {
2943           queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
2944           continue;
2945         }
2946       queue_init(&job2);
2947       flags = SELECTION_NAME|SELECTION_PROVIDES|SELECTION_GLOB;
2948       flags |= SELECTION_CANON|SELECTION_DOTARCH|SELECTION_REL;
2949       if (kindfilter.count)
2950         flags |= SELECTION_SKIP_KIND;
2951       if (mode == MODE_LIST || archfilter_src)
2952         flags |= SELECTION_WITH_SOURCE;
2953       if (argv[i][0] == '/')
2954         flags |= SELECTION_FILELIST | (mode == MODE_ERASE ? SELECTION_INSTALLED_ONLY : 0);
2955       if (!keyname)
2956         rflags = selection_make(pool, &job2, argv[i], flags);
2957       else
2958         rflags = selection_make_matchdeps(pool, &job2, argv[i], flags, pool_str2id(pool, keyname, 1), 0);
2959       if (repofilter.count)
2960         selection_filter(pool, &job2, &repofilter);
2961       if (archfilter.count)
2962         selection_filter(pool, &job2, &archfilter);
2963       if (kindfilter.count)
2964         selection_filter(pool, &job2, &kindfilter);
2965       if (!job2.count)
2966         {
2967           flags |= SELECTION_NOCASE;
2968           if (!keyname)
2969             rflags = selection_make(pool, &job2, argv[i], flags);
2970           else
2971             rflags = selection_make_matchdeps(pool, &job2, argv[i], flags, pool_str2id(pool, keyname, 1), 0);
2972           if (repofilter.count)
2973             selection_filter(pool, &job2, &repofilter);
2974           if (archfilter.count)
2975             selection_filter(pool, &job2, &archfilter);
2976           if (kindfilter.count)
2977             selection_filter(pool, &job2, &kindfilter);
2978           if (job2.count)
2979             printf("[ignoring case for '%s']\n", argv[i]);
2980         }
2981       if (!job2.count)
2982         {
2983           fprintf(stderr, "nothing matches '%s'\n", argv[i]);
2984           exit(1);
2985         }
2986       if (rflags & SELECTION_FILELIST)
2987         printf("[using file list match for '%s']\n", argv[i]);
2988       if (rflags & SELECTION_PROVIDES)
2989         printf("[using capability match for '%s']\n", argv[i]);
2990       for (j = 0; j < job2.count; j++)
2991         queue_push(&job, job2.elements[j]);
2992       queue_free(&job2);
2993     }
2994   keyname = solv_free(keyname);
2995
2996   if (!job.count && (mainmode == MODE_UPDATE || mainmode == MODE_DISTUPGRADE || mainmode == MODE_VERIFY || repofilter.count || archfilter.count || kindfilter.count))
2997     {
2998       queue_push2(&job, SOLVER_SOLVABLE_ALL, 0);
2999       if (repofilter.count)
3000         selection_filter(pool, &job, &repofilter);
3001       if (archfilter.count)
3002         selection_filter(pool, &job, &archfilter);
3003       if (kindfilter.count)
3004         selection_filter(pool, &job, &kindfilter);
3005     }
3006   queue_free(&repofilter);
3007   queue_free(&archfilter);
3008   queue_free(&kindfilter);
3009
3010   if (!job.count && mainmode != MODE_PATCH)
3011     {
3012       printf("no package matched\n");
3013       exit(1);
3014     }
3015
3016   if (mainmode == MODE_LIST || mainmode == MODE_INFO)
3017     {
3018       /* list mode, no solver needed */
3019       Queue q;
3020       queue_init(&q);
3021       for (i = 0; i < job.count; i += 2)
3022         {
3023           int j;
3024           queue_empty(&q);
3025           pool_job2solvables(pool, &q, job.elements[i], job.elements[i + 1]);
3026           for (j = 0; j < q.count; j++)
3027             {
3028               Solvable *s = pool_id2solvable(pool, q.elements[j]);
3029               if (mainmode == MODE_INFO)
3030                 {
3031                   const char *str;
3032                   printf("Name:        %s\n", pool_solvable2str(pool, s));
3033                   printf("Repo:        %s\n", s->repo->name);
3034                   printf("Summary:     %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
3035                   str = solvable_lookup_str(s, SOLVABLE_URL);
3036                   if (str)
3037                     printf("Url:         %s\n", str);
3038                   str = solvable_lookup_str(s, SOLVABLE_LICENSE);
3039                   if (str)
3040                     printf("License:     %s\n", str);
3041 #if 0
3042                   str = solvable_lookup_sourcepkg(s);
3043                   if (str)
3044                     printf("Source:      %s\n", str);
3045 #endif
3046                   printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
3047                   printf("\n");
3048                 }
3049               else
3050                 {
3051 #if 1
3052                   const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de", 1);
3053 #else
3054                   const char *sum = solvable_lookup_str_poollang(s, SOLVABLE_SUMMARY);
3055 #endif
3056                   printf("  - %s [%s]\n", pool_solvable2str(pool, s), s->repo->name);
3057                   if (sum)
3058                     printf("    %s\n", sum);
3059                 }
3060             }
3061         }
3062       queue_free(&q);
3063       queue_free(&job);
3064       pool_free(pool);
3065       free_repoinfos(repoinfos, nrepoinfos);
3066       solv_free(commandlinepkgs);
3067 #ifdef FEDORA
3068       yum_substitute(pool, 0);
3069 #endif
3070       exit(0);
3071     }
3072
3073 #if defined(SUSE) || defined(FEDORA)
3074   if (mainmode == MODE_PATCH)
3075     add_patchjobs(pool, &job);
3076 #endif
3077
3078   // add mode
3079   for (i = 0; i < job.count; i += 2)
3080     {
3081       job.elements[i] |= mode;
3082       if (mode == SOLVER_UPDATE && pool_isemptyupdatejob(pool, job.elements[i], job.elements[i + 1]))
3083         job.elements[i] ^= SOLVER_UPDATE ^ SOLVER_INSTALL;
3084       if (cleandeps)
3085         job.elements[i] |= SOLVER_CLEANDEPS;
3086       if (forcebest)
3087         job.elements[i] |= SOLVER_FORCEBEST;
3088     }
3089
3090   // multiversion test
3091   // queue_push2(&job, SOLVER_MULTIVERSION|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae", 1));
3092   // queue_push2(&job, SOLVER_MULTIVERSION|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-base", 1));
3093   // queue_push2(&job, SOLVER_MULTIVERSION|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-extra", 1));
3094 #if 0
3095   queue_push2(&job, SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, NAMESPACE_LANGUAGE, 0, REL_NAMESPACE, 1));
3096   queue_push2(&job, SOLVER_ERASE|SOLVER_CLEANDEPS|SOLVER_SOLVABLE_PROVIDES, pool_rel2id(pool, NAMESPACE_LANGUAGE, 0, REL_NAMESPACE, 1));
3097 #endif
3098
3099 #ifdef SOFTLOCKS_PATH
3100   addsoftlocks(pool, &job);
3101 #endif
3102
3103 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3104 rerunsolver:
3105 #endif
3106   solv = solver_create(pool);
3107   solver_set_flag(solv, SOLVER_FLAG_SPLITPROVIDES, 1);
3108 #ifdef FEDORA
3109   solver_set_flag(solv, SOLVER_FLAG_ALLOW_VENDORCHANGE, 1);
3110 #endif
3111   if (mainmode == MODE_ERASE)
3112     solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1);      /* don't nag */
3113   solver_set_flag(solv, SOLVER_FLAG_BEST_OBEY_POLICY, 1);
3114
3115   for (;;)
3116     {
3117       Id problem, solution;
3118       int pcnt, scnt;
3119
3120       if (!solver_solve(solv, &job))
3121         break;
3122       pcnt = solver_problem_count(solv);
3123       printf("Found %d problems:\n", pcnt);
3124       for (problem = 1; problem <= pcnt; problem++)
3125         {
3126           int take = 0;
3127           printf("Problem %d/%d:\n", problem, pcnt);
3128           solver_printprobleminfo(solv, problem);
3129           printf("\n");
3130           scnt = solver_solution_count(solv, problem);
3131           for (solution = 1; solution <= scnt; solution++)
3132             {
3133               printf("Solution %d:\n", solution);
3134               solver_printsolution(solv, problem, solution);
3135               printf("\n");
3136             }
3137           for (;;)
3138             {
3139               char inbuf[128], *ip;
3140               printf("Please choose a solution: ");
3141               fflush(stdout);
3142               *inbuf = 0;
3143               if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
3144                 {
3145                   printf("Abort.\n");
3146                   exit(1);
3147                 }
3148               while (*ip == ' ' || *ip == '\t')
3149                 ip++;
3150               if (*ip >= '0' && *ip <= '9')
3151                 {
3152                   take = atoi(ip);
3153                   if (take >= 1 && take <= scnt)
3154                     break;
3155                 }
3156               if (*ip == 's')
3157                 {
3158                   take = 0;
3159                   break;
3160                 }
3161               if (*ip == 'q')
3162                 {
3163                   printf("Abort.\n");
3164                   exit(1);
3165                 }
3166             }
3167           if (!take)
3168             continue;
3169           solver_take_solution(solv, problem, take, &job);
3170         }
3171     }
3172
3173   trans = solver_create_transaction(solv);
3174   if (!trans->steps.count)
3175     {
3176       printf("Nothing to do.\n");
3177       transaction_free(trans);
3178       solver_free(solv);
3179       queue_free(&job);
3180       pool_free(pool);
3181       free_repoinfos(repoinfos, nrepoinfos);
3182       solv_free(commandlinepkgs);
3183 #ifdef FEDORA
3184       yum_substitute(pool, 0);
3185 #endif
3186       exit(1);
3187     }
3188
3189   /* display transaction to the user and ask for confirmation */
3190   printf("\n");
3191   printf("Transaction summary:\n\n");
3192   transaction_print(trans);
3193 #if defined(SUSE)
3194   showdiskusagechanges(trans);
3195 #endif
3196   printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
3197   printf("\n");
3198
3199   if (!yesno("OK to continue (y/n)? "))
3200     {
3201       printf("Abort.\n");
3202       transaction_free(trans);
3203       solver_free(solv);
3204       queue_free(&job);
3205       pool_free(pool);
3206       free_repoinfos(repoinfos, nrepoinfos);
3207       solv_free(commandlinepkgs);
3208 #ifdef FEDORA
3209       yum_substitute(pool, 0);
3210 #endif
3211       exit(1);
3212     }
3213
3214   /* download all new packages */
3215   queue_init(&checkq);
3216   newpkgs = transaction_installedresult(trans, &checkq);
3217   newpkgsfps = 0;
3218   if (newpkgs)
3219     {
3220       int downloadsize = 0;
3221       for (i = 0; i < newpkgs; i++)
3222         {
3223           Solvable *s;
3224
3225           p = checkq.elements[i];
3226           s = pool_id2solvable(pool, p);
3227           downloadsize += solvable_lookup_sizek(s, SOLVABLE_DOWNLOADSIZE, 0);
3228         }
3229       printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
3230       newpkgsfps = solv_calloc(newpkgs, sizeof(*newpkgsfps));
3231       for (i = 0; i < newpkgs; i++)
3232         {
3233           unsigned int medianr;
3234           const char *loc;
3235           Solvable *s;
3236           struct repoinfo *cinfo;
3237           const unsigned char *chksum;
3238           Id chksumtype;
3239
3240           p = checkq.elements[i];
3241           s = pool_id2solvable(pool, p);
3242           if (s->repo == commandlinerepo)
3243             {
3244               loc = solvable_lookup_location(s, &medianr);
3245               if (!loc)
3246                 continue;
3247               if (!(newpkgsfps[i] = fopen(loc, "r")))
3248                 {
3249                   perror(loc);
3250                   exit(1);
3251                 }
3252               putchar('.');
3253               continue;
3254             }
3255           cinfo = s->repo->appdata;
3256           if (!cinfo)
3257             {
3258               printf("%s: no repository information\n", s->repo->name);
3259               exit(1);
3260             }
3261           loc = solvable_lookup_location(s, &medianr);
3262           if (!loc)
3263              continue;
3264 #if defined(ENABLE_RPMDB)
3265           if (pool->installed && pool->installed->nsolvables)
3266             {
3267               if ((newpkgsfps[i] = trydeltadownload(s, cinfo, loc)) != 0)
3268                 {
3269                   putchar('d');
3270                   fflush(stdout);
3271                   continue;             /* delta worked! */
3272                 }
3273             }
3274 #endif
3275 #ifdef ENABLE_SUSEREPO
3276           if (cinfo->type == TYPE_SUSETAGS)
3277             {
3278               const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
3279               loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
3280             }
3281 #endif
3282           chksumtype = 0;
3283           chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3284           if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
3285             {
3286               printf("\n%s: %s not found in repository\n", s->repo->name, loc);
3287               exit(1);
3288             }
3289           putchar('.');
3290           fflush(stdout);
3291         }
3292       putchar('\n');
3293     }
3294
3295 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3296   /* check for file conflicts */
3297   if (newpkgs)
3298     {
3299       Queue conflicts;
3300       struct fcstate fcstate;
3301
3302       printf("Searching for file conflicts\n");
3303       queue_init(&conflicts);
3304       fcstate.rpmstate = rpm_state_create(pool, rootdir);
3305       fcstate.newpkgscnt = newpkgs;
3306       fcstate.checkq = &checkq;
3307       fcstate.newpkgsfps = newpkgsfps;
3308       pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, FINDFILECONFLICTS_USE_SOLVABLEFILELIST | FINDFILECONFLICTS_CHECK_DIRALIASING | FINDFILECONFLICTS_USE_ROOTDIR, &fileconflict_cb, &fcstate);
3309       fcstate.rpmstate = rpm_state_free(fcstate.rpmstate);
3310       if (conflicts.count)
3311         {
3312           printf("\n");
3313           for (i = 0; i < conflicts.count; i += 6)
3314             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]));
3315           printf("\n");
3316           if (yesno("Re-run solver (y/n/q)? "))
3317             {
3318               for (i = 0; i < newpkgs; i++)
3319                 if (newpkgsfps[i])
3320                   fclose(newpkgsfps[i]);
3321               newpkgsfps = solv_free(newpkgsfps);
3322               solver_free(solv);
3323               solv = 0;
3324               pool_add_fileconflicts_deps(pool, &conflicts);
3325               goto rerunsolver;
3326             }
3327         }
3328       queue_free(&conflicts);
3329     }
3330 #endif
3331
3332   /* and finally commit the transaction */
3333   printf("Committing transaction:\n\n");
3334   transaction_order(trans, 0);
3335   for (i = 0; i < trans->steps.count; i++)
3336     {
3337 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3338       const char *evr, *evrp, *nvra;
3339 #endif
3340       Solvable *s;
3341       int j;
3342       FILE *fp;
3343       Id type;
3344
3345       p = trans->steps.elements[i];
3346       s = pool_id2solvable(pool, p);
3347       type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
3348       switch(type)
3349         {
3350         case SOLVER_TRANSACTION_ERASE:
3351           printf("erase %s\n", pool_solvid2str(pool, p));
3352 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3353           if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
3354             continue;
3355           /* strip epoch from evr */
3356           evr = evrp = pool_id2str(pool, s->evr);
3357           while (*evrp >= '0' && *evrp <= '9')
3358             evrp++;
3359           if (evrp > evr && evrp[0] == ':' && evrp[1])
3360             evr = evrp + 1;
3361           nvra = pool_tmpjoin(pool, pool_id2str(pool, s->name), "-", evr);
3362           nvra = pool_tmpappend(pool, nvra, ".", pool_id2str(pool, s->arch));
3363           runrpm("-e", nvra, -1, rootdir);      /* too bad that --querybynumber doesn't work */
3364 #endif
3365 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3366           rundpkg("--remove", pool_id2str(pool, s->name), 0, rootdir);
3367 #endif
3368           break;
3369         case SOLVER_TRANSACTION_INSTALL:
3370         case SOLVER_TRANSACTION_MULTIINSTALL:
3371           printf("install %s\n", pool_solvid2str(pool, p));
3372           for (j = 0; j < newpkgs; j++)
3373             if (checkq.elements[j] == p)
3374               break;
3375           fp = j < newpkgs ? newpkgsfps[j] : 0;
3376           if (!fp)
3377             continue;
3378           rewind(fp);
3379           lseek(fileno(fp), 0, SEEK_SET);
3380 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3381           runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp), rootdir);
3382 #endif
3383 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3384           rundpkg("--install", "/dev/fd/3", fileno(fp), rootdir);
3385 #endif
3386           fclose(fp);
3387           newpkgsfps[j] = 0;
3388           break;
3389         default:
3390           break;
3391         }
3392     }
3393
3394   for (i = 0; i < newpkgs; i++)
3395     if (newpkgsfps[i])
3396       fclose(newpkgsfps[i]);
3397   solv_free(newpkgsfps);
3398   queue_free(&checkq);
3399   transaction_free(trans);
3400   solver_free(solv);
3401   queue_free(&job);
3402   pool_free(pool);
3403   free_repoinfos(repoinfos, nrepoinfos);
3404   solv_free(commandlinepkgs);
3405 #ifdef FEDORA
3406   yum_substitute(pool, 0);
3407 #endif
3408   exit(0);
3409 }