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_rpmmd.h"
36 #include "repo_susetags.h"
37 #include "repo_repomdxml.h"
38 #include "pool_fileconflicts.h"
55 #define TYPE_UNKNOWN 0
56 #define TYPE_SUSETAGS 1
60 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
63 char buf2[4096], *kp, *vp, *kpe;
68 struct repoinfo *repoinfos = 0, *cinfo;
71 rdlen = strlen(reposdir);
72 dir = opendir(reposdir);
78 while ((ent = readdir(dir)) != 0)
80 l = strlen(ent->d_name);
81 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
83 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
84 if ((fp = fopen(buf, "r")) == 0)
90 while(fgets(buf2, sizeof(buf2), fp))
95 while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
98 while (*kp == ' ' || *kp == '\t')
100 if (!*kp || *kp == '#')
106 vp = strrchr(kp, ']');
110 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
111 cinfo = repoinfos + nrepoinfos++;
112 memset(cinfo, 0, sizeof(*cinfo));
113 cinfo->alias = strdup(kp + 1);
116 vp = strchr(kp, '=');
119 for (kpe = vp - 1; kpe >= kp; kpe--)
120 if (*kpe != ' ' && *kpe != '\t')
125 while (*vp == ' ' || *vp == '\t')
128 if (!strcmp(kp, "name"))
129 cinfo->name = strdup(vp);
130 else if (!strcmp(kp, "enabled"))
131 cinfo->enabled = *vp == '0' ? 0 : 1;
132 else if (!strcmp(kp, "autorefresh"))
133 cinfo->autorefresh = *vp == '0' ? 0 : 1;
134 else if (!strcmp(kp, "baseurl"))
135 cinfo->baseurl = strdup(vp);
136 else if (!strcmp(kp, "path"))
137 cinfo->path = strdup(vp);
138 else if (!strcmp(kp, "type"))
140 if (!strcmp(vp, "yast2"))
141 cinfo->type = TYPE_SUSETAGS;
142 else if (!strcmp(vp, "rpm-md"))
143 cinfo->type = TYPE_RPMMD;
145 cinfo->type = TYPE_UNKNOWN;
147 else if (!strcmp(kp, "priority"))
148 cinfo->priority = atoi(vp);
149 else if (!strcmp(kp, "keeppackages"))
150 cinfo->keeppackages = *vp == '0' ? 0 : 1;
153 if (cinfo->type == TYPE_SUSETAGS && cinfo->baseurl)
155 char *old = cinfo->baseurl;
156 cinfo->baseurl = sat_malloc(5 + strlen(old) + 1);
157 sprintf(cinfo->baseurl, "%s/suse", old);
163 *nrepoinfosp = nrepoinfos;
168 cookie_gzread(void *cookie, char *buf, size_t nbytes)
170 return gzread((gzFile *)cookie, buf, nbytes);
174 cookie_gzclose(void *cookie)
176 return gzclose((gzFile *)cookie);
180 myfopen(const char *fn)
182 cookie_io_functions_t cio;
188 suf = strrchr(fn, '.');
189 if (!suf || strcmp(suf, ".gz") != 0)
190 return fopen(fn, "r");
191 gzf = gzopen(fn, "r");
194 memset(&cio, 0, sizeof(cio));
195 cio.read = cookie_gzread;
196 cio.close = cookie_gzclose;
197 return fopencookie(gzf, "r", cio);
201 curlfopen(const char *baseurl, const char *file, int uncompress)
210 if (l && baseurl[l - 1] == '/')
211 snprintf(url, sizeof(url), "%s%s", baseurl, file);
213 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
214 strcpy(tmpl, "/var/tmp/solvXXXXXX");
222 if ((pid = fork()) == (pid_t)-1)
234 execlp("curl", "curl", "-s", "-L", url, (char *)0);
238 while (waitpid(pid, &status, 0) != pid)
240 lseek(fd, 0, SEEK_SET);
243 cookie_io_functions_t cio;
246 sprintf(tmpl, "/dev/fd/%d", fd);
247 gzf = gzopen(tmpl, "r");
251 memset(&cio, 0, sizeof(cio));
252 cio.read = cookie_gzread;
253 cio.close = cookie_gzclose;
254 return fopencookie(gzf, "r", cio);
256 fcntl(fd, F_SETFD, FD_CLOEXEC);
257 return fdopen(fd, "r");
269 pool_setarch(pool, un.machine);
274 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
277 struct repoinfo *cinfo;
281 const char *primaryfile;
283 printf("reading rpm database\n");
284 repo = repo_create(pool, "@System");
285 repo_add_rpmdb(repo, 0, 0, 0);
286 pool_set_installed(pool, repo);
287 for (i = 0; i < nrepoinfos; i++)
289 cinfo = repoinfos + i;
295 printf("reading rpmmd repo '%s'\n", cinfo->alias);
296 if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
298 repo = repo_create(pool, cinfo->alias);
300 repo->appdata = cinfo;
301 repo_add_repomdxml(repo, fp, 0);
304 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, "primary", SEARCH_STRING);
305 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
306 if (dataiterator_step(&di))
308 dataiterator_setpos_parent(&di);
309 primaryfile = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
311 dataiterator_free(&di);
313 primaryfile = "repodata/primary.xml.gz";
314 if ((fp = curlfopen(cinfo->baseurl, primaryfile, 1)) == 0)
316 repo_add_rpmmd(repo, fp, 0, 0);
320 printf("reading susetags repo '%s'\n", cinfo->alias);
321 if ((fp = curlfopen(cinfo->baseurl, "setup/descr/packages.gz", 1)) == 0)
323 repo = repo_create(pool, cinfo->alias);
325 repo->appdata = cinfo;
326 repo_add_susetags(repo, fp, 0, 0, 0);
330 printf("skipping unknown repo '%s'\n", cinfo->alias);
337 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
343 id = str2id(pool, arg, 0);
346 FOR_PROVIDES(p, pp, id)
348 Solvable *s = pool_id2solvable(pool, p);
351 type = SOLVER_SOLVABLE_NAME;
354 type = SOLVER_SOLVABLE_PROVIDES;
359 /* did not find a solvable, see if it's a relation */
360 if ((r = strpbrk(arg, "<=>")) != 0)
364 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
366 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
378 while (*r == ' ' || *r == '\t')
380 revr = *r ? str2id(pool, r, 1) : 0;
381 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
384 FOR_PROVIDES(p, pp, rid)
386 Solvable *s = pool_id2solvable(pool, p);
387 if (pool_match_nevr(pool, s, rid))
389 type = SOLVER_SOLVABLE_NAME;
392 type = SOLVER_SOLVABLE_PROVIDES;
401 queue_push(out, type);
407 yesno(const char *str)
409 char inbuf[128], *ip;
416 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
421 while (*ip == ' ' || *ip == '\t')
428 if (*ip == 'y' || *ip == 'n')
429 return *ip == 'y' ? 1 : 0;
441 fc_cb(Pool *pool, Id p, void *cbdata)
443 struct fcstate *fcstate = cbdata;
451 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
454 s = pool_id2solvable(pool, p);
455 if (pool->installed && s->repo == pool->installed)
457 if (!s->repo->rpmdbid)
459 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
462 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
464 for (i = 0; i < fcstate->newpkgscnt; i++)
465 if (fcstate->checkq->elements[i] == p)
467 if (i == fcstate->newpkgscnt)
469 fp = fcstate->newpkgsfps[i];
471 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
475 runrpm(const char *arg, const char *name, int dupfd3)
480 if ((pid = fork()) == (pid_t)-1)
487 if (dupfd3 != -1 && dupfd3 != 3)
493 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
494 if (strcmp(arg, "-e") == 0)
495 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
497 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
501 while (waitpid(pid, &status, 0) != pid)
505 printf("rpm failed\n");
511 main(int argc, char **argv)
515 struct repoinfo *repoinfos;
517 int i, mode, newpkgs;
521 char inbuf[128], *ip;
524 struct fcstate fcstate;
526 if (!strcmp(argv[1], "install") || !strcmp(argv[1], "in"))
527 mode = SOLVER_INSTALL;
528 else if (!strcmp(argv[1], "erase") || !strcmp(argv[1], "rm"))
530 else if (!strcmp(argv[1], "show"))
532 else if (!strcmp(argv[1], "update") || !strcmp(argv[1], "up"))
533 mode = SOLVER_UPDATE;
536 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
540 pool = pool_create();
541 // pool_setdebuglevel(pool, 2);
543 repoinfos = read_repoinfos(pool, "/etc/zypp/repos.d", &nrepoinfos);
544 read_repos(pool, repoinfos, nrepoinfos);
545 // FOR_REPOS(i, repo)
546 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
547 pool_addfileprovides(pool);
548 pool_createwhatprovides(pool);
551 for (i = 2; i < argc; i++)
552 mkselect(pool, argv[i], 0, &job);
553 if (!job.count && mode == SOLVER_UPDATE)
557 printf("nothing matched\n");
563 /* show mode, no solver needed */
564 for (i = 0; i < job.count; i += 2)
566 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
568 Solvable *s = pool_id2solvable(pool, p);
569 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
576 for (i = 0; i < job.count; i += 2)
577 job.elements[i] |= mode;
582 Id problem, solution;
585 solv = solver_create(pool);
586 solv->ignorealreadyrecommended = 1;
587 solv->updatesystem = updateall;
588 solver_solve(solv, &job);
589 if (!solv->problems.count)
591 pcnt = solver_problem_count(solv);
592 printf("Found %d problems:\n", pcnt);
593 for (problem = 1; problem <= pcnt; problem++)
596 printf("Problem %d:\n", problem);
597 solver_printprobleminfo(solv, problem);
599 scnt = solver_solution_count(solv, problem);
600 for (solution = 1; solution <= pcnt; solution++)
602 printf("Solution %d:\n", solution);
603 solver_printsolution(solv, problem, solution);
608 printf("Please choose a solution: ");
611 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
616 while (*ip == ' ' || *ip == '\t')
618 if (*ip >= '0' && *ip <= '9')
621 if (take >= 1 && take <= solution)
637 solver_take_solution(solv, problem, take, &job);
642 if (!solv->trans.steps.count)
644 printf("Nothing to do.\n");
648 printf("Transaction summary:\n\n");
649 solver_printtransaction(solv);
650 if (!yesno("OK to continue (y/n)? "))
656 trans = &solv->trans;
658 newpkgs = transaction_installedresult(trans, &checkq);
663 printf("Downloading %d packages\n", newpkgs);
664 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
665 for (i = 0; i < newpkgs; i++)
667 unsigned int medianr;
670 struct repoinfo *cinfo;
672 p = checkq.elements[i];
673 s = pool_id2solvable(pool, p);
674 cinfo = s->repo->appdata;
677 printf("%s: no repository information\n", s->repo->name);
680 loc = solvable_get_location(s, &medianr);
681 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
683 printf("%s: %s not found\n", s->repo->name, loc);
693 printf("Searching for file conflicts\n");
694 queue_init(&conflicts);
695 fcstate.rpmdbstate = 0;
696 fcstate.newpkgscnt = newpkgs;
697 fcstate.checkq = &checkq;
698 fcstate.newpkgsfps = newpkgsfps;
699 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
703 for (i = 0; i < conflicts.count; i += 5)
704 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]));
706 if (yesno("Re-run solver (y/n/q)? "))
708 for (i = 0; i < newpkgs; i++)
709 fclose(newpkgsfps[i]);
710 newpkgsfps = sat_free(newpkgsfps);
712 pool_add_fileconflicts_deps(pool, &conflicts);
713 pool_createwhatprovides(pool); /* Hmm... */
717 queue_free(&conflicts);
720 printf("Committing transaction:\n\n");
721 transaction_order(trans, 0);
722 for (i = 0; i < trans->steps.count; i++)
725 const char *evr, *evrp;
730 p = trans->steps.elements[i];
731 s = pool_id2solvable(pool, p);
732 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
735 case SOLVER_TRANSACTION_ERASE:
736 printf("erase %s\n", solvid2str(pool, p));
737 if (strlen(solvid2str(pool, p)) > 255)
739 evr = evrp = id2str(pool, s->evr);
740 while (*evrp >= '0' && *evrp <= '9')
742 if (evrp > evr && evrp[0] == ':' && evrp[1])
744 sprintf(rpmname, "%s-%s.%s", id2str(pool, s->name), evr, id2str(pool, s->arch));
745 runrpm("-e", rpmname, -1);
747 case SOLVER_TRANSACTION_INSTALL:
748 case SOLVER_TRANSACTION_MULTIINSTALL:
749 printf("install %s\n", solvid2str(pool, p));
750 for (j = 0; j < newpkgs; j++)
751 if (checkq.elements[j] == p)
753 fp = j < newpkgs ? newpkgsfps[j] : 0;
755 lseek(fileno(fp), 0, SEEK_SET);
756 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
765 for (i = 0; i < newpkgs; i++)
767 fclose(newpkgsfps[i]);
768 sat_free(newpkgsfps);