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(Pool *pool, const char *reposdir, int *nrepoinfosp)
75 char buf2[4096], *kp, *vp, *kpe;
80 struct repoinfo *repoinfos = 0, *cinfo;
83 rdlen = strlen(reposdir);
84 dir = opendir(reposdir);
90 while ((ent = readdir(dir)) != 0)
92 l = strlen(ent->d_name);
93 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
95 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
96 if ((fp = fopen(buf, "r")) == 0)
102 while(fgets(buf2, sizeof(buf2), fp))
107 while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
110 while (*kp == ' ' || *kp == '\t')
112 if (!*kp || *kp == '#')
118 vp = strrchr(kp, ']');
122 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
123 cinfo = repoinfos + nrepoinfos++;
124 memset(cinfo, 0, sizeof(*cinfo));
125 cinfo->alias = strdup(kp + 1);
127 cinfo->type = TYPE_RPMMD;
130 vp = strchr(kp, '=');
133 for (kpe = vp - 1; kpe >= kp; kpe--)
134 if (*kpe != ' ' && *kpe != '\t')
139 while (*vp == ' ' || *vp == '\t')
142 if (!strcmp(kp, "name"))
143 cinfo->name = strdup(vp);
144 else if (!strcmp(kp, "enabled"))
145 cinfo->enabled = *vp == '0' ? 0 : 1;
146 else if (!strcmp(kp, "autorefresh"))
147 cinfo->autorefresh = *vp == '0' ? 0 : 1;
148 else if (!strcmp(kp, "gpgcheck"))
149 cinfo->gpgcheck = *vp == '0' ? 0 : 1;
150 else if (!strcmp(kp, "baseurl"))
151 cinfo->baseurl = strdup(vp);
152 else if (!strcmp(kp, "path"))
153 cinfo->path = strdup(vp);
154 else if (!strcmp(kp, "type"))
156 if (!strcmp(vp, "yast2"))
157 cinfo->type = TYPE_SUSETAGS;
158 else if (!strcmp(vp, "rpm-md"))
159 cinfo->type = TYPE_RPMMD;
160 else if (!strcmp(vp, "plaindir"))
161 cinfo->type = TYPE_PLAINDIR;
163 cinfo->type = TYPE_UNKNOWN;
165 else if (!strcmp(kp, "priority"))
166 cinfo->priority = atoi(vp);
167 else if (!strcmp(kp, "keeppackages"))
168 cinfo->keeppackages = *vp == '0' ? 0 : 1;
174 *nrepoinfosp = nrepoinfos;
179 cookie_gzread(void *cookie, char *buf, size_t nbytes)
181 return gzread((gzFile *)cookie, buf, nbytes);
185 cookie_gzclose(void *cookie)
187 return gzclose((gzFile *)cookie);
191 myfopen(const char *fn)
193 cookie_io_functions_t cio;
199 suf = strrchr(fn, '.');
200 if (!suf || strcmp(suf, ".gz") != 0)
201 return fopen(fn, "r");
202 gzf = gzopen(fn, "r");
205 memset(&cio, 0, sizeof(cio));
206 cio.read = cookie_gzread;
207 cio.close = cookie_gzclose;
208 return fopencookie(gzf, "r", cio);
212 curlfopen(const char *baseurl, const char *file, int uncompress)
221 if (l && baseurl[l - 1] == '/')
222 snprintf(url, sizeof(url), "%s%s", baseurl, file);
224 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
225 strcpy(tmpl, "/var/tmp/solvXXXXXX");
233 if ((pid = fork()) == (pid_t)-1)
245 execlp("curl", "curl", "-s", "-L", url, (char *)0);
249 while (waitpid(pid, &status, 0) != pid)
251 if (lseek(fd, 0, SEEK_END) == 0)
257 lseek(fd, 0, SEEK_SET);
260 cookie_io_functions_t cio;
263 sprintf(tmpl, "/dev/fd/%d", fd);
264 gzf = gzopen(tmpl, "r");
268 memset(&cio, 0, sizeof(cio));
269 cio.read = cookie_gzread;
270 cio.close = cookie_gzclose;
271 return fopencookie(gzf, "r", cio);
273 fcntl(fd, F_SETFD, FD_CLOEXEC);
274 return fdopen(fd, "r");
286 pool_setarch(pool, un.machine);
291 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
294 struct repoinfo *cinfo;
298 const char *primaryfile;
299 const char *descrdir;
302 printf("reading rpm database\n");
303 repo = repo_create(pool, "@System");
305 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
307 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
309 pool_set_installed(pool, repo);
310 for (i = 0; i < nrepoinfos; i++)
312 cinfo = repoinfos + i;
318 printf("reading rpmmd repo '%s'\n", cinfo->alias);
319 if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
321 repo = repo_create(pool, cinfo->alias);
323 repo->appdata = cinfo;
324 repo->priority = 99 - cinfo->priority;
325 repo_add_repomdxml(repo, fp, 0);
328 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, "primary", SEARCH_STRING);
329 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
330 if (dataiterator_step(&di))
332 dataiterator_setpos_parent(&di);
333 primaryfile = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
335 dataiterator_free(&di);
337 primaryfile = "repodata/primary.xml.gz";
338 if ((fp = curlfopen(cinfo->baseurl, primaryfile, 1)) == 0)
340 repo_add_rpmmd(repo, fp, 0, 0);
344 printf("reading susetags repo '%s'\n", cinfo->alias);
345 repo = repo_create(pool, cinfo->alias);
347 repo->appdata = cinfo;
348 repo->priority = 99 - cinfo->priority;
351 if ((fp = curlfopen(cinfo->baseurl, "content", 0)) != 0)
353 repo_add_content(repo, fp, 0);
355 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
356 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
359 descrdir = "suse/setup/descr";
360 if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages.gz", 0), 1)) == 0)
361 if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages", 0), 0)) == 0)
363 repo_add_susetags(repo, fp, defvendor, 0, 0);
367 printf("skipping unknown repo '%s'\n", cinfo->alias);
374 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
380 id = str2id(pool, arg, 0);
383 FOR_PROVIDES(p, pp, id)
385 Solvable *s = pool_id2solvable(pool, p);
388 type = SOLVER_SOLVABLE_NAME;
391 type = SOLVER_SOLVABLE_PROVIDES;
396 /* did not find a solvable, see if it's a relation */
397 if ((r = strpbrk(arg, "<=>")) != 0)
401 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
403 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
415 while (*r == ' ' || *r == '\t')
417 revr = *r ? str2id(pool, r, 1) : 0;
418 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
421 FOR_PROVIDES(p, pp, rid)
423 Solvable *s = pool_id2solvable(pool, p);
424 if (pool_match_nevr(pool, s, rid))
426 type = SOLVER_SOLVABLE_NAME;
429 type = SOLVER_SOLVABLE_PROVIDES;
438 queue_push(out, type);
444 yesno(const char *str)
446 char inbuf[128], *ip;
453 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
458 while (*ip == ' ' || *ip == '\t')
465 if (*ip == 'y' || *ip == 'n')
466 return *ip == 'y' ? 1 : 0;
478 fc_cb(Pool *pool, Id p, void *cbdata)
480 struct fcstate *fcstate = cbdata;
488 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
491 s = pool_id2solvable(pool, p);
492 if (pool->installed && s->repo == pool->installed)
494 if (!s->repo->rpmdbid)
496 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
499 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
501 for (i = 0; i < fcstate->newpkgscnt; i++)
502 if (fcstate->checkq->elements[i] == p)
504 if (i == fcstate->newpkgscnt)
506 fp = fcstate->newpkgsfps[i];
510 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
514 runrpm(const char *arg, const char *name, int dupfd3)
519 if ((pid = fork()) == (pid_t)-1)
526 if (dupfd3 != -1 && dupfd3 != 3)
532 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
533 if (strcmp(arg, "-e") == 0)
534 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
536 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
540 while (waitpid(pid, &status, 0) != pid)
544 printf("rpm failed\n");
550 nscallback(Pool *pool, void *data, Id name, Id evr)
552 if (name == NAMESPACE_PRODUCTBUDDY)
554 /* SUSE specific hack: each product has an associated rpm */
555 Solvable *s = pool->solvables + evr;
558 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
561 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
564 FOR_PROVIDES(p, pp, cap)
566 Solvable *ps = pool->solvables + p;
567 if (ps->repo == s->repo && ps->arch == s->arch)
577 main(int argc, char **argv)
581 struct repoinfo *repoinfos;
583 int i, mode, newpkgs;
587 char inbuf[128], *ip;
590 struct fcstate fcstate;
592 if (!strcmp(argv[1], "install") || !strcmp(argv[1], "in"))
593 mode = SOLVER_INSTALL;
594 else if (!strcmp(argv[1], "erase") || !strcmp(argv[1], "rm"))
596 else if (!strcmp(argv[1], "show"))
598 else if (!strcmp(argv[1], "update") || !strcmp(argv[1], "up"))
599 mode = SOLVER_UPDATE;
602 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
606 pool = pool_create();
607 pool->nscallback = nscallback;
608 // pool_setdebuglevel(pool, 2);
610 repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
611 read_repos(pool, repoinfos, nrepoinfos);
612 // FOR_REPOS(i, repo)
613 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
614 pool_addfileprovides(pool);
615 pool_createwhatprovides(pool);
618 for (i = 2; i < argc; i++)
619 mkselect(pool, argv[i], 0, &job);
620 if (!job.count && mode == SOLVER_UPDATE)
624 printf("no package matched\n");
630 /* show mode, no solver needed */
631 for (i = 0; i < job.count; i += 2)
633 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
635 Solvable *s = pool_id2solvable(pool, p);
636 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
643 for (i = 0; i < job.count; i += 2)
644 job.elements[i] |= mode;
647 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
648 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
649 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
654 Id problem, solution;
657 solv = solver_create(pool);
658 solv->ignorealreadyrecommended = 1;
659 solv->updatesystem = updateall;
660 solver_solve(solv, &job);
661 if (!solv->problems.count)
663 pcnt = solver_problem_count(solv);
664 printf("Found %d problems:\n", pcnt);
665 for (problem = 1; problem <= pcnt; problem++)
668 printf("Problem %d:\n", problem);
669 solver_printprobleminfo(solv, problem);
671 scnt = solver_solution_count(solv, problem);
672 for (solution = 1; solution <= scnt; solution++)
674 printf("Solution %d:\n", solution);
675 solver_printsolution(solv, problem, solution);
680 printf("Please choose a solution: ");
683 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
688 while (*ip == ' ' || *ip == '\t')
690 if (*ip >= '0' && *ip <= '9')
693 if (take >= 1 && take <= scnt)
709 solver_take_solution(solv, problem, take, &job);
714 if (!solv->trans.steps.count)
716 printf("Nothing to do.\n");
720 printf("Transaction summary:\n\n");
721 solver_printtransaction(solv);
722 if (!yesno("OK to continue (y/n)? "))
728 trans = &solv->trans;
730 newpkgs = transaction_installedresult(trans, &checkq);
735 printf("Downloading %d packages\n", newpkgs);
736 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
737 for (i = 0; i < newpkgs; i++)
739 unsigned int medianr;
742 struct repoinfo *cinfo;
744 p = checkq.elements[i];
745 s = pool_id2solvable(pool, p);
746 cinfo = s->repo->appdata;
749 printf("%s: no repository information\n", s->repo->name);
752 loc = solvable_get_location(s, &medianr);
755 if (cinfo->type == TYPE_SUSETAGS)
757 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
758 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
760 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
762 printf("%s: %s not found in repository\n", s->repo->name, loc);
772 printf("Searching for file conflicts\n");
773 queue_init(&conflicts);
774 fcstate.rpmdbstate = 0;
775 fcstate.newpkgscnt = newpkgs;
776 fcstate.checkq = &checkq;
777 fcstate.newpkgsfps = newpkgsfps;
778 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
782 for (i = 0; i < conflicts.count; i += 5)
783 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]));
785 if (yesno("Re-run solver (y/n/q)? "))
787 for (i = 0; i < newpkgs; i++)
789 fclose(newpkgsfps[i]);
790 newpkgsfps = sat_free(newpkgsfps);
792 pool_add_fileconflicts_deps(pool, &conflicts);
793 pool_createwhatprovides(pool); /* Hmm... */
797 queue_free(&conflicts);
800 printf("Committing transaction:\n\n");
801 transaction_order(trans, 0);
802 for (i = 0; i < trans->steps.count; i++)
804 const char *evr, *evrp, *nvra;
809 p = trans->steps.elements[i];
810 s = pool_id2solvable(pool, p);
811 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
814 case SOLVER_TRANSACTION_ERASE:
815 printf("erase %s\n", solvid2str(pool, p));
816 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
818 /* strip epoch from evr */
819 evr = evrp = id2str(pool, s->evr);
820 while (*evrp >= '0' && *evrp <= '9')
822 if (evrp > evr && evrp[0] == ':' && evrp[1])
824 nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
825 nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
826 runrpm("-e", nvra, -1); /* to bad that --querybynumber doesn't work */
828 case SOLVER_TRANSACTION_INSTALL:
829 case SOLVER_TRANSACTION_MULTIINSTALL:
830 printf("install %s\n", solvid2str(pool, p));
831 for (j = 0; j < newpkgs; j++)
832 if (checkq.elements[j] == p)
834 fp = j < newpkgs ? newpkgsfps[j] : 0;
838 lseek(fileno(fp), 0, SEEK_SET);
839 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
848 for (i = 0; i < newpkgs; i++)
850 fclose(newpkgsfps[i]);
851 sat_free(newpkgsfps);