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)
449 if ((pid = fork()) == (pid_t)-1)
456 if (strcmp(arg, "-e") == 0)
457 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
459 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
463 while (waitpid(pid, &status, 0) != pid)
467 printf("rpm failed\n");
473 main(int argc, char **argv)
477 struct repoinfo *repoinfos;
479 int i, mode, newpkgs;
483 char inbuf[128], *ip;
485 FILE **newpkgsfps = 0;
487 struct fcstate fcstate;
489 pool = pool_create();
490 // pool_setdebuglevel(pool, 2);
492 repoinfos = read_repoinfos(pool, "/etc/zypp/repos.d", &nrepoinfos);
493 read_repos(pool, repoinfos, nrepoinfos);
494 // FOR_REPOS(i, repo)
495 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
496 pool_addfileprovides(pool);
497 pool_createwhatprovides(pool);
498 if (!strcmp(argv[1], "install") || !strcmp(argv[1], "in"))
499 mode = SOLVER_INSTALL;
500 else if (!strcmp(argv[1], "erase") || !strcmp(argv[1], "rm"))
502 else if (!strcmp(argv[1], "show"))
504 else if (!strcmp(argv[1], "update") || !strcmp(argv[1], "up"))
505 mode = SOLVER_UPDATE;
508 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
512 for (i = 2; i < argc; i++)
513 mkselect(pool, argv[i], 0, &job);
514 if (!job.count && mode == SOLVER_UPDATE)
518 printf("nothing matched\n");
523 for (i = 0; i < job.count; i += 2)
525 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
526 printf(" - %s\n", solvid2str(pool, p));
531 for (i = 0; i < job.count; i += 2)
532 job.elements[i] |= mode;
537 Id problem, solution;
540 solv = solver_create(pool);
541 solv->ignorealreadyrecommended = 1;
542 solv->updatesystem = updateall;
543 solver_solve(solv, &job);
544 if (!solv->problems.count)
546 pcnt = solver_problem_count(solv);
547 printf("Found %d problems:\n", pcnt);
548 for (problem = 1; problem <= pcnt; problem++)
551 printf("Problem %d:\n", problem);
552 solver_printprobleminfo(solv, problem);
554 scnt = solver_solution_count(solv, problem);
555 for (solution = 1; solution <= pcnt; solution++)
557 printf("Solution %d:\n", solution);
558 solver_printsolution(solv, problem, solution);
563 printf("Please choose a solution: ");
566 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
571 while (*ip == ' ' || *ip == '\t')
573 if (*ip >= '0' && *ip <= '9')
576 if (take >= 1 && take <= solution)
592 solver_take_solution(solv, problem, take, &job);
597 if (!solv->trans.steps.count)
599 printf("Nothing to do.\n");
603 printf("Transaction summary:\n\n");
604 solver_printtransaction(solv);
605 if (!yesno("OK to continue (y/n)? "))
611 trans = &solv->trans;
613 newpkgs = transaction_installedresult(trans, &checkq);
617 printf("Downloading %d packages\n", newpkgs);
618 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
619 newpkgsps = sat_calloc(newpkgs, sizeof(Id));
620 for (i = 0; i < newpkgs; i++)
623 unsigned int medianr;
626 struct repoinfo *cinfo;
628 p = checkq.elements[i];
630 s = pool_id2solvable(pool, p);
631 loc = solvable_get_location(s, &medianr);
633 for (j = 0; j < nrepoinfos; j++)
634 if (s->repo == repoinfos[j].repo)
636 cinfo = repoinfos + j;
639 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
641 printf("%s: %s not found\n", cinfo->alias, loc);
645 printf("Searching for file conflicts\n");
646 queue_init(&conflicts);
647 fcstate.rpmdbstate = 0;
648 fcstate.newpkgscnt = newpkgs;
649 fcstate.newpkgsfps = newpkgsfps;
650 fcstate.newpkgsps = newpkgsps;
651 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
654 for (i = 0; i < newpkgs; i++)
655 fclose(newpkgsfps[i]);
656 sat_free(newpkgsfps);
660 for (i = 0; i < conflicts.count; i += 5)
661 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]));
663 if (!yesno("Re-run solver (y/n)? "))
668 pool_add_fileconflicts_deps(pool, &conflicts);
669 pool_createwhatprovides(pool);
672 queue_free(&conflicts);
674 transaction_order(trans, 0);
676 printf("Committing transaction:\n");
677 for (i = 0; i < trans->steps.count; i++)
680 const char *evr, *evrp;
685 p = trans->steps.elements[i];
686 s = pool_id2solvable(pool, p);
687 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
690 case SOLVER_TRANSACTION_ERASE:
691 printf("erase %s\n", solvid2str(pool, p));
692 if (strlen(solvid2str(pool, p)) > 255)
694 evr = evrp = id2str(pool, s->evr);
695 while (*evrp >= '0' && *evrp <= '9')
697 if (evrp > evr && evrp[0] == ':' && evrp[1])
699 sprintf(rpmname, "%s-%s.%s", id2str(pool, s->name), evr, id2str(pool, s->arch));
700 runrpm("-e", rpmname);
702 case SOLVER_TRANSACTION_INSTALL:
703 case SOLVER_TRANSACTION_MULTIINSTALL:
704 printf("install %s\n", solvid2str(pool, p));
705 for (j = 0; j < newpkgs; j++)
706 if (newpkgsps[j] == p)
708 fp = j < newpkgs ? newpkgsfps[j] : 0;
710 lseek(fileno(fp), 0, SEEK_SET);
711 sprintf(rpmname, "/dev/fd/%d", fileno(fp));
712 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", rpmname);
718 for (i = 0; i < newpkgs; i++)
719 fclose(newpkgsfps[i]);
720 sat_free(newpkgsfps);