- add deltarpm support
[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 missing:
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 <unistd.h>
22 #include <zlib.h>
23 #include <fcntl.h>
24 #include <assert.h>
25 #include <sys/utsname.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28
29 #include "pool.h"
30 #include "poolarch.h"
31 #include "repo.h"
32 #include "evr.h"
33 #include "util.h"
34 #include "solver.h"
35 #include "solverdebug.h"
36 #include "chksum.h"
37 #include "repo_solv.h"
38
39 #include "repo_write.h"
40 #include "repo_rpmdb.h"
41 #include "repo_products.h"
42 #include "repo_rpmmd.h"
43 #include "repo_susetags.h"
44 #include "repo_repomdxml.h"
45 #include "repo_updateinfoxml.h"
46 #include "repo_deltainfoxml.h"
47 #include "repo_content.h"
48 #include "pool_fileconflicts.h"
49
50
51 #ifdef FEDORA
52 # define REPOINFO_PATH "/etc/yum.repos.d"
53 #else
54 # define REPOINFO_PATH "/etc/zypp/repos.d"
55 # define PRODUCTS_PATH "/etc/products.d"
56 #endif
57
58 #define SOLVCACHE_PATH "/var/cache/solv"
59
60
61 struct repoinfo {
62   Repo *repo;
63
64   char *alias;
65   char *name;
66   int enabled;
67   int autorefresh;
68   char *baseurl;
69   char *path;
70   int type;
71   int gpgcheck;
72   int priority;
73   int keeppackages;
74 };
75
76 #define TYPE_UNKNOWN    0
77 #define TYPE_SUSETAGS   1
78 #define TYPE_RPMMD      2
79 #define TYPE_PLAINDIR   3
80
81 static int
82 read_repoinfos_sort(const void *ap, const void *bp)
83 {
84   const struct repoinfo *a = ap;
85   const struct repoinfo *b = bp;
86   return strcmp(a->alias, b->alias);
87 }
88
89 struct repoinfo *
90 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
91 {
92   char buf[4096];
93   char buf2[4096], *kp, *vp, *kpe;
94   DIR *dir;
95   FILE *fp;
96   struct dirent *ent;
97   int l, rdlen;
98   struct repoinfo *repoinfos = 0, *cinfo;
99   int nrepoinfos = 0;
100
101   rdlen = strlen(reposdir);
102   dir = opendir(reposdir);
103   if (!dir)
104     {
105       *nrepoinfosp = 0;
106       return 0;
107     }
108   while ((ent = readdir(dir)) != 0)
109     {
110       l = strlen(ent->d_name);
111       if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
112         continue;
113       snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
114       if ((fp = fopen(buf, "r")) == 0)
115         {
116           perror(buf);
117           continue;
118         }
119       cinfo = 0;
120       while(fgets(buf2, sizeof(buf2), fp))
121         {
122           l = strlen(buf2);
123           if (l == 0)
124             continue;
125           while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
126             buf2[--l] = 0;
127           kp = buf2;
128           while (*kp == ' ' || *kp == '\t')
129             kp++;
130           if (!*kp || *kp == '#')
131             continue;
132           if (!cinfo)
133             {
134               if (*kp != '[')
135                 continue;
136               vp = strrchr(kp, ']');
137               if (!vp)
138                 continue;
139               *vp = 0;
140               repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
141               cinfo = repoinfos + nrepoinfos++;
142               memset(cinfo, 0, sizeof(*cinfo));
143               cinfo->alias = strdup(kp + 1);
144               cinfo->gpgcheck = 1;
145               cinfo->type = TYPE_RPMMD;
146               continue;
147             }
148           vp = strchr(kp, '=');
149           if (!vp)
150             continue;
151           for (kpe = vp - 1; kpe >= kp; kpe--)
152             if (*kpe != ' ' && *kpe != '\t')
153               break;
154           if (kpe == kp)
155             continue;
156           vp++;
157           while (*vp == ' ' || *vp == '\t')
158             vp++;
159           kpe[1] = 0;
160           if (!strcmp(kp, "name"))
161             cinfo->name = strdup(vp);
162           else if (!strcmp(kp, "enabled"))
163             cinfo->enabled = *vp == '0' ? 0 : 1;
164           else if (!strcmp(kp, "autorefresh"))
165             cinfo->autorefresh = *vp == '0' ? 0 : 1;
166           else if (!strcmp(kp, "gpgcheck"))
167             cinfo->gpgcheck = *vp == '0' ? 0 : 1;
168           else if (!strcmp(kp, "baseurl"))
169             cinfo->baseurl = strdup(vp);
170           else if (!strcmp(kp, "path"))
171             {
172               if (vp && strcmp(vp, "/") != 0)
173                 cinfo->path = strdup(vp);
174             }
175           else if (!strcmp(kp, "type"))
176             {
177               if (!strcmp(vp, "yast2"))
178                 cinfo->type = TYPE_SUSETAGS;
179               else if (!strcmp(vp, "rpm-md"))
180                 cinfo->type = TYPE_RPMMD;
181               else if (!strcmp(vp, "plaindir"))
182                 cinfo->type = TYPE_PLAINDIR;
183               else
184                 cinfo->type = TYPE_UNKNOWN;
185             }
186           else if (!strcmp(kp, "priority"))
187             cinfo->priority = atoi(vp);
188           else if (!strcmp(kp, "keeppackages"))
189             cinfo->keeppackages = *vp == '0' ? 0 : 1;
190         }
191       fclose(fp);
192       cinfo = 0;
193     }
194   closedir(dir);
195   qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
196   *nrepoinfosp = nrepoinfos;
197   return repoinfos;
198 }
199
200 static ssize_t
201 cookie_gzread(void *cookie, char *buf, size_t nbytes)
202 {
203   return gzread((gzFile *)cookie, buf, nbytes);
204 }
205
206 static int
207 cookie_gzclose(void *cookie)
208 {
209   return gzclose((gzFile *)cookie);
210 }
211
212 static inline int
213 opentmpfile()
214 {
215   char tmpl[100];
216   int fd;
217
218   strcpy(tmpl, "/var/tmp/solvXXXXXX");
219   fd = mkstemp(tmpl);
220   if (fd < 0)
221     {
222       perror("mkstemp");
223       exit(1);
224     }
225   unlink(tmpl);
226   return fd;
227 }
228
229 static int
230 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
231 {
232   char buf[1024];
233   unsigned char *sum;
234   void *h;
235   int l;
236
237   h = sat_chksum_create(chksumtype);
238   if (!h)
239     {
240       printf("%s: unknown checksum type\n", file);
241       return 0;
242     }
243   while ((l = read(fd, buf, sizeof(buf))) > 0)
244     sat_chksum_add(h, buf, l);
245   lseek(fd, 0, SEEK_SET);
246   l = 0;
247   sum = sat_chksum_get(h, &l);
248   if (memcmp(sum, chksum, l))
249     {
250       printf("%s: checksum mismatch\n", file);
251       return 0;
252     }
253   return 1;
254 }
255
256 FILE *
257 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype)
258 {
259   pid_t pid;
260   int fd, l;
261   int status;
262   char url[4096];
263   const char *baseurl = cinfo->baseurl;
264
265   l = strlen(baseurl);
266   if (l && baseurl[l - 1] == '/')
267     snprintf(url, sizeof(url), "%s%s", baseurl, file);
268   else
269     snprintf(url, sizeof(url), "%s/%s", baseurl, file);
270   fd = opentmpfile();
271   if ((pid = fork()) == (pid_t)-1)
272     {
273       perror("fork");
274       exit(1);
275     }
276   if (pid == 0)
277     {
278       if (fd != 1)
279         {
280           dup2(fd, 1);
281           close(fd);
282         }
283       execlp("curl", "curl", "-s", "-L", url, (char *)0);
284       perror("curl");
285       _exit(0);
286     }
287   while (waitpid(pid, &status, 0) != pid)
288     ;
289   if (lseek(fd, 0, SEEK_END) == 0)
290     {
291       /* empty file */
292       close(fd);
293       return 0;
294     }
295   lseek(fd, 0, SEEK_SET);
296   if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
297     {
298       close(fd);
299       return 0;
300     }
301   if (uncompress)
302     {
303       char tmpl[100];
304       cookie_io_functions_t cio;
305       gzFile *gzf;
306
307       sprintf(tmpl, "/dev/fd/%d", fd);
308       gzf = gzopen(tmpl, "r");
309       close(fd);
310       if (!gzf)
311         return 0;
312       memset(&cio, 0, sizeof(cio));
313       cio.read = cookie_gzread;
314       cio.close = cookie_gzclose;
315       return fopencookie(gzf, "r", cio);
316     }
317   fcntl(fd, F_SETFD, FD_CLOEXEC);
318   return fdopen(fd, "r");
319 }
320
321 void
322 cleanupgpg(char *gpgdir)
323 {
324   char cmd[256];
325   snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
326   unlink(cmd);
327   snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
328   unlink(cmd);
329   snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
330   unlink(cmd);
331   snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
332   unlink(cmd);
333   snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
334   unlink(cmd);
335   rmdir(gpgdir);
336 }
337
338 int
339 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
340 {
341   char *gpgdir;
342   char *keysfile;
343   const char *pubkey;
344   char cmd[256];
345   FILE *kfp;
346   Solvable *s;
347   Id p;
348   off_t posfp, possigfp;
349   int r;
350
351   gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
352   if (!gpgdir)
353     return 0;
354   keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
355   if (!(kfp = fopen(keysfile, "w")) )
356     {
357       cleanupgpg(gpgdir);
358       return 0;
359     }
360   for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
361     {
362       if (!s->repo)
363         continue;
364       pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
365       if (!pubkey || !*pubkey)
366         continue;
367       if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
368         break;
369       if (fputc('\n', kfp) == EOF)      /* Just in case... */
370         break;
371     }
372   if (fclose(kfp))
373     {
374       cleanupgpg(gpgdir);
375       return 0;
376     }
377   snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --import %s", gpgdir, keysfile);
378   if (system(cmd))
379     {
380       fprintf(stderr, "key import error\n");
381       cleanupgpg(gpgdir);
382       return 0;
383     }
384   unlink(keysfile);
385   posfp = lseek(fileno(fp), 0, SEEK_CUR);
386   lseek(fileno(fp), 0, SEEK_SET);
387   possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
388   lseek(fileno(sigfp), 0, SEEK_SET);
389   snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
390   fcntl(fileno(fp), F_SETFD, 0);        /* clear CLOEXEC */
391   fcntl(fileno(sigfp), F_SETFD, 0);     /* clear CLOEXEC */
392   r = system(cmd);
393   lseek(fileno(sigfp), possigfp, SEEK_SET);
394   lseek(fileno(fp), posfp, SEEK_SET);
395   fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
396   fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
397   cleanupgpg(gpgdir);
398   return r == 0 ? 1 : 0;
399 }
400
401 #define CHKSUM_IDENT "1.1"
402
403 void
404 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
405 {
406   char buf[4096];
407   void *h = sat_chksum_create(chktype);
408   int l;
409
410   sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
411   while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
412     sat_chksum_add(h, buf, l);
413   rewind(fp);
414   sat_chksum_free(h, out);
415 }
416
417 void
418 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
419 {
420   void *h = sat_chksum_create(chktype);
421   sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
422   sat_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
423   sat_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
424   sat_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
425   sat_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
426   sat_chksum_free(h, out);
427 }
428
429 void
430 setarch(Pool *pool)
431 {
432   struct utsname un;
433   if (uname(&un))
434     {
435       perror("uname");
436       exit(1);
437     }
438   pool_setarch(pool, un.machine);
439 }
440
441 char *calccachepath(Repo *repo)
442 {
443   char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
444   p = pool_tmpjoin(repo->pool, p, ".solv", 0);
445   q = p + strlen(SOLVCACHE_PATH) + 1;
446   if (*q == '.')
447     *q = '_';
448   for (; *q; q++)
449     if (*q == '/')
450       *q = '_';
451   return p;
452 }
453
454 int
455 usecachedrepo(Repo *repo, unsigned char *cookie)
456 {
457   FILE *fp;
458   unsigned char mycookie[32];
459
460   if (!(fp = fopen(calccachepath(repo), "r")))
461     return 0;
462   if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
463     {
464       fclose(fp);
465       return 0;
466     }
467   if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
468     {
469       fclose(fp);
470       return 0;
471     }
472   rewind(fp);
473   if (repo_add_solv(repo, fp))
474     {
475       fclose(fp);
476       return 0;
477     }
478   fclose(fp);
479   return 1;
480 }
481
482 void
483 writecachedrepo(Repo *repo, unsigned char *cookie)
484 {
485   Id *addedfileprovides = 0;
486   FILE *fp;
487   int i, fd;
488   char *tmpl;
489   Repodata *info;
490
491   mkdir(SOLVCACHE_PATH, 0755);
492   tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
493   fd = mkstemp(tmpl);
494   if (!fd)
495     {
496       free(tmpl);
497       return;
498     }
499   fchmod(fd, 0444);
500   if (!(fp = fdopen(fd, "w")))
501     {
502       close(fd);
503       unlink(tmpl);
504       free(tmpl);
505       return;
506     }
507   info = repo_add_repodata(repo, 0);
508   pool_addfileprovides_ids(repo->pool, 0, &addedfileprovides);
509   if (addedfileprovides && *addedfileprovides)
510     {
511       for (i = 0; addedfileprovides[i]; i++)
512         repodata_add_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[i]);
513     }
514   sat_free(addedfileprovides);
515   repodata_internalize(info);
516   repo_write(repo, fp, 0, 0, 0);
517   repodata_free(info);
518   if (fwrite(cookie, 32, 1, fp) != 1)
519     {
520       fclose(fp);
521       unlink(tmpl);
522       free(tmpl);
523       return;
524     }
525   if (fclose(fp))
526     {
527       unlink(tmpl);
528       free(tmpl);
529       return;
530     }
531   if (!rename(tmpl, calccachepath(repo)))
532     unlink(tmpl);
533   free(tmpl);
534 }
535
536 static Pool *
537 read_sigs()
538 {
539   Pool *sigpool = pool_create();
540   Repo *repo = repo_create(sigpool, "rpmdbkeys");
541   repo_add_rpmdb_pubkeys(repo, 0, 0);
542   return sigpool;
543 }
544
545 static inline int
546 iscompressed(const char *name)
547 {
548   int l = strlen(name);
549   return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
550 }
551
552 static inline const char *
553 findinrepomd(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
554 {
555   Pool *pool = repo->pool;
556   Dataiterator di;
557   const char *filename;
558
559   filename = 0;
560   *chksump = 0;
561   *chksumtypep = 0;
562   dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
563   dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
564   if (dataiterator_step(&di))
565     {
566       dataiterator_setpos_parent(&di);
567       filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
568       *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
569     }
570   dataiterator_free(&di);
571   if (filename && !*chksumtypep)
572     {
573       printf("no %s file checksum!\n", what);
574       filename = 0;
575     }
576   return filename;
577 }
578
579 void
580 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
581 {
582   Repo *repo;
583   struct repoinfo *cinfo;
584   int i;
585   FILE *fp;
586   Dataiterator di;
587   const char *filename;
588   const unsigned char *filechksum;
589   Id filechksumtype;
590   const char *descrdir;
591   int defvendor;
592   struct stat stb;
593   unsigned char cookie[32];
594   Pool *sigpool = 0;
595
596   repo = repo_create(pool, "@System");
597   printf("rpm database:");
598   if (stat("/var/lib/rpm/Packages", &stb))
599     memset(&stb, 0, sizeof(&stb));
600   calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cookie);
601   if (usecachedrepo(repo, cookie))
602     printf(" cached\n");
603   else
604     {
605       FILE *ofp;
606       printf(" reading\n");
607       int done = 0;
608
609 #ifdef PRODUCTS_PATH
610       repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
611 #endif
612       if ((ofp = fopen(calccachepath(repo), "r")) != 0)
613         {
614           Repo *ref = repo_create(pool, "@System.old");
615           if (!repo_add_solv(ref, ofp))
616             {
617               repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
618               done = 1;
619             }
620           fclose(ofp);
621           repo_free(ref, 1);
622         }
623       if (!done)
624         repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
625       writecachedrepo(repo, cookie);
626     }
627   pool_set_installed(pool, repo);
628
629   for (i = 0; i < nrepoinfos; i++)
630     {
631       cinfo = repoinfos + i;
632       if (!cinfo->enabled)
633         continue;
634
635       repo = repo_create(pool, cinfo->alias);
636       cinfo->repo = repo;
637       repo->appdata = cinfo;
638       repo->priority = 99 - cinfo->priority;
639
640       if (!cinfo->autorefresh && usecachedrepo(repo, 0))
641         {
642           printf("repo '%s':", cinfo->alias);
643           printf(" cached\n");
644           continue;
645         }
646       switch (cinfo->type)
647         {
648         case TYPE_RPMMD:
649           printf("rpmmd repo '%s':", cinfo->alias);
650           fflush(stdout);
651           if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0)) == 0)
652             {
653               printf(" no repomd.xml file, skipped\n");
654               repo_free(repo, 1);
655               cinfo->repo = 0;
656               break;
657             }
658           calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
659           if (usecachedrepo(repo, cookie))
660             {
661               printf(" cached\n");
662               fclose(fp);
663               break;
664             }
665           if (cinfo->gpgcheck)
666             {
667               FILE *sigfp;
668               if ((sigfp = curlfopen(cinfo, "repodata/repomd.xml.asc", 0, 0, 0)) == 0)
669                 {
670                   printf(" unsigned, skipped\n");
671                   fclose(fp);
672                   break;
673                 }
674               if (!sigpool)
675                 sigpool = read_sigs();
676               if (!checksig(sigpool, fp, sigfp))
677                 {
678                   printf(" checksig failed, skipped\n");
679                   fclose(sigfp);
680                   fclose(fp);
681                   break;
682                 }
683               fclose(sigfp);
684             }
685           repo_add_repomdxml(repo, fp, 0);
686           fclose(fp);
687           printf(" reading\n");
688           filename = findinrepomd(repo, "primary", &filechksum, &filechksumtype);
689           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype)) != 0)
690             {
691               repo_add_rpmmd(repo, fp, 0, 0);
692               fclose(fp);
693             }
694
695           filename = findinrepomd(repo, "updateinfo", &filechksum, &filechksumtype);
696           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype)) != 0)
697             {
698               repo_add_updateinfoxml(repo, fp, 0);
699               fclose(fp);
700             }
701
702           filename = findinrepomd(repo, "deltainfo", &filechksum, &filechksumtype);
703           if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype)) != 0)
704             {
705               repo_add_deltainfoxml(repo, fp, 0);
706               fclose(fp);
707             }
708           
709           writecachedrepo(repo, cookie);
710           break;
711
712         case TYPE_SUSETAGS:
713           printf("susetags repo '%s':", cinfo->alias);
714           fflush(stdout);
715           repo = repo_create(pool, cinfo->alias);
716           cinfo->repo = repo;
717           repo->appdata = cinfo;
718           repo->priority = 99 - cinfo->priority;
719           descrdir = 0;
720           defvendor = 0;
721           if ((fp = curlfopen(cinfo, "content", 0, 0, 0)) == 0)
722             {
723               printf(" no content file, skipped\n");
724               repo_free(repo, 1);
725               cinfo->repo = 0;
726               break;
727             }
728           calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
729           if (usecachedrepo(repo, cookie))
730             {
731               printf(" cached\n");
732               fclose(fp);
733               break;
734             }
735           if (cinfo->gpgcheck)
736             {
737               FILE *sigfp;
738               if ((sigfp = curlfopen(cinfo, "content.asc", 0, 0, 0)) == 0)
739                 {
740                   printf(" unsigned, skipped\n");
741                   fclose(fp);
742                   break;
743                 }
744               if (!sigpool)
745                 sigpool = read_sigs();
746               if (!checksig(sigpool, fp, sigfp))
747                 {
748                   printf(" checksig failed, skipped\n");
749                   fclose(sigfp);
750                   fclose(fp);
751                   break;
752                 }
753             }
754           repo_add_content(repo, fp, 0);
755           fclose(fp);
756           defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
757           descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
758           if (!descrdir)
759             descrdir = "suse/setup/descr";
760           filename = 0;
761           dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, "packages.gz", SEARCH_STRING);
762           dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
763           if (dataiterator_step(&di))
764             {
765               dataiterator_setpos_parent(&di);
766               filechksum = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
767               filename = "packages.gz";
768             }
769           dataiterator_free(&di);
770           if (!filename)
771             {
772               dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, "packages", SEARCH_STRING);
773               dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
774               if (dataiterator_step(&di))
775                 {
776                   dataiterator_setpos_parent(&di);
777                   filechksum = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
778                   filename = "packages";
779                 }
780               dataiterator_free(&di);
781             }
782           if (!filename)
783             {
784               printf(" no packages file entry, skipped\n");
785               break;
786             }
787           if (!filechksumtype)
788             {
789               printf(" no packages file checksum, skipped\n");
790               break;
791             }
792           printf(" reading\n");
793           if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype)) == 0)
794             break;
795           repo_add_susetags(repo, fp, defvendor, 0, 0);
796           fclose(fp);
797           writecachedrepo(repo, cookie);
798           break;
799         default:
800           printf("unsupported repo '%s': skipped\n", cinfo->alias);
801           repo_free(repo, 1);
802           cinfo->repo = 0;
803           break;
804         }
805     }
806   if (sigpool)
807     pool_free(sigpool);
808 }
809
810 void
811 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
812 {
813   Id id, p, pp;
814   Id type = 0;
815   const char *r, *r2;
816
817   id = str2id(pool, arg, 0);
818   if (id)
819     {
820       FOR_PROVIDES(p, pp, id)
821         {
822           Solvable *s = pool_id2solvable(pool, p);
823           if (s->name == id)
824             {
825               type = SOLVER_SOLVABLE_NAME;
826               break;
827             }
828           type = SOLVER_SOLVABLE_PROVIDES;
829         }
830     }
831   if (!type)
832     {
833       /* did not find a solvable, see if it's a relation */
834       if ((r = strpbrk(arg, "<=>")) != 0)
835         {
836           Id rid, rname, revr;
837           int rflags = 0;
838           for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
839             r2--;
840           rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
841           for (; *r; r++)
842             {
843               if (*r == '<')
844                 rflags |= REL_LT;
845               else if (*r == '=')
846                 rflags |= REL_EQ;
847               else if (*r == '>')
848                 rflags |= REL_GT;
849               else
850                 break;
851             }
852           while (*r == ' ' || *r == '\t')
853             r++;
854           revr = *r ? str2id(pool, r, 1) : 0;
855           rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
856           if (rid)
857             {
858               FOR_PROVIDES(p, pp, rid)
859                 {
860                   Solvable *s = pool_id2solvable(pool, p);
861                   if (pool_match_nevr(pool, s, rid))
862                     {
863                       type = SOLVER_SOLVABLE_NAME;
864                       break;
865                     }
866                   type = SOLVER_SOLVABLE_PROVIDES;
867                 }
868             }
869           if (type)
870             id = rid;
871         }
872     }
873   if (type)
874     {
875       queue_push(out, type);
876       queue_push(out, id);
877     }
878 }
879
880 int
881 yesno(const char *str)
882 {
883   char inbuf[128], *ip;
884
885   for (;;)
886     {
887       printf("%s", str);
888       fflush(stdout);
889       *inbuf = 0;
890       if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
891         {
892           printf("Abort.\n");
893           exit(1);
894         }
895       while (*ip == ' ' || *ip == '\t')
896         ip++;
897       if (*ip == 'q')
898         {
899           printf("Abort.\n");
900           exit(1);
901         }
902       if (*ip == 'y' || *ip == 'n')
903         return *ip == 'y' ? 1 : 0;
904     }
905 }
906
907 struct fcstate {
908   FILE **newpkgsfps;
909   Queue *checkq;
910   int newpkgscnt;
911   void *rpmdbstate;
912 };
913
914 static void *
915 fc_cb(Pool *pool, Id p, void *cbdata)
916 {
917   struct fcstate *fcstate = cbdata;
918   Solvable *s;
919   Id rpmdbid;
920   int i;
921   FILE *fp;
922
923   if (!p)
924     {
925       rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
926       return 0;
927     }
928   s = pool_id2solvable(pool, p);
929   if (pool->installed && s->repo == pool->installed)
930     {
931       if (!s->repo->rpmdbid)
932         return 0;
933       rpmdbid = s->repo->rpmdbid[p - s->repo->start];
934       if (!rpmdbid)
935         return 0;
936        return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
937     }
938   for (i = 0; i < fcstate->newpkgscnt; i++)
939     if (fcstate->checkq->elements[i] == p)
940       break;
941   if (i == fcstate->newpkgscnt)
942     return 0;
943   fp = fcstate->newpkgsfps[i];
944   if (!fp)
945     return 0;
946   rewind(fp);
947   return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
948 }
949
950 void
951 runrpm(const char *arg, const char *name, int dupfd3)
952 {
953   pid_t pid;
954   int status;
955
956   if ((pid = fork()) == (pid_t)-1)
957     {
958       perror("fork");
959       exit(1);
960     }
961   if (pid == 0)
962     {
963       if (dupfd3 != -1 && dupfd3 != 3)
964         {
965           dup2(dupfd3, 3);
966           close(dupfd3);
967         }
968       if (dupfd3 != -1)
969         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
970       if (strcmp(arg, "-e") == 0)
971         execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
972       else
973         execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
974       perror("rpm");
975       _exit(0);
976     }
977   while (waitpid(pid, &status, 0) != pid)
978     ;
979   if (status)
980     {
981       printf("rpm failed\n");
982       exit(1);
983     }
984 }
985
986 static Id
987 nscallback(Pool *pool, void *data, Id name, Id evr)
988 {
989   if (name == NAMESPACE_PRODUCTBUDDY)
990     {    
991       /* SUSE specific hack: each product has an associated rpm */
992       Solvable *s = pool->solvables + evr; 
993       Id p, pp, cap; 
994       
995       cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
996       if (!cap)
997         return 0;
998       cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
999       if (!cap)
1000         return 0;
1001       FOR_PROVIDES(p, pp, cap) 
1002         {
1003           Solvable *ps = pool->solvables + p; 
1004           if (ps->repo == s->repo && ps->arch == s->arch)
1005             break;
1006         }
1007       return p;
1008     }
1009   return 0;
1010 }
1011
1012
1013 int
1014 main(int argc, char **argv)
1015 {
1016   Pool *pool;
1017   Id p, pp;
1018   struct repoinfo *repoinfos;
1019   int nrepoinfos = 0;
1020   int i, mode, newpkgs;
1021   Queue job, checkq;
1022   Solver *solv = 0;
1023   Transaction *trans;
1024   char inbuf[128], *ip;
1025   int updateall = 0;
1026   int distupgrade = 0;
1027   int patchmode = 0;
1028   FILE **newpkgsfps;
1029   struct fcstate fcstate;
1030
1031   argc--;
1032   argv++;
1033   if (!argv[0])
1034     {
1035       fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
1036       exit(1);
1037     }
1038   if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
1039     mode = SOLVER_INSTALL;
1040   else if (!strcmp(argv[0], "patch"))
1041     {
1042       mode = SOLVER_UPDATE;
1043       patchmode = 1;
1044     }
1045   else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
1046     mode = SOLVER_ERASE;
1047   else if (!strcmp(argv[0], "show"))
1048     mode = 0;
1049   else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
1050     mode = SOLVER_UPDATE;
1051   else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
1052     {
1053       mode = SOLVER_UPDATE;
1054       distupgrade = 1;
1055     }
1056   else
1057     {
1058       fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
1059       exit(1);
1060     }
1061
1062   pool = pool_create();
1063   pool->nscallback = nscallback;
1064   // pool_setdebuglevel(pool, 2);
1065   setarch(pool);
1066   repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
1067   read_repos(pool, repoinfos, nrepoinfos);
1068   // FOR_REPOS(i, repo)
1069   //   printf("%s: %d solvables\n", repo->name, repo->nsolvables);
1070   pool_addfileprovides(pool);
1071   pool_createwhatprovides(pool);
1072
1073   queue_init(&job);
1074   for (i = 1; i < argc; i++)
1075     mkselect(pool, argv[i], 0, &job);
1076   if (!job.count && mode == SOLVER_UPDATE)
1077     updateall = 1;
1078   else if (!job.count)
1079     {
1080       printf("no package matched\n");
1081       exit(1);
1082     }
1083
1084   if (!mode)
1085     {
1086       /* show mode, no solver needed */
1087       for (i = 0; i < job.count; i += 2)
1088         {
1089           FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
1090             {
1091               Solvable *s = pool_id2solvable(pool, p);
1092               printf("  - %s [%s]\n", solvable2str(pool, s), s->repo->name);
1093             }
1094         }
1095       exit(0);
1096     }
1097
1098   if (updateall && patchmode)
1099     {
1100       int pruneyou = 0;
1101       Map installedmap;
1102       Solvable *s;
1103
1104       map_init(&installedmap, pool->nsolvables);
1105       if (pool->installed)
1106         FOR_REPO_SOLVABLES(pool->installed, p, s)
1107           MAPSET(&installedmap, p);
1108
1109       /* install all patches */
1110       updateall = 0;
1111       mode = SOLVER_INSTALL;
1112       for (p = 1; p < pool->nsolvables; p++)
1113         {
1114           const char *type;
1115           int r;
1116           Id p2;
1117
1118           s = pool->solvables + p;
1119           if (strncmp(id2str(pool, s->name), "patch:", 6) != 0)
1120             continue;
1121           FOR_PROVIDES(p2, pp, s->name)
1122             {
1123               Solvable *s2 = pool->solvables + p2;
1124               if (s2->name != s->name)
1125                 continue;
1126               r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
1127               if (r < 0 || (r == 0 && p > p2))
1128                 break;
1129             }
1130           if (p2)
1131             continue;
1132           type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
1133           if (type && !strcmp(type, "optional"))
1134             continue;
1135           r = solvable_trivial_installable_map(s, &installedmap, 0);
1136           if (r == -1)
1137             continue;
1138           if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
1139             {
1140               if (!pruneyou++)
1141                 queue_empty(&job);
1142             }
1143           else if (pruneyou)
1144             continue;
1145           queue_push2(&job, SOLVER_SOLVABLE, p);
1146         }
1147       map_free(&installedmap);
1148     }
1149
1150   // add mode
1151   for (i = 0; i < job.count; i += 2)
1152     job.elements[i] |= mode;
1153
1154   // multiversion test
1155   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
1156   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
1157   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
1158
1159 rerunsolver:
1160   for (;;)
1161     {
1162       Id problem, solution;
1163       int pcnt, scnt;
1164
1165       solv = solver_create(pool);
1166       solv->ignorealreadyrecommended = 1;
1167       solv->updatesystem = updateall;
1168       solv->dosplitprovides = updateall;
1169       if (updateall && distupgrade)
1170         {
1171           solv->distupgrade = 1;
1172           solv->allowdowngrade = 1;
1173           solv->allowarchchange = 1;
1174           solv->allowvendorchange = 1;
1175         }
1176       // queue_push2(&job, SOLVER_DISTUPGRADE, 3);
1177       solver_solve(solv, &job);
1178       if (!solv->problems.count)
1179         break;
1180       pcnt = solver_problem_count(solv);
1181       printf("Found %d problems:\n", pcnt);
1182       for (problem = 1; problem <= pcnt; problem++)
1183         {
1184           int take = 0;
1185           printf("Problem %d:\n", problem);
1186           solver_printprobleminfo(solv, problem);
1187           printf("\n");
1188           scnt = solver_solution_count(solv, problem);
1189           for (solution = 1; solution <= scnt; solution++)
1190             {
1191               printf("Solution %d:\n", solution);
1192               solver_printsolution(solv, problem, solution);
1193               printf("\n");
1194             }
1195           for (;;)
1196             {
1197               printf("Please choose a solution: ");
1198               fflush(stdout);
1199               *inbuf = 0;
1200               if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
1201                 {
1202                   printf("Abort.\n");
1203                   exit(1);
1204                 }
1205               while (*ip == ' ' || *ip == '\t')
1206                 ip++;
1207               if (*ip >= '0' && *ip <= '9')
1208                 {
1209                   take = atoi(ip);
1210                   if (take >= 1 && take <= scnt)
1211                     break;
1212                 }
1213               if (*ip == 's')
1214                 {
1215                   take = 0;
1216                   break;
1217                 }
1218               if (*ip == 'q')
1219                 {
1220                   printf("Abort.\n");
1221                   exit(1);
1222                 }
1223             }
1224           if (!take)
1225             continue;
1226           solver_take_solution(solv, problem, take, &job);
1227         }
1228       solver_free(solv);
1229       solv = 0;
1230     }
1231   if (!solv->trans.steps.count)
1232     {
1233       printf("Nothing to do.\n");
1234       exit(1);
1235     }
1236   printf("\n");
1237   printf("Transaction summary:\n\n");
1238   solver_printtransaction(solv);
1239   if (!yesno("OK to continue (y/n)? "))
1240     {
1241       printf("Abort.\n");
1242       exit(1);
1243     }
1244
1245   trans = &solv->trans;
1246   queue_init(&checkq);
1247   newpkgs = transaction_installedresult(trans, &checkq);
1248   newpkgsfps = 0;
1249
1250   if (newpkgs)
1251     {
1252       printf("Downloading %d packages\n", newpkgs);
1253       newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
1254       for (i = 0; i < newpkgs; i++)
1255         {
1256           unsigned int medianr;
1257           char *loc;
1258           Solvable *s;
1259           struct repoinfo *cinfo;
1260           const unsigned char *chksum;
1261           Id chksumtype;
1262           Dataiterator di;
1263
1264           p = checkq.elements[i];
1265           s = pool_id2solvable(pool, p);
1266           cinfo = s->repo->appdata;
1267           if (!cinfo)
1268             {
1269               printf("%s: no repository information\n", s->repo->name);
1270               exit(1);
1271             }
1272           loc = solvable_get_location(s, &medianr);
1273           if (!loc)
1274              continue;
1275
1276           if (pool->installed && pool->installed->nsolvables)
1277             {
1278               /* try a delta first */
1279               dataiterator_init(&di, pool, 0, SOLVID_META, DELTA_PACKAGE_NAME, id2str(pool, s->name), SEARCH_STRING);
1280               dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
1281               while (dataiterator_step(&di))
1282                 {
1283                   Id baseevr, op;
1284
1285                   dataiterator_setpos_parent(&di);
1286                   if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
1287                       pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
1288                     continue;
1289                   baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
1290                   FOR_PROVIDES(op, pp, s->name)
1291                     {
1292                       Solvable *os = pool->solvables + op;
1293                       if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
1294                         break;
1295                     }
1296                   if (op)
1297                     {
1298                       /* base is installed, run sequence check */
1299                       const char *seqname;
1300                       const char *seqevr;
1301                       const char *seqnum;
1302                       const char *seq;
1303                       const char *dloc;
1304                       FILE *fp;
1305                       char cmd[128];
1306                       int newfd;
1307
1308                       seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
1309                       seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
1310                       seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
1311                       seq = pool_tmpjoin(pool, seqname, "-", seqevr);
1312                       seq = pool_tmpjoin(pool, seq, "-", seqnum);
1313                       if (system(pool_tmpjoin(pool, "applydeltarpm -c -s ", seq, 0)) != 0)
1314                         continue;       /* didn't match */
1315                       /* looks good, download delta */
1316                       chksumtype = 0;
1317                       chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
1318                       if (!chksumtype)
1319                         continue;       /* no way! */
1320                       dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
1321                       dloc = pool_tmpjoin(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
1322                       dloc = pool_tmpjoin(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
1323                       dloc = pool_tmpjoin(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
1324                       if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype)) == 0)
1325                         continue;
1326                       /* got it, now reconstruct */
1327                       newfd = opentmpfile();
1328                       sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
1329                       fcntl(fileno(fp), F_SETFD, 0);
1330                       if (system(cmd))
1331                         {
1332                           close(newfd);
1333                           fclose(fp);
1334                           continue;
1335                         }
1336                       lseek(newfd, 0, SEEK_SET);
1337                       chksumtype = 0;
1338                       chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
1339                       if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
1340                         {
1341                           close(newfd);
1342                           fclose(fp);
1343                           continue;
1344                         }
1345                       newpkgsfps[i] = fdopen(newfd, "r");
1346                       fclose(fp);
1347                       break;
1348                     }
1349                 }
1350               dataiterator_free(&di);
1351             }
1352           
1353           if (newpkgsfps[i])
1354             {
1355               putchar('d');
1356               fflush(stdout);
1357               continue;         /* delta worked! */
1358             }
1359           if (cinfo->type == TYPE_SUSETAGS)
1360             {
1361               const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
1362               loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
1363             }
1364           chksumtype = 0;
1365           chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
1366           if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype)) == 0)
1367             {
1368               printf("\n%s: %s not found in repository\n", s->repo->name, loc);
1369               exit(1);
1370             }
1371           putchar('.');
1372           fflush(stdout);
1373         }
1374       putchar('\n');
1375     }
1376
1377   if (newpkgs)
1378     {
1379       Queue conflicts;
1380
1381       printf("Searching for file conflicts\n");
1382       queue_init(&conflicts);
1383       fcstate.rpmdbstate = 0;
1384       fcstate.newpkgscnt = newpkgs;
1385       fcstate.checkq = &checkq;
1386       fcstate.newpkgsfps = newpkgsfps;
1387       pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
1388       if (conflicts.count)
1389         {
1390           printf("\n");
1391           for (i = 0; i < conflicts.count; i += 5)
1392             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]));
1393           printf("\n");
1394           if (yesno("Re-run solver (y/n/q)? "))
1395             {
1396               for (i = 0; i < newpkgs; i++)
1397                 if (newpkgsfps[i])
1398                   fclose(newpkgsfps[i]);
1399               newpkgsfps = sat_free(newpkgsfps);
1400               solver_free(solv);
1401               pool_add_fileconflicts_deps(pool, &conflicts);
1402               pool_createwhatprovides(pool);    /* Hmm... */
1403               goto rerunsolver;
1404             }
1405         }
1406       queue_free(&conflicts);
1407     }
1408
1409   printf("Committing transaction:\n\n");
1410   transaction_order(trans, 0);
1411   for (i = 0; i < trans->steps.count; i++)
1412     {
1413       const char *evr, *evrp, *nvra;
1414       Solvable *s;
1415       int j;
1416       FILE *fp;
1417
1418       p = trans->steps.elements[i];
1419       s = pool_id2solvable(pool, p);
1420       Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
1421       switch(type)
1422         {
1423         case SOLVER_TRANSACTION_ERASE:
1424           printf("erase %s\n", solvid2str(pool, p));
1425           if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
1426             continue;
1427           /* strip epoch from evr */
1428           evr = evrp = id2str(pool, s->evr);
1429           while (*evrp >= '0' && *evrp <= '9')
1430             evrp++;
1431           if (evrp > evr && evrp[0] == ':' && evrp[1])
1432             evr = evrp + 1;
1433           nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
1434           nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
1435           runrpm("-e", nvra, -1);       /* to bad that --querybynumber doesn't work */
1436           break;
1437         case SOLVER_TRANSACTION_INSTALL:
1438         case SOLVER_TRANSACTION_MULTIINSTALL:
1439           printf("install %s\n", solvid2str(pool, p));
1440           for (j = 0; j < newpkgs; j++)
1441             if (checkq.elements[j] == p)
1442               break;
1443           fp = j < newpkgs ? newpkgsfps[j] : 0;
1444           if (!fp)
1445             continue;
1446           rewind(fp);
1447           lseek(fileno(fp), 0, SEEK_SET);
1448           runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
1449           fclose(fp);
1450           newpkgsfps[j] = 0;
1451           break;
1452         default:
1453           break;
1454         }
1455     }
1456
1457   for (i = 0; i < newpkgs; i++)
1458     if (newpkgsfps[i])
1459       fclose(newpkgsfps[i]);
1460   sat_free(newpkgsfps);
1461   queue_free(&checkq);
1462   solver_free(solv);
1463   queue_free(&job);
1464   pool_free(pool);
1465   exit(0);
1466 }