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"))
167 if (vp && strcmp(vp, "/") != 0)
168 cinfo->path = strdup(vp);
170 else if (!strcmp(kp, "type"))
172 if (!strcmp(vp, "yast2"))
173 cinfo->type = TYPE_SUSETAGS;
174 else if (!strcmp(vp, "rpm-md"))
175 cinfo->type = TYPE_RPMMD;
176 else if (!strcmp(vp, "plaindir"))
177 cinfo->type = TYPE_PLAINDIR;
179 cinfo->type = TYPE_UNKNOWN;
181 else if (!strcmp(kp, "priority"))
182 cinfo->priority = atoi(vp);
183 else if (!strcmp(kp, "keeppackages"))
184 cinfo->keeppackages = *vp == '0' ? 0 : 1;
190 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
191 *nrepoinfosp = nrepoinfos;
196 cookie_gzread(void *cookie, char *buf, size_t nbytes)
198 return gzread((gzFile *)cookie, buf, nbytes);
202 cookie_gzclose(void *cookie)
204 return gzclose((gzFile *)cookie);
208 myfopen(const char *fn)
210 cookie_io_functions_t cio;
216 suf = strrchr(fn, '.');
217 if (!suf || strcmp(suf, ".gz") != 0)
218 return fopen(fn, "r");
219 gzf = gzopen(fn, "r");
222 memset(&cio, 0, sizeof(cio));
223 cio.read = cookie_gzread;
224 cio.close = cookie_gzclose;
225 return fopencookie(gzf, "r", cio);
229 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress)
236 const char *baseurl = cinfo->baseurl;
239 if (l && baseurl[l - 1] == '/')
240 snprintf(url, sizeof(url), "%s%s", baseurl, file);
242 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
243 strcpy(tmpl, "/var/tmp/solvXXXXXX");
251 if ((pid = fork()) == (pid_t)-1)
263 execlp("curl", "curl", "-s", "-L", url, (char *)0);
267 while (waitpid(pid, &status, 0) != pid)
269 if (lseek(fd, 0, SEEK_END) == 0)
275 lseek(fd, 0, SEEK_SET);
278 cookie_io_functions_t cio;
281 sprintf(tmpl, "/dev/fd/%d", fd);
282 gzf = gzopen(tmpl, "r");
286 memset(&cio, 0, sizeof(cio));
287 cio.read = cookie_gzread;
288 cio.close = cookie_gzclose;
289 return fopencookie(gzf, "r", cio);
291 fcntl(fd, F_SETFD, FD_CLOEXEC);
292 return fdopen(fd, "r");
296 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
299 void *h = sat_chksum_create(chktype);
302 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
303 sat_chksum_add(h, buf, l);
305 sat_chksum_free(h, out);
309 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
311 void *h = sat_chksum_create(chktype);
312 sat_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
313 sat_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
314 sat_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
315 sat_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
316 sat_chksum_free(h, out);
328 pool_setarch(pool, un.machine);
331 char *calccachepath(Repo *repo)
333 char *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
335 q = p + strlen(SOLVCACHE_PATH) + 1;
345 usecachedrepo(Repo *repo, unsigned char *cookie)
348 unsigned char mycookie[32];
350 if (!(fp = fopen(calccachepath(repo), "r")))
352 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
357 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
363 if (repo_add_solv(repo, fp))
373 writecachedrepo(Repo *repo, unsigned char *cookie)
375 Id *addedfileprovides = 0;
381 mkdir(SOLVCACHE_PATH, 0755);
382 tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
390 if (!(fp = fdopen(fd, "w")))
397 info = repo_add_repodata(repo, 0);
398 pool_addfileprovides_ids(repo->pool, 0, &addedfileprovides);
399 if (addedfileprovides && *addedfileprovides)
401 for (i = 0; addedfileprovides[i]; i++)
402 repodata_add_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[i]);
404 sat_free(addedfileprovides);
405 repodata_internalize(info);
406 repo_write(repo, fp, 0, 0, 0);
408 if (fwrite(cookie, 32, 1, fp) != 1)
421 if (!rename(tmpl, calccachepath(repo)))
427 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
430 struct repoinfo *cinfo;
434 const char *primaryfile;
435 const char *descrdir;
439 unsigned char cookie[32];
441 repo = repo_create(pool, "@System");
442 printf("rpm database:");
443 if (stat("/var/lib/rpm/Packages", &stb))
444 memset(&stb, 0, sizeof(&stb));
445 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cookie);
446 if (usecachedrepo(repo, cookie))
451 printf(" reading\n");
455 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
457 if ((ofp = fopen(calccachepath(repo), "r")) != 0)
459 Repo *ref = repo_create(pool, "@System.old");
460 if (!repo_add_solv(ref, ofp))
462 repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
469 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
470 writecachedrepo(repo, cookie);
472 pool_set_installed(pool, repo);
474 for (i = 0; i < nrepoinfos; i++)
476 cinfo = repoinfos + i;
480 repo = repo_create(pool, cinfo->alias);
482 repo->appdata = cinfo;
483 repo->priority = 99 - cinfo->priority;
485 if (!cinfo->autorefresh && usecachedrepo(repo, 0))
487 printf("repo '%s':", cinfo->alias);
494 printf("rpmmd repo '%s':", cinfo->alias);
496 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0)) == 0)
498 printf(" no data, skipped\n");
503 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
504 if (usecachedrepo(repo, cookie))
510 printf(" reading\n");
511 repo_add_repomdxml(repo, fp, 0);
514 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, "primary", SEARCH_STRING);
515 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
516 if (dataiterator_step(&di))
518 dataiterator_setpos_parent(&di);
519 primaryfile = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
521 dataiterator_free(&di);
523 primaryfile = "repodata/primary.xml.gz";
524 if ((fp = curlfopen(cinfo, primaryfile, 1)) == 0)
526 repo_add_rpmmd(repo, fp, 0, 0);
528 writecachedrepo(repo, cookie);
531 printf("susetags repo '%s':", cinfo->alias);
533 repo = repo_create(pool, cinfo->alias);
535 repo->appdata = cinfo;
536 repo->priority = 99 - cinfo->priority;
539 if ((fp = curlfopen(cinfo, "content", 0)) != 0)
541 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
542 if (usecachedrepo(repo, cookie))
548 havecontent = 1; /* ok to write cache */
549 repo_add_content(repo, fp, 0);
551 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
552 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
555 descrdir = "suse/setup/descr";
556 printf(" reading\n");
557 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/packages.gz", 0), 1)) == 0)
558 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/packages", 0), 0)) == 0)
560 repo_add_susetags(repo, fp, defvendor, 0, 0);
563 writecachedrepo(repo, cookie);
566 printf("unsupported repo '%s': skipped\n", cinfo->alias);
575 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
581 id = str2id(pool, arg, 0);
584 FOR_PROVIDES(p, pp, id)
586 Solvable *s = pool_id2solvable(pool, p);
589 type = SOLVER_SOLVABLE_NAME;
592 type = SOLVER_SOLVABLE_PROVIDES;
597 /* did not find a solvable, see if it's a relation */
598 if ((r = strpbrk(arg, "<=>")) != 0)
602 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
604 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
616 while (*r == ' ' || *r == '\t')
618 revr = *r ? str2id(pool, r, 1) : 0;
619 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
622 FOR_PROVIDES(p, pp, rid)
624 Solvable *s = pool_id2solvable(pool, p);
625 if (pool_match_nevr(pool, s, rid))
627 type = SOLVER_SOLVABLE_NAME;
630 type = SOLVER_SOLVABLE_PROVIDES;
639 queue_push(out, type);
645 yesno(const char *str)
647 char inbuf[128], *ip;
654 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
659 while (*ip == ' ' || *ip == '\t')
666 if (*ip == 'y' || *ip == 'n')
667 return *ip == 'y' ? 1 : 0;
679 fc_cb(Pool *pool, Id p, void *cbdata)
681 struct fcstate *fcstate = cbdata;
689 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
692 s = pool_id2solvable(pool, p);
693 if (pool->installed && s->repo == pool->installed)
695 if (!s->repo->rpmdbid)
697 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
700 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
702 for (i = 0; i < fcstate->newpkgscnt; i++)
703 if (fcstate->checkq->elements[i] == p)
705 if (i == fcstate->newpkgscnt)
707 fp = fcstate->newpkgsfps[i];
711 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
715 runrpm(const char *arg, const char *name, int dupfd3)
720 if ((pid = fork()) == (pid_t)-1)
727 if (dupfd3 != -1 && dupfd3 != 3)
733 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
734 if (strcmp(arg, "-e") == 0)
735 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
737 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
741 while (waitpid(pid, &status, 0) != pid)
745 printf("rpm failed\n");
751 nscallback(Pool *pool, void *data, Id name, Id evr)
753 if (name == NAMESPACE_PRODUCTBUDDY)
755 /* SUSE specific hack: each product has an associated rpm */
756 Solvable *s = pool->solvables + evr;
759 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
762 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
765 FOR_PROVIDES(p, pp, cap)
767 Solvable *ps = pool->solvables + p;
768 if (ps->repo == s->repo && ps->arch == s->arch)
778 main(int argc, char **argv)
782 struct repoinfo *repoinfos;
784 int i, mode, newpkgs;
788 char inbuf[128], *ip;
791 struct fcstate fcstate;
795 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
796 mode = SOLVER_INSTALL;
797 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
799 else if (!strcmp(argv[0], "show"))
801 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
802 mode = SOLVER_UPDATE;
805 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
809 pool = pool_create();
810 pool->nscallback = nscallback;
811 // pool_setdebuglevel(pool, 2);
813 repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
814 read_repos(pool, repoinfos, nrepoinfos);
815 // FOR_REPOS(i, repo)
816 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
817 pool_addfileprovides(pool);
818 pool_createwhatprovides(pool);
821 for (i = 1; i < argc; i++)
822 mkselect(pool, argv[i], 0, &job);
823 if (!job.count && mode == SOLVER_UPDATE)
827 printf("no package matched\n");
833 /* show mode, no solver needed */
834 for (i = 0; i < job.count; i += 2)
836 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
838 Solvable *s = pool_id2solvable(pool, p);
839 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
846 for (i = 0; i < job.count; i += 2)
847 job.elements[i] |= mode;
850 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
851 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
852 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
857 Id problem, solution;
860 solv = solver_create(pool);
861 solv->ignorealreadyrecommended = 1;
862 solv->updatesystem = updateall;
863 solver_solve(solv, &job);
864 if (!solv->problems.count)
866 pcnt = solver_problem_count(solv);
867 printf("Found %d problems:\n", pcnt);
868 for (problem = 1; problem <= pcnt; problem++)
871 printf("Problem %d:\n", problem);
872 solver_printprobleminfo(solv, problem);
874 scnt = solver_solution_count(solv, problem);
875 for (solution = 1; solution <= scnt; solution++)
877 printf("Solution %d:\n", solution);
878 solver_printsolution(solv, problem, solution);
883 printf("Please choose a solution: ");
886 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
891 while (*ip == ' ' || *ip == '\t')
893 if (*ip >= '0' && *ip <= '9')
896 if (take >= 1 && take <= scnt)
912 solver_take_solution(solv, problem, take, &job);
917 if (!solv->trans.steps.count)
919 printf("Nothing to do.\n");
923 printf("Transaction summary:\n\n");
924 solver_printtransaction(solv);
925 if (!yesno("OK to continue (y/n)? "))
931 trans = &solv->trans;
933 newpkgs = transaction_installedresult(trans, &checkq);
938 printf("Downloading %d packages\n", newpkgs);
939 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
940 for (i = 0; i < newpkgs; i++)
942 unsigned int medianr;
945 struct repoinfo *cinfo;
947 p = checkq.elements[i];
948 s = pool_id2solvable(pool, p);
949 cinfo = s->repo->appdata;
952 printf("%s: no repository information\n", s->repo->name);
955 loc = solvable_get_location(s, &medianr);
958 if (cinfo->type == TYPE_SUSETAGS)
960 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
961 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
963 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0)) == 0)
965 printf("%s: %s not found in repository\n", s->repo->name, loc);
975 printf("Searching for file conflicts\n");
976 queue_init(&conflicts);
977 fcstate.rpmdbstate = 0;
978 fcstate.newpkgscnt = newpkgs;
979 fcstate.checkq = &checkq;
980 fcstate.newpkgsfps = newpkgsfps;
981 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
985 for (i = 0; i < conflicts.count; i += 5)
986 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]));
988 if (yesno("Re-run solver (y/n/q)? "))
990 for (i = 0; i < newpkgs; i++)
992 fclose(newpkgsfps[i]);
993 newpkgsfps = sat_free(newpkgsfps);
995 pool_add_fileconflicts_deps(pool, &conflicts);
996 pool_createwhatprovides(pool); /* Hmm... */
1000 queue_free(&conflicts);
1003 printf("Committing transaction:\n\n");
1004 transaction_order(trans, 0);
1005 for (i = 0; i < trans->steps.count; i++)
1007 const char *evr, *evrp, *nvra;
1012 p = trans->steps.elements[i];
1013 s = pool_id2solvable(pool, p);
1014 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
1017 case SOLVER_TRANSACTION_ERASE:
1018 printf("erase %s\n", solvid2str(pool, p));
1019 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
1021 /* strip epoch from evr */
1022 evr = evrp = id2str(pool, s->evr);
1023 while (*evrp >= '0' && *evrp <= '9')
1025 if (evrp > evr && evrp[0] == ':' && evrp[1])
1027 nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
1028 nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
1029 runrpm("-e", nvra, -1); /* to bad that --querybynumber doesn't work */
1031 case SOLVER_TRANSACTION_INSTALL:
1032 case SOLVER_TRANSACTION_MULTIINSTALL:
1033 printf("install %s\n", solvid2str(pool, p));
1034 for (j = 0; j < newpkgs; j++)
1035 if (checkq.elements[j] == p)
1037 fp = j < newpkgs ? newpkgsfps[j] : 0;
1041 lseek(fileno(fp), 0, SEEK_SET);
1042 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
1051 for (i = 0; i < newpkgs; i++)
1053 fclose(newpkgsfps[i]);
1054 sat_free(newpkgsfps);
1055 queue_free(&checkq);