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