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