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