e7092a61deb13b6d37cf0260c14c72716fd84453
[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  * - signature and checksum verification
12  * - vendor policy loading
13  */
14
15 #define _GNU_SOURCE
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <dirent.h>
20 #include <unistd.h>
21 #include <zlib.h>
22 #include <fcntl.h>
23 #include <sys/utsname.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26
27 #include "pool.h"
28 #include "poolarch.h"
29 #include "repo.h"
30 #include "util.h"
31 #include "solver.h"
32 #include "solverdebug.h"
33 #include "chksum.h"
34 #include "repo_solv.h"
35
36 #include "repo_write.h"
37 #include "repo_rpmdb.h"
38 #include "repo_products.h"
39 #include "repo_rpmmd.h"
40 #include "repo_susetags.h"
41 #include "repo_repomdxml.h"
42 #include "repo_content.h"
43 #include "pool_fileconflicts.h"
44
45
46 #ifdef FEDORA
47 # define REPOINFO_PATH "/etc/yum.repos.d"
48 #else
49 # define REPOINFO_PATH "/etc/zypp/repos.d"
50 # define PRODUCTS_PATH "/etc/products.d"
51 #endif
52
53 #define SOLVCACHE_PATH "/var/cache/solv"
54
55
56 struct repoinfo {
57   Repo *repo;
58
59   char *alias;
60   char *name;
61   int enabled;
62   int autorefresh;
63   char *baseurl;
64   char *path;
65   int type;
66   int gpgcheck;
67   int priority;
68   int keeppackages;
69 };
70
71 #define TYPE_UNKNOWN    0
72 #define TYPE_SUSETAGS   1
73 #define TYPE_RPMMD      2
74 #define TYPE_PLAINDIR   3
75
76 static int
77 read_repoinfos_sort(const void *ap, const void *bp)
78 {
79   const struct repoinfo *a = ap;
80   const struct repoinfo *b = bp;
81   return strcmp(a->alias, b->alias);
82 }
83
84 struct repoinfo *
85 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
86 {
87   char buf[4096];
88   char buf2[4096], *kp, *vp, *kpe;
89   DIR *dir;
90   FILE *fp;
91   struct dirent *ent;
92   int l, rdlen;
93   struct repoinfo *repoinfos = 0, *cinfo;
94   int nrepoinfos = 0;
95
96   rdlen = strlen(reposdir);
97   dir = opendir(reposdir);
98   if (!dir)
99     {
100       *nrepoinfosp = 0;
101       return 0;
102     }
103   while ((ent = readdir(dir)) != 0)
104     {
105       l = strlen(ent->d_name);
106       if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
107         continue;
108       snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
109       if ((fp = fopen(buf, "r")) == 0)
110         {
111           perror(buf);
112           continue;
113         }
114       cinfo = 0;
115       while(fgets(buf2, sizeof(buf2), fp))
116         {
117           l = strlen(buf2);
118           if (l == 0)
119             continue;
120           while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
121             buf2[--l] = 0;
122           kp = buf2;
123           while (*kp == ' ' || *kp == '\t')
124             kp++;
125           if (!*kp || *kp == '#')
126             continue;
127           if (!cinfo)
128             {
129               if (*kp != '[')
130                 continue;
131               vp = strrchr(kp, ']');
132               if (!vp)
133                 continue;
134               *vp = 0;
135               repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
136               cinfo = repoinfos + nrepoinfos++;
137               memset(cinfo, 0, sizeof(*cinfo));
138               cinfo->alias = strdup(kp + 1);
139               cinfo->gpgcheck = 1;
140               cinfo->type = TYPE_RPMMD;
141               continue;
142             }
143           vp = strchr(kp, '=');
144           if (!vp)
145             continue;
146           for (kpe = vp - 1; kpe >= kp; kpe--)
147             if (*kpe != ' ' && *kpe != '\t')
148               break;
149           if (kpe == kp)
150             continue;
151           vp++;
152           while (*vp == ' ' || *vp == '\t')
153             vp++;
154           kpe[1] = 0;
155           if (!strcmp(kp, "name"))
156             cinfo->name = strdup(vp);
157           else if (!strcmp(kp, "enabled"))
158             cinfo->enabled = *vp == '0' ? 0 : 1;
159           else if (!strcmp(kp, "autorefresh"))
160             cinfo->autorefresh = *vp == '0' ? 0 : 1;
161           else if (!strcmp(kp, "gpgcheck"))
162             cinfo->gpgcheck = *vp == '0' ? 0 : 1;
163           else if (!strcmp(kp, "baseurl"))
164             cinfo->baseurl = strdup(vp);
165           else if (!strcmp(kp, "path"))
166             cinfo->path = strdup(vp);
167           else if (!strcmp(kp, "type"))
168             {
169               if (!strcmp(vp, "yast2"))
170                 cinfo->type = TYPE_SUSETAGS;
171               else if (!strcmp(vp, "rpm-md"))
172                 cinfo->type = TYPE_RPMMD;
173               else if (!strcmp(vp, "plaindir"))
174                 cinfo->type = TYPE_PLAINDIR;
175               else
176                 cinfo->type = TYPE_UNKNOWN;
177             }
178           else if (!strcmp(kp, "priority"))
179             cinfo->priority = atoi(vp);
180           else if (!strcmp(kp, "keeppackages"))
181             cinfo->keeppackages = *vp == '0' ? 0 : 1;
182         }
183       fclose(fp);
184       cinfo = 0;
185     }
186   closedir(dir);
187   qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
188   *nrepoinfosp = nrepoinfos;
189   return repoinfos;
190 }
191
192 static ssize_t
193 cookie_gzread(void *cookie, char *buf, size_t nbytes)
194 {
195   return gzread((gzFile *)cookie, buf, nbytes);
196 }
197
198 static int
199 cookie_gzclose(void *cookie)
200 {
201   return gzclose((gzFile *)cookie);
202 }
203
204 FILE *
205 myfopen(const char *fn)
206 {
207   cookie_io_functions_t cio;
208   char *suf;
209   gzFile *gzf;
210
211   if (!fn)
212     return 0;
213   suf = strrchr(fn, '.');
214   if (!suf || strcmp(suf, ".gz") != 0)
215     return fopen(fn, "r");
216   gzf = gzopen(fn, "r");
217   if (!gzf)
218     return 0;
219   memset(&cio, 0, sizeof(cio));
220   cio.read = cookie_gzread;
221   cio.close = cookie_gzclose;
222   return  fopencookie(gzf, "r", cio);
223 }
224
225 FILE *
226 curlfopen(const char *baseurl, const char *file, int uncompress)
227 {
228   pid_t pid;
229   int fd, l;
230   int status;
231   char tmpl[100];
232   char url[4096];
233
234   l = strlen(baseurl);
235   if (l && baseurl[l - 1] == '/')
236     snprintf(url, sizeof(url), "%s%s", baseurl, file);
237   else
238     snprintf(url, sizeof(url), "%s/%s", baseurl, file);
239   strcpy(tmpl, "/var/tmp/solvXXXXXX");
240   fd = mkstemp(tmpl);
241   if (fd < 0)
242     {
243       perror("mkstemp");
244       exit(1);
245     }
246   unlink(tmpl);
247   if ((pid = fork()) == (pid_t)-1)
248     {
249       perror("fork");
250       exit(1);
251     }
252   if (pid == 0)
253     {
254       if (fd != 1)
255         {
256           dup2(fd, 1);
257           close(fd);
258         }
259       execlp("curl", "curl", "-s", "-L", url, (char *)0);
260       perror("curl");
261       _exit(0);
262     }
263   while (waitpid(pid, &status, 0) != pid)
264     ;
265   if (lseek(fd, 0, SEEK_END) == 0)
266     {
267       /* empty file */
268       close(fd);
269       return 0;
270     }
271   lseek(fd, 0, SEEK_SET);
272   if (uncompress)
273     {
274       cookie_io_functions_t cio;
275       gzFile *gzf;
276
277       sprintf(tmpl, "/dev/fd/%d", fd);
278       gzf = gzopen(tmpl, "r");
279       close(fd);
280       if (!gzf)
281         return 0;
282       memset(&cio, 0, sizeof(cio));
283       cio.read = cookie_gzread;
284       cio.close = cookie_gzclose;
285       return fopencookie(gzf, "r", cio);
286     }
287   fcntl(fd, F_SETFD, FD_CLOEXEC);
288   return fdopen(fd, "r");
289 }
290
291 void
292 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
293 {
294   char buf[4096];
295   void *h = sat_chksum_create(chktype);
296   int l;
297
298   while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
299     sat_chksum_add(h, buf, l);
300   rewind(fp);
301   sat_chksum_free(h, out);
302 }
303
304 void
305 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
306 {
307   void *h = sat_chksum_create(chktype);
308   sat_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
309   sat_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
310   sat_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
311   sat_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
312   sat_chksum_free(h, out);
313 }
314
315 void
316 setarch(Pool *pool)
317 {
318   struct utsname un;
319   if (uname(&un))
320     {
321       perror("uname");
322       exit(1);
323     }
324   pool_setarch(pool, un.machine);
325 }
326
327 char *calccachepath(Repo *repo)
328 {
329   char *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
330   char *q = p;
331   q = p + strlen(SOLVCACHE_PATH) + 1;
332   if (*q == '.')
333     *q = '_';
334   for (; *q; q++)
335     if (*q == '/')
336       *q = '_';
337   return p;
338 }
339
340 int
341 usecachedrepo(Repo *repo, unsigned char *cookie)
342 {
343   FILE *fp;
344   unsigned char mycookie[32];
345
346   if (!(fp = fopen(calccachepath(repo), "r")))
347     return 0;
348   if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
349     {
350       fclose(fp);
351       return 0;
352     }
353   if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
354     {
355       fclose(fp);
356       return 0;
357     }
358   rewind(fp);
359   if (repo_add_solv(repo, fp))
360     {
361       fclose(fp);
362       return 0;
363     }
364   fclose(fp);
365   return 1;
366 }
367
368 void
369 writecachedrepo(Repo *repo, unsigned char *cookie)
370 {
371   Id *addedfileprovides = 0;
372   FILE *fp;
373   int i, fd;
374   char *tmpl;
375   Repodata *info;
376
377   mkdir(SOLVCACHE_PATH, 0755);
378   tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
379   fd = mkstemp(tmpl);
380   if (!fd)
381     {
382       free(tmpl);
383       return;
384     }
385   fchmod(fd, 0444);
386   if (!(fp = fdopen(fd, "w")))
387     {
388       close(fd);
389       unlink(tmpl);
390       free(tmpl);
391       return;
392     }
393   info = repo_add_repodata(repo, 0);
394   pool_addfileprovides_ids(repo->pool, 0, &addedfileprovides);
395   if (addedfileprovides && *addedfileprovides)
396     {
397       for (i = 0; addedfileprovides[i]; i++)
398         repodata_add_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[i]);
399     }
400   sat_free(addedfileprovides);
401   repodata_internalize(info);
402   repo_write(repo, fp, 0, 0, 0);
403   repodata_free(info);
404   fwrite(cookie, 32, 1, fp);
405   if (fclose(fp))
406     {
407       unlink(tmpl);
408       free(tmpl);
409       return;
410     }
411   if (!rename(tmpl, calccachepath(repo)))
412     unlink(tmpl);
413   free(tmpl);
414 }
415
416 void
417 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
418 {
419   Repo *repo;
420   struct repoinfo *cinfo;
421   int i;
422   FILE *fp;
423   Dataiterator di;
424   const char *primaryfile;
425   const char *descrdir;
426   int defvendor;
427   int havecontent;
428   struct stat stb;
429   unsigned char cookie[32];
430
431   repo = repo_create(pool, "@System");
432   printf("rpm database:");
433   if (stat("/var/lib/rpm/Packages", &stb))
434     memset(&stb, 0, sizeof(&stb));
435   calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cookie);
436   if (usecachedrepo(repo, cookie))
437     printf(" cached\n");
438   else
439     {
440       FILE *ofp;
441       printf(" reading\n");
442       int done = 0;
443
444 #ifdef PRODUCTS_PATH
445       repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
446 #endif
447       if ((ofp = fopen(calccachepath(repo), "r")) != 0)
448         {
449           Repo *ref = repo_create(pool, "@System.old");
450           if (!repo_add_solv(ref, ofp))
451             {
452               repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
453               done = 1;
454             }
455           fclose(ofp);
456           repo_free(ref, 1);
457         }
458       if (!done)
459         repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
460       writecachedrepo(repo, cookie);
461     }
462   pool_set_installed(pool, repo);
463
464   for (i = 0; i < nrepoinfos; i++)
465     {
466       cinfo = repoinfos + i;
467       if (!cinfo->enabled)
468         continue;
469
470       repo = repo_create(pool, cinfo->alias);
471       cinfo->repo = repo;
472       repo->appdata = cinfo;
473       repo->priority = 99 - cinfo->priority;
474
475       switch (cinfo->type)
476         {
477         case TYPE_RPMMD:
478           printf("rpmmd repo '%s':", cinfo->alias);
479           fflush(stdout);
480           if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
481             {
482               printf(" no data, skipped\n");
483               repo_free(repo, 1);
484               cinfo->repo = 0;
485               break;
486             }
487           calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
488           if (usecachedrepo(repo, cookie))
489             {
490               printf(" cached\n");
491               fclose(fp);
492               break;
493             }
494           printf(" reading\n");
495           repo_add_repomdxml(repo, fp, 0);
496           fclose(fp);
497           primaryfile = 0;
498           dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, "primary", SEARCH_STRING);
499           dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
500           if (dataiterator_step(&di))
501             {
502               dataiterator_setpos_parent(&di);
503               primaryfile = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
504             }
505           dataiterator_free(&di);
506           if (!primaryfile)
507             primaryfile = "repodata/primary.xml.gz";
508           if ((fp = curlfopen(cinfo->baseurl, primaryfile, 1)) == 0)
509             continue;
510           repo_add_rpmmd(repo, fp, 0, 0);
511           fclose(fp);
512           writecachedrepo(repo, cookie);
513           break;
514         case TYPE_SUSETAGS:
515           printf("susetags repo '%s':", cinfo->alias);
516           fflush(stdout);
517           repo = repo_create(pool, cinfo->alias);
518           cinfo->repo = repo;
519           repo->appdata = cinfo;
520           repo->priority = 99 - cinfo->priority;
521           descrdir = 0;
522           defvendor = 0;
523           if ((fp = curlfopen(cinfo->baseurl, "content", 0)) != 0)
524             {
525               calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
526               if (usecachedrepo(repo, cookie))
527                 {
528                   printf(" cached\n");
529                   fclose(fp);
530                   break;
531                 }
532               havecontent = 1;  /* ok to write cache */
533               repo_add_content(repo, fp, 0);
534               fclose(fp);
535               defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
536               descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
537             }
538           if (!descrdir)
539             descrdir = "suse/setup/descr";
540           printf(" reading\n");
541           if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages.gz", 0), 1)) == 0)
542             if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages", 0), 0)) == 0)
543               break;
544           repo_add_susetags(repo, fp, defvendor, 0, 0);
545           fclose(fp);
546           if (havecontent)
547             writecachedrepo(repo, cookie);
548           break;
549         default:
550           printf("unsupported repo '%s': skipped\n", cinfo->alias);
551           repo_free(repo, 1);
552           cinfo->repo = 0;
553           break;
554         }
555     }
556 }
557
558 void
559 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
560 {
561   Id id, p, pp;
562   Id type = 0;
563   const char *r, *r2;
564
565   id = str2id(pool, arg, 0);
566   if (id)
567     {
568       FOR_PROVIDES(p, pp, id)
569         {
570           Solvable *s = pool_id2solvable(pool, p);
571           if (s->name == id)
572             {
573               type = SOLVER_SOLVABLE_NAME;
574               break;
575             }
576           type = SOLVER_SOLVABLE_PROVIDES;
577         }
578     }
579   if (!type)
580     {
581       /* did not find a solvable, see if it's a relation */
582       if ((r = strpbrk(arg, "<=>")) != 0)
583         {
584           Id rid, rname, revr;
585           int rflags = 0;
586           for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
587             r2--;
588           rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
589           for (; *r; r++)
590             {
591               if (*r == '<')
592                 rflags |= REL_LT;
593               else if (*r == '=')
594                 rflags |= REL_EQ;
595               else if (*r == '>')
596                 rflags |= REL_GT;
597               else
598                 break;
599             }
600           while (*r == ' ' || *r == '\t')
601             r++;
602           revr = *r ? str2id(pool, r, 1) : 0;
603           rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
604           if (rid)
605             {
606               FOR_PROVIDES(p, pp, rid)
607                 {
608                   Solvable *s = pool_id2solvable(pool, p);
609                   if (pool_match_nevr(pool, s, rid))
610                     {
611                       type = SOLVER_SOLVABLE_NAME;
612                       break;
613                     }
614                   type = SOLVER_SOLVABLE_PROVIDES;
615                 }
616             }
617           if (type)
618             id = rid;
619         }
620     }
621   if (type)
622     {
623       queue_push(out, type);
624       queue_push(out, id);
625     }
626 }
627
628 int
629 yesno(const char *str)
630 {
631   char inbuf[128], *ip;
632
633   for (;;)
634     {
635       printf("%s", str);
636       fflush(stdout);
637       *inbuf = 0;
638       if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
639         {
640           printf("Abort.\n");
641           exit(1);
642         }
643       while (*ip == ' ' || *ip == '\t')
644         ip++;
645       if (*ip == 'q')
646         {
647           printf("Abort.\n");
648           exit(1);
649         }
650       if (*ip == 'y' || *ip == 'n')
651         return *ip == 'y' ? 1 : 0;
652     }
653 }
654
655 struct fcstate {
656   FILE **newpkgsfps;
657   Queue *checkq;
658   int newpkgscnt;
659   void *rpmdbstate;
660 };
661
662 static void *
663 fc_cb(Pool *pool, Id p, void *cbdata)
664 {
665   struct fcstate *fcstate = cbdata;
666   Solvable *s;
667   Id rpmdbid;
668   int i;
669   FILE *fp;
670
671   if (!p)
672     {
673       rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
674       return 0;
675     }
676   s = pool_id2solvable(pool, p);
677   if (pool->installed && s->repo == pool->installed)
678     {
679       if (!s->repo->rpmdbid)
680         return 0;
681       rpmdbid = s->repo->rpmdbid[p - s->repo->start];
682       if (!rpmdbid)
683         return 0;
684        return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
685     }
686   for (i = 0; i < fcstate->newpkgscnt; i++)
687     if (fcstate->checkq->elements[i] == p)
688       break;
689   if (i == fcstate->newpkgscnt)
690     return 0;
691   fp = fcstate->newpkgsfps[i];
692   if (!fp)
693     return 0;
694   rewind(fp);
695   return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
696 }
697
698 void
699 runrpm(const char *arg, const char *name, int dupfd3)
700 {
701   pid_t pid;
702   int status;
703
704   if ((pid = fork()) == (pid_t)-1)
705     {
706       perror("fork");
707       exit(1);
708     }
709   if (pid == 0)
710     {
711       if (dupfd3 != -1 && dupfd3 != 3)
712         {
713           dup2(dupfd3, 3);
714           close(dupfd3);
715         }
716       if (dupfd3 != -1)
717         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
718       if (strcmp(arg, "-e") == 0)
719         execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
720       else
721         execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
722       perror("rpm");
723       _exit(0);
724     }
725   while (waitpid(pid, &status, 0) != pid)
726     ;
727   if (status)
728     {
729       printf("rpm failed\n");
730       exit(1);
731     }
732 }
733
734 static Id
735 nscallback(Pool *pool, void *data, Id name, Id evr)
736 {
737   if (name == NAMESPACE_PRODUCTBUDDY)
738     {    
739       /* SUSE specific hack: each product has an associated rpm */
740       Solvable *s = pool->solvables + evr; 
741       Id p, pp, cap; 
742       
743       cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
744       if (!cap)
745         return 0;
746       cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
747       if (!cap)
748         return 0;
749       FOR_PROVIDES(p, pp, cap) 
750         {
751           Solvable *ps = pool->solvables + p; 
752           if (ps->repo == s->repo && ps->arch == s->arch)
753             break;
754         }
755       return p;
756     }
757   return 0;
758 }
759
760
761 int
762 main(int argc, char **argv)
763 {
764   Pool *pool;
765   Id p, pp;
766   struct repoinfo *repoinfos;
767   int nrepoinfos = 0;
768   int i, mode, newpkgs;
769   Queue job, checkq;
770   Solver *solv = 0;
771   Transaction *trans;
772   char inbuf[128], *ip;
773   int updateall = 0;
774   FILE **newpkgsfps;
775   struct fcstate fcstate;
776
777   argc--;
778   argv++;
779   if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
780     mode = SOLVER_INSTALL;
781   else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
782     mode = SOLVER_ERASE;
783   else if (!strcmp(argv[0], "show"))
784     mode = 0;
785   else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
786     mode = SOLVER_UPDATE;
787   else
788     {
789       fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
790       exit(1);
791     }
792
793   pool = pool_create();
794   pool->nscallback = nscallback;
795   // pool_setdebuglevel(pool, 2);
796   setarch(pool);
797   repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
798   read_repos(pool, repoinfos, nrepoinfos);
799   // FOR_REPOS(i, repo)
800   //   printf("%s: %d solvables\n", repo->name, repo->nsolvables);
801   pool_addfileprovides(pool);
802   pool_createwhatprovides(pool);
803
804   queue_init(&job);
805   for (i = 1; i < argc; i++)
806     mkselect(pool, argv[i], 0, &job);
807   if (!job.count && mode == SOLVER_UPDATE)
808     updateall = 1;
809   else if (!job.count)
810     {
811       printf("no package matched\n");
812       exit(1);
813     }
814
815   if (!mode)
816     {
817       /* show mode, no solver needed */
818       for (i = 0; i < job.count; i += 2)
819         {
820           FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
821             {
822               Solvable *s = pool_id2solvable(pool, p);
823               printf("  - %s [%s]\n", solvable2str(pool, s), s->repo->name);
824             }
825         }
826       exit(0);
827     }
828
829   // add mode
830   for (i = 0; i < job.count; i += 2)
831     job.elements[i] |= mode;
832
833   // multiversion test
834   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
835   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
836   // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
837
838 rerunsolver:
839   for (;;)
840     {
841       Id problem, solution;
842       int pcnt, scnt;
843
844       solv = solver_create(pool);
845       solv->ignorealreadyrecommended = 1;
846       solv->updatesystem = updateall;
847       solver_solve(solv, &job);
848       if (!solv->problems.count)
849         break;
850       pcnt = solver_problem_count(solv);
851       printf("Found %d problems:\n", pcnt);
852       for (problem = 1; problem <= pcnt; problem++)
853         {
854           int take = 0;
855           printf("Problem %d:\n", problem);
856           solver_printprobleminfo(solv, problem);
857           printf("\n");
858           scnt = solver_solution_count(solv, problem);
859           for (solution = 1; solution <= scnt; solution++)
860             {
861               printf("Solution %d:\n", solution);
862               solver_printsolution(solv, problem, solution);
863               printf("\n");
864             }
865           for (;;)
866             {
867               printf("Please choose a solution: ");
868               fflush(stdout);
869               *inbuf = 0;
870               if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
871                 {
872                   printf("Abort.\n");
873                   exit(1);
874                 }
875               while (*ip == ' ' || *ip == '\t')
876                 ip++;
877               if (*ip >= '0' && *ip <= '9')
878                 {
879                   take = atoi(ip);
880                   if (take >= 1 && take <= scnt)
881                     break;
882                 }
883               if (*ip == 's')
884                 {
885                   take = 0;
886                   break;
887                 }
888               if (*ip == 'q')
889                 {
890                   printf("Abort.\n");
891                   exit(1);
892                 }
893             }
894           if (!take)
895             continue;
896           solver_take_solution(solv, problem, take, &job);
897         }
898       solver_free(solv);
899       solv = 0;
900     }
901   if (!solv->trans.steps.count)
902     {
903       printf("Nothing to do.\n");
904       exit(1);
905     }
906   printf("\n");
907   printf("Transaction summary:\n\n");
908   solver_printtransaction(solv);
909   if (!yesno("OK to continue (y/n)? "))
910     {
911       printf("Abort.\n");
912       exit(1);
913     }
914
915   trans = &solv->trans;
916   queue_init(&checkq);
917   newpkgs = transaction_installedresult(trans, &checkq);
918   newpkgsfps = 0;
919
920   if (newpkgs)
921     {
922       printf("Downloading %d packages\n", newpkgs);
923       newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
924       for (i = 0; i < newpkgs; i++)
925         {
926           unsigned int medianr;
927           char *loc;
928           Solvable *s;
929           struct repoinfo *cinfo;
930
931           p = checkq.elements[i];
932           s = pool_id2solvable(pool, p);
933           cinfo = s->repo->appdata;
934           if (!cinfo)
935             {
936               printf("%s: no repository information\n", s->repo->name);
937               exit(1);
938             }
939           loc = solvable_get_location(s, &medianr);
940           if (!loc)
941              continue;
942           if (cinfo->type == TYPE_SUSETAGS)
943             {
944               const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
945               loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
946             }
947           if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
948             {
949               printf("%s: %s not found in repository\n", s->repo->name, loc);
950               exit(1);
951             }
952         }
953     }
954
955   if (newpkgs)
956     {
957       Queue conflicts;
958
959       printf("Searching for file conflicts\n");
960       queue_init(&conflicts);
961       fcstate.rpmdbstate = 0;
962       fcstate.newpkgscnt = newpkgs;
963       fcstate.checkq = &checkq;
964       fcstate.newpkgsfps = newpkgsfps;
965       pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
966       if (conflicts.count)
967         {
968           printf("\n");
969           for (i = 0; i < conflicts.count; i += 5)
970             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]));
971           printf("\n");
972           if (yesno("Re-run solver (y/n/q)? "))
973             {
974               for (i = 0; i < newpkgs; i++)
975                 if (newpkgsfps[i])
976                   fclose(newpkgsfps[i]);
977               newpkgsfps = sat_free(newpkgsfps);
978               solver_free(solv);
979               pool_add_fileconflicts_deps(pool, &conflicts);
980               pool_createwhatprovides(pool);    /* Hmm... */
981               goto rerunsolver;
982             }
983         }
984       queue_free(&conflicts);
985     }
986
987   printf("Committing transaction:\n\n");
988   transaction_order(trans, 0);
989   for (i = 0; i < trans->steps.count; i++)
990     {
991       const char *evr, *evrp, *nvra;
992       Solvable *s;
993       int j;
994       FILE *fp;
995
996       p = trans->steps.elements[i];
997       s = pool_id2solvable(pool, p);
998       Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
999       switch(type)
1000         {
1001         case SOLVER_TRANSACTION_ERASE:
1002           printf("erase %s\n", solvid2str(pool, p));
1003           if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
1004             continue;
1005           /* strip epoch from evr */
1006           evr = evrp = id2str(pool, s->evr);
1007           while (*evrp >= '0' && *evrp <= '9')
1008             evrp++;
1009           if (evrp > evr && evrp[0] == ':' && evrp[1])
1010             evr = evrp + 1;
1011           nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
1012           nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
1013           runrpm("-e", nvra, -1);       /* to bad that --querybynumber doesn't work */
1014           break;
1015         case SOLVER_TRANSACTION_INSTALL:
1016         case SOLVER_TRANSACTION_MULTIINSTALL:
1017           printf("install %s\n", solvid2str(pool, p));
1018           for (j = 0; j < newpkgs; j++)
1019             if (checkq.elements[j] == p)
1020               break;
1021           fp = j < newpkgs ? newpkgsfps[j] : 0;
1022           if (!fp)
1023             continue;
1024           rewind(fp);
1025           lseek(fileno(fp), 0, SEEK_SET);
1026           runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
1027           fclose(fp);
1028           newpkgsfps[j] = 0;
1029           break;
1030         default:
1031           break;
1032         }
1033     }
1034
1035   for (i = 0; i < newpkgs; i++)
1036     if (newpkgsfps[i])
1037       fclose(newpkgsfps[i]);
1038   sat_free(newpkgsfps);
1039   queue_free(&checkq);
1040   solver_free(solv);
1041   queue_free(&job);
1042   pool_free(pool);
1043   exit(0);
1044 }