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