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