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_add_repomdxml(repo, fp, 0);
286 if ((fp = curlfopen(cinfo->baseurl, "repodata/primary.xml.gz", 1)) == 0)
288 repo_add_rpmmd(repo, fp, 0, 0);
292 printf("reading susetags repo '%s'\n", cinfo->alias);
293 if ((fp = curlfopen(cinfo->baseurl, "setup/descr/packages.gz", 1)) == 0)
295 repo = repo_create(pool, cinfo->alias);
297 repo_add_susetags(repo, fp, 0, 0, 0);
301 printf("skipping unknown repo '%s'\n", cinfo->alias);
308 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
314 id = str2id(pool, arg, 0);
317 FOR_PROVIDES(p, pp, id)
319 Solvable *s = pool_id2solvable(pool, p);
322 type = SOLVER_SOLVABLE_NAME;
325 type = SOLVER_SOLVABLE_PROVIDES;
330 /* did not find a solvable, see if it's a relation */
331 if ((r = strpbrk(arg, "<=>")) != 0)
335 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
337 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
349 while (*r == ' ' || *r == '\t')
351 revr = *r ? str2id(pool, r, 1) : 0;
352 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
355 FOR_PROVIDES(p, pp, rid)
357 Solvable *s = pool_id2solvable(pool, p);
358 if (pool_match_nevr(pool, s, rid))
360 type = SOLVER_SOLVABLE_NAME;
363 type = SOLVER_SOLVABLE_PROVIDES;
372 queue_push(out, type);
378 yesno(const char *str)
380 char inbuf[128], *ip;
387 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
392 while (*ip == ' ' || *ip == '\t')
399 if (*ip == 'y' || *ip == 'n')
400 return *ip == 'y' ? 1 : 0;
412 fc_cb(Pool *pool, Id p, void *cbdata)
414 struct fcstate *fcstate = cbdata;
422 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
425 s = pool_id2solvable(pool, p);
426 if (pool->installed && s->repo == pool->installed)
428 if (!s->repo->rpmdbid)
430 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
433 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
435 for (i = 0; i < fcstate->newpkgscnt; i++)
436 if (fcstate->newpkgsps[i] == p)
438 if (i == fcstate->newpkgscnt)
440 fp = fcstate->newpkgsfps[i];
442 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
446 runrpm(const char *arg, const char *name, int dupfd3)
451 if ((pid = fork()) == (pid_t)-1)
458 if (dupfd3 != -1 && dupfd3 != 3)
464 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
465 if (strcmp(arg, "-e") == 0)
466 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
468 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
472 while (waitpid(pid, &status, 0) != pid)
476 printf("rpm failed\n");
482 main(int argc, char **argv)
486 struct repoinfo *repoinfos;
488 int i, mode, newpkgs;
492 char inbuf[128], *ip;
494 FILE **newpkgsfps = 0;
496 struct fcstate fcstate;
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 pool = pool_create();
513 // pool_setdebuglevel(pool, 2);
515 repoinfos = read_repoinfos(pool, "/etc/zypp/repos.d", &nrepoinfos);
516 read_repos(pool, repoinfos, nrepoinfos);
517 // FOR_REPOS(i, repo)
518 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
519 pool_addfileprovides(pool);
520 pool_createwhatprovides(pool);
523 for (i = 2; i < argc; i++)
524 mkselect(pool, argv[i], 0, &job);
525 if (!job.count && mode == SOLVER_UPDATE)
529 printf("nothing matched\n");
534 for (i = 0; i < job.count; i += 2)
536 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
538 Solvable *s = pool_id2solvable(pool, p);
539 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
545 for (i = 0; i < job.count; i += 2)
546 job.elements[i] |= mode;
551 Id problem, solution;
554 solv = solver_create(pool);
555 solv->ignorealreadyrecommended = 1;
556 solv->updatesystem = updateall;
557 solver_solve(solv, &job);
558 if (!solv->problems.count)
560 pcnt = solver_problem_count(solv);
561 printf("Found %d problems:\n", pcnt);
562 for (problem = 1; problem <= pcnt; problem++)
565 printf("Problem %d:\n", problem);
566 solver_printprobleminfo(solv, problem);
568 scnt = solver_solution_count(solv, problem);
569 for (solution = 1; solution <= pcnt; solution++)
571 printf("Solution %d:\n", solution);
572 solver_printsolution(solv, problem, solution);
577 printf("Please choose a solution: ");
580 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
585 while (*ip == ' ' || *ip == '\t')
587 if (*ip >= '0' && *ip <= '9')
590 if (take >= 1 && take <= solution)
606 solver_take_solution(solv, problem, take, &job);
611 if (!solv->trans.steps.count)
613 printf("Nothing to do.\n");
617 printf("Transaction summary:\n\n");
618 solver_printtransaction(solv);
619 if (!yesno("OK to continue (y/n)? "))
625 trans = &solv->trans;
627 newpkgs = transaction_installedresult(trans, &checkq);
631 printf("Downloading %d packages\n", newpkgs);
632 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
633 newpkgsps = sat_calloc(newpkgs, sizeof(Id));
634 for (i = 0; i < newpkgs; i++)
637 unsigned int medianr;
640 struct repoinfo *cinfo;
642 p = checkq.elements[i];
644 s = pool_id2solvable(pool, p);
645 loc = solvable_get_location(s, &medianr);
647 for (j = 0; j < nrepoinfos; j++)
648 if (s->repo == repoinfos[j].repo)
650 cinfo = repoinfos + j;
653 if ((newpkgsfps[i] = curlfopen(cinfo->baseurl, loc, 0)) == 0)
655 printf("%s: %s not found\n", cinfo->alias, loc);
659 printf("Searching for file conflicts\n");
660 queue_init(&conflicts);
661 fcstate.rpmdbstate = 0;
662 fcstate.newpkgscnt = newpkgs;
663 fcstate.newpkgsfps = newpkgsfps;
664 fcstate.newpkgsps = newpkgsps;
665 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
668 for (i = 0; i < newpkgs; i++)
669 fclose(newpkgsfps[i]);
670 sat_free(newpkgsfps);
675 for (i = 0; i < conflicts.count; i += 5)
676 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]));
678 if (!yesno("Re-run solver (y/n)? "))
683 pool_add_fileconflicts_deps(pool, &conflicts);
684 pool_createwhatprovides(pool);
687 queue_free(&conflicts);
689 transaction_order(trans, 0);
691 printf("Committing transaction:\n\n");
692 for (i = 0; i < trans->steps.count; i++)
695 const char *evr, *evrp;
700 p = trans->steps.elements[i];
701 s = pool_id2solvable(pool, p);
702 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
705 case SOLVER_TRANSACTION_ERASE:
706 printf("erase %s\n", solvid2str(pool, p));
707 if (strlen(solvid2str(pool, p)) > 255)
709 evr = evrp = id2str(pool, s->evr);
710 while (*evrp >= '0' && *evrp <= '9')
712 if (evrp > evr && evrp[0] == ':' && evrp[1])
714 sprintf(rpmname, "%s-%s.%s", id2str(pool, s->name), evr, id2str(pool, s->arch));
715 runrpm("-e", rpmname, -1);
717 case SOLVER_TRANSACTION_INSTALL:
718 case SOLVER_TRANSACTION_MULTIINSTALL:
719 printf("install %s\n", solvid2str(pool, p));
720 for (j = 0; j < newpkgs; j++)
721 if (newpkgsps[j] == p)
723 fp = j < newpkgs ? newpkgsfps[j] : 0;
725 lseek(fileno(fp), 0, SEEK_SET);
726 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
734 for (i = 0; i < newpkgs; i++)
736 fclose(newpkgsfps[i]);
737 sat_free(newpkgsfps);