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_add_repomdxml(repo, fp, 0);
312 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, "primary", SEARCH_STRING);
313 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
314 if (dataiterator_step(&di))
316 dataiterator_setpos_parent(&di);
317 primaryfile = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
319 dataiterator_free(&di);
321 primaryfile = "repodata/primary.xml.gz";
322 if ((fp = curlfopen(cinfo->baseurl, primaryfile, 1)) == 0)
324 repo_add_rpmmd(repo, fp, 0, 0);
328 printf("reading susetags repo '%s'\n", cinfo->alias);
329 repo = repo_create(pool, cinfo->alias);
331 repo->appdata = cinfo;
334 if ((fp = curlfopen(cinfo->baseurl, "content", 0)) != 0)
336 repo_add_content(repo, fp, 0);
338 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
339 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
342 descrdir = "suse/setup/descr";
343 if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages.gz", 0), 1)) == 0)
344 if ((fp = curlfopen(cinfo->baseurl, pool_tmpjoin(pool, descrdir, "/packages", 0), 0)) == 0)
346 repo_add_susetags(repo, fp, defvendor, 0, 0);
350 printf("skipping unknown repo '%s'\n", cinfo->alias);
357 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
363 id = str2id(pool, arg, 0);
366 FOR_PROVIDES(p, pp, id)
368 Solvable *s = pool_id2solvable(pool, p);
371 type = SOLVER_SOLVABLE_NAME;
374 type = SOLVER_SOLVABLE_PROVIDES;
379 /* did not find a solvable, see if it's a relation */
380 if ((r = strpbrk(arg, "<=>")) != 0)
384 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
386 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
398 while (*r == ' ' || *r == '\t')
400 revr = *r ? str2id(pool, r, 1) : 0;
401 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
404 FOR_PROVIDES(p, pp, rid)
406 Solvable *s = pool_id2solvable(pool, p);
407 if (pool_match_nevr(pool, s, rid))
409 type = SOLVER_SOLVABLE_NAME;
412 type = SOLVER_SOLVABLE_PROVIDES;
421 queue_push(out, type);
427 yesno(const char *str)
429 char inbuf[128], *ip;
436 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
441 while (*ip == ' ' || *ip == '\t')
448 if (*ip == 'y' || *ip == 'n')
449 return *ip == 'y' ? 1 : 0;
461 fc_cb(Pool *pool, Id p, void *cbdata)
463 struct fcstate *fcstate = cbdata;
471 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
474 s = pool_id2solvable(pool, p);
475 if (pool->installed && s->repo == pool->installed)
477 if (!s->repo->rpmdbid)
479 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
482 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
484 for (i = 0; i < fcstate->newpkgscnt; i++)
485 if (fcstate->checkq->elements[i] == p)
487 if (i == fcstate->newpkgscnt)
489 fp = fcstate->newpkgsfps[i];
491 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
495 runrpm(const char *arg, const char *name, int dupfd3)
500 if ((pid = fork()) == (pid_t)-1)
507 if (dupfd3 != -1 && dupfd3 != 3)
513 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
514 if (strcmp(arg, "-e") == 0)
515 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
517 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
521 while (waitpid(pid, &status, 0) != pid)
525 printf("rpm failed\n");
531 main(int argc, char **argv)
535 struct repoinfo *repoinfos;
537 int i, mode, newpkgs;
541 char inbuf[128], *ip;
544 struct fcstate fcstate;
546 if (!strcmp(argv[1], "install") || !strcmp(argv[1], "in"))
547 mode = SOLVER_INSTALL;
548 else if (!strcmp(argv[1], "erase") || !strcmp(argv[1], "rm"))
550 else if (!strcmp(argv[1], "show"))
552 else if (!strcmp(argv[1], "update") || !strcmp(argv[1], "up"))
553 mode = SOLVER_UPDATE;
556 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
560 pool = pool_create();
561 // pool_setdebuglevel(pool, 2);
563 repoinfos = read_repoinfos(pool, ZYPP_REPOINFO_PATH, &nrepoinfos);
564 read_repos(pool, repoinfos, nrepoinfos);
565 // FOR_REPOS(i, repo)
566 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
567 pool_addfileprovides(pool);
568 pool_createwhatprovides(pool);
571 for (i = 2; i < argc; i++)
572 mkselect(pool, argv[i], 0, &job);
573 if (!job.count && mode == SOLVER_UPDATE)
577 printf("nothing matched\n");
583 /* show mode, no solver needed */
584 for (i = 0; i < job.count; i += 2)
586 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
588 Solvable *s = pool_id2solvable(pool, p);
589 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
596 for (i = 0; i < job.count; i += 2)
597 job.elements[i] |= mode;
600 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
601 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
602 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
607 Id problem, solution;
610 solv = solver_create(pool);
611 solv->ignorealreadyrecommended = 1;
612 solv->updatesystem = updateall;
613 solver_solve(solv, &job);
614 if (!solv->problems.count)
616 pcnt = solver_problem_count(solv);
617 printf("Found %d problems:\n", pcnt);
618 for (problem = 1; problem <= pcnt; problem++)
621 printf("Problem %d:\n", problem);
622 solver_printprobleminfo(solv, problem);
624 scnt = solver_solution_count(solv, problem);
625 for (solution = 1; solution <= pcnt; solution++)
627 printf("Solution %d:\n", solution);
628 solver_printsolution(solv, problem, solution);
633 printf("Please choose a solution: ");
636 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
641 while (*ip == ' ' || *ip == '\t')
643 if (*ip >= '0' && *ip <= '9')
646 if (take >= 1 && take <= solution)
662 solver_take_solution(solv, problem, take, &job);
667 if (!solv->trans.steps.count)
669 printf("Nothing to do.\n");
673 printf("Transaction summary:\n\n");
674 solver_printtransaction(solv);
675 if (!yesno("OK to continue (y/n)? "))
681 trans = &solv->trans;
683 newpkgs = transaction_installedresult(trans, &checkq);
688 printf("Downloading %d packages\n", newpkgs);
689 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
690 for (i = 0; i < newpkgs; i++)
692 unsigned int medianr;
695 struct repoinfo *cinfo;
697 p = checkq.elements[i];
698 s = pool_id2solvable(pool, p);
699 cinfo = s->repo->appdata;
702 printf("%s: no repository information\n", s->repo->name);
705 loc = solvable_get_location(s, &medianr);
706 if (cinfo->type == TYPE_SUSETAGS)
708 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
709 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
711 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
713 printf("%s: %s not found\n", s->repo->name, loc);
723 printf("Searching for file conflicts\n");
724 queue_init(&conflicts);
725 fcstate.rpmdbstate = 0;
726 fcstate.newpkgscnt = newpkgs;
727 fcstate.checkq = &checkq;
728 fcstate.newpkgsfps = newpkgsfps;
729 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
733 for (i = 0; i < conflicts.count; i += 5)
734 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]));
736 if (yesno("Re-run solver (y/n/q)? "))
738 for (i = 0; i < newpkgs; i++)
739 fclose(newpkgsfps[i]);
740 newpkgsfps = sat_free(newpkgsfps);
742 pool_add_fileconflicts_deps(pool, &conflicts);
743 pool_createwhatprovides(pool); /* Hmm... */
747 queue_free(&conflicts);
750 printf("Committing transaction:\n\n");
751 transaction_order(trans, 0);
752 for (i = 0; i < trans->steps.count; i++)
755 const char *evr, *evrp;
760 p = trans->steps.elements[i];
761 s = pool_id2solvable(pool, p);
762 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
765 case SOLVER_TRANSACTION_ERASE:
766 printf("erase %s\n", solvid2str(pool, p));
767 if (strlen(solvid2str(pool, p)) > 255)
769 evr = evrp = id2str(pool, s->evr);
770 while (*evrp >= '0' && *evrp <= '9')
772 if (evrp > evr && evrp[0] == ':' && evrp[1])
774 sprintf(rpmname, "%s-%s.%s", id2str(pool, s->name), evr, id2str(pool, s->arch));
775 runrpm("-e", rpmname, -1);
777 case SOLVER_TRANSACTION_INSTALL:
778 case SOLVER_TRANSACTION_MULTIINSTALL:
779 printf("install %s\n", solvid2str(pool, p));
780 for (j = 0; j < newpkgs; j++)
781 if (checkq.elements[j] == p)
783 fp = j < newpkgs ? newpkgsfps[j] : 0;
785 lseek(fileno(fp), 0, SEEK_SET);
786 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
795 for (i = 0; i < newpkgs; i++)
797 fclose(newpkgsfps[i]);
798 sat_free(newpkgsfps);