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