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 * - signature and checksum verification
12 * - vendor policy loading
23 #include <sys/utsname.h>
24 #include <sys/types.h>
32 #include "solverdebug.h"
34 #include "repo_solv.h"
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"
47 # define REPOINFO_PATH "/etc/yum.repos.d"
49 # define REPOINFO_PATH "/etc/zypp/repos.d"
50 # define PRODUCTS_PATH "/etc/products.d"
53 #define SOLVCACHE_PATH "/var/cache/solv"
71 #define TYPE_UNKNOWN 0
72 #define TYPE_SUSETAGS 1
74 #define TYPE_PLAINDIR 3
77 read_repoinfos_sort(const void *ap, const void *bp)
79 const struct repoinfo *a = ap;
80 const struct repoinfo *b = bp;
81 return strcmp(a->alias, b->alias);
85 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
88 char buf2[4096], *kp, *vp, *kpe;
93 struct repoinfo *repoinfos = 0, *cinfo;
96 rdlen = strlen(reposdir);
97 dir = opendir(reposdir);
103 while ((ent = readdir(dir)) != 0)
105 l = strlen(ent->d_name);
106 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
108 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
109 if ((fp = fopen(buf, "r")) == 0)
115 while(fgets(buf2, sizeof(buf2), fp))
120 while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
123 while (*kp == ' ' || *kp == '\t')
125 if (!*kp || *kp == '#')
131 vp = strrchr(kp, ']');
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);
140 cinfo->type = TYPE_RPMMD;
143 vp = strchr(kp, '=');
146 for (kpe = vp - 1; kpe >= kp; kpe--)
147 if (*kpe != ' ' && *kpe != '\t')
152 while (*vp == ' ' || *vp == '\t')
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"))
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;
176 cinfo->type = TYPE_UNKNOWN;
178 else if (!strcmp(kp, "priority"))
179 cinfo->priority = atoi(vp);
180 else if (!strcmp(kp, "keeppackages"))
181 cinfo->keeppackages = *vp == '0' ? 0 : 1;
187 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
188 *nrepoinfosp = nrepoinfos;
193 cookie_gzread(void *cookie, char *buf, size_t nbytes)
195 return gzread((gzFile *)cookie, buf, nbytes);
199 cookie_gzclose(void *cookie)
201 return gzclose((gzFile *)cookie);
205 myfopen(const char *fn)
207 cookie_io_functions_t cio;
213 suf = strrchr(fn, '.');
214 if (!suf || strcmp(suf, ".gz") != 0)
215 return fopen(fn, "r");
216 gzf = gzopen(fn, "r");
219 memset(&cio, 0, sizeof(cio));
220 cio.read = cookie_gzread;
221 cio.close = cookie_gzclose;
222 return fopencookie(gzf, "r", cio);
226 curlfopen(const char *baseurl, const char *file, int uncompress)
235 if (l && baseurl[l - 1] == '/')
236 snprintf(url, sizeof(url), "%s%s", baseurl, file);
238 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
239 strcpy(tmpl, "/var/tmp/solvXXXXXX");
247 if ((pid = fork()) == (pid_t)-1)
259 execlp("curl", "curl", "-s", "-L", url, (char *)0);
263 while (waitpid(pid, &status, 0) != pid)
265 if (lseek(fd, 0, SEEK_END) == 0)
271 lseek(fd, 0, SEEK_SET);
274 cookie_io_functions_t cio;
277 sprintf(tmpl, "/dev/fd/%d", fd);
278 gzf = gzopen(tmpl, "r");
282 memset(&cio, 0, sizeof(cio));
283 cio.read = cookie_gzread;
284 cio.close = cookie_gzclose;
285 return fopencookie(gzf, "r", cio);
287 fcntl(fd, F_SETFD, FD_CLOEXEC);
288 return fdopen(fd, "r");
292 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
295 void *h = sat_chksum_create(chktype);
298 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
299 sat_chksum_add(h, buf, l);
301 sat_chksum_free(h, out);
305 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
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);
324 pool_setarch(pool, un.machine);
327 char *calccachepath(Repo *repo)
329 char *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
331 q = p + strlen(SOLVCACHE_PATH) + 1;
341 usecachedrepo(Repo *repo, unsigned char *cookie)
344 unsigned char mycookie[32];
346 if (!(fp = fopen(calccachepath(repo), "r")))
348 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
353 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
359 if (repo_add_solv(repo, fp))
369 writecachedrepo(Repo *repo, unsigned char *cookie)
371 Id *addedfileprovides = 0;
377 mkdir(SOLVCACHE_PATH, 0755);
378 tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
386 if (!(fp = fdopen(fd, "w")))
393 info = repo_add_repodata(repo, 0);
394 pool_addfileprovides_ids(repo->pool, 0, &addedfileprovides);
395 if (addedfileprovides && *addedfileprovides)
397 for (i = 0; addedfileprovides[i]; i++)
398 repodata_add_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[i]);
400 sat_free(addedfileprovides);
401 repodata_internalize(info);
402 repo_write(repo, fp, 0, 0, 0);
404 fwrite(cookie, 32, 1, fp);
411 if (!rename(tmpl, calccachepath(repo)))
417 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
420 struct repoinfo *cinfo;
424 const char *primaryfile;
425 const char *descrdir;
429 unsigned char cookie[32];
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))
441 printf(" reading\n");
445 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
447 if ((ofp = fopen(calccachepath(repo), "r")) != 0)
449 Repo *ref = repo_create(pool, "@System.old");
450 if (!repo_add_solv(ref, ofp))
452 repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
459 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
460 writecachedrepo(repo, cookie);
462 pool_set_installed(pool, repo);
464 for (i = 0; i < nrepoinfos; i++)
466 cinfo = repoinfos + i;
470 repo = repo_create(pool, cinfo->alias);
472 repo->appdata = cinfo;
473 repo->priority = 99 - cinfo->priority;
478 printf("rpmmd repo '%s':", cinfo->alias);
480 if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
482 printf(" no data, skipped\n");
487 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
488 if (usecachedrepo(repo, cookie))
494 printf(" reading\n");
495 repo_add_repomdxml(repo, fp, 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))
502 dataiterator_setpos_parent(&di);
503 primaryfile = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
505 dataiterator_free(&di);
507 primaryfile = "repodata/primary.xml.gz";
508 if ((fp = curlfopen(cinfo->baseurl, primaryfile, 1)) == 0)
510 repo_add_rpmmd(repo, fp, 0, 0);
512 writecachedrepo(repo, cookie);
515 printf("susetags repo '%s':", cinfo->alias);
517 repo = repo_create(pool, cinfo->alias);
519 repo->appdata = cinfo;
520 repo->priority = 99 - cinfo->priority;
523 if ((fp = curlfopen(cinfo->baseurl, "content", 0)) != 0)
525 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
526 if (usecachedrepo(repo, cookie))
532 havecontent = 1; /* ok to write cache */
533 repo_add_content(repo, fp, 0);
535 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
536 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_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)
544 repo_add_susetags(repo, fp, defvendor, 0, 0);
547 writecachedrepo(repo, cookie);
550 printf("unsupported repo '%s': skipped\n", cinfo->alias);
559 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
565 id = str2id(pool, arg, 0);
568 FOR_PROVIDES(p, pp, id)
570 Solvable *s = pool_id2solvable(pool, p);
573 type = SOLVER_SOLVABLE_NAME;
576 type = SOLVER_SOLVABLE_PROVIDES;
581 /* did not find a solvable, see if it's a relation */
582 if ((r = strpbrk(arg, "<=>")) != 0)
586 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
588 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
600 while (*r == ' ' || *r == '\t')
602 revr = *r ? str2id(pool, r, 1) : 0;
603 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
606 FOR_PROVIDES(p, pp, rid)
608 Solvable *s = pool_id2solvable(pool, p);
609 if (pool_match_nevr(pool, s, rid))
611 type = SOLVER_SOLVABLE_NAME;
614 type = SOLVER_SOLVABLE_PROVIDES;
623 queue_push(out, type);
629 yesno(const char *str)
631 char inbuf[128], *ip;
638 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
643 while (*ip == ' ' || *ip == '\t')
650 if (*ip == 'y' || *ip == 'n')
651 return *ip == 'y' ? 1 : 0;
663 fc_cb(Pool *pool, Id p, void *cbdata)
665 struct fcstate *fcstate = cbdata;
673 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
676 s = pool_id2solvable(pool, p);
677 if (pool->installed && s->repo == pool->installed)
679 if (!s->repo->rpmdbid)
681 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
684 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
686 for (i = 0; i < fcstate->newpkgscnt; i++)
687 if (fcstate->checkq->elements[i] == p)
689 if (i == fcstate->newpkgscnt)
691 fp = fcstate->newpkgsfps[i];
695 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
699 runrpm(const char *arg, const char *name, int dupfd3)
704 if ((pid = fork()) == (pid_t)-1)
711 if (dupfd3 != -1 && dupfd3 != 3)
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);
721 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
725 while (waitpid(pid, &status, 0) != pid)
729 printf("rpm failed\n");
735 nscallback(Pool *pool, void *data, Id name, Id evr)
737 if (name == NAMESPACE_PRODUCTBUDDY)
739 /* SUSE specific hack: each product has an associated rpm */
740 Solvable *s = pool->solvables + evr;
743 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
746 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
749 FOR_PROVIDES(p, pp, cap)
751 Solvable *ps = pool->solvables + p;
752 if (ps->repo == s->repo && ps->arch == s->arch)
762 main(int argc, char **argv)
766 struct repoinfo *repoinfos;
768 int i, mode, newpkgs;
772 char inbuf[128], *ip;
775 struct fcstate fcstate;
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"))
783 else if (!strcmp(argv[0], "show"))
785 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
786 mode = SOLVER_UPDATE;
789 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
793 pool = pool_create();
794 pool->nscallback = nscallback;
795 // pool_setdebuglevel(pool, 2);
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);
805 for (i = 1; i < argc; i++)
806 mkselect(pool, argv[i], 0, &job);
807 if (!job.count && mode == SOLVER_UPDATE)
811 printf("no package matched\n");
817 /* show mode, no solver needed */
818 for (i = 0; i < job.count; i += 2)
820 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
822 Solvable *s = pool_id2solvable(pool, p);
823 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
830 for (i = 0; i < job.count; i += 2)
831 job.elements[i] |= mode;
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));
841 Id problem, solution;
844 solv = solver_create(pool);
845 solv->ignorealreadyrecommended = 1;
846 solv->updatesystem = updateall;
847 solver_solve(solv, &job);
848 if (!solv->problems.count)
850 pcnt = solver_problem_count(solv);
851 printf("Found %d problems:\n", pcnt);
852 for (problem = 1; problem <= pcnt; problem++)
855 printf("Problem %d:\n", problem);
856 solver_printprobleminfo(solv, problem);
858 scnt = solver_solution_count(solv, problem);
859 for (solution = 1; solution <= scnt; solution++)
861 printf("Solution %d:\n", solution);
862 solver_printsolution(solv, problem, solution);
867 printf("Please choose a solution: ");
870 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
875 while (*ip == ' ' || *ip == '\t')
877 if (*ip >= '0' && *ip <= '9')
880 if (take >= 1 && take <= scnt)
896 solver_take_solution(solv, problem, take, &job);
901 if (!solv->trans.steps.count)
903 printf("Nothing to do.\n");
907 printf("Transaction summary:\n\n");
908 solver_printtransaction(solv);
909 if (!yesno("OK to continue (y/n)? "))
915 trans = &solv->trans;
917 newpkgs = transaction_installedresult(trans, &checkq);
922 printf("Downloading %d packages\n", newpkgs);
923 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
924 for (i = 0; i < newpkgs; i++)
926 unsigned int medianr;
929 struct repoinfo *cinfo;
931 p = checkq.elements[i];
932 s = pool_id2solvable(pool, p);
933 cinfo = s->repo->appdata;
936 printf("%s: no repository information\n", s->repo->name);
939 loc = solvable_get_location(s, &medianr);
942 if (cinfo->type == TYPE_SUSETAGS)
944 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
945 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
947 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
949 printf("%s: %s not found in repository\n", s->repo->name, loc);
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);
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]));
972 if (yesno("Re-run solver (y/n/q)? "))
974 for (i = 0; i < newpkgs; i++)
976 fclose(newpkgsfps[i]);
977 newpkgsfps = sat_free(newpkgsfps);
979 pool_add_fileconflicts_deps(pool, &conflicts);
980 pool_createwhatprovides(pool); /* Hmm... */
984 queue_free(&conflicts);
987 printf("Committing transaction:\n\n");
988 transaction_order(trans, 0);
989 for (i = 0; i < trans->steps.count; i++)
991 const char *evr, *evrp, *nvra;
996 p = trans->steps.elements[i];
997 s = pool_id2solvable(pool, p);
998 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
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])
1005 /* strip epoch from evr */
1006 evr = evrp = id2str(pool, s->evr);
1007 while (*evrp >= '0' && *evrp <= '9')
1009 if (evrp > evr && evrp[0] == ':' && 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 */
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)
1021 fp = j < newpkgs ? newpkgsfps[j] : 0;
1025 lseek(fileno(fp), 0, SEEK_SET);
1026 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
1035 for (i = 0; i < newpkgs; i++)
1037 fclose(newpkgsfps[i]);
1038 sat_free(newpkgsfps);
1039 queue_free(&checkq);