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