fix build on debian
[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 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 #define CHKSUM_IDENT "1.1"
987
988 void
989 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
990 {
991   char buf[4096];
992   void *h = solv_chksum_create(chktype);
993   int l;
994
995   solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
996   while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
997     solv_chksum_add(h, buf, l);
998   rewind(fp);
999   solv_chksum_free(h, out);
1000 }
1001
1002 void
1003 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
1004 {
1005   void *h = solv_chksum_create(chktype);
1006   solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
1007   solv_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
1008   solv_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
1009   solv_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
1010   solv_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
1011   solv_chksum_free(h, out);
1012 }
1013
1014 void
1015 setarch(Pool *pool)
1016 {
1017   struct utsname un;
1018   if (uname(&un))
1019     {
1020       perror("uname");
1021       exit(1);
1022     }
1023   pool_setarch(pool, un.machine);
1024 }
1025
1026 char *calccachepath(Repo *repo, const char *repoext)
1027 {
1028   char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
1029   if (repoext)
1030     {
1031       p = pool_tmpappend(repo->pool, p, "_", repoext);
1032       p = pool_tmpappend(repo->pool, p, ".solvx", 0);
1033     }
1034   else
1035     p = pool_tmpappend(repo->pool, p, ".solv", 0);
1036   q = p + strlen(SOLVCACHE_PATH) + 1;
1037   if (*q == '.')
1038     *q = '_';
1039   for (; *q; q++)
1040     if (*q == '/')
1041       *q = '_';
1042   return p;
1043 }
1044
1045 int
1046 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie, int mark)
1047 {
1048   FILE *fp;
1049   unsigned char mycookie[32];
1050   unsigned char myextcookie[32];
1051   struct repoinfo *cinfo;
1052   int flags;
1053
1054   cinfo = repo->appdata;
1055   if (!(fp = fopen(calccachepath(repo, repoext), "r")))
1056     return 0;
1057   if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
1058     {
1059       fclose(fp);
1060       return 0;
1061     }
1062   if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
1063     {
1064       fclose(fp);
1065       return 0;
1066     }
1067   if (cinfo && !repoext)
1068     {
1069       if (fseek(fp, -sizeof(mycookie) * 2, SEEK_END) || fread(myextcookie, sizeof(myextcookie), 1, fp) != 1)
1070         {
1071           fclose(fp);
1072           return 0;
1073         }
1074     }
1075   rewind(fp);
1076
1077   flags = 0;
1078   if (repoext)
1079     {
1080       flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
1081       if (strcmp(repoext, "DL") != 0)
1082         flags |= REPO_LOCALPOOL;        /* no local pool for DL so that we can compare IDs */
1083     }
1084
1085   if (repo_add_solv(repo, fp, flags))
1086     {
1087       fclose(fp);
1088       return 0;
1089     }
1090   if (cinfo && !repoext)
1091     {
1092       memcpy(cinfo->cookie, mycookie, sizeof(mycookie));
1093       memcpy(cinfo->extcookie, myextcookie, sizeof(myextcookie));
1094     }
1095   if (mark)
1096     futimes(fileno(fp), 0);     /* try to set modification time */
1097   fclose(fp);
1098   return 1;
1099 }
1100
1101 void
1102 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
1103 {
1104   FILE *fp;
1105   int i, fd;
1106   char *tmpl;
1107   struct repoinfo *cinfo;
1108   int onepiece;
1109
1110   cinfo = repo->appdata;
1111   mkdir(SOLVCACHE_PATH, 0755);
1112   /* use dupjoin instead of tmpjoin because tmpl must survive repo_write */
1113   tmpl = solv_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
1114   fd = mkstemp(tmpl);
1115   if (fd < 0)
1116     {
1117       free(tmpl);
1118       return;
1119     }
1120   fchmod(fd, 0444);
1121   if (!(fp = fdopen(fd, "w")))
1122     {
1123       close(fd);
1124       unlink(tmpl);
1125       free(tmpl);
1126       return;
1127     }
1128
1129   onepiece = 1;
1130   for (i = repo->start; i < repo->end; i++)
1131    if (repo->pool->solvables[i].repo != repo)
1132      break;
1133   if (i < repo->end)
1134     onepiece = 0;
1135
1136   if (!info)
1137     repo_write(repo, fp);
1138   else if (repoext)
1139     repodata_write(info, fp);
1140   else
1141     {
1142       int oldnrepodata = repo->nrepodata;
1143       repo->nrepodata = oldnrepodata > 2 ? 2 : oldnrepodata;    /* XXX: do this right */
1144       repo_write(repo, fp);
1145       repo->nrepodata = oldnrepodata;
1146       onepiece = 0;
1147     }
1148
1149   if (!repoext && cinfo)
1150     {
1151       if (!cinfo->extcookie[0])
1152         {
1153           /* create the ext cookie and append it */
1154           /* we just need some unique ID */
1155           struct stat stb;
1156           if (!fstat(fileno(fp), &stb))
1157             {
1158               int i;
1159
1160               calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
1161               for (i = 0; i < 32; i++)
1162                 cinfo->extcookie[i] ^= cookie[i];
1163             }
1164           if (cinfo->extcookie[0] == 0)
1165             cinfo->extcookie[0] = 1;
1166         }
1167       if (fwrite(cinfo->extcookie, 32, 1, fp) != 1)
1168         {
1169           fclose(fp);
1170           unlink(tmpl);
1171           free(tmpl);
1172           return;
1173         }
1174     }
1175   /* append our cookie describing the metadata state */
1176   if (fwrite(cookie, 32, 1, fp) != 1)
1177     {
1178       fclose(fp);
1179       unlink(tmpl);
1180       free(tmpl);
1181       return;
1182     }
1183   if (fclose(fp))
1184     {
1185       unlink(tmpl);
1186       free(tmpl);
1187       return;
1188     }
1189   if (onepiece)
1190     {
1191       /* switch to just saved repo to activate paging and save memory */
1192       FILE *fp = fopen(tmpl, "r");
1193       if (fp)
1194         {
1195           if (!repoext)
1196             {
1197               /* main repo */
1198               repo_empty(repo, 1);
1199               if (repo_add_solv(repo, fp, SOLV_ADD_NO_STUBS))
1200                 {
1201                   /* oops, no way to recover from here */
1202                   fprintf(stderr, "internal error\n");
1203                   exit(1);
1204                 }
1205             }
1206           else
1207             {
1208               /* make sure repodata contains complete repo */
1209               /* (this is how repodata_write saves it) */
1210               repodata_extend_block(info, repo->start, repo->end - repo->start);
1211               info->state = REPODATA_LOADING;
1212               /* no need for LOCALPOOL as pool already contains ids */
1213               repo_add_solv(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1214               info->state = REPODATA_AVAILABLE; /* in case the load failed */
1215             }
1216           fclose(fp);
1217         }
1218     }
1219   if (!rename(tmpl, calccachepath(repo, repoext)))
1220     unlink(tmpl);
1221   free(tmpl);
1222 }
1223
1224
1225 static Pool *
1226 read_sigs()
1227 {
1228   Pool *sigpool = pool_create();
1229 #if defined(ENABLE_RPMDB_PUBKEY)
1230   Repo *repo = repo_create(sigpool, "rpmdbkeys");
1231   repo_add_rpmdb_pubkeys(repo, 0);
1232 #endif
1233   return sigpool;
1234 }
1235
1236
1237 #ifdef ENABLE_RPMMD
1238 /* repomd helpers */
1239
1240 static inline const char *
1241 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1242 {
1243   Pool *pool = repo->pool;
1244   Dataiterator di;
1245   const char *filename;
1246
1247   filename = 0;
1248   *chksump = 0;
1249   *chksumtypep = 0;
1250   dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1251   dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1252   if (dataiterator_step(&di))
1253     {
1254       dataiterator_setpos_parent(&di);
1255       filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1256       *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
1257     }
1258   dataiterator_free(&di);
1259   if (filename && !*chksumtypep)
1260     {
1261       printf("no %s file checksum!\n", what);
1262       filename = 0;
1263     }
1264   return filename;
1265 }
1266
1267 int
1268 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
1269 {
1270   Id chksumtype, handle;
1271   const unsigned char *chksum;
1272   const char *filename;
1273
1274   filename = repomd_find(repo, what, &chksum, &chksumtype);
1275   if (!filename)
1276     return 0;
1277   if (!strcmp(what, "prestodelta"))
1278     what = "deltainfo";
1279   handle = repodata_new_handle(data);
1280   repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
1281   repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
1282   repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
1283   if (!strcmp(what, "deltainfo"))
1284     {
1285       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
1286       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
1287     }
1288   if (!strcmp(what, "filelists"))
1289     {
1290       repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1291       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1292     }
1293   repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1294   return 1;
1295 }
1296
1297 int
1298 repomd_load_ext(Repo *repo, Repodata *data)
1299 {
1300   const char *filename, *repomdtype;
1301   char ext[3];
1302   FILE *fp;
1303   struct repoinfo *cinfo;
1304   const unsigned char *filechksum;
1305   Id filechksumtype;
1306   int r = 0;
1307
1308   cinfo = repo->appdata;
1309   repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
1310   if (!repomdtype)
1311     return 0;
1312   if (!strcmp(repomdtype, "filelists"))
1313     strcpy(ext, "FL");
1314   else if (!strcmp(repomdtype, "deltainfo"))
1315     strcpy(ext, "DL");
1316   else
1317     return 0;
1318 #if 1
1319   printf("[%s:%s", repo->name, ext);
1320 #endif
1321   if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1322     {
1323       printf(" cached]\n");fflush(stdout);
1324       return 1;
1325     }
1326   printf(" fetching]\n"); fflush(stdout);
1327   filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
1328   filechksumtype = 0;
1329   filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, REPOSITORY_REPOMD_CHECKSUM, &filechksumtype);
1330   if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1331     return 0;
1332   if (!strcmp(ext, "FL"))
1333     r = repo_add_rpmmd(repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1334   else if (!strcmp(ext, "DL"))
1335     r = repo_add_deltainfoxml(repo, fp, REPO_USE_LOADING);
1336   fclose(fp);
1337   if (r)
1338     {
1339       printf("%s\n", pool_errstr(repo->pool));
1340       return 0;
1341     }
1342   writecachedrepo(repo, data, ext, cinfo->extcookie);
1343   return 1;
1344 }
1345
1346 #endif
1347
1348
1349 #ifdef ENABLE_SUSEREPO
1350 /* susetags helpers */
1351
1352 static inline const char *
1353 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1354 {
1355   Pool *pool = repo->pool;
1356   Dataiterator di;
1357   const char *filename;
1358
1359   filename = 0;
1360   *chksump = 0;
1361   *chksumtypep = 0;
1362   dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
1363   dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1364   if (dataiterator_step(&di))
1365     {
1366       dataiterator_setpos_parent(&di);
1367       *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
1368       filename = what;
1369     }
1370   dataiterator_free(&di);
1371   if (filename && !*chksumtypep)
1372     {
1373       printf("no %s file checksum!\n", what);
1374       filename = 0;
1375     }
1376   return filename;
1377 }
1378
1379 static Id susetags_langtags[] = {
1380   SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
1381   SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
1382   SOLVABLE_EULA, REPOKEY_TYPE_STR,
1383   SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
1384   SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
1385   SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
1386   0, 0
1387 };
1388
1389 void
1390 susetags_add_ext(Repo *repo, Repodata *data)
1391 {
1392   Pool *pool = repo->pool;
1393   Dataiterator di;
1394   char ext[3];
1395   Id handle, filechksumtype;
1396   const unsigned char *filechksum;
1397   int i;
1398
1399   dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
1400   dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1401   while (dataiterator_step(&di))
1402     {
1403       if (strncmp(di.kv.str, "packages.", 9) != 0)
1404         continue;
1405       if (!strcmp(di.kv.str + 9, "gz"))
1406         continue;
1407       if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
1408         continue;
1409       ext[0] = di.kv.str[9];
1410       ext[1] = di.kv.str[10];
1411       ext[2] = 0;
1412       if (!strcmp(ext, "en"))
1413         continue;
1414       if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
1415         continue;
1416       handle = repodata_new_handle(data);
1417       repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
1418       if (filechksumtype)
1419         repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
1420       if (!strcmp(ext, "DU"))
1421         {
1422           repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
1423           repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
1424         }
1425       else if (!strcmp(ext, "FL"))
1426         {
1427           repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1428           repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1429         }
1430       else
1431         {
1432           for (i = 0; susetags_langtags[i]; i += 2)
1433             {
1434               repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
1435               repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
1436             }
1437         }
1438       repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1439     }
1440   dataiterator_free(&di);
1441 }
1442
1443 int
1444 susetags_load_ext(Repo *repo, Repodata *data)
1445 {
1446   const char *filename, *descrdir;
1447   Id defvendor;
1448   char ext[3];
1449   FILE *fp;
1450   struct repoinfo *cinfo;
1451   const unsigned char *filechksum;
1452   Id filechksumtype;
1453
1454   cinfo = repo->appdata;
1455   filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
1456   if (!filename)
1457     return 0;
1458   /* susetags load */
1459   ext[0] = filename[9];
1460   ext[1] = filename[10];
1461   ext[2] = 0;
1462 #if 1
1463   printf("[%s:%s", repo->name, ext);
1464 #endif
1465   if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1466     {
1467       printf(" cached]\n"); fflush(stdout);
1468       return 1;
1469     }
1470 #if 1
1471   printf(" fetching]\n"); fflush(stdout);
1472 #endif
1473   defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1474   descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1475   if (!descrdir)
1476     descrdir = "suse/setup/descr";
1477   filechksumtype = 0;
1478   filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
1479   if ((fp = curlfopen(cinfo, pool_tmpjoin(repo->pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1480     return 0;
1481   if (repo_add_susetags(repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES))
1482     {
1483       fclose(fp);
1484       printf("%s\n", pool_errstr(repo->pool));
1485       return 0;
1486     }
1487   fclose(fp);
1488   writecachedrepo(repo, data, ext, cinfo->extcookie);
1489   return 1;
1490 }
1491 #endif
1492
1493
1494
1495 /* load callback */
1496
1497 int
1498 load_stub(Pool *pool, Repodata *data, void *dp)
1499 {
1500   struct repoinfo *cinfo = data->repo->appdata;
1501   switch (cinfo->type)
1502     {
1503 #ifdef ENABLE_SUSEREPO
1504     case TYPE_SUSETAGS:
1505       return susetags_load_ext(data->repo, data);
1506 #endif
1507 #ifdef ENABLE_RPMMD
1508     case TYPE_RPMMD:
1509       return repomd_load_ext(data->repo, data);
1510 #endif
1511     default:
1512       return 0;
1513     }
1514 }
1515
1516 static unsigned char installedcookie[32];
1517
1518 #ifdef ENABLE_DEBIAN
1519
1520 const char *
1521 debian_find_component(struct repoinfo *cinfo, FILE *fp, char *comp, const unsigned char **chksump, Id *chksumtypep)
1522 {
1523   char buf[4096];
1524   Id chksumtype;
1525   unsigned char *chksum;
1526   Id curchksumtype;
1527   int l, compl;
1528   char *ch, *fn, *bp;
1529   char *filename;
1530   static char *basearch;
1531   char *binarydir;
1532   int lbinarydir;
1533
1534   if (!basearch)
1535     {
1536       struct utsname un;
1537       if (uname(&un))
1538         {
1539           perror("uname");
1540           exit(1);
1541         }
1542       basearch = strdup(un.machine);
1543       if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
1544         basearch[1] = '3';
1545     }
1546   binarydir = solv_dupjoin("binary-", basearch, "/");
1547   lbinarydir = strlen(binarydir);
1548   compl = strlen(comp);
1549   rewind(fp);
1550   curchksumtype = 0;
1551   filename = 0;
1552   chksum = solv_malloc(32);
1553   chksumtype = 0;
1554   while(fgets(buf, sizeof(buf), fp))
1555     {
1556       l = strlen(buf);
1557       if (l == 0)
1558         continue;
1559       while (l && (buf[l - 1] == '\n' || buf[l - 1] == ' ' || buf[l - 1] == '\t'))
1560         buf[--l] = 0;
1561       if (!strncasecmp(buf, "MD5Sum:", 7))
1562         {
1563           curchksumtype = REPOKEY_TYPE_MD5;
1564           continue;
1565         }
1566       if (!strncasecmp(buf, "SHA1:", 5))
1567         {
1568           curchksumtype = REPOKEY_TYPE_SHA1;
1569           continue;
1570         }
1571       if (!strncasecmp(buf, "SHA256:", 7))
1572         {
1573           curchksumtype = REPOKEY_TYPE_SHA256;
1574           continue;
1575         }
1576       if (!curchksumtype)
1577         continue;
1578       bp = buf;
1579       if (*bp++ != ' ')
1580         {
1581           curchksumtype = 0;
1582           continue;
1583         }
1584       ch = bp;
1585       while (*bp && *bp != ' ' && *bp != '\t')
1586         bp++;
1587       if (!*bp)
1588         continue;
1589       *bp++ = 0;
1590       while (*bp == ' ' || *bp == '\t')
1591         bp++;
1592       while (*bp && *bp != ' ' && *bp != '\t')
1593         bp++;
1594       if (!*bp)
1595         continue;
1596       while (*bp == ' ' || *bp == '\t')
1597         bp++;
1598       fn = bp;
1599       if (strncmp(fn, comp, compl) != 0 || fn[compl] != '/')
1600         continue;
1601       bp += compl + 1;
1602       if (strncmp(bp, binarydir, lbinarydir))
1603         continue;
1604       bp += lbinarydir;
1605       if (!strcmp(bp, "Packages") || !strcmp(bp, "Packages.gz"))
1606         {
1607           unsigned char curchksum[32];
1608           int curl;
1609           if (filename && !strcmp(bp, "Packages"))
1610             continue;
1611           curl = solv_chksum_len(curchksumtype);
1612           if (!curl || (chksumtype && solv_chksum_len(chksumtype) > curl))
1613             continue;
1614           if (solv_hex2bin((const char **)&ch, curchksum, sizeof(curchksum)) != curl)
1615             continue;
1616           solv_free(filename);
1617           filename = strdup(fn);
1618           chksumtype = curchksumtype;
1619           memcpy(chksum, curchksum, curl);
1620         }
1621     }
1622   free(binarydir);
1623   if (filename)
1624     {
1625       fn = solv_dupjoin("/", filename, 0);
1626       solv_free(filename);
1627       filename = solv_dupjoin("dists/", cinfo->name, fn);
1628       solv_free(fn);
1629     }
1630   if (!chksumtype)
1631     chksum = solv_free(chksum);
1632   *chksump = chksum;
1633   *chksumtypep = chksumtype;
1634   return filename;
1635 }
1636 #endif
1637
1638 void
1639 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
1640 {
1641   Repo *repo;
1642   struct repoinfo *cinfo;
1643   int i;
1644   FILE *fp;
1645   FILE *sigfp;
1646   const char *filename;
1647   const unsigned char *filechksum;
1648   Id filechksumtype;
1649 #ifdef ENABLE_SUSEREPO
1650   const char *descrdir;
1651   int defvendor;
1652 #endif
1653   struct stat stb;
1654   Pool *sigpool = 0;
1655 #if defined(ENABLE_SUSEREPO) || defined(ENABLE_RPMMD)
1656   Repodata *data;
1657 #endif
1658   int badchecksum;
1659   int dorefresh;
1660 #if defined(ENABLE_DEBIAN)
1661   FILE *fpr;
1662   int j;
1663 #endif
1664
1665   repo = repo_create(pool, "@System");
1666 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1667   printf("rpm database:");
1668   if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/rpm/Packages"), &stb))
1669     memset(&stb, 0, sizeof(&stb));
1670 #endif
1671 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1672   printf("dpgk database:");
1673   if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/dpkg/status"), &stb))
1674     memset(&stb, 0, sizeof(&stb));
1675 #endif
1676 #ifdef NOSYSTEM
1677   printf("no installed database:");
1678   memset(&stb, 0, sizeof(&stb));
1679 #endif
1680   calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, installedcookie);
1681   if (usecachedrepo(repo, 0, installedcookie, 0))
1682     printf(" cached\n");
1683   else
1684     {
1685 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1686       FILE *ofp;
1687       Repo *ref = 0;
1688 #endif
1689       printf(" reading\n");
1690
1691 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1692 # if defined(ENABLE_SUSEREPO) && defined(PRODUCTS_PATH)
1693       if (repo_add_products(repo, PRODUCTS_PATH, REPO_NO_INTERNALIZE | REPO_USE_ROOTDIR))
1694         {
1695           fprintf(stderr, "product reading failed: %s\n", pool_errstr(pool));
1696           exit(1);
1697         }
1698 # endif
1699       if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1700         {
1701           ref = repo_create(pool, "@System.old");
1702           if (repo_add_solv(ref, ofp, 0))
1703             {
1704               repo_free(ref, 1);
1705               ref = 0;
1706             }
1707           fclose(ofp);
1708         }
1709       if (repo_add_rpmdb(repo, ref, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
1710         {
1711           fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1712           exit(1);
1713         }
1714       if (ref)
1715         repo_free(ref, 1);
1716 #endif
1717 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1718       if (repo_add_debdb(repo, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
1719         {
1720           fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1721           exit(1);
1722         }
1723 #endif
1724       writecachedrepo(repo, 0, 0, installedcookie);
1725     }
1726   pool_set_installed(pool, repo);
1727
1728   for (i = 0; i < nrepoinfos; i++)
1729     {
1730       cinfo = repoinfos + i;
1731       if (!cinfo->enabled)
1732         continue;
1733
1734       repo = repo_create(pool, cinfo->alias);
1735       cinfo->repo = repo;
1736       repo->appdata = cinfo;
1737       repo->priority = 99 - cinfo->priority;
1738
1739       dorefresh = cinfo->autorefresh;
1740       if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0), &stb) == 0)
1741         {
1742           if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1743             dorefresh = 0;
1744         }
1745       if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1746         {
1747           printf("repo '%s':", cinfo->alias);
1748           printf(" cached\n");
1749           continue;
1750         }
1751       badchecksum = 0;
1752       switch (cinfo->type)
1753         {
1754 #ifdef ENABLE_RPMMD
1755         case TYPE_RPMMD:
1756           printf("rpmmd repo '%s':", cinfo->alias);
1757           fflush(stdout);
1758           if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1759             {
1760               printf(" no repomd.xml file, skipped\n");
1761               repo_free(repo, 1);
1762               cinfo->repo = 0;
1763               break;
1764             }
1765           calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1766           if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1767             {
1768               printf(" cached\n");
1769               fclose(fp);
1770               break;
1771             }
1772           if (cinfo->repo_gpgcheck)
1773             {
1774               sigfp = curlfopen(cinfo, "repodata/repomd.xml.asc", 0, 0, 0, 0);
1775               if (!sigfp)
1776                 {
1777                   printf(" unsigned, skipped\n");
1778                   fclose(fp);
1779                   break;
1780                 }
1781               if (!sigpool)
1782                 sigpool = read_sigs();
1783               if (!checksig(sigpool, fp, sigfp))
1784                 {
1785                   printf(" checksig failed, skipped\n");
1786                   fclose(sigfp);
1787                   fclose(fp);
1788                   break;
1789                 }
1790               fclose(sigfp);
1791             }
1792           if (repo_add_repomdxml(repo, fp, 0))
1793             {
1794               printf("repomd.xml: %s\n", pool_errstr(pool));
1795               fclose(fp);
1796               break;    /* hopeless */
1797             }
1798           fclose(fp);
1799           printf(" fetching\n");
1800           filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1801           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1802             {
1803               if (repo_add_rpmmd(repo, fp, 0, 0))
1804                 {
1805                   printf("primary: %s\n", pool_errstr(pool));
1806                   badchecksum = 1;
1807                 }
1808               fclose(fp);
1809             }
1810           if (badchecksum)
1811             break;      /* hopeless */
1812
1813           filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1814           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1815             {
1816               if (repo_add_updateinfoxml(repo, fp, 0))
1817                 {
1818                   printf("updateinfo: %s\n", pool_errstr(pool));
1819                   badchecksum = 1;
1820                 }
1821               fclose(fp);
1822             }
1823
1824           data = repo_add_repodata(repo, 0);
1825           if (!repomd_add_ext(repo, data, "deltainfo"))
1826             repomd_add_ext(repo, data, "prestodelta");
1827           repomd_add_ext(repo, data, "filelists");
1828           repodata_internalize(data);
1829           if (!badchecksum)
1830             writecachedrepo(repo, 0, 0, cinfo->cookie);
1831           repodata_create_stubs(repo_last_repodata(repo));
1832           break;
1833 #endif
1834
1835 #ifdef ENABLE_SUSEREPO
1836         case TYPE_SUSETAGS:
1837           printf("susetags repo '%s':", cinfo->alias);
1838           fflush(stdout);
1839           descrdir = 0;
1840           defvendor = 0;
1841           if ((fp = curlfopen(cinfo, "content", 0, 0, 0, 0)) == 0)
1842             {
1843               printf(" no content file, skipped\n");
1844               repo_free(repo, 1);
1845               cinfo->repo = 0;
1846               break;
1847             }
1848           calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1849           if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1850             {
1851               printf(" cached\n");
1852               fclose(fp);
1853               break;
1854             }
1855           if (cinfo->repo_gpgcheck)
1856             {
1857               sigfp = curlfopen(cinfo, "content.asc", 0, 0, 0, 0);
1858               if (!sigfp)
1859                 {
1860                   printf(" unsigned, skipped\n");
1861                   fclose(fp);
1862                   break;
1863                 }
1864               if (sigfp)
1865                 {
1866                   if (!sigpool)
1867                     sigpool = read_sigs();
1868                   if (!checksig(sigpool, fp, sigfp))
1869                     {
1870                       printf(" checksig failed, skipped\n");
1871                       fclose(sigfp);
1872                       fclose(fp);
1873                       break;
1874                     }
1875                   fclose(sigfp);
1876                 }
1877             }
1878           if (repo_add_content(repo, fp, 0))
1879             {
1880               printf("content: %s\n", pool_errstr(pool));
1881               fclose(fp);
1882               break;    /* hopeless */
1883             }
1884           fclose(fp);
1885           defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1886           descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1887           if (!descrdir)
1888             descrdir = "suse/setup/descr";
1889           filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1890           if (!filename)
1891             filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1892           if (!filename)
1893             {
1894               printf(" no packages file entry, skipped\n");
1895               break;
1896             }
1897           printf(" fetching\n");
1898           if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
1899             break;      /* hopeless */
1900           if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|SUSETAGS_RECORD_SHARES))
1901             {
1902               printf("packages: %s\n", pool_errstr(pool));
1903               fclose(fp);
1904               break;    /* hopeless */
1905             }
1906           fclose(fp);
1907           /* add default language */
1908           filename = susetags_find(repo, "packages.en.gz", &filechksum, &filechksumtype);
1909           if (!filename)
1910             filename = susetags_find(repo, "packages.en", &filechksum, &filechksumtype);
1911           if (filename)
1912             {
1913               if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1914                 {
1915                   if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|REPO_REUSE_REPODATA|REPO_EXTEND_SOLVABLES))
1916                     {
1917                       printf("packages.en: %s\n", pool_errstr(pool));
1918                       badchecksum = 1;
1919                     }
1920                   fclose(fp);
1921                 }
1922             }
1923           filename = susetags_find(repo, "patterns", &filechksum, &filechksumtype);
1924           if (filename)
1925             {
1926               if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1927                 {
1928                   char pbuf[256];
1929                   while (fgets(pbuf, sizeof(pbuf), fp))
1930                     {
1931                       int l = strlen(pbuf);
1932                       FILE *fp2;
1933                       if (l && pbuf[l - 1] == '\n')
1934                         pbuf[--l] = 0;
1935                       if (!*pbuf || *pbuf == '.' || strchr(pbuf, '/') != 0)
1936                         continue;
1937                       filename = susetags_find(repo, pbuf, &filechksum, &filechksumtype);
1938                       if (filename && (fp2 = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1939                         {
1940                           if (repo_add_susetags(repo, fp2, defvendor, 0, REPO_NO_INTERNALIZE))
1941                             {
1942                               printf("%s: %s\n", pbuf, pool_errstr(pool));
1943                               badchecksum = 1;
1944                             }
1945                           fclose(fp2);
1946                         }
1947                     }
1948                   fclose(fp);
1949                 }
1950             }
1951           repo_internalize(repo);
1952           data = repo_add_repodata(repo, 0);
1953           susetags_add_ext(repo, data);
1954           repodata_internalize(data);
1955           if (!badchecksum)
1956             writecachedrepo(repo, 0, 0, cinfo->cookie);
1957           repodata_create_stubs(repo_last_repodata(repo));
1958           break;
1959 #endif
1960
1961 #if defined(ENABLE_DEBIAN)
1962         case TYPE_DEBIAN:
1963           printf("debian repo '%s':", cinfo->alias);
1964           fflush(stdout);
1965           filename = solv_dupjoin("dists/", cinfo->name, "/Release");
1966           if ((fpr = curlfopen(cinfo, filename, 0, 0, 0, 0)) == 0)
1967             {
1968               printf(" no Release file, skipped\n");
1969               repo_free(repo, 1);
1970               cinfo->repo = 0;
1971               free((char *)filename);
1972               break;
1973             }
1974           solv_free((char *)filename);
1975           if (cinfo->repo_gpgcheck)
1976             {
1977               filename = solv_dupjoin("dists/", cinfo->name, "/Release.gpg");
1978               sigfp = curlfopen(cinfo, filename, 0, 0, 0, 0);
1979               solv_free((char *)filename);
1980               if (!sigfp)
1981                 {
1982                   printf(" unsigned, skipped\n");
1983                   fclose(fpr);
1984                   break;
1985                 }
1986               if (!sigpool)
1987                 sigpool = read_sigs();
1988               if (!checksig(sigpool, fpr, sigfp))
1989                 {
1990                   printf(" checksig failed, skipped\n");
1991                   fclose(sigfp);
1992                   fclose(fpr);
1993                   break;
1994                 }
1995               fclose(sigfp);
1996             }
1997           calc_checksum_fp(fpr, REPOKEY_TYPE_SHA256, cinfo->cookie);
1998           if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1999             {
2000               printf(" cached\n");
2001               fclose(fpr);
2002               break;
2003             }
2004           printf(" fetching\n");
2005           for (j = 0; j < cinfo->ncomponents; j++)
2006             {
2007               if (!(filename = debian_find_component(cinfo, fpr, cinfo->components[j], &filechksum, &filechksumtype)))
2008                 {
2009                   printf("[component %s not found]\n", cinfo->components[j]);
2010                   continue;
2011                 }
2012               if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
2013                 {
2014                   if (repo_add_debpackages(repo, fp, 0))
2015                     {
2016                       printf("component %s: %s\n", cinfo->components[j], pool_errstr(pool));
2017                       badchecksum = 1;
2018                     }
2019                   fclose(fp);
2020                 }
2021               solv_free((char *)filechksum);
2022               solv_free((char *)filename);
2023             }
2024           fclose(fpr);
2025           if (!badchecksum)
2026             writecachedrepo(repo, 0, 0, cinfo->cookie);
2027           break;
2028 #endif
2029
2030         default:
2031           printf("unsupported repo '%s': skipped\n", cinfo->alias);
2032           repo_free(repo, 1);
2033           cinfo->repo = 0;
2034           break;
2035         }
2036     }
2037   if (sigpool)
2038     pool_free(sigpool);
2039 }
2040
2041
2042 int
2043 str2archid(Pool *pool, char *arch)
2044 {
2045   Id id;
2046   if (!*arch)
2047     return 0;
2048   id = pool_str2id(pool, arch, 0);
2049   if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
2050     return id;
2051   if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
2052     return 0;
2053   return id;
2054 }
2055
2056
2057 #define DEPGLOB_NAME     1
2058 #define DEPGLOB_DEP      2
2059 #define DEPGLOB_NAMEDEP  3
2060
2061 int
2062 depglob(Pool *pool, char *name, Queue *job, int what)
2063 {
2064   Id p, pp;
2065   Id id = pool_str2id(pool, name, 0);
2066   int i, match = 0;
2067
2068   if (id)
2069     {
2070       FOR_PROVIDES(p, pp, id)
2071         {
2072           Solvable *s = pool->solvables + p;
2073           match = 1;
2074           if (s->name == id && (what & DEPGLOB_NAME) != 0)
2075             {
2076               queue_push2(job, SOLVER_SOLVABLE_NAME, id);
2077               return 1;
2078             }
2079         }
2080       if (match)
2081         {
2082           if (what == DEPGLOB_NAMEDEP)
2083             printf("[using capability match for '%s']\n", name);
2084           queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
2085           return 1;
2086         }
2087     }
2088
2089   if (strpbrk(name, "[*?") == 0)
2090     return 0;
2091
2092   if ((what & DEPGLOB_NAME) != 0)
2093     {
2094       /* looks like a name glob. hard work. */
2095       for (p = 1; p < pool->nsolvables; p++)
2096         {
2097           Solvable *s = pool->solvables + p;
2098           if (!s->repo || !pool_installable(pool, s))
2099             continue;
2100           id = s->name;
2101           if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
2102             {
2103               for (i = 0; i < job->count; i += 2)
2104                 if (job->elements[i] == SOLVER_SOLVABLE_NAME && job->elements[i + 1] == id)
2105                   break;
2106               if (i == job->count)
2107                 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
2108               match = 1;
2109             }
2110         }
2111       if (match)
2112         return 1;
2113     }
2114   if ((what & DEPGLOB_DEP))
2115     {
2116       /* looks like a dep glob. really hard work. */
2117       for (id = 1; id < pool->ss.nstrings; id++)
2118         {
2119           if (!pool->whatprovides[id])
2120             continue;
2121           if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
2122             {
2123               if (!match && what == DEPGLOB_NAMEDEP)
2124                 printf("[using capability match for '%s']\n", name);
2125               for (i = 0; i < job->count; i += 2)
2126                 if (job->elements[i] == SOLVER_SOLVABLE_PROVIDES && job->elements[i + 1] == id)
2127                   break;
2128               if (i == job->count)
2129                 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
2130               match = 1;
2131             }
2132         }
2133       if (match)
2134         return 1;
2135     }
2136   return 0;
2137 }
2138
2139 int
2140 limitrelation(Pool *pool, Queue *job, int flags, Id evr)
2141 {
2142   int i, j;
2143   Id p, pp;
2144   for (i = j = 0; i < job->count; i += 2)
2145     {
2146       Id select = job->elements[i] & SOLVER_SELECTMASK;
2147       if (select != SOLVER_SOLVABLE_NAME && select != SOLVER_SOLVABLE_PROVIDES)
2148         {
2149           fprintf(stderr, "limitrelation only works on name/provides jobs\n");
2150           exit(1);
2151         }
2152       job->elements[i + 1] = pool_rel2id(pool, job->elements[i + 1], evr, flags, 1);
2153       if (flags == REL_ARCH)
2154         job->elements[i] |= SOLVER_SETARCH;
2155       if (flags == REL_EQ && select == SOLVER_SOLVABLE_NAME && job->elements[i])
2156         {
2157 #ifndef DEBIAN
2158           const char *evrstr = pool_id2str(pool, evr);
2159           if (!strchr(evrstr, '-'))
2160             job->elements[i] |= SOLVER_SETEV;
2161           else
2162 #endif
2163             job->elements[i] |= SOLVER_SETEVR;
2164         }
2165       /* make sure we still have matches */
2166       FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
2167         break;
2168       if (p)
2169         {
2170           job->elements[j] = job->elements[i];
2171           job->elements[j + 1] = job->elements[i + 1];
2172           j += 2;
2173         }
2174     }
2175   queue_truncate(job, j);
2176   return j / 2;
2177 }
2178
2179 int
2180 limitrelation_arch(Pool *pool, Queue *job, int flags, char *evr)
2181 {
2182   char *r;
2183   Id archid;
2184   if ((r = strrchr(evr, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
2185     {
2186       *r = 0;
2187       limitrelation(pool, job, REL_ARCH, archid);
2188       limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
2189       *r = '.';
2190     }
2191   else
2192     limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
2193   return job->count / 2;
2194 }
2195
2196 int
2197 limitrepo(Pool *pool, Id repofilter, Queue *job)
2198 {
2199   Queue mq;
2200   Id p, pp;
2201   int i, j;
2202   Solvable *s;
2203
2204   queue_init(&mq);
2205   for (i = j = 0; i < job->count; i += 2)
2206     {
2207       queue_empty(&mq);
2208       FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
2209         {
2210           s = pool_id2solvable(pool, p);
2211           if (s->repo && s->repo->repoid == repofilter)
2212              queue_push(&mq, p);
2213         }
2214       if (mq.count)
2215         {
2216           /* here we assume that repo == vendor, so we also set SOLVER_SETVENDOR */
2217           Id flags = (job->elements[i] & SOLVER_SETMASK) | SOLVER_SETVENDOR | SOLVER_SETREPO;
2218           if ((job->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_NAME)
2219             flags |= SOLVER_SETNAME;
2220           if (mq.count == 1)
2221             {
2222               job->elements[j] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET | flags;
2223               job->elements[j + 1] = mq.elements[0];
2224             }
2225           else
2226             {
2227               job->elements[j] = SOLVER_SOLVABLE_ONE_OF | flags;
2228               job->elements[j + 1] = pool_queuetowhatprovides(pool, &mq);
2229             }
2230           j += 2;
2231         }
2232     }
2233   queue_truncate(job, j);
2234   queue_free(&mq);
2235   return j / 2;
2236 }
2237
2238 int
2239 mkselect(Pool *pool, int mode, char *name, Queue *job)
2240 {
2241   char *r, *r2;
2242   Id archid;
2243
2244   if (*name == '/')
2245     {
2246       Dataiterator di;
2247       int type = strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
2248       Queue q;
2249       queue_init(&q);
2250       dataiterator_init(&di, pool, mode == SOLVER_ERASE ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
2251       while (dataiterator_step(&di))
2252         {
2253           Solvable *s = pool->solvables + di.solvid;
2254           if (!s->repo || !pool_installable(pool, s))
2255             continue;
2256           queue_push(&q, di.solvid);
2257           dataiterator_skip_solvable(&di);
2258         }
2259       dataiterator_free(&di);
2260       if (q.count)
2261         {
2262           printf("[using file list match for '%s']\n", name);
2263           if (q.count > 1)
2264             queue_push2(job, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
2265           else
2266             queue_push2(job, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
2267           queue_free(&q);
2268           return job->count / 2;
2269         }
2270     }
2271   if ((r = strpbrk(name, "<=>")) != 0)
2272     {
2273       /* relation case, support:
2274        * depglob rel
2275        * depglob.arch rel
2276        */
2277       int rflags = 0;
2278       int nend = r - name;
2279       char oldnend;
2280       for (; *r; r++)
2281         {
2282           if (*r == '<')
2283             rflags |= REL_LT;
2284           else if (*r == '=')
2285             rflags |= REL_EQ;
2286           else if (*r == '>')
2287             rflags |= REL_GT;
2288           else
2289             break;
2290         }
2291       while (*r && *r == ' ' && *r == '\t')
2292         r++;
2293       while (nend && (name[nend - 1] == ' ' || name[nend -1 ] == '\t'))
2294         nend--;
2295       if (!*name || !*r)
2296         {
2297           fprintf(stderr, "bad relation\n");
2298           exit(1);
2299         }
2300       oldnend = name[nend];
2301       name[nend] = 0;
2302       if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2303         {
2304           name[nend] = oldnend;
2305           limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
2306           return job->count / 2;
2307         }
2308       if ((r2 = strrchr(name, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
2309         {
2310           *r2 = 0;
2311           if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2312             {
2313               name[nend] = oldnend;
2314               *r2 = '.';
2315               limitrelation(pool, job, REL_ARCH, archid);
2316               limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
2317               return job->count / 2;
2318             }
2319           *r2 = '.';
2320         }
2321       name[nend] = oldnend;
2322     }
2323   else
2324     {
2325       /* no relation case, support:
2326        * depglob
2327        * depglob.arch
2328        * nameglob-version
2329        * nameglob-version.arch
2330        * nameglob-version-release
2331        * nameglob-version-release.arch
2332        */
2333       if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2334         return job->count / 2;
2335       if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
2336         {
2337           *r = 0;
2338           if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2339             {
2340               *r = '.';
2341               limitrelation(pool, job, REL_ARCH, archid);
2342               return job->count / 2;
2343             }
2344           *r = '.';
2345         }
2346       if ((r = strrchr(name, '-')) != 0)
2347         {
2348           *r = 0;
2349           if (depglob(pool, name, job, DEPGLOB_NAME))
2350             {
2351               /* have just the version */
2352               limitrelation_arch(pool, job, REL_EQ, r + 1);
2353               *r = '-';
2354               return job->count / 2;
2355             }
2356           if ((r2 = strrchr(name, '-')) != 0)
2357             {
2358               *r = '-';
2359               *r2 = 0;
2360               r = r2;
2361               if (depglob(pool, name, job, DEPGLOB_NAME))
2362                 {
2363                   /* have version-release */
2364                   limitrelation_arch(pool, job, REL_EQ, r + 1);
2365                   *r = '-';
2366                   return job->count / 2;
2367                 }
2368             }
2369           *r = '-';
2370         }
2371     }
2372   return 0;
2373 }
2374
2375
2376 int
2377 yesno(const char *str)
2378 {
2379   char inbuf[128], *ip;
2380
2381   for (;;)
2382     {
2383       printf("%s", str);
2384       fflush(stdout);
2385       *inbuf = 0;
2386       if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2387         {
2388           printf("Abort.\n");
2389           exit(1);
2390         }
2391       while (*ip == ' ' || *ip == '\t')
2392         ip++;
2393       if (*ip == 'q')
2394         {
2395           printf("Abort.\n");
2396           exit(1);
2397         }
2398       if (*ip == 'y' || *ip == 'n')
2399         return *ip == 'y' ? 1 : 0;
2400     }
2401 }
2402
2403 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2404
2405 struct fcstate {
2406   FILE **newpkgsfps;
2407   Queue *checkq;
2408   int newpkgscnt;
2409   void *rpmdbstate;
2410 };
2411
2412 static void *
2413 fileconflict_cb(Pool *pool, Id p, void *cbdata)
2414 {
2415   struct fcstate *fcstate = cbdata;
2416   Solvable *s;
2417   Id rpmdbid;
2418   int i;
2419   FILE *fp;
2420
2421   if (!p)
2422     {
2423       rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
2424       return 0;
2425     }
2426   s = pool_id2solvable(pool, p);
2427   if (pool->installed && s->repo == pool->installed)
2428     {
2429       if (!s->repo->rpmdbid)
2430         return 0;
2431       rpmdbid = s->repo->rpmdbid[p - s->repo->start];
2432       if (!rpmdbid)
2433         return 0;
2434        return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
2435     }
2436   for (i = 0; i < fcstate->newpkgscnt; i++)
2437     if (fcstate->checkq->elements[i] == p)
2438       break;
2439   if (i == fcstate->newpkgscnt)
2440     return 0;
2441   fp = fcstate->newpkgsfps[i];
2442   if (!fp)
2443     return 0;
2444   rewind(fp);
2445   return rpm_byfp(fp, pool_solvable2str(pool, s), &fcstate->rpmdbstate);
2446 }
2447
2448
2449 void
2450 runrpm(const char *arg, const char *name, int dupfd3, const char *rootdir)
2451 {
2452   pid_t pid;
2453   int status;
2454
2455   if ((pid = fork()) == (pid_t)-1)
2456     {
2457       perror("fork");
2458       exit(1);
2459     }
2460   if (pid == 0)
2461     {
2462       if (!rootdir)
2463         rootdir = "/";
2464       if (dupfd3 != -1 && dupfd3 != 3)
2465         {
2466           dup2(dupfd3, 3);
2467           close(dupfd3);
2468         }
2469       if (dupfd3 != -1)
2470         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
2471       if (strcmp(arg, "-e") == 0)
2472         execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2473       else
2474         execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2475       perror("rpm");
2476       _exit(0);
2477     }
2478   while (waitpid(pid, &status, 0) != pid)
2479     ;
2480   if (status)
2481     {
2482       printf("rpm failed\n");
2483       exit(1);
2484     }
2485 }
2486
2487 #endif
2488
2489 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2490
2491 void
2492 rundpkg(const char *arg, const char *name, int dupfd3, const char *rootdir)
2493 {
2494   pid_t pid;
2495   int status;
2496
2497   if ((pid = fork()) == (pid_t)-1)
2498     {
2499       perror("fork");
2500       exit(1);
2501     }
2502   if (pid == 0)
2503     {
2504       if (!rootdir)
2505         rootdir = "/";
2506       if (dupfd3 != -1 && dupfd3 != 3)
2507         {
2508           dup2(dupfd3, 3);
2509           close(dupfd3);
2510         }
2511       if (dupfd3 != -1)
2512         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
2513       if (strcmp(arg, "--install") == 0)
2514         execlp("dpkg", "dpkg", "--install", "--root", rootdir, "--force", "all", name, (char *)0);
2515       else
2516         execlp("dpkg", "dpkg", "--remove", "--root", rootdir, "--force", "all", name, (char *)0);
2517       perror("dpkg");
2518       _exit(0);
2519     }
2520   while (waitpid(pid, &status, 0) != pid)
2521     ;
2522   if (status)
2523     {
2524       printf("dpkg failed\n");
2525       exit(1);
2526     }
2527 }
2528
2529 #endif
2530
2531 static Id
2532 nscallback(Pool *pool, void *data, Id name, Id evr)
2533 {
2534   if (name == NAMESPACE_PRODUCTBUDDY)
2535     {
2536       /* SUSE specific hack: each product has an associated rpm */
2537       Solvable *s = pool->solvables + evr;
2538       Id p, pp, cap;
2539       Id bestp = 0;
2540
2541       cap = pool_str2id(pool, pool_tmpjoin(pool, "product(", pool_id2str(pool, s->name) + 8, ")"), 0);
2542       if (!cap)
2543         return 0;
2544       cap = pool_rel2id(pool, cap, s->evr, REL_EQ, 0);
2545       if (!cap)
2546         return 0;
2547       FOR_PROVIDES(p, pp, cap)
2548         {
2549           Solvable *ps = pool->solvables + p;
2550           if (ps->repo == s->repo && ps->arch == s->arch)
2551             if (!bestp || pool_evrcmp(pool, pool->solvables[bestp].evr, ps->evr, EVRCMP_COMPARE) < 0)
2552               bestp = p;
2553         }
2554       return bestp;
2555     }
2556   return 0;
2557 }
2558
2559 #ifdef SOFTLOCKS_PATH
2560
2561 void
2562 addsoftlocks(Pool *pool, Queue *job)
2563 {
2564   FILE *fp;
2565   Id type, id, p, pp;
2566   char *bp, *ep, buf[4096];
2567
2568   if ((fp = fopen(SOFTLOCKS_PATH, "r")) == 0)
2569     return;
2570   while((bp = fgets(buf, sizeof(buf), fp)) != 0)
2571     {
2572       while (*bp == ' ' || *bp == '\t')
2573         bp++;
2574       if (!*bp || *bp == '#')
2575         continue;
2576       for (ep = bp; *ep; ep++)
2577         if (*ep == ' ' || *ep == '\t' || *ep == '\n')
2578           break;
2579       *ep = 0;
2580       type = SOLVER_SOLVABLE_NAME;
2581       if (!strncmp(bp, "provides:", 9) && bp[9])
2582         {
2583           type = SOLVER_SOLVABLE_PROVIDES;
2584           bp += 9;
2585         }
2586       id = pool_str2id(pool, bp, 1);
2587       if (pool->installed)
2588         {
2589           FOR_JOB_SELECT(p, pp, type, id)
2590             if (pool->solvables[p].repo == pool->installed)
2591               break;
2592           if (p)
2593             continue;   /* ignore, as it is already installed */
2594         }
2595       queue_push2(job, SOLVER_LOCK|SOLVER_WEAK|type, id);
2596     }
2597   fclose(fp);
2598 }
2599
2600 #endif
2601
2602
2603 void
2604 rewrite_repos(Pool *pool, Queue *addedfileprovides, Queue *addedfileprovides_inst)
2605 {
2606   Repo *repo;
2607   Repodata *data;
2608   Map providedids;
2609   Queue fileprovidesq;
2610   int i, j, n;
2611   struct repoinfo *cinfo;
2612
2613   map_init(&providedids, pool->ss.nstrings);
2614   queue_init(&fileprovidesq);
2615   for (i = 0; i < addedfileprovides->count; i++)
2616     MAPSET(&providedids, addedfileprovides->elements[i]);
2617   FOR_REPOS(i, repo)
2618     {
2619       /* make sure all repodatas but the first are extensions */
2620       if (repo->nrepodata < 2)
2621         continue;
2622       data = repo_id2repodata(repo, 1);
2623       if (data->loadcallback)
2624         continue;
2625       for (j = 2; j < repo->nrepodata; j++)
2626         {
2627           Repodata *edata = repo_id2repodata(repo, j);
2628           if (!edata->loadcallback)
2629             break;
2630         }
2631       if (j < repo->nrepodata)
2632         continue;       /* found a non-externsion repodata, can't rewrite  */
2633       if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
2634         {
2635           if (repo == pool->installed && addedfileprovides_inst)
2636             {
2637               for (j = 0; j < addedfileprovides->count; j++)
2638                 MAPCLR(&providedids, addedfileprovides->elements[j]);
2639               for (j = 0; j < addedfileprovides_inst->count; j++)
2640                 MAPSET(&providedids, addedfileprovides_inst->elements[j]);
2641             }
2642           n = 0;
2643           for (j = 0; j < fileprovidesq.count; j++)
2644             if (MAPTST(&providedids, fileprovidesq.elements[j]))
2645               n++;
2646           if (repo == pool->installed && addedfileprovides_inst)
2647             {
2648               for (j = 0; j < addedfileprovides_inst->count; j++)
2649                 MAPCLR(&providedids, addedfileprovides_inst->elements[j]);
2650               for (j = 0; j < addedfileprovides->count; j++)
2651                 MAPSET(&providedids, addedfileprovides->elements[j]);
2652               if (n == addedfileprovides_inst->count)
2653                 continue;       /* nothing new added */
2654             }
2655           else if (n == addedfileprovides->count)
2656             continue;   /* nothing new added */
2657         }
2658       repodata_set_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, repo == pool->installed && addedfileprovides_inst ? addedfileprovides_inst : addedfileprovides);
2659       repodata_internalize(data);
2660       cinfo = repo->appdata;
2661       writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
2662     }
2663   queue_free(&fileprovidesq);
2664   map_free(&providedids);
2665 }
2666
2667 static void
2668 select_patches(Pool *pool, Queue *job)
2669 {
2670   Id p, pp;
2671   int pruneyou = 0;
2672   Map installedmap, noobsmap;
2673   Solvable *s;
2674
2675   map_init(&noobsmap, 0);
2676   map_init(&installedmap, pool->nsolvables);
2677   solver_calculate_noobsmap(pool, job, &noobsmap);
2678   if (pool->installed)
2679     FOR_REPO_SOLVABLES(pool->installed, p, s)
2680       MAPSET(&installedmap, p);
2681
2682   /* install all patches */
2683   for (p = 1; p < pool->nsolvables; p++)
2684     {
2685       const char *type;
2686       int r;
2687       Id p2;
2688
2689       s = pool->solvables + p;
2690       if (strncmp(pool_id2str(pool, s->name), "patch:", 6) != 0)
2691         continue;
2692       FOR_PROVIDES(p2, pp, s->name)
2693         {
2694           Solvable *s2 = pool->solvables + p2;
2695           if (s2->name != s->name)
2696             continue;
2697           r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2698           if (r < 0 || (r == 0 && p > p2))
2699             break;
2700         }
2701       if (p2)
2702         continue;
2703       type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2704       if (type && !strcmp(type, "optional"))
2705         continue;
2706       r = solvable_trivial_installable_map(s, &installedmap, 0, &noobsmap);
2707       if (r == -1)
2708         continue;
2709       if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2710         {
2711           if (!pruneyou++)
2712             queue_empty(job);
2713         }
2714       else if (pruneyou)
2715         continue;
2716       queue_push2(job, SOLVER_SOLVABLE, p);
2717     }
2718   map_free(&installedmap);
2719   map_free(&noobsmap);
2720 }
2721
2722 #define MODE_LIST        0
2723 #define MODE_INSTALL     1
2724 #define MODE_ERASE       2
2725 #define MODE_UPDATE      3
2726 #define MODE_DISTUPGRADE 4
2727 #define MODE_VERIFY      5
2728 #define MODE_PATCH       6
2729 #define MODE_INFO        7
2730 #define MODE_REPOLIST    8
2731 #define MODE_SEARCH      9
2732
2733 void
2734 usage(int r)
2735 {
2736   fprintf(stderr, "Usage: solv COMMAND <select>\n");
2737   fprintf(stderr, "\n");
2738   fprintf(stderr, "    dist-upgrade: replace installed packages with\n");
2739   fprintf(stderr, "                  versions from the repositories\n");
2740   fprintf(stderr, "    erase:        erase installed packages\n");
2741   fprintf(stderr, "    info:         display package information\n");
2742   fprintf(stderr, "    install:      install packages\n");
2743   fprintf(stderr, "    list:         list packages\n");
2744   fprintf(stderr, "    repos:        list enabled repositories\n");
2745   fprintf(stderr, "    search:       search name/summary/description\n");
2746   fprintf(stderr, "    update:       update installed packages\n");
2747   fprintf(stderr, "    verify:       check dependencies of installed packages\n");
2748   fprintf(stderr, "\n");
2749   exit(r);
2750 }
2751
2752 int
2753 main(int argc, char **argv)
2754 {
2755   Pool *pool;
2756   Repo *commandlinerepo = 0;
2757   Id *commandlinepkgs = 0;
2758   Id p, pp;
2759   struct repoinfo *repoinfos;
2760   int nrepoinfos = 0;
2761   int mainmode = 0, mode = 0;
2762   int i, newpkgs;
2763   Queue job, checkq;
2764   Solver *solv = 0;
2765   Transaction *trans;
2766   char inbuf[128], *ip;
2767   int allpkgs = 0;
2768   FILE **newpkgsfps;
2769   Queue addedfileprovides;
2770   Queue addedfileprovides_inst;
2771   Id repofilter = 0;
2772   int cleandeps = 0;
2773   char *rootdir = 0;
2774
2775   argc--;
2776   argv++;
2777   if (!argv[0])
2778     usage(1);
2779   if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2780     {
2781       mainmode = MODE_INSTALL;
2782       mode = SOLVER_INSTALL;
2783     }
2784   else if (!strcmp(argv[0], "patch"))
2785     {
2786       mainmode = MODE_PATCH;
2787       mode = SOLVER_INSTALL;
2788     }
2789   else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2790     {
2791       mainmode = MODE_ERASE;
2792       mode = SOLVER_ERASE;
2793     }
2794   else if (!strcmp(argv[0], "list"))
2795     {
2796       mainmode = MODE_LIST;
2797       mode = 0;
2798     }
2799   else if (!strcmp(argv[0], "info"))
2800     {
2801       mainmode = MODE_INFO;
2802       mode = 0;
2803     }
2804   else if (!strcmp(argv[0], "search"))
2805     {
2806       mainmode = MODE_SEARCH;
2807       mode = 0;
2808     }
2809   else if (!strcmp(argv[0], "verify"))
2810     {
2811       mainmode = MODE_VERIFY;
2812       mode = SOLVER_VERIFY;
2813     }
2814   else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2815     {
2816       mainmode = MODE_UPDATE;
2817       mode = SOLVER_UPDATE;
2818     }
2819   else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2820     {
2821       mainmode = MODE_DISTUPGRADE;
2822       mode = SOLVER_UPDATE;
2823     }
2824   else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2825     {
2826       mainmode = MODE_REPOLIST;
2827       mode = 0;
2828     }
2829   else
2830     usage(1);
2831
2832   for (;;)
2833     {
2834       if (argc > 2 && !strcmp(argv[1], "--root"))
2835         {
2836           rootdir = argv[2];
2837           argc -= 2;
2838           argv += 2;
2839         }
2840
2841       else if (argc > 1 && !strcmp(argv[1], "--clean"))
2842         {
2843           cleandeps = 1;
2844           argc--;
2845           argv++;
2846         }
2847       else
2848         break;
2849     }
2850
2851   pool = pool_create();
2852   pool_set_rootdir(pool, rootdir);
2853
2854 #if 0
2855   {
2856     const char *langs[] = {"de_DE", "de", "en"};
2857     pool_set_languages(pool, langs, sizeof(langs)/sizeof(*langs));
2858   }
2859 #endif
2860
2861   pool_setloadcallback(pool, load_stub, 0);
2862   pool->nscallback = nscallback;
2863   // pool_setdebuglevel(pool, 2);
2864   setarch(pool);
2865   repoinfos = read_repoinfos(pool, &nrepoinfos);
2866
2867   if (mainmode == MODE_REPOLIST)
2868     {
2869       int j = 1;
2870       for (i = 0; i < nrepoinfos; i++)
2871         {
2872           struct repoinfo *cinfo = repoinfos + i;
2873           if (!cinfo->enabled)
2874             continue;
2875           printf("%d: %-20s %s (prio %d)\n", j++, cinfo->alias, cinfo->name, cinfo->priority);
2876         }
2877       exit(0);
2878     }
2879
2880   read_repos(pool, repoinfos, nrepoinfos);
2881
2882   if (argc > 2 && !strcmp(argv[1], "-r"))
2883     {
2884       const char *rname = argv[2], *rp;
2885       for (rp = rname; *rp; rp++)
2886         if (*rp <= '0' || *rp >= '9')
2887           break;
2888       if (!*rp)
2889         {
2890           /* repo specified by number */
2891           int rnum = atoi(rname);
2892           for (i = 0; i < nrepoinfos; i++)
2893             {
2894               struct repoinfo *cinfo = repoinfos + i;
2895               if (!cinfo->enabled)
2896                 continue;
2897               if (--rnum == 0)
2898                 repofilter = cinfo->repo->repoid;
2899             }
2900         }
2901       else
2902         {
2903           /* repo specified by alias */
2904           Repo *repo;
2905           FOR_REPOS(i, repo)
2906             {
2907               if (!strcasecmp(rname, repo->name))
2908                 repofilter = repo->repoid;
2909             }
2910         }
2911       if (!repofilter)
2912         {
2913           fprintf(stderr, "%s: no such repo\n", rname);
2914           exit(1);
2915         }
2916       argc -= 2;
2917       argv += 2;
2918     }
2919   if (mainmode == MODE_SEARCH)
2920     {
2921       Dataiterator di;
2922       Map m;
2923       if (argc != 2)
2924         usage(1);
2925       map_init(&m, pool->nsolvables);
2926       dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2927       dataiterator_set_keyname(&di, SOLVABLE_NAME);
2928       dataiterator_set_search(&di, 0, 0);
2929       while (dataiterator_step(&di))
2930         MAPSET(&m, di.solvid);
2931       dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2932       dataiterator_set_search(&di, 0, 0);
2933       while (dataiterator_step(&di))
2934         MAPSET(&m, di.solvid);
2935       dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2936       dataiterator_set_search(&di, 0, 0);
2937       while (dataiterator_step(&di))
2938         MAPSET(&m, di.solvid);
2939       dataiterator_free(&di);
2940
2941       for (p = 1; p < pool->nsolvables; p++)
2942         {
2943           Solvable *s = pool_id2solvable(pool, p);
2944           if (!MAPTST(&m, p))
2945             continue;
2946           printf("  - %s: %s\n", pool_solvable2str(pool, s), solvable_lookup_str(s, SOLVABLE_SUMMARY));
2947         }
2948       map_free(&m);
2949       exit(0);
2950     }
2951
2952
2953   if (mainmode == MODE_LIST || mainmode == MODE_INSTALL)
2954     {
2955       for (i = 1; i < argc; i++)
2956         {
2957           int l;
2958           l = strlen(argv[i]);
2959 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2960           if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2961             continue;
2962 #endif
2963 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2964           if (l <= 4 || strcmp(argv[i] + l - 4, ".deb"))
2965             continue;
2966 #endif
2967           if (access(argv[i], R_OK))
2968             {
2969               perror(argv[i]);
2970               exit(1);
2971             }
2972           if (!commandlinepkgs)
2973             commandlinepkgs = solv_calloc(argc, sizeof(Id));
2974           if (!commandlinerepo)
2975             commandlinerepo = repo_create(pool, "@commandline");
2976           p = 0;
2977 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2978           p = repo_add_rpm(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2979 #endif
2980 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2981           p = repo_add_deb(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2982 #endif
2983           if (!p)
2984             {
2985               fprintf(stderr, "could not add '%s'\n", argv[i]);
2986               exit(1);
2987             }
2988           commandlinepkgs[i] = p;
2989         }
2990       if (commandlinerepo)
2991         repo_internalize(commandlinerepo);
2992     }
2993
2994   // FOR_REPOS(i, repo)
2995   //   printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2996   queue_init(&addedfileprovides);
2997   queue_init(&addedfileprovides_inst);
2998   pool_addfileprovides_queue(pool, &addedfileprovides, &addedfileprovides_inst);
2999   if (addedfileprovides.count || addedfileprovides_inst.count)
3000     rewrite_repos(pool, &addedfileprovides, &addedfileprovides_inst);
3001   queue_free(&addedfileprovides);
3002   queue_free(&addedfileprovides_inst);
3003   pool_createwhatprovides(pool);
3004
3005   queue_init(&job);
3006   for (i = 1; i < argc; i++)
3007     {
3008       Queue job2;
3009       int j;
3010
3011       if (commandlinepkgs && commandlinepkgs[i])
3012         {
3013           queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
3014           continue;
3015         }
3016       queue_init(&job2);
3017       if (!mkselect(pool, mode, argv[i], &job2))
3018         {
3019           fprintf(stderr, "nothing matches '%s'\n", argv[i]);
3020           exit(1);
3021         }
3022       if (repofilter && !limitrepo(pool, repofilter, &job2))
3023         {
3024           fprintf(stderr, "nothing in repo matches '%s'\n", argv[i]);
3025           exit(1);
3026         }
3027       for (j = 0; j < job2.count; j++)
3028         queue_push(&job, job2.elements[j]);
3029       queue_free(&job2);
3030     }
3031
3032   if (!job.count && mainmode != MODE_UPDATE && mainmode != MODE_DISTUPGRADE && mainmode != MODE_VERIFY && mainmode != MODE_PATCH)
3033     {
3034       printf("no package matched\n");
3035       exit(1);
3036     }
3037
3038   if (!job.count)
3039     allpkgs = 1;
3040
3041   if (mainmode == MODE_LIST || mainmode == MODE_INFO)
3042     {
3043       /* list mode, no solver needed */
3044       for (i = 0; i < job.count; i += 2)
3045         {
3046           Id how = job.elements[i] & SOLVER_SELECTMASK;
3047           FOR_JOB_SELECT(p, pp, how, job.elements[i + 1])
3048             {
3049               Solvable *s = pool_id2solvable(pool, p);
3050               if (mainmode == MODE_INFO)
3051                 {
3052                   const char *str;
3053                   printf("Name:        %s\n", pool_solvable2str(pool, s));
3054                   printf("Repo:        %s\n", s->repo->name);
3055                   printf("Summary:     %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
3056                   str = solvable_lookup_str(s, SOLVABLE_URL);
3057                   if (str)
3058                     printf("Url:         %s\n", str);
3059                   str = solvable_lookup_str(s, SOLVABLE_LICENSE);
3060                   if (str)
3061                     printf("License:     %s\n", str);
3062                   printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
3063                   printf("\n");
3064                 }
3065               else
3066                 {
3067 #if 1
3068                   const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de", 1);
3069 #else
3070                   const char *sum = solvable_lookup_str_poollang(s, SOLVABLE_SUMMARY);
3071 #endif
3072                   printf("  - %s [%s]\n", pool_solvable2str(pool, s), s->repo->name);
3073                   if (sum)
3074                     printf("    %s\n", sum);
3075                 }
3076             }
3077         }
3078       queue_free(&job);
3079       pool_free(pool);
3080       free_repoinfos(repoinfos, nrepoinfos);
3081       solv_free(commandlinepkgs);
3082 #ifdef FEDORA
3083       yum_substitute(pool, 0);
3084 #endif
3085       exit(0);
3086     }
3087
3088   if (mainmode == MODE_PATCH)
3089     select_patches(pool, &job);
3090
3091   // add mode
3092   for (i = 0; i < job.count; i += 2)
3093     {
3094       if (mode == SOLVER_UPDATE)
3095         {
3096           /* make update of not installed packages an install */
3097           FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
3098             if (pool->installed && pool->solvables[p].repo == pool->installed)
3099               break;
3100           if (!p)
3101             {
3102               job.elements[i] |= SOLVER_INSTALL;
3103               if (cleandeps)
3104                 job.elements[i] |= SOLVER_CLEANDEPS;
3105               continue;
3106             }
3107         }
3108       job.elements[i] |= mode;
3109       if (cleandeps)
3110         job.elements[i] |= SOLVER_CLEANDEPS;
3111     }
3112
3113   if (mainmode == MODE_DISTUPGRADE && allpkgs)
3114     {
3115       if (repofilter)
3116         queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_REPO, repofilter);
3117       else
3118         queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_ALL, 0);
3119     }
3120   if (mainmode == MODE_UPDATE && allpkgs)
3121     {
3122       if (repofilter)
3123         queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_REPO, repofilter);
3124       else
3125         queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_ALL, 0);
3126     }
3127   if (mainmode == MODE_VERIFY && allpkgs)
3128     {
3129       if (repofilter)
3130         queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_REPO, repofilter);
3131       else
3132         queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_ALL, 0);
3133     }
3134
3135   // multiversion test
3136   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae", 1));
3137   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-base", 1));
3138   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-extra", 1));
3139
3140 #ifdef SOFTLOCKS_PATH
3141   addsoftlocks(pool, &job);
3142 #endif
3143
3144 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3145 rerunsolver:
3146 #endif
3147   for (;;)
3148     {
3149       Id problem, solution;
3150       int pcnt, scnt;
3151
3152       solv = solver_create(pool);
3153       solver_set_flag(solv, SOLVER_FLAG_SPLITPROVIDES, 1);
3154       if (mainmode == MODE_ERASE)
3155         solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1);  /* don't nag */
3156
3157       if (!solver_solve(solv, &job))
3158         break;
3159       pcnt = solver_problem_count(solv);
3160       printf("Found %d problems:\n", pcnt);
3161       for (problem = 1; problem <= pcnt; problem++)
3162         {
3163           int take = 0;
3164           printf("Problem %d:\n", problem);
3165           solver_printprobleminfo(solv, problem);
3166           printf("\n");
3167           scnt = solver_solution_count(solv, problem);
3168           for (solution = 1; solution <= scnt; solution++)
3169             {
3170               printf("Solution %d:\n", solution);
3171               solver_printsolution(solv, problem, solution);
3172               printf("\n");
3173             }
3174           for (;;)
3175             {
3176               printf("Please choose a solution: ");
3177               fflush(stdout);
3178               *inbuf = 0;
3179               if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
3180                 {
3181                   printf("Abort.\n");
3182                   exit(1);
3183                 }
3184               while (*ip == ' ' || *ip == '\t')
3185                 ip++;
3186               if (*ip >= '0' && *ip <= '9')
3187                 {
3188                   take = atoi(ip);
3189                   if (take >= 1 && take <= scnt)
3190                     break;
3191                 }
3192               if (*ip == 's')
3193                 {
3194                   take = 0;
3195                   break;
3196                 }
3197               if (*ip == 'q')
3198                 {
3199                   printf("Abort.\n");
3200                   exit(1);
3201                 }
3202             }
3203           if (!take)
3204             continue;
3205           solver_take_solution(solv, problem, take, &job);
3206         }
3207       solver_free(solv);
3208       solv = 0;
3209     }
3210
3211   trans = solver_create_transaction(solv);
3212   if (!trans->steps.count)
3213     {
3214       printf("Nothing to do.\n");
3215       transaction_free(trans);
3216       solver_free(solv);
3217       queue_free(&job);
3218       pool_free(pool);
3219       free_repoinfos(repoinfos, nrepoinfos);
3220       solv_free(commandlinepkgs);
3221 #ifdef FEDORA
3222       yum_substitute(pool, 0);
3223 #endif
3224       exit(1);
3225     }
3226   printf("\n");
3227   printf("Transaction summary:\n\n");
3228   transaction_print(trans);
3229
3230 #if defined(SUSE)
3231   if (1)
3232     {
3233       DUChanges duc[4];
3234       int i;
3235
3236       duc[0].path = "/";
3237       duc[1].path = "/usr/share/man";
3238       duc[2].path = "/sbin";
3239       duc[3].path = "/etc";
3240       transaction_calc_duchanges(trans, duc, 4);
3241       for (i = 0; i < 4; i++)
3242         printf("duchanges %s: %d K  %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
3243     }
3244 #endif
3245   printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
3246   printf("\n");
3247
3248   if (!yesno("OK to continue (y/n)? "))
3249     {
3250       printf("Abort.\n");
3251       transaction_free(trans);
3252       solver_free(solv);
3253       queue_free(&job);
3254       pool_free(pool);
3255       free_repoinfos(repoinfos, nrepoinfos);
3256       solv_free(commandlinepkgs);
3257 #ifdef FEDORA
3258       yum_substitute(pool, 0);
3259 #endif
3260       exit(1);
3261     }
3262
3263   queue_init(&checkq);
3264   newpkgs = transaction_installedresult(trans, &checkq);
3265   newpkgsfps = 0;
3266
3267   if (newpkgs)
3268     {
3269       int downloadsize = 0;
3270       for (i = 0; i < newpkgs; i++)
3271         {
3272           Solvable *s;
3273
3274           p = checkq.elements[i];
3275           s = pool_id2solvable(pool, p);
3276           downloadsize += solvable_lookup_sizek(s, SOLVABLE_DOWNLOADSIZE, 0);
3277         }
3278       printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
3279       newpkgsfps = solv_calloc(newpkgs, sizeof(*newpkgsfps));
3280       for (i = 0; i < newpkgs; i++)
3281         {
3282           unsigned int medianr;
3283           const char *loc;
3284           Solvable *s;
3285           struct repoinfo *cinfo;
3286           const unsigned char *chksum;
3287           Id chksumtype;
3288           Dataiterator di;
3289
3290           p = checkq.elements[i];
3291           s = pool_id2solvable(pool, p);
3292           if (s->repo == commandlinerepo)
3293             {
3294               loc = solvable_get_location(s, &medianr);
3295               if (!(newpkgsfps[i] = fopen(loc, "r")))
3296                 {
3297                   perror(loc);
3298                   exit(1);
3299                 }
3300               putchar('.');
3301               continue;
3302             }
3303           cinfo = s->repo->appdata;
3304           if (!cinfo)
3305             {
3306               printf("%s: no repository information\n", s->repo->name);
3307               exit(1);
3308             }
3309           loc = solvable_get_location(s, &medianr);
3310           if (!loc)
3311              continue;
3312
3313           if (pool->installed && pool->installed->nsolvables)
3314             {
3315               /* try a delta first */
3316               char *matchname = strdup(pool_id2str(pool, s->name));
3317               dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
3318               dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
3319               while (dataiterator_step(&di))
3320                 {
3321                   Id baseevr, op;
3322
3323                   dataiterator_setpos_parent(&di);
3324                   if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
3325                       pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
3326                     continue;
3327                   baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
3328                   FOR_PROVIDES(op, pp, s->name)
3329                     {
3330                       Solvable *os = pool->solvables + op;
3331                       if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
3332                         break;
3333                     }
3334                   if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
3335                     {
3336                       /* base is installed, run sequence check */
3337                       const char *seqname;
3338                       const char *seqevr;
3339                       const char *seqnum;
3340                       const char *seq;
3341                       const char *dloc;
3342                       const char *archstr;
3343                       FILE *fp;
3344                       char cmd[128];
3345                       int newfd;
3346
3347                       archstr = pool_id2str(pool, s->arch);
3348                       if (strlen(archstr) > 10 || strchr(archstr, '\'') != 0)
3349                         continue;
3350
3351                       seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
3352                       seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
3353                       seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
3354                       seq = pool_tmpjoin(pool, seqname, "-", seqevr);
3355                       seq = pool_tmpappend(pool, seq, "-", seqnum);
3356                       if (strchr(seq, '\'') != 0)
3357                         continue;
3358 #ifdef FEDORA
3359                       sprintf(cmd, "/usr/bin/applydeltarpm -a '%s' -c -s '", archstr);
3360 #else
3361                       sprintf(cmd, "/usr/bin/applydeltarpm -c -s '");
3362 #endif
3363                       if (system(pool_tmpjoin(pool, cmd, seq, "'")) != 0)
3364                         continue;       /* didn't match */
3365                       /* looks good, download delta */
3366                       chksumtype = 0;
3367                       chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
3368                       if (!chksumtype)
3369                         continue;       /* no way! */
3370                       dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
3371                       dloc = pool_tmpappend(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
3372                       dloc = pool_tmpappend(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
3373                       dloc = pool_tmpappend(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
3374                       if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
3375                         continue;
3376                       /* got it, now reconstruct */
3377                       newfd = opentmpfile();
3378 #ifdef FEDORA
3379                       sprintf(cmd, "applydeltarpm -a '%s' /dev/fd/%d /dev/fd/%d", archstr, fileno(fp), newfd);
3380 #else
3381                       sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
3382 #endif
3383                       fcntl(fileno(fp), F_SETFD, 0);
3384                       if (system(cmd))
3385                         {
3386                           close(newfd);
3387                           fclose(fp);
3388                           continue;
3389                         }
3390                       lseek(newfd, 0, SEEK_SET);
3391                       chksumtype = 0;
3392                       chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3393                       if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
3394                         {
3395                           close(newfd);
3396                           fclose(fp);
3397                           continue;
3398                         }
3399                       newpkgsfps[i] = fdopen(newfd, "r");
3400                       fclose(fp);
3401                       break;
3402                     }
3403                 }
3404               dataiterator_free(&di);
3405               solv_free(matchname);
3406             }
3407
3408           if (newpkgsfps[i])
3409             {
3410               putchar('d');
3411               fflush(stdout);
3412               continue;         /* delta worked! */
3413             }
3414           if (cinfo->type == TYPE_SUSETAGS)
3415             {
3416               const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
3417               loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
3418             }
3419           chksumtype = 0;
3420           chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3421           if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
3422             {
3423               printf("\n%s: %s not found in repository\n", s->repo->name, loc);
3424               exit(1);
3425             }
3426           putchar('.');
3427           fflush(stdout);
3428         }
3429       putchar('\n');
3430     }
3431
3432 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3433   if (newpkgs)
3434     {
3435       Queue conflicts;
3436       struct fcstate fcstate;
3437
3438       printf("Searching for file conflicts\n");
3439       queue_init(&conflicts);
3440       fcstate.rpmdbstate = 0;
3441       fcstate.newpkgscnt = newpkgs;
3442       fcstate.checkq = &checkq;
3443       fcstate.newpkgsfps = newpkgsfps;
3444       pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fileconflict_cb, &fcstate);
3445       if (conflicts.count)
3446         {
3447           printf("\n");
3448           for (i = 0; i < conflicts.count; i += 5)
3449             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]));
3450           printf("\n");
3451           if (yesno("Re-run solver (y/n/q)? "))
3452             {
3453               for (i = 0; i < newpkgs; i++)
3454                 if (newpkgsfps[i])
3455                   fclose(newpkgsfps[i]);
3456               newpkgsfps = solv_free(newpkgsfps);
3457               solver_free(solv);
3458               pool_add_fileconflicts_deps(pool, &conflicts);
3459               pool_createwhatprovides(pool);    /* Hmm... */
3460               goto rerunsolver;
3461             }
3462         }
3463       queue_free(&conflicts);
3464     }
3465 #endif
3466
3467   printf("Committing transaction:\n\n");
3468   transaction_order(trans, 0);
3469   for (i = 0; i < trans->steps.count; i++)
3470     {
3471 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3472       const char *evr, *evrp, *nvra;
3473 #endif
3474       Solvable *s;
3475       int j;
3476       FILE *fp;
3477
3478       p = trans->steps.elements[i];
3479       s = pool_id2solvable(pool, p);
3480       Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
3481       switch(type)
3482         {
3483         case SOLVER_TRANSACTION_ERASE:
3484           printf("erase %s\n", pool_solvid2str(pool, p));
3485 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3486           if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
3487             continue;
3488           /* strip epoch from evr */
3489           evr = evrp = pool_id2str(pool, s->evr);
3490           while (*evrp >= '0' && *evrp <= '9')
3491             evrp++;
3492           if (evrp > evr && evrp[0] == ':' && evrp[1])
3493             evr = evrp + 1;
3494           nvra = pool_tmpjoin(pool, pool_id2str(pool, s->name), "-", evr);
3495           nvra = pool_tmpappend(pool, nvra, ".", pool_id2str(pool, s->arch));
3496           runrpm("-e", nvra, -1, rootdir);      /* too bad that --querybynumber doesn't work */
3497 #endif
3498 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3499           rundpkg("--remove", pool_id2str(pool, s->name), 0, rootdir);
3500 #endif
3501           break;
3502         case SOLVER_TRANSACTION_INSTALL:
3503         case SOLVER_TRANSACTION_MULTIINSTALL:
3504           printf("install %s\n", pool_solvid2str(pool, p));
3505           for (j = 0; j < newpkgs; j++)
3506             if (checkq.elements[j] == p)
3507               break;
3508           fp = j < newpkgs ? newpkgsfps[j] : 0;
3509           if (!fp)
3510             continue;
3511           rewind(fp);
3512           lseek(fileno(fp), 0, SEEK_SET);
3513 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3514           runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp), rootdir);
3515 #endif
3516 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3517           rundpkg("--install", "/dev/fd/3", fileno(fp), rootdir);
3518 #endif
3519           fclose(fp);
3520           newpkgsfps[j] = 0;
3521           break;
3522         default:
3523           break;
3524         }
3525     }
3526
3527   for (i = 0; i < newpkgs; i++)
3528     if (newpkgsfps[i])
3529       fclose(newpkgsfps[i]);
3530   solv_free(newpkgsfps);
3531   queue_free(&checkq);
3532   transaction_free(trans);
3533   solver_free(solv);
3534   queue_free(&job);
3535   pool_free(pool);
3536   free_repoinfos(repoinfos, nrepoinfos);
3537   solv_free(commandlinepkgs);
3538 #ifdef FEDORA
3539   yum_substitute(pool, 0);
3540 #endif
3541   exit(0);
3542 }