8 #include <sys/utsname.h>
17 #include "solverdebug.h"
19 #include "repo_rpmdb.h"
20 #include "repo_rpmmd.h"
21 #include "repo_susetags.h"
22 #include "repo_repomdxml.h"
23 #include "pool_fileconflicts.h"
25 /* solv, a little demo application */
41 #define TYPE_UNKNOWN 0
42 #define TYPE_SUSETAGS 1
46 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
49 char buf2[4096], *kp, *vp, *kpe;
54 struct repoinfo *repoinfos = 0, *cinfo;
57 rdlen = strlen(reposdir);
58 dir = opendir(reposdir);
64 while ((ent = readdir(dir)) != 0)
66 l = strlen(ent->d_name);
67 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
69 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
70 if ((fp = fopen(buf, "r")) == 0)
76 while(fgets(buf2, sizeof(buf2), fp))
81 while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
84 while (*kp == ' ' || *kp == '\t')
86 if (!*kp || *kp == '#')
92 vp = strrchr(kp, ']');
96 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
97 cinfo = repoinfos + nrepoinfos++;
98 memset(cinfo, 0, sizeof(*cinfo));
99 cinfo->alias = strdup(kp + 1);
102 vp = strchr(kp, '=');
105 for (kpe = vp - 1; kpe >= kp; kpe--)
106 if (*kpe != ' ' && *kpe != '\t')
111 while (*vp == ' ' || *vp == '\t')
114 if (!strcmp(kp, "name"))
115 cinfo->name = strdup(vp);
116 else if (!strcmp(kp, "enabled"))
117 cinfo->enabled = *vp == '0' ? 0 : 1;
118 else if (!strcmp(kp, "autorefresh"))
119 cinfo->autorefresh = *vp == '0' ? 0 : 1;
120 else if (!strcmp(kp, "baseurl"))
121 cinfo->baseurl = strdup(vp);
122 else if (!strcmp(kp, "path"))
123 cinfo->path = strdup(vp);
124 else if (!strcmp(kp, "type"))
126 if (!strcmp(vp, "yast2"))
127 cinfo->type = TYPE_SUSETAGS;
128 else if (!strcmp(vp, "rpm-md"))
129 cinfo->type = TYPE_RPMMD;
131 cinfo->type = TYPE_UNKNOWN;
133 else if (!strcmp(kp, "priority"))
134 cinfo->priority = atoi(vp);
135 else if (!strcmp(kp, "keeppackages"))
136 cinfo->keeppackages = *vp == '0' ? 0 : 1;
139 if (cinfo->type == TYPE_SUSETAGS && cinfo->baseurl)
141 char *old = cinfo->baseurl;
142 cinfo->baseurl = sat_malloc(5 + strlen(old) + 1);
143 sprintf(cinfo->baseurl, "%s/suse", old);
149 *nrepoinfosp = nrepoinfos;
154 cookie_gzread(void *cookie, char *buf, size_t nbytes)
156 return gzread((gzFile *)cookie, buf, nbytes);
160 cookie_gzclose(void *cookie)
162 return gzclose((gzFile *)cookie);
166 myfopen(const char *fn)
168 cookie_io_functions_t cio;
174 suf = strrchr(fn, '.');
175 if (!suf || strcmp(suf, ".gz") != 0)
176 return fopen(fn, "r");
177 gzf = gzopen(fn, "r");
180 memset(&cio, 0, sizeof(cio));
181 cio.read = cookie_gzread;
182 cio.close = cookie_gzclose;
183 return fopencookie(gzf, "r", cio);
187 curlfopen(char *baseurl, char *file, int uncompress)
196 if (l && baseurl[l - 1] == '/')
197 snprintf(url, sizeof(url), "%s%s", baseurl, file);
199 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
200 strcpy(tmpl, "/var/tmp/solvXXXXXX");
208 if ((pid = fork()) == (pid_t)-1)
220 execlp("curl", "curl", "-s", "-L", url, (char *)0);
224 while (waitpid(pid, &status, 0) != pid)
226 lseek(fd, 0, SEEK_SET);
229 cookie_io_functions_t cio;
232 sprintf(tmpl, "/dev/fd/%d", fd);
233 gzf = gzopen(tmpl, "r");
237 memset(&cio, 0, sizeof(cio));
238 cio.read = cookie_gzread;
239 cio.close = cookie_gzclose;
240 return fopencookie(gzf, "r", cio);
242 return fdopen(fd, "r");
254 pool_setarch(pool, un.machine);
258 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
261 struct repoinfo *cinfo;
265 printf("reading rpm database\n");
266 repo = repo_create(pool, "@System");
267 repo_add_rpmdb(repo, 0, 0, 0);
268 pool_set_installed(pool, repo);
269 for (i = 0; i < nrepoinfos; i++)
271 cinfo = repoinfos + i;
277 printf("reading rpmmd repo '%s'\n", cinfo->alias);
278 if ((fp = curlfopen(cinfo->baseurl, "repodata/repomd.xml", 0)) == 0)
280 repo = repo_create(pool, cinfo->alias);
282 repo_add_repomdxml(repo, fp, 0);
284 if ((fp = curlfopen(cinfo->baseurl, "repodata/primary.xml.gz", 1)) == 0)
286 repo_add_rpmmd(repo, fp, 0, 0);
290 printf("reading susetags repo '%s'\n", cinfo->alias);
291 if ((fp = curlfopen(cinfo->baseurl, "setup/descr/packages.gz", 1)) == 0)
293 repo = repo_create(pool, cinfo->alias);
295 repo_add_susetags(repo, fp, 0, 0, 0);
299 printf("skipping unknown repo '%s'\n", cinfo->alias);
306 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
312 id = str2id(pool, arg, 0);
315 FOR_PROVIDES(p, pp, id)
317 Solvable *s = pool_id2solvable(pool, p);
320 type = SOLVER_SOLVABLE_NAME;
323 type = SOLVER_SOLVABLE_PROVIDES;
328 /* did not find a solvable, see if it's a relation */
329 if ((r = strpbrk(arg, "<=>")) != 0)
333 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
335 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
347 while (*r == ' ' || *r == '\t')
349 revr = *r ? str2id(pool, r, 1) : 0;
350 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
353 FOR_PROVIDES(p, pp, rid)
355 Solvable *s = pool_id2solvable(pool, p);
356 if (pool_match_nevr(pool, s, rid))
358 type = SOLVER_SOLVABLE_NAME;
361 type = SOLVER_SOLVABLE_PROVIDES;
370 queue_push(out, type);
376 yesno(const char *str)
378 char inbuf[128], *ip;
385 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
390 while (*ip == ' ' || *ip == '\t')
397 if (*ip == 'y' || *ip == 'n')
398 return *ip == 'y' ? 1 : 0;
410 fc_cb(Pool *pool, Id p, void *cbdata)
412 struct fcstate *fcstate = cbdata;
420 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
423 s = pool_id2solvable(pool, p);
424 if (pool->installed && s->repo == pool->installed)
426 if (!s->repo->rpmdbid)
428 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
431 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
433 for (i = 0; i < fcstate->newpkgscnt; i++)
434 if (fcstate->newpkgsps[i] == p)
436 if (i == fcstate->newpkgscnt)
438 fp = fcstate->newpkgsfps[i];
440 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
444 runrpm(const char *arg, const char *name, int dupfd3, FILE **newpkgsfps, int newpkgs)
449 if ((pid = fork()) == (pid_t)-1)
457 for (i = 0; i < newpkgs; i++)
461 if (dupfd3 != -1 && fileno(newpkgsfps[i]) == dupfd3)
463 close(fileno(newpkgsfps[i]));
465 if (dupfd3 != -1 && dupfd3 != 3)
470 if (strcmp(arg, "-e") == 0)
471 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
473 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
477 while (waitpid(pid, &status, 0) != pid)
481 printf("rpm failed\n");
487 main(int argc, char **argv)
491 struct repoinfo *repoinfos;
493 int i, mode, newpkgs;
497 char inbuf[128], *ip;
499 FILE **newpkgsfps = 0;
501 struct fcstate fcstate;
503 if (!strcmp(argv[1], "install") || !strcmp(argv[1], "in"))
504 mode = SOLVER_INSTALL;
505 else if (!strcmp(argv[1], "erase") || !strcmp(argv[1], "rm"))
507 else if (!strcmp(argv[1], "show"))
509 else if (!strcmp(argv[1], "update") || !strcmp(argv[1], "up"))
510 mode = SOLVER_UPDATE;
513 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
517 pool = pool_create();
518 // pool_setdebuglevel(pool, 2);
520 repoinfos = read_repoinfos(pool, "/etc/zypp/repos.d", &nrepoinfos);
521 read_repos(pool, repoinfos, nrepoinfos);
522 // FOR_REPOS(i, repo)
523 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
524 pool_addfileprovides(pool);
525 pool_createwhatprovides(pool);
528 for (i = 2; i < argc; i++)
529 mkselect(pool, argv[i], 0, &job);
530 if (!job.count && mode == SOLVER_UPDATE)
534 printf("nothing matched\n");
539 for (i = 0; i < job.count; i += 2)
541 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
543 Solvable *s = pool_id2solvable(pool, p);
544 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
550 for (i = 0; i < job.count; i += 2)
551 job.elements[i] |= mode;
556 Id problem, solution;
559 solv = solver_create(pool);
560 solv->ignorealreadyrecommended = 1;
561 solv->updatesystem = updateall;
562 solver_solve(solv, &job);
563 if (!solv->problems.count)
565 pcnt = solver_problem_count(solv);
566 printf("Found %d problems:\n", pcnt);
567 for (problem = 1; problem <= pcnt; problem++)
570 printf("Problem %d:\n", problem);
571 solver_printprobleminfo(solv, problem);
573 scnt = solver_solution_count(solv, problem);
574 for (solution = 1; solution <= pcnt; solution++)
576 printf("Solution %d:\n", solution);
577 solver_printsolution(solv, problem, solution);
582 printf("Please choose a solution: ");
585 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
590 while (*ip == ' ' || *ip == '\t')
592 if (*ip >= '0' && *ip <= '9')
595 if (take >= 1 && take <= solution)
611 solver_take_solution(solv, problem, take, &job);
616 if (!solv->trans.steps.count)
618 printf("Nothing to do.\n");
622 printf("Transaction summary:\n\n");
623 solver_printtransaction(solv);
624 if (!yesno("OK to continue (y/n)? "))
630 trans = &solv->trans;
632 newpkgs = transaction_installedresult(trans, &checkq);
636 printf("Downloading %d packages\n", newpkgs);
637 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
638 newpkgsps = sat_calloc(newpkgs, sizeof(Id));
639 for (i = 0; i < newpkgs; i++)
642 unsigned int medianr;
645 struct repoinfo *cinfo;
647 p = checkq.elements[i];
649 s = pool_id2solvable(pool, p);
650 loc = solvable_get_location(s, &medianr);
652 for (j = 0; j < nrepoinfos; j++)
653 if (s->repo == repoinfos[j].repo)
655 cinfo = repoinfos + j;
658 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
660 printf("%s: %s not found\n", cinfo->alias, loc);
664 printf("Searching for file conflicts\n");
665 queue_init(&conflicts);
666 fcstate.rpmdbstate = 0;
667 fcstate.newpkgscnt = newpkgs;
668 fcstate.newpkgsfps = newpkgsfps;
669 fcstate.newpkgsps = newpkgsps;
670 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
673 for (i = 0; i < newpkgs; i++)
674 fclose(newpkgsfps[i]);
675 sat_free(newpkgsfps);
679 for (i = 0; i < conflicts.count; i += 5)
680 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]));
682 if (!yesno("Re-run solver (y/n)? "))
687 pool_add_fileconflicts_deps(pool, &conflicts);
688 pool_createwhatprovides(pool);
691 queue_free(&conflicts);
693 transaction_order(trans, 0);
695 printf("Committing transaction:\n\n");
696 for (i = 0; i < trans->steps.count; i++)
699 const char *evr, *evrp;
704 p = trans->steps.elements[i];
705 s = pool_id2solvable(pool, p);
706 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
709 case SOLVER_TRANSACTION_ERASE:
710 printf("erase %s\n", solvid2str(pool, p));
711 if (strlen(solvid2str(pool, p)) > 255)
713 evr = evrp = id2str(pool, s->evr);
714 while (*evrp >= '0' && *evrp <= '9')
716 if (evrp > evr && evrp[0] == ':' && evrp[1])
718 sprintf(rpmname, "%s-%s.%s", id2str(pool, s->name), evr, id2str(pool, s->arch));
719 runrpm("-e", rpmname, -1, newpkgsfps, newpkgs);
721 case SOLVER_TRANSACTION_INSTALL:
722 case SOLVER_TRANSACTION_MULTIINSTALL:
723 printf("install %s\n", solvid2str(pool, p));
724 for (j = 0; j < newpkgs; j++)
725 if (newpkgsps[j] == p)
727 fp = j < newpkgs ? newpkgsfps[j] : 0;
729 lseek(fileno(fp), 0, SEEK_SET);
730 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp), newpkgsfps, newpkgs);
738 for (i = 0; i < newpkgs; i++)
740 fclose(newpkgsfps[i]);
741 sat_free(newpkgsfps);