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