2 * Copyright (c) 2009, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
8 /* solv, a little software installer demoing the sat solver library */
11 * - repository data caching
12 * - signature verification
23 #include <sys/utsname.h>
24 #include <sys/types.h>
32 #include "solverdebug.h"
34 #include "repo_rpmdb.h"
35 #include "repo_products.h"
36 #include "repo_rpmmd.h"
37 #include "repo_susetags.h"
38 #include "repo_repomdxml.h"
39 #include "repo_content.h"
40 #include "pool_fileconflicts.h"
44 # define REPOINFO_PATH "/etc/yum.repos.d"
46 # define REPOINFO_PATH "/etc/zypp/repos.d"
47 # define PRODUCTS_PATH "/etc/products.d"
66 #define TYPE_UNKNOWN 0
67 #define TYPE_SUSETAGS 1
69 #define TYPE_PLAINDIR 3
72 read_repoinfos_sort(const void *ap, const void *bp)
74 const struct repoinfo *a = ap;
75 const struct repoinfo *b = bp;
76 return strcmp(a->alias, b->alias);
80 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
83 char buf2[4096], *kp, *vp, *kpe;
88 struct repoinfo *repoinfos = 0, *cinfo;
91 rdlen = strlen(reposdir);
92 dir = opendir(reposdir);
98 while ((ent = readdir(dir)) != 0)
100 l = strlen(ent->d_name);
101 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
103 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
104 if ((fp = fopen(buf, "r")) == 0)
110 while(fgets(buf2, sizeof(buf2), fp))
115 while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
118 while (*kp == ' ' || *kp == '\t')
120 if (!*kp || *kp == '#')
126 vp = strrchr(kp, ']');
130 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
131 cinfo = repoinfos + nrepoinfos++;
132 memset(cinfo, 0, sizeof(*cinfo));
133 cinfo->alias = strdup(kp + 1);
135 cinfo->type = TYPE_RPMMD;
138 vp = strchr(kp, '=');
141 for (kpe = vp - 1; kpe >= kp; kpe--)
142 if (*kpe != ' ' && *kpe != '\t')
147 while (*vp == ' ' || *vp == '\t')
150 if (!strcmp(kp, "name"))
151 cinfo->name = strdup(vp);
152 else if (!strcmp(kp, "enabled"))
153 cinfo->enabled = *vp == '0' ? 0 : 1;
154 else if (!strcmp(kp, "autorefresh"))
155 cinfo->autorefresh = *vp == '0' ? 0 : 1;
156 else if (!strcmp(kp, "gpgcheck"))
157 cinfo->gpgcheck = *vp == '0' ? 0 : 1;
158 else if (!strcmp(kp, "baseurl"))
159 cinfo->baseurl = strdup(vp);
160 else if (!strcmp(kp, "path"))
161 cinfo->path = strdup(vp);
162 else if (!strcmp(kp, "type"))
164 if (!strcmp(vp, "yast2"))
165 cinfo->type = TYPE_SUSETAGS;
166 else if (!strcmp(vp, "rpm-md"))
167 cinfo->type = TYPE_RPMMD;
168 else if (!strcmp(vp, "plaindir"))
169 cinfo->type = TYPE_PLAINDIR;
171 cinfo->type = TYPE_UNKNOWN;
173 else if (!strcmp(kp, "priority"))
174 cinfo->priority = atoi(vp);
175 else if (!strcmp(kp, "keeppackages"))
176 cinfo->keeppackages = *vp == '0' ? 0 : 1;
182 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
183 *nrepoinfosp = nrepoinfos;
188 cookie_gzread(void *cookie, char *buf, size_t nbytes)
190 return gzread((gzFile *)cookie, buf, nbytes);
194 cookie_gzclose(void *cookie)
196 return gzclose((gzFile *)cookie);
200 myfopen(const char *fn)
202 cookie_io_functions_t cio;
208 suf = strrchr(fn, '.');
209 if (!suf || strcmp(suf, ".gz") != 0)
210 return fopen(fn, "r");
211 gzf = gzopen(fn, "r");
214 memset(&cio, 0, sizeof(cio));
215 cio.read = cookie_gzread;
216 cio.close = cookie_gzclose;
217 return fopencookie(gzf, "r", cio);
221 curlfopen(const char *baseurl, const char *file, int uncompress)
230 if (l && baseurl[l - 1] == '/')
231 snprintf(url, sizeof(url), "%s%s", baseurl, file);
233 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
234 strcpy(tmpl, "/var/tmp/solvXXXXXX");
242 if ((pid = fork()) == (pid_t)-1)
254 execlp("curl", "curl", "-s", "-L", url, (char *)0);
258 while (waitpid(pid, &status, 0) != pid)
260 if (lseek(fd, 0, SEEK_END) == 0)
266 lseek(fd, 0, SEEK_SET);
269 cookie_io_functions_t cio;
272 sprintf(tmpl, "/dev/fd/%d", fd);
273 gzf = gzopen(tmpl, "r");
277 memset(&cio, 0, sizeof(cio));
278 cio.read = cookie_gzread;
279 cio.close = cookie_gzclose;
280 return fopencookie(gzf, "r", cio);
282 fcntl(fd, F_SETFD, FD_CLOEXEC);
283 return fdopen(fd, "r");
295 pool_setarch(pool, un.machine);
300 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
303 struct repoinfo *cinfo;
307 const char *primaryfile;
308 const char *descrdir;
311 printf("reading rpm database\n");
312 repo = repo_create(pool, "@System");
314 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
316 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
318 pool_set_installed(pool, repo);
319 for (i = 0; i < nrepoinfos; i++)
321 cinfo = repoinfos + i;
327 printf("reading rpmmd repo '%s'\n", cinfo->alias);
328 if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
330 repo = repo_create(pool, cinfo->alias);
332 repo->appdata = cinfo;
333 repo->priority = 99 - cinfo->priority;
334 repo_add_repomdxml(repo, fp, 0);
337 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, "primary", SEARCH_STRING);
338 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
339 if (dataiterator_step(&di))
341 dataiterator_setpos_parent(&di);
342 primaryfile = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
344 dataiterator_free(&di);
346 primaryfile = "repodata/primary.xml.gz";
347 if ((fp = curlfopen(cinfo->baseurl, primaryfile, 1)) == 0)
349 repo_add_rpmmd(repo, fp, 0, 0);
353 printf("reading susetags repo '%s'\n", cinfo->alias);
354 repo = repo_create(pool, cinfo->alias);
356 repo->appdata = cinfo;
357 repo->priority = 99 - cinfo->priority;
360 if ((fp = curlfopen(cinfo->baseurl, "content", 0)) != 0)
362 repo_add_content(repo, fp, 0);
364 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
365 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
368 descrdir = "suse/setup/descr";
369 if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages.gz", 0), 1)) == 0)
370 if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages", 0), 0)) == 0)
372 repo_add_susetags(repo, fp, defvendor, 0, 0);
376 printf("skipping unknown repo '%s'\n", cinfo->alias);
383 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
389 id = str2id(pool, arg, 0);
392 FOR_PROVIDES(p, pp, id)
394 Solvable *s = pool_id2solvable(pool, p);
397 type = SOLVER_SOLVABLE_NAME;
400 type = SOLVER_SOLVABLE_PROVIDES;
405 /* did not find a solvable, see if it's a relation */
406 if ((r = strpbrk(arg, "<=>")) != 0)
410 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
412 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
424 while (*r == ' ' || *r == '\t')
426 revr = *r ? str2id(pool, r, 1) : 0;
427 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
430 FOR_PROVIDES(p, pp, rid)
432 Solvable *s = pool_id2solvable(pool, p);
433 if (pool_match_nevr(pool, s, rid))
435 type = SOLVER_SOLVABLE_NAME;
438 type = SOLVER_SOLVABLE_PROVIDES;
447 queue_push(out, type);
453 yesno(const char *str)
455 char inbuf[128], *ip;
462 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
467 while (*ip == ' ' || *ip == '\t')
474 if (*ip == 'y' || *ip == 'n')
475 return *ip == 'y' ? 1 : 0;
487 fc_cb(Pool *pool, Id p, void *cbdata)
489 struct fcstate *fcstate = cbdata;
497 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
500 s = pool_id2solvable(pool, p);
501 if (pool->installed && s->repo == pool->installed)
503 if (!s->repo->rpmdbid)
505 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
508 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
510 for (i = 0; i < fcstate->newpkgscnt; i++)
511 if (fcstate->checkq->elements[i] == p)
513 if (i == fcstate->newpkgscnt)
515 fp = fcstate->newpkgsfps[i];
519 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
523 runrpm(const char *arg, const char *name, int dupfd3)
528 if ((pid = fork()) == (pid_t)-1)
535 if (dupfd3 != -1 && dupfd3 != 3)
541 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
542 if (strcmp(arg, "-e") == 0)
543 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
545 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
549 while (waitpid(pid, &status, 0) != pid)
553 printf("rpm failed\n");
559 nscallback(Pool *pool, void *data, Id name, Id evr)
561 if (name == NAMESPACE_PRODUCTBUDDY)
563 /* SUSE specific hack: each product has an associated rpm */
564 Solvable *s = pool->solvables + evr;
567 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
570 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
573 FOR_PROVIDES(p, pp, cap)
575 Solvable *ps = pool->solvables + p;
576 if (ps->repo == s->repo && ps->arch == s->arch)
586 main(int argc, char **argv)
590 struct repoinfo *repoinfos;
592 int i, mode, newpkgs;
596 char inbuf[128], *ip;
599 struct fcstate fcstate;
603 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
604 mode = SOLVER_INSTALL;
605 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
607 else if (!strcmp(argv[0], "show"))
609 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
610 mode = SOLVER_UPDATE;
613 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
617 pool = pool_create();
618 pool->nscallback = nscallback;
619 // pool_setdebuglevel(pool, 2);
621 repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
622 read_repos(pool, repoinfos, nrepoinfos);
623 // FOR_REPOS(i, repo)
624 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
625 pool_addfileprovides(pool);
626 pool_createwhatprovides(pool);
629 for (i = 1; i < argc; i++)
630 mkselect(pool, argv[i], 0, &job);
631 if (!job.count && mode == SOLVER_UPDATE)
635 printf("no package matched\n");
641 /* show mode, no solver needed */
642 for (i = 0; i < job.count; i += 2)
644 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
646 Solvable *s = pool_id2solvable(pool, p);
647 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
654 for (i = 0; i < job.count; i += 2)
655 job.elements[i] |= mode;
658 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
659 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
660 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
665 Id problem, solution;
668 solv = solver_create(pool);
669 solv->ignorealreadyrecommended = 1;
670 solv->updatesystem = updateall;
671 solver_solve(solv, &job);
672 if (!solv->problems.count)
674 pcnt = solver_problem_count(solv);
675 printf("Found %d problems:\n", pcnt);
676 for (problem = 1; problem <= pcnt; problem++)
679 printf("Problem %d:\n", problem);
680 solver_printprobleminfo(solv, problem);
682 scnt = solver_solution_count(solv, problem);
683 for (solution = 1; solution <= scnt; solution++)
685 printf("Solution %d:\n", solution);
686 solver_printsolution(solv, problem, solution);
691 printf("Please choose a solution: ");
694 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
699 while (*ip == ' ' || *ip == '\t')
701 if (*ip >= '0' && *ip <= '9')
704 if (take >= 1 && take <= scnt)
720 solver_take_solution(solv, problem, take, &job);
725 if (!solv->trans.steps.count)
727 printf("Nothing to do.\n");
731 printf("Transaction summary:\n\n");
732 solver_printtransaction(solv);
733 if (!yesno("OK to continue (y/n)? "))
739 trans = &solv->trans;
741 newpkgs = transaction_installedresult(trans, &checkq);
746 printf("Downloading %d packages\n", newpkgs);
747 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
748 for (i = 0; i < newpkgs; i++)
750 unsigned int medianr;
753 struct repoinfo *cinfo;
755 p = checkq.elements[i];
756 s = pool_id2solvable(pool, p);
757 cinfo = s->repo->appdata;
760 printf("%s: no repository information\n", s->repo->name);
763 loc = solvable_get_location(s, &medianr);
766 if (cinfo->type == TYPE_SUSETAGS)
768 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
769 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
771 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
773 printf("%s: %s not found in repository\n", s->repo->name, loc);
783 printf("Searching for file conflicts\n");
784 queue_init(&conflicts);
785 fcstate.rpmdbstate = 0;
786 fcstate.newpkgscnt = newpkgs;
787 fcstate.checkq = &checkq;
788 fcstate.newpkgsfps = newpkgsfps;
789 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
793 for (i = 0; i < conflicts.count; i += 5)
794 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]));
796 if (yesno("Re-run solver (y/n/q)? "))
798 for (i = 0; i < newpkgs; i++)
800 fclose(newpkgsfps[i]);
801 newpkgsfps = sat_free(newpkgsfps);
803 pool_add_fileconflicts_deps(pool, &conflicts);
804 pool_createwhatprovides(pool); /* Hmm... */
808 queue_free(&conflicts);
811 printf("Committing transaction:\n\n");
812 transaction_order(trans, 0);
813 for (i = 0; i < trans->steps.count; i++)
815 const char *evr, *evrp, *nvra;
820 p = trans->steps.elements[i];
821 s = pool_id2solvable(pool, p);
822 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
825 case SOLVER_TRANSACTION_ERASE:
826 printf("erase %s\n", solvid2str(pool, p));
827 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
829 /* strip epoch from evr */
830 evr = evrp = id2str(pool, s->evr);
831 while (*evrp >= '0' && *evrp <= '9')
833 if (evrp > evr && evrp[0] == ':' && evrp[1])
835 nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
836 nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
837 runrpm("-e", nvra, -1); /* to bad that --querybynumber doesn't work */
839 case SOLVER_TRANSACTION_INSTALL:
840 case SOLVER_TRANSACTION_MULTIINSTALL:
841 printf("install %s\n", solvid2str(pool, p));
842 for (j = 0; j < newpkgs; j++)
843 if (checkq.elements[j] == p)
845 fp = j < newpkgs ? newpkgsfps[j] : 0;
849 lseek(fileno(fp), 0, SEEK_SET);
850 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
859 for (i = 0; i < newpkgs; i++)
861 fclose(newpkgsfps[i]);
862 sat_free(newpkgsfps);