9 #include <sys/utsname.h>
10 #include <sys/types.h>
18 #include "solverdebug.h"
20 #include "repo_rpmdb.h"
21 #include "repo_rpmmd.h"
22 #include "repo_susetags.h"
23 #include "repo_repomdxml.h"
24 #include "pool_fileconflicts.h"
26 /* solv, a little demo application */
42 #define TYPE_UNKNOWN 0
43 #define TYPE_SUSETAGS 1
47 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
50 char buf2[4096], *kp, *vp, *kpe;
55 struct repoinfo *repoinfos = 0, *cinfo;
58 rdlen = strlen(reposdir);
59 dir = opendir(reposdir);
65 while ((ent = readdir(dir)) != 0)
67 l = strlen(ent->d_name);
68 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
70 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
71 if ((fp = fopen(buf, "r")) == 0)
77 while(fgets(buf2, sizeof(buf2), fp))
82 while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
85 while (*kp == ' ' || *kp == '\t')
87 if (!*kp || *kp == '#')
93 vp = strrchr(kp, ']');
97 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
98 cinfo = repoinfos + nrepoinfos++;
99 memset(cinfo, 0, sizeof(*cinfo));
100 cinfo->alias = strdup(kp + 1);
103 vp = strchr(kp, '=');
106 for (kpe = vp - 1; kpe >= kp; kpe--)
107 if (*kpe != ' ' && *kpe != '\t')
112 while (*vp == ' ' || *vp == '\t')
115 if (!strcmp(kp, "name"))
116 cinfo->name = strdup(vp);
117 else if (!strcmp(kp, "enabled"))
118 cinfo->enabled = *vp == '0' ? 0 : 1;
119 else if (!strcmp(kp, "autorefresh"))
120 cinfo->autorefresh = *vp == '0' ? 0 : 1;
121 else if (!strcmp(kp, "baseurl"))
122 cinfo->baseurl = strdup(vp);
123 else if (!strcmp(kp, "path"))
124 cinfo->path = strdup(vp);
125 else if (!strcmp(kp, "type"))
127 if (!strcmp(vp, "yast2"))
128 cinfo->type = TYPE_SUSETAGS;
129 else if (!strcmp(vp, "rpm-md"))
130 cinfo->type = TYPE_RPMMD;
132 cinfo->type = TYPE_UNKNOWN;
134 else if (!strcmp(kp, "priority"))
135 cinfo->priority = atoi(vp);
136 else if (!strcmp(kp, "keeppackages"))
137 cinfo->keeppackages = *vp == '0' ? 0 : 1;
140 if (cinfo->type == TYPE_SUSETAGS && cinfo->baseurl)
142 char *old = cinfo->baseurl;
143 cinfo->baseurl = sat_malloc(5 + strlen(old) + 1);
144 sprintf(cinfo->baseurl, "%s/suse", old);
150 *nrepoinfosp = nrepoinfos;
155 cookie_gzread(void *cookie, char *buf, size_t nbytes)
157 return gzread((gzFile *)cookie, buf, nbytes);
161 cookie_gzclose(void *cookie)
163 return gzclose((gzFile *)cookie);
167 myfopen(const char *fn)
169 cookie_io_functions_t cio;
175 suf = strrchr(fn, '.');
176 if (!suf || strcmp(suf, ".gz") != 0)
177 return fopen(fn, "r");
178 gzf = gzopen(fn, "r");
181 memset(&cio, 0, sizeof(cio));
182 cio.read = cookie_gzread;
183 cio.close = cookie_gzclose;
184 return fopencookie(gzf, "r", cio);
188 curlfopen(char *baseurl, char *file, int uncompress)
197 if (l && baseurl[l - 1] == '/')
198 snprintf(url, sizeof(url), "%s%s", baseurl, file);
200 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
201 strcpy(tmpl, "/var/tmp/solvXXXXXX");
209 if ((pid = fork()) == (pid_t)-1)
221 execlp("curl", "curl", "-s", "-L", url, (char *)0);
225 while (waitpid(pid, &status, 0) != pid)
227 lseek(fd, 0, SEEK_SET);
230 cookie_io_functions_t cio;
233 sprintf(tmpl, "/dev/fd/%d", fd);
234 gzf = gzopen(tmpl, "r");
238 memset(&cio, 0, sizeof(cio));
239 cio.read = cookie_gzread;
240 cio.close = cookie_gzclose;
241 return fopencookie(gzf, "r", cio);
243 fcntl(fd, F_SETFD, FD_CLOEXEC);
244 return fdopen(fd, "r");
256 pool_setarch(pool, un.machine);
260 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
263 struct repoinfo *cinfo;
267 printf("reading rpm database\n");
268 repo = repo_create(pool, "@System");
269 repo_add_rpmdb(repo, 0, 0, 0);
270 pool_set_installed(pool, repo);
271 for (i = 0; i < nrepoinfos; i++)
273 cinfo = repoinfos + i;
279 printf("reading rpmmd repo '%s'\n", cinfo->alias);
280 if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
282 repo = repo_create(pool, cinfo->alias);
284 repo->appdata = cinfo;
285 repo_add_repomdxml(repo, fp, 0);
287 if ((fp = curlfopen(cinfo->baseurl, "repodata/primary.xml.gz", 1)) == 0)
289 repo_add_rpmmd(repo, fp, 0, 0);
293 printf("reading susetags repo '%s'\n", cinfo->alias);
294 if ((fp = curlfopen(cinfo->baseurl, "setup/descr/packages.gz", 1)) == 0)
296 repo = repo_create(pool, cinfo->alias);
298 repo->appdata = cinfo;
299 repo_add_susetags(repo, fp, 0, 0, 0);
303 printf("skipping unknown repo '%s'\n", cinfo->alias);
310 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
316 id = str2id(pool, arg, 0);
319 FOR_PROVIDES(p, pp, id)
321 Solvable *s = pool_id2solvable(pool, p);
324 type = SOLVER_SOLVABLE_NAME;
327 type = SOLVER_SOLVABLE_PROVIDES;
332 /* did not find a solvable, see if it's a relation */
333 if ((r = strpbrk(arg, "<=>")) != 0)
337 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
339 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
351 while (*r == ' ' || *r == '\t')
353 revr = *r ? str2id(pool, r, 1) : 0;
354 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
357 FOR_PROVIDES(p, pp, rid)
359 Solvable *s = pool_id2solvable(pool, p);
360 if (pool_match_nevr(pool, s, rid))
362 type = SOLVER_SOLVABLE_NAME;
365 type = SOLVER_SOLVABLE_PROVIDES;
374 queue_push(out, type);
380 yesno(const char *str)
382 char inbuf[128], *ip;
389 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
394 while (*ip == ' ' || *ip == '\t')
401 if (*ip == 'y' || *ip == 'n')
402 return *ip == 'y' ? 1 : 0;
414 fc_cb(Pool *pool, Id p, void *cbdata)
416 struct fcstate *fcstate = cbdata;
424 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
427 s = pool_id2solvable(pool, p);
428 if (pool->installed && s->repo == pool->installed)
430 if (!s->repo->rpmdbid)
432 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
435 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
437 for (i = 0; i < fcstate->newpkgscnt; i++)
438 if (fcstate->checkq->elements[i] == p)
440 if (i == fcstate->newpkgscnt)
442 fp = fcstate->newpkgsfps[i];
444 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
448 runrpm(const char *arg, const char *name, int dupfd3)
453 if ((pid = fork()) == (pid_t)-1)
460 if (dupfd3 != -1 && dupfd3 != 3)
466 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
467 if (strcmp(arg, "-e") == 0)
468 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
470 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
474 while (waitpid(pid, &status, 0) != pid)
478 printf("rpm failed\n");
484 main(int argc, char **argv)
488 struct repoinfo *repoinfos;
490 int i, mode, newpkgs;
494 char inbuf[128], *ip;
496 FILE **newpkgsfps = 0;
497 struct fcstate fcstate;
499 if (!strcmp(argv[1], "install") || !strcmp(argv[1], "in"))
500 mode = SOLVER_INSTALL;
501 else if (!strcmp(argv[1], "erase") || !strcmp(argv[1], "rm"))
503 else if (!strcmp(argv[1], "show"))
505 else if (!strcmp(argv[1], "update") || !strcmp(argv[1], "up"))
506 mode = SOLVER_UPDATE;
509 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
513 pool = pool_create();
514 // pool_setdebuglevel(pool, 2);
516 repoinfos = read_repoinfos(pool, "/etc/zypp/repos.d", &nrepoinfos);
517 read_repos(pool, repoinfos, nrepoinfos);
518 // FOR_REPOS(i, repo)
519 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
520 pool_addfileprovides(pool);
521 pool_createwhatprovides(pool);
524 for (i = 2; i < argc; i++)
525 mkselect(pool, argv[i], 0, &job);
526 if (!job.count && mode == SOLVER_UPDATE)
530 printf("nothing matched\n");
535 for (i = 0; i < job.count; i += 2)
537 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
539 Solvable *s = pool_id2solvable(pool, p);
540 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
546 for (i = 0; i < job.count; i += 2)
547 job.elements[i] |= mode;
552 Id problem, solution;
555 solv = solver_create(pool);
556 solv->ignorealreadyrecommended = 1;
557 solv->updatesystem = updateall;
558 solver_solve(solv, &job);
559 if (!solv->problems.count)
561 pcnt = solver_problem_count(solv);
562 printf("Found %d problems:\n", pcnt);
563 for (problem = 1; problem <= pcnt; problem++)
566 printf("Problem %d:\n", problem);
567 solver_printprobleminfo(solv, problem);
569 scnt = solver_solution_count(solv, problem);
570 for (solution = 1; solution <= pcnt; solution++)
572 printf("Solution %d:\n", solution);
573 solver_printsolution(solv, problem, solution);
578 printf("Please choose a solution: ");
581 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
586 while (*ip == ' ' || *ip == '\t')
588 if (*ip >= '0' && *ip <= '9')
591 if (take >= 1 && take <= solution)
607 solver_take_solution(solv, problem, take, &job);
612 if (!solv->trans.steps.count)
614 printf("Nothing to do.\n");
618 printf("Transaction summary:\n\n");
619 solver_printtransaction(solv);
620 if (!yesno("OK to continue (y/n)? "))
626 trans = &solv->trans;
628 newpkgs = transaction_installedresult(trans, &checkq);
632 printf("Downloading %d packages\n", newpkgs);
633 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
634 for (i = 0; i < newpkgs; i++)
636 unsigned int medianr;
639 struct repoinfo *cinfo;
641 p = checkq.elements[i];
642 s = pool_id2solvable(pool, p);
643 cinfo = s->repo->appdata;
646 printf("%s: no repository information\n", s->repo->name);
649 loc = solvable_get_location(s, &medianr);
650 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
652 printf("%s: %s not found\n", s->repo->name, loc);
656 printf("Searching for file conflicts\n");
657 queue_init(&conflicts);
658 fcstate.rpmdbstate = 0;
659 fcstate.newpkgscnt = newpkgs;
660 fcstate.checkq = &checkq;
661 fcstate.newpkgsfps = newpkgsfps;
662 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
665 for (i = 0; i < newpkgs; i++)
666 fclose(newpkgsfps[i]);
667 sat_free(newpkgsfps);
671 for (i = 0; i < conflicts.count; i += 5)
672 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]));
674 if (!yesno("Re-run solver (y/n)? "))
679 pool_add_fileconflicts_deps(pool, &conflicts);
680 pool_createwhatprovides(pool);
683 queue_free(&conflicts);
685 transaction_order(trans, 0);
687 printf("Committing transaction:\n\n");
688 for (i = 0; i < trans->steps.count; i++)
691 const char *evr, *evrp;
696 p = trans->steps.elements[i];
697 s = pool_id2solvable(pool, p);
698 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
701 case SOLVER_TRANSACTION_ERASE:
702 printf("erase %s\n", solvid2str(pool, p));
703 if (strlen(solvid2str(pool, p)) > 255)
705 evr = evrp = id2str(pool, s->evr);
706 while (*evrp >= '0' && *evrp <= '9')
708 if (evrp > evr && evrp[0] == ':' && evrp[1])
710 sprintf(rpmname, "%s-%s.%s", id2str(pool, s->name), evr, id2str(pool, s->arch));
711 runrpm("-e", rpmname, -1);
713 case SOLVER_TRANSACTION_INSTALL:
714 case SOLVER_TRANSACTION_MULTIINSTALL:
715 printf("install %s\n", solvid2str(pool, p));
716 for (j = 0; j < newpkgs; j++)
717 if (checkq.elements[j] == p)
719 fp = j < newpkgs ? newpkgsfps[j] : 0;
721 lseek(fileno(fp), 0, SEEK_SET);
722 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
730 for (i = 0; i < newpkgs; i++)
732 fclose(newpkgsfps[i]);
733 sat_free(newpkgsfps);