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 * - vendor policy loading
12 * - soft locks file handling
13 * - multi version handling
25 #include <sys/utsname.h>
26 #include <sys/types.h>
35 #include "solverdebug.h"
37 #include "repo_solv.h"
39 #include "repo_write.h"
40 #include "repo_rpmdb.h"
41 #include "repo_products.h"
42 #include "repo_rpmmd.h"
43 #include "repo_susetags.h"
44 #include "repo_repomdxml.h"
45 #include "repo_updateinfoxml.h"
46 #include "repo_deltainfoxml.h"
47 #include "repo_content.h"
48 #include "pool_fileconflicts.h"
52 # define REPOINFO_PATH "/etc/yum.repos.d"
54 # define REPOINFO_PATH "/etc/zypp/repos.d"
55 # define PRODUCTS_PATH "/etc/products.d"
58 #define SOLVCACHE_PATH "/var/cache/solv"
79 yum_substitute(Pool *pool, char *line)
84 while ((p2 = strchr(p, '$')) != 0)
86 if (!strncmp(p2, "$releasever", 11))
88 static char *releaseevr;
94 fp = popen("rpm --nodigest --nosignature -q --qf '%{VERSION}\n' --whatprovides redhat-release", "r");
95 fread(buf, 1, sizeof(buf), fp);
98 while (*bp != ' ' && *bp != '\t' && *bp != '\n')
101 releaseevr = strdup(buf);
104 p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
105 p2 = p + (p2 - line);
107 p = p2 + strlen(releaseevr);
110 if (!strncmp(p2, "$basearch", 9))
112 static char *basearch;
121 basearch = strdup(un.machine);
122 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
126 p = pool_tmpjoin(pool, line, basearch, p2 + 9);
127 p2 = p + (p2 - line);
129 p = p2 + strlen(basearch);
138 #define TYPE_UNKNOWN 0
139 #define TYPE_SUSETAGS 1
141 #define TYPE_PLAINDIR 3
144 read_repoinfos_sort(const void *ap, const void *bp)
146 const struct repoinfo *a = ap;
147 const struct repoinfo *b = bp;
148 return strcmp(a->alias, b->alias);
152 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
155 char buf2[4096], *kp, *vp, *kpe;
160 struct repoinfo *repoinfos = 0, *cinfo;
163 rdlen = strlen(reposdir);
164 dir = opendir(reposdir);
170 while ((ent = readdir(dir)) != 0)
172 l = strlen(ent->d_name);
173 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
175 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
176 if ((fp = fopen(buf, "r")) == 0)
182 while(fgets(buf2, sizeof(buf2), fp))
187 while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
190 while (*kp == ' ' || *kp == '\t')
192 if (!*kp || *kp == '#')
195 kp = yum_substitute(pool, kp);
198 vp = strrchr(kp, ']');
202 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
203 cinfo = repoinfos + nrepoinfos++;
204 memset(cinfo, 0, sizeof(*cinfo));
205 cinfo->alias = strdup(kp + 1);
206 cinfo->type = TYPE_RPMMD;
207 cinfo->autorefresh = 1;
212 vp = strchr(kp, '=');
215 for (kpe = vp - 1; kpe >= kp; kpe--)
216 if (*kpe != ' ' && *kpe != '\t')
221 while (*vp == ' ' || *vp == '\t')
224 if (!strcmp(kp, "name"))
225 cinfo->name = strdup(vp);
226 else if (!strcmp(kp, "enabled"))
227 cinfo->enabled = *vp == '0' ? 0 : 1;
228 else if (!strcmp(kp, "autorefresh"))
229 cinfo->autorefresh = *vp == '0' ? 0 : 1;
230 else if (!strcmp(kp, "gpgcheck"))
231 cinfo->gpgcheck = *vp == '0' ? 0 : 1;
232 else if (!strcmp(kp, "baseurl"))
233 cinfo->baseurl = strdup(vp);
234 else if (!strcmp(kp, "mirrorlist"))
236 if (strstr(vp, "metalink"))
237 cinfo->metalink = strdup(vp);
239 else if (!strcmp(kp, "path"))
241 if (vp && strcmp(vp, "/") != 0)
242 cinfo->path = strdup(vp);
244 else if (!strcmp(kp, "type"))
246 if (!strcmp(vp, "yast2"))
247 cinfo->type = TYPE_SUSETAGS;
248 else if (!strcmp(vp, "rpm-md"))
249 cinfo->type = TYPE_RPMMD;
250 else if (!strcmp(vp, "plaindir"))
251 cinfo->type = TYPE_PLAINDIR;
253 cinfo->type = TYPE_UNKNOWN;
255 else if (!strcmp(kp, "priority"))
256 cinfo->priority = atoi(vp);
257 else if (!strcmp(kp, "keeppackages"))
258 cinfo->keeppackages = *vp == '0' ? 0 : 1;
264 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
265 *nrepoinfosp = nrepoinfos;
270 cookie_gzread(void *cookie, char *buf, size_t nbytes)
272 return gzread((gzFile *)cookie, buf, nbytes);
276 cookie_gzclose(void *cookie)
278 return gzclose((gzFile *)cookie);
287 strcpy(tmpl, "/var/tmp/solvXXXXXX");
299 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
306 h = sat_chksum_create(chksumtype);
309 printf("%s: unknown checksum type\n", file);
312 while ((l = read(fd, buf, sizeof(buf))) > 0)
313 sat_chksum_add(h, buf, l);
314 lseek(fd, 0, SEEK_SET);
316 sum = sat_chksum_get(h, &l);
317 if (memcmp(sum, chksum, l))
319 printf("%s: checksum mismatch\n", file);
326 findmetalinkurl(FILE *fp)
328 char buf[4096], *bp, *ep;
329 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
331 while (*bp == ' ' || *bp == '\t')
333 if (strncmp(bp, "<url", 4))
335 bp = strchr(bp, '>');
339 ep = strstr(bp, "repodata/repomd.xml</url>");
343 if (strncmp(bp, "http", 4))
351 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype)
357 const char *baseurl = cinfo->baseurl;
361 if (!cinfo->metalink)
363 if (file != cinfo->metalink)
365 FILE *fp = curlfopen(cinfo, cinfo->metalink, 0, 0, 0);
368 cinfo->baseurl = findmetalinkurl(fp);
372 return curlfopen(cinfo, file, uncompress, chksum, chksumtype);
374 snprintf(url, sizeof(url), "%s", file);
379 if (l && baseurl[l - 1] == '/')
380 snprintf(url, sizeof(url), "%s%s", baseurl, file);
382 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
385 if ((pid = fork()) == (pid_t)-1)
397 execlp("curl", "curl", "-f", "-s", "-L", url, (char *)0);
401 while (waitpid(pid, &status, 0) != pid)
403 if (lseek(fd, 0, SEEK_END) == 0)
409 lseek(fd, 0, SEEK_SET);
410 if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
418 cookie_io_functions_t cio;
421 sprintf(tmpl, "/dev/fd/%d", fd);
422 gzf = gzopen(tmpl, "r");
426 memset(&cio, 0, sizeof(cio));
427 cio.read = cookie_gzread;
428 cio.close = cookie_gzclose;
429 return fopencookie(gzf, "r", cio);
431 fcntl(fd, F_SETFD, FD_CLOEXEC);
432 return fdopen(fd, "r");
436 cleanupgpg(char *gpgdir)
439 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
441 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
443 snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
445 snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
447 snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
453 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
462 off_t posfp, possigfp;
465 gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
468 keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
469 if (!(kfp = fopen(keysfile, "w")) )
475 for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
479 pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
480 if (!pubkey || !*pubkey)
482 if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
484 if (fputc('\n', kfp) == EOF) /* Just in case... */
488 if (fclose(kfp) || !nkeys)
493 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --import %s", gpgdir, keysfile);
496 fprintf(stderr, "key import error\n");
501 posfp = lseek(fileno(fp), 0, SEEK_CUR);
502 lseek(fileno(fp), 0, SEEK_SET);
503 possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
504 lseek(fileno(sigfp), 0, SEEK_SET);
505 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
506 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
507 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
509 lseek(fileno(sigfp), possigfp, SEEK_SET);
510 lseek(fileno(fp), posfp, SEEK_SET);
511 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
512 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
514 return r == 0 ? 1 : 0;
517 #define CHKSUM_IDENT "1.1"
520 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
523 void *h = sat_chksum_create(chktype);
526 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
527 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
528 sat_chksum_add(h, buf, l);
530 sat_chksum_free(h, out);
534 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
536 void *h = sat_chksum_create(chktype);
537 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
538 sat_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
539 sat_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
540 sat_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
541 sat_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
542 sat_chksum_free(h, out);
554 pool_setarch(pool, un.machine);
557 char *calccachepath(Repo *repo)
559 char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
560 p = pool_tmpjoin(repo->pool, p, ".solv", 0);
561 q = p + strlen(SOLVCACHE_PATH) + 1;
571 usecachedrepo(Repo *repo, unsigned char *cookie)
574 unsigned char mycookie[32];
576 if (!(fp = fopen(calccachepath(repo), "r")))
578 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
583 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
589 if (repo_add_solv(repo, fp))
599 writecachedrepo(Repo *repo, unsigned char *cookie)
601 Id *addedfileprovides = 0;
607 mkdir(SOLVCACHE_PATH, 0755);
608 tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
616 if (!(fp = fdopen(fd, "w")))
623 info = repo_add_repodata(repo, 0);
624 pool_addfileprovides_ids(repo->pool, 0, &addedfileprovides);
625 if (addedfileprovides && *addedfileprovides)
627 for (i = 0; addedfileprovides[i]; i++)
628 repodata_add_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[i]);
630 sat_free(addedfileprovides);
631 repodata_internalize(info);
632 repo_write(repo, fp, 0, 0, 0);
633 repo_free_repodata(repo, info);
634 if (fwrite(cookie, 32, 1, fp) != 1)
647 if (!rename(tmpl, calccachepath(repo)))
655 Pool *sigpool = pool_create();
656 Repo *repo = repo_create(sigpool, "rpmdbkeys");
657 repo_add_rpmdb_pubkeys(repo, 0, 0);
662 iscompressed(const char *name)
664 int l = strlen(name);
665 return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
668 static inline const char *
669 findinrepomd(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
671 Pool *pool = repo->pool;
673 const char *filename;
678 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
679 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
680 if (dataiterator_step(&di))
682 dataiterator_setpos_parent(&di);
683 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
684 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
686 dataiterator_free(&di);
687 if (filename && !*chksumtypep)
689 printf("no %s file checksum!\n", what);
696 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
699 struct repoinfo *cinfo;
704 const char *filename;
705 const unsigned char *filechksum;
707 const char *descrdir;
710 unsigned char cookie[32];
713 repo = repo_create(pool, "@System");
714 printf("rpm database:");
715 if (stat("/var/lib/rpm/Packages", &stb))
716 memset(&stb, 0, sizeof(&stb));
717 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cookie);
718 if (usecachedrepo(repo, cookie))
723 printf(" reading\n");
727 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
729 if ((ofp = fopen(calccachepath(repo), "r")) != 0)
731 Repo *ref = repo_create(pool, "@System.old");
732 if (!repo_add_solv(ref, ofp))
734 repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
741 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
742 writecachedrepo(repo, cookie);
744 pool_set_installed(pool, repo);
746 for (i = 0; i < nrepoinfos; i++)
748 cinfo = repoinfos + i;
752 repo = repo_create(pool, cinfo->alias);
754 repo->appdata = cinfo;
755 repo->priority = 99 - cinfo->priority;
757 if (!cinfo->autorefresh && usecachedrepo(repo, 0))
759 printf("repo '%s':", cinfo->alias);
766 printf("rpmmd repo '%s':", cinfo->alias);
768 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0)) == 0)
770 printf(" no repomd.xml file, skipped\n");
775 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
776 if (usecachedrepo(repo, cookie))
782 sigfp = curlfopen(cinfo, "repodata/repomd.xml.asc", 0, 0, 0);
786 printf(" unsigned, skipped\n");
794 sigpool = read_sigs();
795 if (!checksig(sigpool, fp, sigfp))
797 printf(" checksig failed, skipped\n");
804 repo_add_repomdxml(repo, fp, 0);
806 printf(" reading\n");
807 filename = findinrepomd(repo, "primary", &filechksum, &filechksumtype);
808 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype)) != 0)
810 repo_add_rpmmd(repo, fp, 0, 0);
814 filename = findinrepomd(repo, "updateinfo", &filechksum, &filechksumtype);
815 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype)) != 0)
817 repo_add_updateinfoxml(repo, fp, 0);
821 filename = findinrepomd(repo, "deltainfo", &filechksum, &filechksumtype);
823 filename = findinrepomd(repo, "prestodelta", &filechksum, &filechksumtype);
824 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype)) != 0)
826 repo_add_deltainfoxml(repo, fp, 0);
830 writecachedrepo(repo, cookie);
834 printf("susetags repo '%s':", cinfo->alias);
836 repo = repo_create(pool, cinfo->alias);
838 repo->appdata = cinfo;
839 repo->priority = 99 - cinfo->priority;
842 if ((fp = curlfopen(cinfo, "content", 0, 0, 0)) == 0)
844 printf(" no content file, skipped\n");
849 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
850 if (usecachedrepo(repo, cookie))
856 sigfp = curlfopen(cinfo, "content.asc", 0, 0, 0);
859 printf(" unsigned, skipped\n");
866 sigpool = read_sigs();
867 if (!checksig(sigpool, fp, sigfp))
869 printf(" checksig failed, skipped\n");
876 repo_add_content(repo, fp, 0);
878 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
879 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
881 descrdir = "suse/setup/descr";
883 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, "packages.gz", SEARCH_STRING);
884 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
885 if (dataiterator_step(&di))
887 dataiterator_setpos_parent(&di);
888 filechksum = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
889 filename = "packages.gz";
891 dataiterator_free(&di);
894 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, "packages", SEARCH_STRING);
895 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
896 if (dataiterator_step(&di))
898 dataiterator_setpos_parent(&di);
899 filechksum = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
900 filename = "packages";
902 dataiterator_free(&di);
906 printf(" no packages file entry, skipped\n");
911 printf(" no packages file checksum, skipped\n");
914 printf(" reading\n");
915 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype)) == 0)
917 repo_add_susetags(repo, fp, defvendor, 0, 0);
919 writecachedrepo(repo, cookie);
922 printf("unsupported repo '%s': skipped\n", cinfo->alias);
933 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
939 id = str2id(pool, arg, 0);
942 FOR_PROVIDES(p, pp, id)
944 Solvable *s = pool_id2solvable(pool, p);
947 type = SOLVER_SOLVABLE_NAME;
950 type = SOLVER_SOLVABLE_PROVIDES;
955 /* did not find a solvable, see if it's a relation */
956 if ((r = strpbrk(arg, "<=>")) != 0)
960 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
962 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
974 while (*r == ' ' || *r == '\t')
976 revr = *r ? str2id(pool, r, 1) : 0;
977 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
980 FOR_PROVIDES(p, pp, rid)
982 Solvable *s = pool_id2solvable(pool, p);
983 if (pool_match_nevr(pool, s, rid))
985 type = SOLVER_SOLVABLE_NAME;
988 type = SOLVER_SOLVABLE_PROVIDES;
997 queue_push(out, type);
1003 yesno(const char *str)
1005 char inbuf[128], *ip;
1012 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
1017 while (*ip == ' ' || *ip == '\t')
1024 if (*ip == 'y' || *ip == 'n')
1025 return *ip == 'y' ? 1 : 0;
1037 fc_cb(Pool *pool, Id p, void *cbdata)
1039 struct fcstate *fcstate = cbdata;
1047 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
1050 s = pool_id2solvable(pool, p);
1051 if (pool->installed && s->repo == pool->installed)
1053 if (!s->repo->rpmdbid)
1055 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
1058 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
1060 for (i = 0; i < fcstate->newpkgscnt; i++)
1061 if (fcstate->checkq->elements[i] == p)
1063 if (i == fcstate->newpkgscnt)
1065 fp = fcstate->newpkgsfps[i];
1069 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
1073 runrpm(const char *arg, const char *name, int dupfd3)
1078 if ((pid = fork()) == (pid_t)-1)
1085 if (dupfd3 != -1 && dupfd3 != 3)
1091 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
1092 if (strcmp(arg, "-e") == 0)
1093 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1095 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1099 while (waitpid(pid, &status, 0) != pid)
1103 printf("rpm failed\n");
1109 nscallback(Pool *pool, void *data, Id name, Id evr)
1111 if (name == NAMESPACE_PRODUCTBUDDY)
1113 /* SUSE specific hack: each product has an associated rpm */
1114 Solvable *s = pool->solvables + evr;
1117 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
1120 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
1123 FOR_PROVIDES(p, pp, cap)
1125 Solvable *ps = pool->solvables + p;
1126 if (ps->repo == s->repo && ps->arch == s->arch)
1136 main(int argc, char **argv)
1140 struct repoinfo *repoinfos;
1142 int i, mode, newpkgs;
1146 char inbuf[128], *ip;
1148 int distupgrade = 0;
1151 struct fcstate fcstate;
1157 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
1160 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
1161 mode = SOLVER_INSTALL;
1162 else if (!strcmp(argv[0], "patch"))
1164 mode = SOLVER_UPDATE;
1167 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
1168 mode = SOLVER_ERASE;
1169 else if (!strcmp(argv[0], "show"))
1171 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
1172 mode = SOLVER_UPDATE;
1173 else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
1175 mode = SOLVER_UPDATE;
1180 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
1184 pool = pool_create();
1185 pool->nscallback = nscallback;
1186 // pool_setdebuglevel(pool, 2);
1188 repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
1189 read_repos(pool, repoinfos, nrepoinfos);
1190 // FOR_REPOS(i, repo)
1191 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
1192 pool_addfileprovides(pool);
1193 pool_createwhatprovides(pool);
1196 for (i = 1; i < argc; i++)
1197 mkselect(pool, argv[i], 0, &job);
1198 if (!job.count && mode == SOLVER_UPDATE)
1200 else if (!job.count)
1202 printf("no package matched\n");
1208 /* show mode, no solver needed */
1209 for (i = 0; i < job.count; i += 2)
1211 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
1213 Solvable *s = pool_id2solvable(pool, p);
1214 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
1220 if (updateall && patchmode)
1226 map_init(&installedmap, pool->nsolvables);
1227 if (pool->installed)
1228 FOR_REPO_SOLVABLES(pool->installed, p, s)
1229 MAPSET(&installedmap, p);
1231 /* install all patches */
1233 mode = SOLVER_INSTALL;
1234 for (p = 1; p < pool->nsolvables; p++)
1240 s = pool->solvables + p;
1241 if (strncmp(id2str(pool, s->name), "patch:", 6) != 0)
1243 FOR_PROVIDES(p2, pp, s->name)
1245 Solvable *s2 = pool->solvables + p2;
1246 if (s2->name != s->name)
1248 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
1249 if (r < 0 || (r == 0 && p > p2))
1254 type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
1255 if (type && !strcmp(type, "optional"))
1257 r = solvable_trivial_installable_map(s, &installedmap, 0);
1260 if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
1267 queue_push2(&job, SOLVER_SOLVABLE, p);
1269 map_free(&installedmap);
1273 for (i = 0; i < job.count; i += 2)
1274 job.elements[i] |= mode;
1276 // multiversion test
1277 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
1278 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
1279 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
1284 Id problem, solution;
1287 solv = solver_create(pool);
1288 solv->ignorealreadyrecommended = 1;
1289 solv->updatesystem = updateall;
1290 solv->dosplitprovides = updateall;
1291 if (updateall && distupgrade)
1293 solv->distupgrade = 1;
1294 solv->allowdowngrade = 1;
1295 solv->allowarchchange = 1;
1296 solv->allowvendorchange = 1;
1298 // queue_push2(&job, SOLVER_DISTUPGRADE, 3);
1299 solver_solve(solv, &job);
1300 if (!solv->problems.count)
1302 pcnt = solver_problem_count(solv);
1303 printf("Found %d problems:\n", pcnt);
1304 for (problem = 1; problem <= pcnt; problem++)
1307 printf("Problem %d:\n", problem);
1308 solver_printprobleminfo(solv, problem);
1310 scnt = solver_solution_count(solv, problem);
1311 for (solution = 1; solution <= scnt; solution++)
1313 printf("Solution %d:\n", solution);
1314 solver_printsolution(solv, problem, solution);
1319 printf("Please choose a solution: ");
1322 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
1327 while (*ip == ' ' || *ip == '\t')
1329 if (*ip >= '0' && *ip <= '9')
1332 if (take >= 1 && take <= scnt)
1348 solver_take_solution(solv, problem, take, &job);
1353 if (!solv->trans.steps.count)
1355 printf("Nothing to do.\n");
1359 printf("Transaction summary:\n\n");
1360 solver_printtransaction(solv);
1361 if (!yesno("OK to continue (y/n)? "))
1367 trans = &solv->trans;
1368 queue_init(&checkq);
1369 newpkgs = transaction_installedresult(trans, &checkq);
1374 printf("Downloading %d packages\n", newpkgs);
1375 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
1376 for (i = 0; i < newpkgs; i++)
1378 unsigned int medianr;
1381 struct repoinfo *cinfo;
1382 const unsigned char *chksum;
1386 p = checkq.elements[i];
1387 s = pool_id2solvable(pool, p);
1388 cinfo = s->repo->appdata;
1391 printf("%s: no repository information\n", s->repo->name);
1394 loc = solvable_get_location(s, &medianr);
1398 if (pool->installed && pool->installed->nsolvables)
1400 /* try a delta first */
1401 dataiterator_init(&di, pool, 0, SOLVID_META, DELTA_PACKAGE_NAME, id2str(pool, s->name), SEARCH_STRING);
1402 dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
1403 while (dataiterator_step(&di))
1407 dataiterator_setpos_parent(&di);
1408 if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
1409 pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
1411 baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
1412 FOR_PROVIDES(op, pp, s->name)
1414 Solvable *os = pool->solvables + op;
1415 if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
1420 /* base is installed, run sequence check */
1421 const char *seqname;
1430 seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
1431 seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
1432 seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
1433 seq = pool_tmpjoin(pool, seqname, "-", seqevr);
1434 seq = pool_tmpjoin(pool, seq, "-", seqnum);
1435 if (system(pool_tmpjoin(pool, "applydeltarpm -c -s ", seq, 0)) != 0)
1436 continue; /* didn't match */
1437 /* looks good, download delta */
1439 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
1441 continue; /* no way! */
1442 dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
1443 dloc = pool_tmpjoin(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
1444 dloc = pool_tmpjoin(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
1445 dloc = pool_tmpjoin(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
1446 if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype)) == 0)
1448 /* got it, now reconstruct */
1449 newfd = opentmpfile();
1450 sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
1451 fcntl(fileno(fp), F_SETFD, 0);
1458 lseek(newfd, 0, SEEK_SET);
1460 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
1461 if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
1467 newpkgsfps[i] = fdopen(newfd, "r");
1472 dataiterator_free(&di);
1479 continue; /* delta worked! */
1481 if (cinfo->type == TYPE_SUSETAGS)
1483 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
1484 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
1487 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
1488 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype)) == 0)
1490 printf("\n%s: %s not found in repository\n", s->repo->name, loc);
1503 printf("Searching for file conflicts\n");
1504 queue_init(&conflicts);
1505 fcstate.rpmdbstate = 0;
1506 fcstate.newpkgscnt = newpkgs;
1507 fcstate.checkq = &checkq;
1508 fcstate.newpkgsfps = newpkgsfps;
1509 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
1510 if (conflicts.count)
1513 for (i = 0; i < conflicts.count; i += 5)
1514 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]));
1516 if (yesno("Re-run solver (y/n/q)? "))
1518 for (i = 0; i < newpkgs; i++)
1520 fclose(newpkgsfps[i]);
1521 newpkgsfps = sat_free(newpkgsfps);
1523 pool_add_fileconflicts_deps(pool, &conflicts);
1524 pool_createwhatprovides(pool); /* Hmm... */
1528 queue_free(&conflicts);
1531 printf("Committing transaction:\n\n");
1532 transaction_order(trans, 0);
1533 for (i = 0; i < trans->steps.count; i++)
1535 const char *evr, *evrp, *nvra;
1540 p = trans->steps.elements[i];
1541 s = pool_id2solvable(pool, p);
1542 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
1545 case SOLVER_TRANSACTION_ERASE:
1546 printf("erase %s\n", solvid2str(pool, p));
1547 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
1549 /* strip epoch from evr */
1550 evr = evrp = id2str(pool, s->evr);
1551 while (*evrp >= '0' && *evrp <= '9')
1553 if (evrp > evr && evrp[0] == ':' && evrp[1])
1555 nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
1556 nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
1557 runrpm("-e", nvra, -1); /* to bad that --querybynumber doesn't work */
1559 case SOLVER_TRANSACTION_INSTALL:
1560 case SOLVER_TRANSACTION_MULTIINSTALL:
1561 printf("install %s\n", solvid2str(pool, p));
1562 for (j = 0; j < newpkgs; j++)
1563 if (checkq.elements[j] == p)
1565 fp = j < newpkgs ? newpkgsfps[j] : 0;
1569 lseek(fileno(fp), 0, SEEK_SET);
1570 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
1579 for (i = 0; i < newpkgs; i++)
1581 fclose(newpkgsfps[i]);
1582 sat_free(newpkgsfps);
1583 queue_free(&checkq);