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(char *baseurl, 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);
273 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
276 struct repoinfo *cinfo;
280 printf("reading rpm database\n");
281 repo = repo_create(pool, "@System");
282 repo_add_rpmdb(repo, 0, 0, 0);
283 pool_set_installed(pool, repo);
284 for (i = 0; i < nrepoinfos; i++)
286 cinfo = repoinfos + i;
292 printf("reading rpmmd repo '%s'\n", cinfo->alias);
293 if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
295 repo = repo_create(pool, cinfo->alias);
297 repo->appdata = cinfo;
298 repo_add_repomdxml(repo, fp, 0);
300 if ((fp = curlfopen(cinfo->baseurl, "repodata/primary.xml.gz", 1)) == 0)
302 repo_add_rpmmd(repo, fp, 0, 0);
306 printf("reading susetags repo '%s'\n", cinfo->alias);
307 if ((fp = curlfopen(cinfo->baseurl, "setup/descr/packages.gz", 1)) == 0)
309 repo = repo_create(pool, cinfo->alias);
311 repo->appdata = cinfo;
312 repo_add_susetags(repo, fp, 0, 0, 0);
316 printf("skipping unknown repo '%s'\n", cinfo->alias);
323 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
329 id = str2id(pool, arg, 0);
332 FOR_PROVIDES(p, pp, id)
334 Solvable *s = pool_id2solvable(pool, p);
337 type = SOLVER_SOLVABLE_NAME;
340 type = SOLVER_SOLVABLE_PROVIDES;
345 /* did not find a solvable, see if it's a relation */
346 if ((r = strpbrk(arg, "<=>")) != 0)
350 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
352 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
364 while (*r == ' ' || *r == '\t')
366 revr = *r ? str2id(pool, r, 1) : 0;
367 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
370 FOR_PROVIDES(p, pp, rid)
372 Solvable *s = pool_id2solvable(pool, p);
373 if (pool_match_nevr(pool, s, rid))
375 type = SOLVER_SOLVABLE_NAME;
378 type = SOLVER_SOLVABLE_PROVIDES;
387 queue_push(out, type);
393 yesno(const char *str)
395 char inbuf[128], *ip;
402 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
407 while (*ip == ' ' || *ip == '\t')
414 if (*ip == 'y' || *ip == 'n')
415 return *ip == 'y' ? 1 : 0;
427 fc_cb(Pool *pool, Id p, void *cbdata)
429 struct fcstate *fcstate = cbdata;
437 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
440 s = pool_id2solvable(pool, p);
441 if (pool->installed && s->repo == pool->installed)
443 if (!s->repo->rpmdbid)
445 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
448 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
450 for (i = 0; i < fcstate->newpkgscnt; i++)
451 if (fcstate->checkq->elements[i] == p)
453 if (i == fcstate->newpkgscnt)
455 fp = fcstate->newpkgsfps[i];
457 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
461 runrpm(const char *arg, const char *name, int dupfd3)
466 if ((pid = fork()) == (pid_t)-1)
473 if (dupfd3 != -1 && dupfd3 != 3)
479 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
480 if (strcmp(arg, "-e") == 0)
481 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
483 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
487 while (waitpid(pid, &status, 0) != pid)
491 printf("rpm failed\n");
497 main(int argc, char **argv)
501 struct repoinfo *repoinfos;
503 int i, mode, newpkgs;
507 char inbuf[128], *ip;
510 struct fcstate fcstate;
512 if (!strcmp(argv[1], "install") || !strcmp(argv[1], "in"))
513 mode = SOLVER_INSTALL;
514 else if (!strcmp(argv[1], "erase") || !strcmp(argv[1], "rm"))
516 else if (!strcmp(argv[1], "show"))
518 else if (!strcmp(argv[1], "update") || !strcmp(argv[1], "up"))
519 mode = SOLVER_UPDATE;
522 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
526 pool = pool_create();
527 // pool_setdebuglevel(pool, 2);
529 repoinfos = read_repoinfos(pool, "/etc/zypp/repos.d", &nrepoinfos);
530 read_repos(pool, repoinfos, nrepoinfos);
531 // FOR_REPOS(i, repo)
532 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
533 pool_addfileprovides(pool);
534 pool_createwhatprovides(pool);
537 for (i = 2; i < argc; i++)
538 mkselect(pool, argv[i], 0, &job);
539 if (!job.count && mode == SOLVER_UPDATE)
543 printf("nothing matched\n");
549 /* show mode, no solver needed */
550 for (i = 0; i < job.count; i += 2)
552 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
554 Solvable *s = pool_id2solvable(pool, p);
555 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
562 for (i = 0; i < job.count; i += 2)
563 job.elements[i] |= mode;
568 Id problem, solution;
571 solv = solver_create(pool);
572 solv->ignorealreadyrecommended = 1;
573 solv->updatesystem = updateall;
574 solver_solve(solv, &job);
575 if (!solv->problems.count)
577 pcnt = solver_problem_count(solv);
578 printf("Found %d problems:\n", pcnt);
579 for (problem = 1; problem <= pcnt; problem++)
582 printf("Problem %d:\n", problem);
583 solver_printprobleminfo(solv, problem);
585 scnt = solver_solution_count(solv, problem);
586 for (solution = 1; solution <= pcnt; solution++)
588 printf("Solution %d:\n", solution);
589 solver_printsolution(solv, problem, solution);
594 printf("Please choose a solution: ");
597 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
602 while (*ip == ' ' || *ip == '\t')
604 if (*ip >= '0' && *ip <= '9')
607 if (take >= 1 && take <= solution)
623 solver_take_solution(solv, problem, take, &job);
628 if (!solv->trans.steps.count)
630 printf("Nothing to do.\n");
634 printf("Transaction summary:\n\n");
635 solver_printtransaction(solv);
636 if (!yesno("OK to continue (y/n)? "))
642 trans = &solv->trans;
644 newpkgs = transaction_installedresult(trans, &checkq);
649 printf("Downloading %d packages\n", newpkgs);
650 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
651 for (i = 0; i < newpkgs; i++)
653 unsigned int medianr;
656 struct repoinfo *cinfo;
658 p = checkq.elements[i];
659 s = pool_id2solvable(pool, p);
660 cinfo = s->repo->appdata;
663 printf("%s: no repository information\n", s->repo->name);
666 loc = solvable_get_location(s, &medianr);
667 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
669 printf("%s: %s not found\n", s->repo->name, loc);
679 printf("Searching for file conflicts\n");
680 queue_init(&conflicts);
681 fcstate.rpmdbstate = 0;
682 fcstate.newpkgscnt = newpkgs;
683 fcstate.checkq = &checkq;
684 fcstate.newpkgsfps = newpkgsfps;
685 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
689 for (i = 0; i < conflicts.count; i += 5)
690 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]));
692 if (yesno("Re-run solver (y/n/q)? "))
694 for (i = 0; i < newpkgs; i++)
695 fclose(newpkgsfps[i]);
696 newpkgsfps = sat_free(newpkgsfps);
698 pool_add_fileconflicts_deps(pool, &conflicts);
699 pool_createwhatprovides(pool); /* Hmm... */
703 queue_free(&conflicts);
706 printf("Committing transaction:\n\n");
707 transaction_order(trans, 0);
708 for (i = 0; i < trans->steps.count; i++)
711 const char *evr, *evrp;
716 p = trans->steps.elements[i];
717 s = pool_id2solvable(pool, p);
718 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
721 case SOLVER_TRANSACTION_ERASE:
722 printf("erase %s\n", solvid2str(pool, p));
723 if (strlen(solvid2str(pool, p)) > 255)
725 evr = evrp = id2str(pool, s->evr);
726 while (*evrp >= '0' && *evrp <= '9')
728 if (evrp > evr && evrp[0] == ':' && evrp[1])
730 sprintf(rpmname, "%s-%s.%s", id2str(pool, s->name), evr, id2str(pool, s->arch));
731 runrpm("-e", rpmname, -1);
733 case SOLVER_TRANSACTION_INSTALL:
734 case SOLVER_TRANSACTION_MULTIINSTALL:
735 printf("install %s\n", solvid2str(pool, p));
736 for (j = 0; j < newpkgs; j++)
737 if (checkq.elements[j] == p)
739 fp = j < newpkgs ? newpkgsfps[j] : 0;
741 lseek(fileno(fp), 0, SEEK_SET);
742 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
751 for (i = 0; i < newpkgs; i++)
753 fclose(newpkgsfps[i]);
754 sat_free(newpkgsfps);