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