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