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"
43 #define ZYPP_REPOINFO_PATH "/etc/zypp/repos.d"
44 #define ZYPP_PRODUCTS_PATH "/etc/products.d"
60 #define TYPE_UNKNOWN 0
61 #define TYPE_SUSETAGS 1
65 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
68 char buf2[4096], *kp, *vp, *kpe;
73 struct repoinfo *repoinfos = 0, *cinfo;
76 rdlen = strlen(reposdir);
77 dir = opendir(reposdir);
83 while ((ent = readdir(dir)) != 0)
85 l = strlen(ent->d_name);
86 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
88 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
89 if ((fp = fopen(buf, "r")) == 0)
95 while(fgets(buf2, sizeof(buf2), fp))
100 while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
103 while (*kp == ' ' || *kp == '\t')
105 if (!*kp || *kp == '#')
111 vp = strrchr(kp, ']');
115 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
116 cinfo = repoinfos + nrepoinfos++;
117 memset(cinfo, 0, sizeof(*cinfo));
118 cinfo->alias = strdup(kp + 1);
121 vp = strchr(kp, '=');
124 for (kpe = vp - 1; kpe >= kp; kpe--)
125 if (*kpe != ' ' && *kpe != '\t')
130 while (*vp == ' ' || *vp == '\t')
133 if (!strcmp(kp, "name"))
134 cinfo->name = strdup(vp);
135 else if (!strcmp(kp, "enabled"))
136 cinfo->enabled = *vp == '0' ? 0 : 1;
137 else if (!strcmp(kp, "autorefresh"))
138 cinfo->autorefresh = *vp == '0' ? 0 : 1;
139 else if (!strcmp(kp, "baseurl"))
140 cinfo->baseurl = strdup(vp);
141 else if (!strcmp(kp, "path"))
142 cinfo->path = strdup(vp);
143 else if (!strcmp(kp, "type"))
145 if (!strcmp(vp, "yast2"))
146 cinfo->type = TYPE_SUSETAGS;
147 else if (!strcmp(vp, "rpm-md"))
148 cinfo->type = TYPE_RPMMD;
150 cinfo->type = TYPE_UNKNOWN;
152 else if (!strcmp(kp, "priority"))
153 cinfo->priority = atoi(vp);
154 else if (!strcmp(kp, "keeppackages"))
155 cinfo->keeppackages = *vp == '0' ? 0 : 1;
161 *nrepoinfosp = nrepoinfos;
166 cookie_gzread(void *cookie, char *buf, size_t nbytes)
168 return gzread((gzFile *)cookie, buf, nbytes);
172 cookie_gzclose(void *cookie)
174 return gzclose((gzFile *)cookie);
178 myfopen(const char *fn)
180 cookie_io_functions_t cio;
186 suf = strrchr(fn, '.');
187 if (!suf || strcmp(suf, ".gz") != 0)
188 return fopen(fn, "r");
189 gzf = gzopen(fn, "r");
192 memset(&cio, 0, sizeof(cio));
193 cio.read = cookie_gzread;
194 cio.close = cookie_gzclose;
195 return fopencookie(gzf, "r", cio);
199 curlfopen(const char *baseurl, const char *file, int uncompress)
208 if (l && baseurl[l - 1] == '/')
209 snprintf(url, sizeof(url), "%s%s", baseurl, file);
211 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
212 strcpy(tmpl, "/var/tmp/solvXXXXXX");
220 if ((pid = fork()) == (pid_t)-1)
232 execlp("curl", "curl", "-s", "-L", url, (char *)0);
236 while (waitpid(pid, &status, 0) != pid)
238 if (lseek(fd, 0, SEEK_END) == 0)
244 lseek(fd, 0, SEEK_SET);
247 cookie_io_functions_t cio;
250 sprintf(tmpl, "/dev/fd/%d", fd);
251 gzf = gzopen(tmpl, "r");
255 memset(&cio, 0, sizeof(cio));
256 cio.read = cookie_gzread;
257 cio.close = cookie_gzclose;
258 return fopencookie(gzf, "r", cio);
260 fcntl(fd, F_SETFD, FD_CLOEXEC);
261 return fdopen(fd, "r");
273 pool_setarch(pool, un.machine);
278 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
281 struct repoinfo *cinfo;
285 const char *primaryfile;
286 const char *descrdir;
289 printf("reading rpm database\n");
290 repo = repo_create(pool, "@System");
291 repo_add_products(repo, ZYPP_PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
292 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
294 pool_set_installed(pool, repo);
295 for (i = 0; i < nrepoinfos; i++)
297 cinfo = repoinfos + i;
303 printf("reading rpmmd repo '%s'\n", cinfo->alias);
304 if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
306 repo = repo_create(pool, cinfo->alias);
308 repo->appdata = cinfo;
309 repo->priority = 99 - cinfo->priority;
310 repo_add_repomdxml(repo, fp, 0);
313 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, "primary", SEARCH_STRING);
314 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
315 if (dataiterator_step(&di))
317 dataiterator_setpos_parent(&di);
318 primaryfile = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
320 dataiterator_free(&di);
322 primaryfile = "repodata/primary.xml.gz";
323 if ((fp = curlfopen(cinfo->baseurl, primaryfile, 1)) == 0)
325 repo_add_rpmmd(repo, fp, 0, 0);
329 printf("reading susetags repo '%s'\n", cinfo->alias);
330 repo = repo_create(pool, cinfo->alias);
332 repo->appdata = cinfo;
333 repo->priority = 99 - cinfo->priority;
336 if ((fp = curlfopen(cinfo->baseurl, "content", 0)) != 0)
338 repo_add_content(repo, fp, 0);
340 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
341 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
344 descrdir = "suse/setup/descr";
345 if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages.gz", 0), 1)) == 0)
346 if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages", 0), 0)) == 0)
348 repo_add_susetags(repo, fp, defvendor, 0, 0);
352 printf("skipping unknown repo '%s'\n", cinfo->alias);
359 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
365 id = str2id(pool, arg, 0);
368 FOR_PROVIDES(p, pp, id)
370 Solvable *s = pool_id2solvable(pool, p);
373 type = SOLVER_SOLVABLE_NAME;
376 type = SOLVER_SOLVABLE_PROVIDES;
381 /* did not find a solvable, see if it's a relation */
382 if ((r = strpbrk(arg, "<=>")) != 0)
386 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
388 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
400 while (*r == ' ' || *r == '\t')
402 revr = *r ? str2id(pool, r, 1) : 0;
403 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
406 FOR_PROVIDES(p, pp, rid)
408 Solvable *s = pool_id2solvable(pool, p);
409 if (pool_match_nevr(pool, s, rid))
411 type = SOLVER_SOLVABLE_NAME;
414 type = SOLVER_SOLVABLE_PROVIDES;
423 queue_push(out, type);
429 yesno(const char *str)
431 char inbuf[128], *ip;
438 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
443 while (*ip == ' ' || *ip == '\t')
450 if (*ip == 'y' || *ip == 'n')
451 return *ip == 'y' ? 1 : 0;
463 fc_cb(Pool *pool, Id p, void *cbdata)
465 struct fcstate *fcstate = cbdata;
473 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
476 s = pool_id2solvable(pool, p);
477 if (pool->installed && s->repo == pool->installed)
479 if (!s->repo->rpmdbid)
481 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
484 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
486 for (i = 0; i < fcstate->newpkgscnt; i++)
487 if (fcstate->checkq->elements[i] == p)
489 if (i == fcstate->newpkgscnt)
491 fp = fcstate->newpkgsfps[i];
495 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
499 runrpm(const char *arg, const char *name, int dupfd3)
504 if ((pid = fork()) == (pid_t)-1)
511 if (dupfd3 != -1 && dupfd3 != 3)
517 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
518 if (strcmp(arg, "-e") == 0)
519 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
521 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
525 while (waitpid(pid, &status, 0) != pid)
529 printf("rpm failed\n");
535 nscallback(Pool *pool, void *data, Id name, Id evr)
537 if (name == NAMESPACE_PRODUCTBUDDY)
539 /* SUSE specific hack: each product has an associated rpm */
540 Solvable *s = pool->solvables + evr;
543 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
546 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
549 FOR_PROVIDES(p, pp, cap)
551 Solvable *ps = pool->solvables + p;
552 if (ps->repo == s->repo && ps->arch == s->arch)
562 main(int argc, char **argv)
566 struct repoinfo *repoinfos;
568 int i, mode, newpkgs;
572 char inbuf[128], *ip;
575 struct fcstate fcstate;
577 if (!strcmp(argv[1], "install") || !strcmp(argv[1], "in"))
578 mode = SOLVER_INSTALL;
579 else if (!strcmp(argv[1], "erase") || !strcmp(argv[1], "rm"))
581 else if (!strcmp(argv[1], "show"))
583 else if (!strcmp(argv[1], "update") || !strcmp(argv[1], "up"))
584 mode = SOLVER_UPDATE;
587 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
591 pool = pool_create();
592 pool->nscallback = nscallback;
593 // pool_setdebuglevel(pool, 2);
595 repoinfos = read_repoinfos(pool, ZYPP_REPOINFO_PATH, &nrepoinfos);
596 read_repos(pool, repoinfos, nrepoinfos);
597 // FOR_REPOS(i, repo)
598 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
599 pool_addfileprovides(pool);
600 pool_createwhatprovides(pool);
603 for (i = 2; i < argc; i++)
604 mkselect(pool, argv[i], 0, &job);
605 if (!job.count && mode == SOLVER_UPDATE)
609 printf("nothing matched\n");
615 /* show mode, no solver needed */
616 for (i = 0; i < job.count; i += 2)
618 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
620 Solvable *s = pool_id2solvable(pool, p);
621 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
628 for (i = 0; i < job.count; i += 2)
629 job.elements[i] |= mode;
632 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
633 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
634 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
639 Id problem, solution;
642 solv = solver_create(pool);
643 solv->ignorealreadyrecommended = 1;
644 solv->updatesystem = updateall;
645 solver_solve(solv, &job);
646 if (!solv->problems.count)
648 pcnt = solver_problem_count(solv);
649 printf("Found %d problems:\n", pcnt);
650 for (problem = 1; problem <= pcnt; problem++)
653 printf("Problem %d:\n", problem);
654 solver_printprobleminfo(solv, problem);
656 scnt = solver_solution_count(solv, problem);
657 for (solution = 1; solution <= pcnt; solution++)
659 printf("Solution %d:\n", solution);
660 solver_printsolution(solv, problem, solution);
665 printf("Please choose a solution: ");
668 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
673 while (*ip == ' ' || *ip == '\t')
675 if (*ip >= '0' && *ip <= '9')
678 if (take >= 1 && take <= solution)
694 solver_take_solution(solv, problem, take, &job);
699 if (!solv->trans.steps.count)
701 printf("Nothing to do.\n");
705 printf("Transaction summary:\n\n");
706 solver_printtransaction(solv);
707 if (!yesno("OK to continue (y/n)? "))
713 trans = &solv->trans;
715 newpkgs = transaction_installedresult(trans, &checkq);
720 printf("Downloading %d packages\n", newpkgs);
721 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
722 for (i = 0; i < newpkgs; i++)
724 unsigned int medianr;
727 struct repoinfo *cinfo;
729 p = checkq.elements[i];
730 s = pool_id2solvable(pool, p);
731 cinfo = s->repo->appdata;
734 printf("%s: no repository information\n", s->repo->name);
737 loc = solvable_get_location(s, &medianr);
740 if (cinfo->type == TYPE_SUSETAGS)
742 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
743 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
745 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
747 printf("%s: %s not found in repository\n", s->repo->name, loc);
757 printf("Searching for file conflicts\n");
758 queue_init(&conflicts);
759 fcstate.rpmdbstate = 0;
760 fcstate.newpkgscnt = newpkgs;
761 fcstate.checkq = &checkq;
762 fcstate.newpkgsfps = newpkgsfps;
763 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
767 for (i = 0; i < conflicts.count; i += 5)
768 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]));
770 if (yesno("Re-run solver (y/n/q)? "))
772 for (i = 0; i < newpkgs; i++)
774 fclose(newpkgsfps[i]);
775 newpkgsfps = sat_free(newpkgsfps);
777 pool_add_fileconflicts_deps(pool, &conflicts);
778 pool_createwhatprovides(pool); /* Hmm... */
782 queue_free(&conflicts);
785 printf("Committing transaction:\n\n");
786 transaction_order(trans, 0);
787 for (i = 0; i < trans->steps.count; i++)
790 const char *evr, *evrp;
795 p = trans->steps.elements[i];
796 s = pool_id2solvable(pool, p);
797 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
800 case SOLVER_TRANSACTION_ERASE:
801 printf("erase %s\n", solvid2str(pool, p));
802 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
804 if (strlen(solvid2str(pool, p)) > 255)
806 evr = evrp = id2str(pool, s->evr);
807 while (*evrp >= '0' && *evrp <= '9')
809 if (evrp > evr && evrp[0] == ':' && evrp[1])
811 sprintf(rpmname, "%s-%s.%s", id2str(pool, s->name), evr, id2str(pool, s->arch));
812 runrpm("-e", rpmname, -1);
814 case SOLVER_TRANSACTION_INSTALL:
815 case SOLVER_TRANSACTION_MULTIINSTALL:
816 printf("install %s\n", solvid2str(pool, p));
817 for (j = 0; j < newpkgs; j++)
818 if (checkq.elements[j] == p)
820 fp = j < newpkgs ? newpkgsfps[j] : 0;
824 lseek(fileno(fp), 0, SEEK_SET);
825 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
834 for (i = 0; i < newpkgs; i++)
836 fclose(newpkgsfps[i]);
837 sat_free(newpkgsfps);