- use appdata for repo
[platform/upstream/libsolv.git] / examples / solv.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <dirent.h>
6 #include <unistd.h>
7 #include <zlib.h>
8 #include <fcntl.h>
9 #include <sys/utsname.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
12
13 #include "pool.h"
14 #include "poolarch.h"
15 #include "repo.h"
16 #include "util.h"
17 #include "solver.h"
18 #include "solverdebug.h"
19
20 #include "repo_rpmdb.h"
21 #include "repo_rpmmd.h"
22 #include "repo_susetags.h"
23 #include "repo_repomdxml.h"
24 #include "pool_fileconflicts.h"
25
26 /* solv, a little demo application */
27
28 struct repoinfo {
29   Repo *repo;
30
31   char *alias;
32   char *name;
33   int enabled;
34   int autorefresh;
35   char *baseurl;
36   char *path;
37   int type;
38   int priority;
39   int keeppackages;
40 };
41
42 #define TYPE_UNKNOWN 0
43 #define TYPE_SUSETAGS 1
44 #define TYPE_RPMMD 2
45
46 struct repoinfo *
47 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
48 {
49   char buf[4096];
50   char buf2[4096], *kp, *vp, *kpe;
51   DIR *dir;
52   FILE *fp;
53   struct dirent *ent;
54   int l, rdlen;
55   struct repoinfo *repoinfos = 0, *cinfo;
56   int nrepoinfos = 0;
57
58   rdlen = strlen(reposdir);
59   dir = opendir(reposdir);
60   if (!dir)
61     {
62       *nrepoinfosp = 0;
63       return 0;
64     }
65   while ((ent = readdir(dir)) != 0)
66     {
67       l = strlen(ent->d_name);
68       if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
69         continue;
70       snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
71       if ((fp = fopen(buf, "r")) == 0)
72         {
73           perror(buf);
74           continue;
75         }
76       cinfo = 0;
77       while(fgets(buf2, sizeof(buf2), fp))
78         {
79           l = strlen(buf2);
80           if (l == 0)
81             continue;
82           while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
83             buf2[--l] = 0;
84           kp = buf2;
85           while (*kp == ' ' || *kp == '\t')
86             kp++;
87           if (!*kp || *kp == '#')
88             continue;
89           if (!cinfo)
90             {
91               if (*kp != '[')
92                 continue;
93               vp = strrchr(kp, ']');
94               if (!vp)
95                 continue;
96               *vp = 0;
97               repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
98               cinfo = repoinfos + nrepoinfos++;
99               memset(cinfo, 0, sizeof(*cinfo));
100               cinfo->alias = strdup(kp + 1);
101               continue;
102             }
103           vp = strchr(kp, '=');
104           if (!vp)
105             continue;
106           for (kpe = vp - 1; kpe >= kp; kpe--)
107             if (*kpe != ' ' && *kpe != '\t')
108               break;
109           if (kpe == kp)
110             continue;
111           vp++;
112           while (*vp == ' ' || *vp == '\t')
113             vp++;
114           kpe[1] = 0;
115           if (!strcmp(kp, "name"))
116             cinfo->name = strdup(vp);
117           else if (!strcmp(kp, "enabled"))
118             cinfo->enabled = *vp == '0' ? 0 : 1;
119           else if (!strcmp(kp, "autorefresh"))
120             cinfo->autorefresh = *vp == '0' ? 0 : 1;
121           else if (!strcmp(kp, "baseurl"))
122             cinfo->baseurl = strdup(vp);
123           else if (!strcmp(kp, "path"))
124             cinfo->path = strdup(vp);
125           else if (!strcmp(kp, "type"))
126             {
127               if (!strcmp(vp, "yast2"))
128                 cinfo->type = TYPE_SUSETAGS;
129               else if (!strcmp(vp, "rpm-md"))
130                 cinfo->type = TYPE_RPMMD;
131               else
132                 cinfo->type = TYPE_UNKNOWN;
133             }
134           else if (!strcmp(kp, "priority"))
135             cinfo->priority = atoi(vp);
136           else if (!strcmp(kp, "keeppackages"))
137             cinfo->keeppackages = *vp == '0' ? 0 : 1;
138         }
139       fclose(fp);
140       if (cinfo->type == TYPE_SUSETAGS && cinfo->baseurl)
141         {
142           char *old = cinfo->baseurl;
143           cinfo->baseurl = sat_malloc(5 + strlen(old) + 1);
144           sprintf(cinfo->baseurl, "%s/suse", old);
145           free(old);
146         }
147       cinfo = 0;
148     }
149   closedir(dir);
150   *nrepoinfosp = nrepoinfos;
151   return repoinfos;
152 }
153
154 static ssize_t
155 cookie_gzread(void *cookie, char *buf, size_t nbytes)
156 {
157   return gzread((gzFile *)cookie, buf, nbytes);
158 }
159
160 static int
161 cookie_gzclose(void *cookie)
162 {
163   return gzclose((gzFile *)cookie);
164 }
165
166 FILE *
167 myfopen(const char *fn)
168 {
169   cookie_io_functions_t cio;
170   char *suf;
171   gzFile *gzf;
172
173   if (!fn)
174     return 0;
175   suf = strrchr(fn, '.');
176   if (!suf || strcmp(suf, ".gz") != 0)
177     return fopen(fn, "r");
178   gzf = gzopen(fn, "r");
179   if (!gzf)
180     return 0;
181   memset(&cio, 0, sizeof(cio));
182   cio.read = cookie_gzread;
183   cio.close = cookie_gzclose;
184   return  fopencookie(gzf, "r", cio);
185 }
186
187 FILE *
188 curlfopen(char *baseurl, char *file, int uncompress)
189 {
190   pid_t pid;
191   int fd, l;
192   int status;
193   char tmpl[100];
194   char url[4096];
195
196   l = strlen(baseurl);
197   if (l && baseurl[l - 1] == '/')
198     snprintf(url, sizeof(url), "%s%s", baseurl, file);
199   else
200     snprintf(url, sizeof(url), "%s/%s", baseurl, file);
201   strcpy(tmpl, "/var/tmp/solvXXXXXX");
202   fd = mkstemp(tmpl);
203   if (fd < 0)
204     {
205       perror("mkstemp");
206       exit(1);
207     }
208   unlink(tmpl);
209   if ((pid = fork()) == (pid_t)-1)
210     {
211       perror("fork");
212       exit(1);
213     }
214   if (pid == 0)
215     {
216       if (fd != 1)
217         {
218           dup2(fd, 1);
219           close(fd);
220         }
221       execlp("curl", "curl", "-s", "-L", url, (char *)0);
222       perror("curl");
223       _exit(0);
224     }
225   while (waitpid(pid, &status, 0) != pid)
226     ;
227   lseek(fd, 0, SEEK_SET);
228   if (uncompress)
229     {
230       cookie_io_functions_t cio;
231       gzFile *gzf;
232
233       sprintf(tmpl, "/dev/fd/%d", fd);
234       gzf = gzopen(tmpl, "r");
235       close(fd);
236       if (!gzf)
237         return 0;
238       memset(&cio, 0, sizeof(cio));
239       cio.read = cookie_gzread;
240       cio.close = cookie_gzclose;
241       return fopencookie(gzf, "r", cio);
242     }
243   fcntl(fd, F_SETFD, FD_CLOEXEC);
244   return fdopen(fd, "r");
245 }
246
247 void
248 setarch(Pool *pool)
249 {
250   struct utsname un;
251   if (uname(&un))
252     {
253       perror("uname");
254       exit(1);
255     }
256   pool_setarch(pool, un.machine);
257 }
258
259 void
260 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
261 {
262   Repo *repo;
263   struct repoinfo *cinfo;
264   int i;
265   FILE *fp;
266
267   printf("reading rpm database\n");
268   repo = repo_create(pool, "@System");
269   repo_add_rpmdb(repo, 0, 0, 0);
270   pool_set_installed(pool, repo);
271   for (i = 0; i < nrepoinfos; i++)
272     {
273       cinfo = repoinfos + i;
274       if (!cinfo->enabled)
275         continue;
276       switch (cinfo->type)
277         {
278         case TYPE_RPMMD:
279           printf("reading rpmmd repo '%s'\n", cinfo->alias);
280           if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
281             break;
282           repo = repo_create(pool, cinfo->alias);
283           cinfo->repo = repo;
284           repo->appdata = cinfo;
285           repo_add_repomdxml(repo, fp, 0);
286           fclose(fp);
287           if ((fp = curlfopen(cinfo->baseurl, "repodata/primary.xml.gz", 1)) == 0)
288             continue;
289           repo_add_rpmmd(repo, fp, 0, 0);
290           fclose(fp);
291           break;
292         case TYPE_SUSETAGS:
293           printf("reading susetags repo '%s'\n", cinfo->alias);
294           if ((fp = curlfopen(cinfo->baseurl, "setup/descr/packages.gz", 1)) == 0)
295             break;
296           repo = repo_create(pool, cinfo->alias);
297           cinfo->repo = repo;
298           repo->appdata = cinfo;
299           repo_add_susetags(repo, fp, 0, 0, 0);
300           fclose(fp);
301           break;
302         default:
303           printf("skipping unknown repo '%s'\n", cinfo->alias);
304           break;
305         }
306     }
307 }
308
309 void
310 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
311 {
312   Id id, p, pp;
313   Id type = 0;
314   const char *r, *r2;
315
316   id = str2id(pool, arg, 0);
317   if (id)
318     {
319       FOR_PROVIDES(p, pp, id)
320         {
321           Solvable *s = pool_id2solvable(pool, p);
322           if (s->name == id)
323             {
324               type = SOLVER_SOLVABLE_NAME;
325               break;
326             }
327           type = SOLVER_SOLVABLE_PROVIDES;
328         }
329     }
330   if (!type)
331     {
332       /* did not find a solvable, see if it's a relation */
333       if ((r = strpbrk(arg, "<=>")) != 0)
334         {
335           Id rid, rname, revr;
336           int rflags = 0;
337           for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
338             r2--;
339           rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
340           for (; *r; r++)
341             {
342               if (*r == '<')
343                 rflags |= REL_LT;
344               else if (*r == '=')
345                 rflags |= REL_EQ;
346               else if (*r == '>')
347                 rflags |= REL_GT;
348               else
349                 break;
350             }
351           while (*r == ' ' || *r == '\t')
352             r++;
353           revr = *r ? str2id(pool, r, 1) : 0;
354           rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
355           if (rid)
356             {
357               FOR_PROVIDES(p, pp, rid)
358                 {
359                   Solvable *s = pool_id2solvable(pool, p);
360                   if (pool_match_nevr(pool, s, rid))
361                     {
362                       type = SOLVER_SOLVABLE_NAME;
363                       break;
364                     }
365                   type = SOLVER_SOLVABLE_PROVIDES;
366                 }
367             }
368           if (type)
369             id = rid;
370         }
371     }
372   if (type)
373     {
374       queue_push(out, type);
375       queue_push(out, id);
376     }
377 }
378
379 int
380 yesno(const char *str)
381 {
382   char inbuf[128], *ip;
383
384   for (;;)
385     {
386       printf("%s", str);
387       fflush(stdout);
388       *inbuf = 0;
389       if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
390         {
391           printf("Abort.\n");
392           exit(1);
393         }
394       while (*ip == ' ' || *ip == '\t')
395         ip++;
396       if (*ip == 'q')
397         {
398           printf("Abort.\n");
399           exit(1);
400         }
401       if (*ip == 'y' || *ip == 'n')
402         return *ip == 'y' ? 1 : 0;
403     }
404 }
405
406 struct fcstate {
407   FILE **newpkgsfps;
408   Queue *checkq;
409   int newpkgscnt;
410   void *rpmdbstate;
411 };
412
413 static void *
414 fc_cb(Pool *pool, Id p, void *cbdata)
415 {
416   struct fcstate *fcstate = cbdata;
417   Solvable *s;
418   Id rpmdbid;
419   int i;
420   FILE *fp;
421
422   if (!p)
423     {
424       rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
425       return 0;
426     }
427   s = pool_id2solvable(pool, p);
428   if (pool->installed && s->repo == pool->installed)
429     {
430       if (!s->repo->rpmdbid)
431         return 0;
432       rpmdbid = s->repo->rpmdbid[p - s->repo->start];
433       if (!rpmdbid)
434         return 0;
435        return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
436     }
437   for (i = 0; i < fcstate->newpkgscnt; i++)
438     if (fcstate->checkq->elements[i] == p)
439       break;
440   if (i == fcstate->newpkgscnt)
441     return 0;
442   fp = fcstate->newpkgsfps[i];
443   rewind(fp);
444   return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
445 }
446
447 void
448 runrpm(const char *arg, const char *name, int dupfd3)
449 {
450   pid_t pid;
451   int status;
452
453   if ((pid = fork()) == (pid_t)-1)
454     {
455       perror("fork");
456       exit(1);
457     }
458   if (pid == 0)
459     {
460       if (dupfd3 != -1 && dupfd3 != 3)
461         {
462           dup2(dupfd3, 3);
463           close(dupfd3);
464         }
465       if (dupfd3 != -1)
466         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
467       if (strcmp(arg, "-e") == 0)
468         execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
469       else
470         execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
471       perror("rpm");
472       _exit(0);
473     }
474   while (waitpid(pid, &status, 0) != pid)
475     ;
476   if (status)
477     {
478       printf("rpm failed\n");
479       exit(1);
480     }
481 }
482
483 int
484 main(int argc, char **argv)
485 {
486   Pool *pool;
487   Id p, pp;
488   struct repoinfo *repoinfos;
489   int nrepoinfos = 0;
490   int i, mode, newpkgs;
491   Queue job, checkq;
492   Solver *solv = 0;
493   Transaction *trans;
494   char inbuf[128], *ip;
495   int updateall = 0;
496   FILE **newpkgsfps = 0;
497   struct fcstate fcstate;
498
499   if (!strcmp(argv[1], "install") || !strcmp(argv[1], "in"))
500     mode = SOLVER_INSTALL;
501   else if (!strcmp(argv[1], "erase") || !strcmp(argv[1], "rm"))
502     mode = SOLVER_ERASE;
503   else if (!strcmp(argv[1], "show"))
504     mode = 0;
505   else if (!strcmp(argv[1], "update") || !strcmp(argv[1], "up"))
506     mode = SOLVER_UPDATE;
507   else
508     {
509       fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
510       exit(1);
511     }
512
513   pool = pool_create();
514   // pool_setdebuglevel(pool, 2);
515   setarch(pool);
516   repoinfos = read_repoinfos(pool, "/etc/zypp/repos.d", &nrepoinfos);
517   read_repos(pool, repoinfos, nrepoinfos);
518   // FOR_REPOS(i, repo)
519   //   printf("%s: %d solvables\n", repo->name, repo->nsolvables);
520   pool_addfileprovides(pool);
521   pool_createwhatprovides(pool);
522
523   queue_init(&job);
524   for (i = 2; i < argc; i++)
525     mkselect(pool, argv[i], 0, &job);
526   if (!job.count && mode == SOLVER_UPDATE)
527     updateall = 1;
528   else if (!job.count)
529     {
530       printf("nothing matched\n");
531       exit(1);
532     }
533   if (!mode)
534     {
535       for (i = 0; i < job.count; i += 2)
536         {
537           FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
538             {
539               Solvable *s = pool_id2solvable(pool, p);
540               printf("  - %s [%s]\n", solvable2str(pool, s), s->repo->name);
541             }
542         }
543       exit(0);
544     }
545   // add mode
546   for (i = 0; i < job.count; i += 2)
547     job.elements[i] |= mode;
548
549 rerunsolver:
550   for (;;)
551     {
552       Id problem, solution;
553       int pcnt, scnt;
554
555       solv = solver_create(pool);
556       solv->ignorealreadyrecommended = 1;
557       solv->updatesystem = updateall;
558       solver_solve(solv, &job);
559       if (!solv->problems.count)
560         break;
561       pcnt = solver_problem_count(solv);
562       printf("Found %d problems:\n", pcnt);
563       for (problem = 1; problem <= pcnt; problem++)
564         {
565           int take = 0;
566           printf("Problem %d:\n", problem);
567           solver_printprobleminfo(solv, problem);
568           printf("\n");
569           scnt = solver_solution_count(solv, problem);
570           for (solution = 1; solution <= pcnt; solution++)
571             {
572               printf("Solution %d:\n", solution);
573               solver_printsolution(solv, problem, solution);
574               printf("\n");
575             }
576           for (;;)
577             {
578               printf("Please choose a solution: ");
579               fflush(stdout);
580               *inbuf = 0;
581               if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
582                 {
583                   printf("Abort.\n");
584                   exit(1);
585                 }
586               while (*ip == ' ' || *ip == '\t')
587                 ip++;
588               if (*ip >= '0' && *ip <= '9')
589                 {
590                   take = atoi(ip);
591                   if (take >= 1 && take <= solution)
592                     break;
593                 }
594               if (*ip == 's')
595                 {
596                   take = 0;
597                   break;
598                 }
599               if (*ip == 'q')
600                 {
601                   printf("Abort.\n");
602                   exit(1);
603                 }
604             }
605           if (!take)
606             continue;
607           solver_take_solution(solv, problem, take, &job);
608         }
609       solver_free(solv);
610       solv = 0;
611     }
612   if (!solv->trans.steps.count)
613     {
614       printf("Nothing to do.\n");
615       exit(1);
616     }
617   printf("\n");
618   printf("Transaction summary:\n\n");
619   solver_printtransaction(solv);
620   if (!yesno("OK to continue (y/n)? "))
621     {
622       printf("Abort.\n");
623       exit(1);
624     }
625
626   trans = &solv->trans;
627   queue_init(&checkq);
628   newpkgs = transaction_installedresult(trans, &checkq);
629   if (newpkgs)
630     {
631       Queue conflicts;
632       printf("Downloading %d packages\n", newpkgs);
633       newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
634       for (i = 0; i < newpkgs; i++)
635         {
636           unsigned int medianr;
637           char *loc;
638           Solvable *s;
639           struct repoinfo *cinfo;
640
641           p = checkq.elements[i];
642           s = pool_id2solvable(pool, p);
643           cinfo = s->repo->appdata;
644           if (!cinfo)
645             {
646               printf("%s: no repository information\n", s->repo->name);
647               exit(1);
648             }
649           loc = solvable_get_location(s, &medianr);
650           if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
651             {
652               printf("%s: %s not found\n", s->repo->name, loc);
653               exit(1);
654             }
655         }
656       printf("Searching for file conflicts\n");
657       queue_init(&conflicts);
658       fcstate.rpmdbstate = 0;
659       fcstate.newpkgscnt = newpkgs;
660       fcstate.checkq = &checkq;
661       fcstate.newpkgsfps = newpkgsfps;
662       pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
663       if (conflicts.count)
664         {
665           for (i = 0; i < newpkgs; i++)
666             fclose(newpkgsfps[i]);
667           sat_free(newpkgsfps);
668           solver_free(solv);
669
670           printf("\n");
671           for (i = 0; i < conflicts.count; i += 5)
672             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]));
673           printf("\n");
674           if (!yesno("Re-run solver (y/n)? "))
675             {
676               printf("Abort.\n");
677               exit(1);
678             }
679           pool_add_fileconflicts_deps(pool, &conflicts);
680           pool_createwhatprovides(pool);
681           goto rerunsolver;
682         }
683       queue_free(&conflicts);
684     }
685   transaction_order(trans, 0);
686
687   printf("Committing transaction:\n\n");
688   for (i = 0; i < trans->steps.count; i++)
689     {
690       char rpmname[256];
691       const char *evr, *evrp;
692       Solvable *s;
693       int j;
694       FILE *fp;
695
696       p = trans->steps.elements[i];
697       s = pool_id2solvable(pool, p);
698       Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
699       switch(type)
700         {
701         case SOLVER_TRANSACTION_ERASE:
702           printf("erase %s\n", solvid2str(pool, p));
703           if (strlen(solvid2str(pool, p)) > 255)
704             continue;
705           evr = evrp = id2str(pool, s->evr);
706           while (*evrp >= '0' && *evrp <= '9')
707             evrp++;
708           if (evrp > evr && evrp[0] == ':' && evrp[1])
709             evr = evrp + 1;
710           sprintf(rpmname, "%s-%s.%s", id2str(pool, s->name), evr, id2str(pool, s->arch));
711           runrpm("-e", rpmname, -1);
712           break;
713         case SOLVER_TRANSACTION_INSTALL:
714         case SOLVER_TRANSACTION_MULTIINSTALL:
715           printf("install %s\n", solvid2str(pool, p));
716           for (j = 0; j < newpkgs; j++)
717             if (checkq.elements[j] == p)
718               break;
719           fp = j < newpkgs ? newpkgsfps[j] : 0;
720           rewind(fp);
721           lseek(fileno(fp), 0, SEEK_SET);
722           runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
723           fclose(fp);
724           newpkgsfps[j] = 0;
725           break;
726         default:
727           break;
728         }
729     }
730   for (i = 0; i < newpkgs; i++)
731     if (newpkgsfps[i])
732       fclose(newpkgsfps[i]);
733   sat_free(newpkgsfps);
734   queue_free(&checkq);
735   solver_free(solv);
736   queue_free(&job);
737   pool_free(pool);
738   exit(0);
739 }