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