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"
76 unsigned char extcookie[32];
81 yum_substitute(Pool *pool, char *line)
86 while ((p2 = strchr(p, '$')) != 0)
88 if (!strncmp(p2, "$releasever", 11))
90 static char *releaseevr;
96 fp = popen("rpm --nodigest --nosignature -q --qf '%{VERSION}\n' --whatprovides redhat-release", "r");
97 fread(buf, 1, sizeof(buf), fp);
100 while (*bp != ' ' && *bp != '\t' && *bp != '\n')
103 releaseevr = strdup(buf);
106 p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
107 p2 = p + (p2 - line);
109 p = p2 + strlen(releaseevr);
112 if (!strncmp(p2, "$basearch", 9))
114 static char *basearch;
123 basearch = strdup(un.machine);
124 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
128 p = pool_tmpjoin(pool, line, basearch, p2 + 9);
129 p2 = p + (p2 - line);
131 p = p2 + strlen(basearch);
140 #define TYPE_UNKNOWN 0
141 #define TYPE_SUSETAGS 1
143 #define TYPE_PLAINDIR 3
146 read_repoinfos_sort(const void *ap, const void *bp)
148 const struct repoinfo *a = ap;
149 const struct repoinfo *b = bp;
150 return strcmp(a->alias, b->alias);
154 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
157 char buf2[4096], *kp, *vp, *kpe;
162 struct repoinfo *repoinfos = 0, *cinfo;
165 rdlen = strlen(reposdir);
166 dir = opendir(reposdir);
172 while ((ent = readdir(dir)) != 0)
174 l = strlen(ent->d_name);
175 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
177 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
178 if ((fp = fopen(buf, "r")) == 0)
184 while(fgets(buf2, sizeof(buf2), fp))
189 while (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t')
192 while (*kp == ' ' || *kp == '\t')
194 if (!*kp || *kp == '#')
198 kp = yum_substitute(pool, kp);
202 vp = strrchr(kp, ']');
206 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
207 cinfo = repoinfos + nrepoinfos++;
208 memset(cinfo, 0, sizeof(*cinfo));
209 cinfo->alias = strdup(kp + 1);
210 cinfo->type = TYPE_RPMMD;
211 cinfo->autorefresh = 1;
216 vp = strchr(kp, '=');
219 for (kpe = vp - 1; kpe >= kp; kpe--)
220 if (*kpe != ' ' && *kpe != '\t')
225 while (*vp == ' ' || *vp == '\t')
228 if (!strcmp(kp, "name"))
229 cinfo->name = strdup(vp);
230 else if (!strcmp(kp, "enabled"))
231 cinfo->enabled = *vp == '0' ? 0 : 1;
232 else if (!strcmp(kp, "autorefresh"))
233 cinfo->autorefresh = *vp == '0' ? 0 : 1;
234 else if (!strcmp(kp, "gpgcheck"))
235 cinfo->gpgcheck = *vp == '0' ? 0 : 1;
236 else if (!strcmp(kp, "baseurl"))
237 cinfo->baseurl = strdup(vp);
238 else if (!strcmp(kp, "mirrorlist"))
240 if (strstr(vp, "metalink"))
241 cinfo->metalink = strdup(vp);
243 else if (!strcmp(kp, "path"))
245 if (vp && strcmp(vp, "/") != 0)
246 cinfo->path = strdup(vp);
248 else if (!strcmp(kp, "type"))
250 if (!strcmp(vp, "yast2"))
251 cinfo->type = TYPE_SUSETAGS;
252 else if (!strcmp(vp, "rpm-md"))
253 cinfo->type = TYPE_RPMMD;
254 else if (!strcmp(vp, "plaindir"))
255 cinfo->type = TYPE_PLAINDIR;
257 cinfo->type = TYPE_UNKNOWN;
259 else if (!strcmp(kp, "priority"))
260 cinfo->priority = atoi(vp);
261 else if (!strcmp(kp, "keeppackages"))
262 cinfo->keeppackages = *vp == '0' ? 0 : 1;
268 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
269 *nrepoinfosp = nrepoinfos;
274 cookie_gzread(void *cookie, char *buf, size_t nbytes)
276 return gzread((gzFile *)cookie, buf, nbytes);
280 cookie_gzclose(void *cookie)
282 return gzclose((gzFile *)cookie);
291 strcpy(tmpl, "/var/tmp/solvXXXXXX");
303 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
310 h = sat_chksum_create(chksumtype);
313 printf("%s: unknown checksum type\n", file);
316 while ((l = read(fd, buf, sizeof(buf))) > 0)
317 sat_chksum_add(h, buf, l);
318 lseek(fd, 0, SEEK_SET);
320 sum = sat_chksum_get(h, &l);
321 if (memcmp(sum, chksum, l))
323 printf("%s: checksum mismatch\n", file);
330 findmetalinkurl(FILE *fp)
332 char buf[4096], *bp, *ep;
333 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
335 while (*bp == ' ' || *bp == '\t')
337 if (strncmp(bp, "<url", 4))
339 bp = strchr(bp, '>');
343 ep = strstr(bp, "repodata/repomd.xml</url>");
347 if (strncmp(bp, "http", 4))
355 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype)
361 const char *baseurl = cinfo->baseurl;
365 if (!cinfo->metalink)
367 if (file != cinfo->metalink)
369 FILE *fp = curlfopen(cinfo, cinfo->metalink, 0, 0, 0);
372 cinfo->baseurl = findmetalinkurl(fp);
376 return curlfopen(cinfo, file, uncompress, chksum, chksumtype);
378 snprintf(url, sizeof(url), "%s", file);
383 if (l && baseurl[l - 1] == '/')
384 snprintf(url, sizeof(url), "%s%s", baseurl, file);
386 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
389 if ((pid = fork()) == (pid_t)-1)
401 execlp("curl", "curl", "-f", "-s", "-L", url, (char *)0);
405 while (waitpid(pid, &status, 0) != pid)
407 if (lseek(fd, 0, SEEK_END) == 0)
413 lseek(fd, 0, SEEK_SET);
414 if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
422 cookie_io_functions_t cio;
425 sprintf(tmpl, "/dev/fd/%d", fd);
426 gzf = gzopen(tmpl, "r");
430 memset(&cio, 0, sizeof(cio));
431 cio.read = cookie_gzread;
432 cio.close = cookie_gzclose;
433 return fopencookie(gzf, "r", cio);
435 fcntl(fd, F_SETFD, FD_CLOEXEC);
436 return fdopen(fd, "r");
440 cleanupgpg(char *gpgdir)
443 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
445 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
447 snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
449 snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
451 snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
457 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
466 off_t posfp, possigfp;
469 gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
472 keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
473 if (!(kfp = fopen(keysfile, "w")) )
479 for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
483 pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
484 if (!pubkey || !*pubkey)
486 if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
488 if (fputc('\n', kfp) == EOF) /* Just in case... */
492 if (fclose(kfp) || !nkeys)
497 snprintf(cmd, sizeof(cmd), "gpg2 -q --homedir %s --import %s", gpgdir, keysfile);
500 fprintf(stderr, "key import error\n");
505 posfp = lseek(fileno(fp), 0, SEEK_CUR);
506 lseek(fileno(fp), 0, SEEK_SET);
507 possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
508 lseek(fileno(sigfp), 0, SEEK_SET);
509 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
510 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
511 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
513 lseek(fileno(sigfp), possigfp, SEEK_SET);
514 lseek(fileno(fp), posfp, SEEK_SET);
515 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
516 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
518 return r == 0 ? 1 : 0;
521 #define CHKSUM_IDENT "1.1"
524 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
527 void *h = sat_chksum_create(chktype);
530 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
531 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
532 sat_chksum_add(h, buf, l);
534 sat_chksum_free(h, out);
538 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
540 void *h = sat_chksum_create(chktype);
541 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
542 sat_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
543 sat_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
544 sat_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
545 sat_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
546 sat_chksum_free(h, out);
558 pool_setarch(pool, un.machine);
561 char *calccachepath(Repo *repo, const char *repoext)
563 char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
566 p = pool_tmpjoin(repo->pool, p, "_", repoext);
567 p = pool_tmpjoin(repo->pool, p, ".solvx", 0);
570 p = pool_tmpjoin(repo->pool, p, ".solv", 0);
571 q = p + strlen(SOLVCACHE_PATH) + 1;
581 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie)
584 unsigned char mycookie[32];
585 struct repoinfo *cinfo;
588 cinfo = repo->appdata;
589 if (!(fp = fopen(calccachepath(repo, repoext), "r")))
591 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
596 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
602 if (repoext && !strcmp(repoext, "DL"))
603 flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
605 flags = REPO_USE_LOADING|REPO_LOCALPOOL|REPO_EXTEND_SOLVABLES;
607 if (repo_add_solv_flags(repo, fp, flags))
612 if (!repoext && cinfo)
615 if (!fstat(fileno(fp), &stb))
616 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
623 myrepodatafilter(Repo *repo, Repokey *key, void *kfdata)
625 Repodata *data = kfdata;
627 if (key->name == 1 && key->size != data - repo->repodata)
629 if (key->storage == KEY_STORAGE_SOLVABLE)
630 return KEY_STORAGE_DROPPED;
631 return repo_write_stdkeyfilter(repo, key, kfdata);
635 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
637 Id *addedfileprovides = 0;
642 struct repoinfo *cinfo;
644 cinfo = repo->appdata;
645 mkdir(SOLVCACHE_PATH, 0755);
646 tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
654 if (!(fp = fdopen(fd, "w")))
665 info = repo_add_repodata(repo, 0);
668 pool_addfileprovides_ids(repo->pool, 0, &addedfileprovides);
669 if (addedfileprovides && *addedfileprovides)
671 for (i = 0; addedfileprovides[i]; i++)
672 repodata_add_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[i]);
674 sat_free(addedfileprovides);
675 repodata_internalize(info);
676 repo_write(repo, fp, repo_write_stdkeyfilter, 0, 0);
679 repo_write(repo, fp, myrepodatafilter, info, 0);
682 if (fwrite(cookie, 32, 1, fp) != 1)
695 if (!repoext && cinfo)
698 if (!stat(tmpl, &stb))
699 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
701 if (!rename(tmpl, calccachepath(repo, repoext)))
709 Pool *sigpool = pool_create();
710 Repo *repo = repo_create(sigpool, "rpmdbkeys");
711 repo_add_rpmdb_pubkeys(repo, 0, 0);
716 iscompressed(const char *name)
718 int l = strlen(name);
719 return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
722 static inline const char *
723 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
725 Pool *pool = repo->pool;
727 const char *filename;
732 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
733 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
734 if (dataiterator_step(&di))
736 dataiterator_setpos_parent(&di);
737 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
738 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
740 dataiterator_free(&di);
741 if (filename && !*chksumtypep)
743 printf("no %s file checksum!\n", what);
750 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
752 Pool *pool = repo->pool;
754 Id chksumtype, handle;
755 const unsigned char *chksum;
756 const char *filename;
758 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
759 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
760 if (!dataiterator_step(&di))
762 dataiterator_free(&di);
765 if (!strcmp(what, "prestodelta"))
767 dataiterator_setpos_parent(&di);
768 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
769 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, &chksumtype);
770 if (!filename || !chksum)
772 dataiterator_free(&di);
775 handle = repodata_new_handle(data);
776 repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
777 repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
779 repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
780 if (!strcmp(what, "deltainfo"))
782 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
783 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
784 repodata_add_idarray(data, handle, REPOSITORY_KEYS, DELTA_PACKAGE_NAME);
785 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_ID);
786 repodata_add_idarray(data, handle, REPOSITORY_KEYS, DELTA_PACKAGE_EVR);
787 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_ID);
788 repodata_add_idarray(data, handle, REPOSITORY_KEYS, DELTA_PACKAGE_ARCH);
789 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_ID);
790 repodata_add_idarray(data, handle, REPOSITORY_KEYS, DELTA_BASE_EVR);
791 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_ID);
793 if (!strcmp(what, "filelists"))
795 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
796 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
798 dataiterator_free(&di);
799 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
803 static inline const char *
804 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
806 Pool *pool = repo->pool;
808 const char *filename;
813 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
814 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
815 if (dataiterator_step(&di))
817 dataiterator_setpos_parent(&di);
818 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
821 dataiterator_free(&di);
822 if (filename && !*chksumtypep)
824 printf("no %s file checksum!\n", what);
832 load_stub(Pool *pool, Repodata *data, void *dp)
834 const char *filename, *descrdir, *repomdtype;
835 const unsigned char *filechksum;
837 struct repoinfo *cinfo;
842 cinfo = data->repo->appdata;
844 filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
848 ext[0] = filename[9];
849 ext[1] = filename[10];
852 printf("[%s:%s", data->repo->name, ext);
854 if (usecachedrepo(data->repo, ext, cinfo->extcookie))
856 printf(" cached]"); fflush(stdout);
860 printf(" loading]"); fflush(stdout);
862 defvendor = repo_lookup_id(data->repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
863 descrdir = repo_lookup_str(data->repo, SOLVID_META, SUSETAGS_DESCRDIR);
865 descrdir = "suse/setup/descr";
867 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
868 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype)) == 0)
870 repo_add_susetags(data->repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
872 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
876 repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
879 if (!strcmp(repomdtype, "filelists"))
881 else if (!strcmp(repomdtype, "deltainfo"))
886 printf("[%s:%s", data->repo->name, ext);
888 if (usecachedrepo(data->repo, ext, cinfo->extcookie))
890 printf(" cached]");fflush(stdout);
893 printf(" loading]"); fflush(stdout);
894 filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
896 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
897 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype)) == 0)
899 if (!strcmp(ext, "FL"))
900 repo_add_rpmmd(data->repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
901 else if (!strcmp(ext, "DL"))
902 repo_add_deltainfoxml(data->repo, fp, REPO_USE_LOADING);
904 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
911 Id susetags_langtags[] = {
912 SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
913 SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
914 SOLVABLE_EULA, REPOKEY_TYPE_STR,
915 SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
916 SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
917 SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
922 susetags_add_ext(Repo *repo, Repodata *data)
924 Pool *pool = repo->pool;
927 Id handle, filechksumtype;
928 const unsigned char *filechksum;
931 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
932 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
933 while (dataiterator_step(&di))
935 if (strncmp(di.kv.str, "packages.", 9) != 0)
937 if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
939 ext[0] = di.kv.str[9];
940 ext[1] = di.kv.str[10];
942 if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
944 handle = repodata_new_handle(data);
945 repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
947 repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
948 if (!strcmp(ext, "DU"))
950 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
951 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
953 if (!strcmp(ext, "FL"))
955 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
956 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
960 for (i = 0; susetags_langtags[i]; i += 2)
962 repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
963 repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
966 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
968 dataiterator_free(&di);
972 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
975 struct repoinfo *cinfo;
979 const char *filename;
980 const unsigned char *filechksum;
982 const char *descrdir;
985 unsigned char cookie[32];
989 repo = repo_create(pool, "@System");
990 printf("rpm database:");
991 if (stat("/var/lib/rpm/Packages", &stb))
992 memset(&stb, 0, sizeof(&stb));
993 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cookie);
994 if (usecachedrepo(repo, 0, cookie))
999 printf(" reading\n");
1002 #ifdef PRODUCTS_PATH
1003 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
1005 if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1007 Repo *ref = repo_create(pool, "@System.old");
1008 if (!repo_add_solv(ref, ofp))
1010 repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
1017 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
1018 writecachedrepo(repo, 0, 0, cookie);
1020 pool_set_installed(pool, repo);
1022 for (i = 0; i < nrepoinfos; i++)
1024 cinfo = repoinfos + i;
1025 if (!cinfo->enabled)
1028 repo = repo_create(pool, cinfo->alias);
1030 repo->appdata = cinfo;
1031 repo->priority = 99 - cinfo->priority;
1033 if (!cinfo->autorefresh && usecachedrepo(repo, 0, 0))
1035 printf("repo '%s':", cinfo->alias);
1036 printf(" cached\n");
1039 switch (cinfo->type)
1042 printf("rpmmd repo '%s':", cinfo->alias);
1044 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0)) == 0)
1046 printf(" no repomd.xml file, skipped\n");
1051 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
1052 if (usecachedrepo(repo, 0, cookie))
1054 printf(" cached\n");
1058 sigfp = curlfopen(cinfo, "repodata/repomd.xml.asc", 0, 0, 0);
1062 printf(" unsigned, skipped\n");
1070 sigpool = read_sigs();
1071 if (!checksig(sigpool, fp, sigfp))
1073 printf(" checksig failed, skipped\n");
1080 repo_add_repomdxml(repo, fp, 0);
1082 printf(" reading\n");
1083 filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1084 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype)) != 0)
1086 repo_add_rpmmd(repo, fp, 0, 0);
1090 filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1091 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype)) != 0)
1093 repo_add_updateinfoxml(repo, fp, 0);
1097 data = repo_last_repodata(repo);
1098 if (!repomd_add_ext(repo, data, "deltainfo"))
1099 repomd_add_ext(repo, data, "prestodelta");
1100 repomd_add_ext(repo, data, "filelists");
1101 repodata_internalize(data);
1102 writecachedrepo(repo, data, 0, cookie);
1103 repodata_create_stubs(data);
1107 printf("susetags repo '%s':", cinfo->alias);
1111 if ((fp = curlfopen(cinfo, "content", 0, 0, 0)) == 0)
1113 printf(" no content file, skipped\n");
1118 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cookie);
1119 if (usecachedrepo(repo, 0, cookie))
1121 printf(" cached\n");
1125 sigfp = curlfopen(cinfo, "content.asc", 0, 0, 0);
1128 printf(" unsigned, skipped\n");
1135 sigpool = read_sigs();
1136 if (!checksig(sigpool, fp, sigfp))
1138 printf(" checksig failed, skipped\n");
1145 repo_add_content(repo, fp, 0);
1147 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1148 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1150 descrdir = "suse/setup/descr";
1151 filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1153 filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1156 printf(" no packages file entry, skipped\n");
1159 printf(" reading\n");
1160 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype)) == 0)
1162 repo_add_susetags(repo, fp, defvendor, 0, 0);
1164 data = repo_last_repodata(repo);
1165 susetags_add_ext(repo, data);
1166 repodata_internalize(data);
1167 writecachedrepo(repo, data, 0, cookie);
1168 repodata_create_stubs(data);
1171 printf("unsupported repo '%s': skipped\n", cinfo->alias);
1182 mkselect(Pool *pool, const char *arg, int flags, Queue *out)
1188 id = str2id(pool, arg, 0);
1191 FOR_PROVIDES(p, pp, id)
1193 Solvable *s = pool_id2solvable(pool, p);
1196 type = SOLVER_SOLVABLE_NAME;
1199 type = SOLVER_SOLVABLE_PROVIDES;
1204 /* did not find a solvable, see if it's a relation */
1205 if ((r = strpbrk(arg, "<=>")) != 0)
1207 Id rid, rname, revr;
1209 for (r2 = r; r2 > arg && (r2[-1] == ' ' || r2[-1] == '\t'); )
1211 rname = r2 > arg ? strn2id(pool, arg, r2 - arg, 1) : 0;
1223 while (*r == ' ' || *r == '\t')
1225 revr = *r ? str2id(pool, r, 1) : 0;
1226 rid = rname && revr ? rel2id(pool, rname, revr, rflags, 1) : 0;
1229 FOR_PROVIDES(p, pp, rid)
1231 Solvable *s = pool_id2solvable(pool, p);
1232 if (pool_match_nevr(pool, s, rid))
1234 type = SOLVER_SOLVABLE_NAME;
1237 type = SOLVER_SOLVABLE_PROVIDES;
1246 queue_push(out, type);
1247 queue_push(out, id);
1252 yesno(const char *str)
1254 char inbuf[128], *ip;
1261 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
1266 while (*ip == ' ' || *ip == '\t')
1273 if (*ip == 'y' || *ip == 'n')
1274 return *ip == 'y' ? 1 : 0;
1286 fc_cb(Pool *pool, Id p, void *cbdata)
1288 struct fcstate *fcstate = cbdata;
1296 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
1299 s = pool_id2solvable(pool, p);
1300 if (pool->installed && s->repo == pool->installed)
1302 if (!s->repo->rpmdbid)
1304 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
1307 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
1309 for (i = 0; i < fcstate->newpkgscnt; i++)
1310 if (fcstate->checkq->elements[i] == p)
1312 if (i == fcstate->newpkgscnt)
1314 fp = fcstate->newpkgsfps[i];
1318 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
1322 runrpm(const char *arg, const char *name, int dupfd3)
1327 if ((pid = fork()) == (pid_t)-1)
1334 if (dupfd3 != -1 && dupfd3 != 3)
1340 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
1341 if (strcmp(arg, "-e") == 0)
1342 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1344 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1348 while (waitpid(pid, &status, 0) != pid)
1352 printf("rpm failed\n");
1358 nscallback(Pool *pool, void *data, Id name, Id evr)
1360 if (name == NAMESPACE_PRODUCTBUDDY)
1362 /* SUSE specific hack: each product has an associated rpm */
1363 Solvable *s = pool->solvables + evr;
1366 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
1369 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
1372 FOR_PROVIDES(p, pp, cap)
1374 Solvable *ps = pool->solvables + p;
1375 if (ps->repo == s->repo && ps->arch == s->arch)
1385 main(int argc, char **argv)
1389 struct repoinfo *repoinfos;
1391 int i, mode, newpkgs;
1395 char inbuf[128], *ip;
1397 int distupgrade = 0;
1400 struct fcstate fcstate;
1406 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
1409 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
1410 mode = SOLVER_INSTALL;
1411 else if (!strcmp(argv[0], "patch"))
1413 mode = SOLVER_UPDATE;
1416 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
1417 mode = SOLVER_ERASE;
1418 else if (!strcmp(argv[0], "show"))
1420 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
1421 mode = SOLVER_UPDATE;
1422 else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
1424 mode = SOLVER_UPDATE;
1429 fprintf(stderr, "Usage: solv install|erase|update|show <select>\n");
1433 pool = pool_create();
1434 pool_setloadcallback(pool, load_stub, 0);
1435 pool->nscallback = nscallback;
1436 // pool_setdebuglevel(pool, 2);
1438 repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
1439 read_repos(pool, repoinfos, nrepoinfos);
1440 // FOR_REPOS(i, repo)
1441 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
1442 pool_addfileprovides(pool);
1443 pool_createwhatprovides(pool);
1446 for (i = 1; i < argc; i++)
1447 mkselect(pool, argv[i], 0, &job);
1448 if (!job.count && mode == SOLVER_UPDATE)
1450 else if (!job.count)
1452 printf("no package matched\n");
1458 /* show mode, no solver needed */
1459 for (i = 0; i < job.count; i += 2)
1461 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
1463 Solvable *s = pool_id2solvable(pool, p);
1464 const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de");
1465 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
1467 printf(" %s\n", sum);
1473 if (updateall && patchmode)
1479 map_init(&installedmap, pool->nsolvables);
1480 if (pool->installed)
1481 FOR_REPO_SOLVABLES(pool->installed, p, s)
1482 MAPSET(&installedmap, p);
1484 /* install all patches */
1486 mode = SOLVER_INSTALL;
1487 for (p = 1; p < pool->nsolvables; p++)
1493 s = pool->solvables + p;
1494 if (strncmp(id2str(pool, s->name), "patch:", 6) != 0)
1496 FOR_PROVIDES(p2, pp, s->name)
1498 Solvable *s2 = pool->solvables + p2;
1499 if (s2->name != s->name)
1501 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
1502 if (r < 0 || (r == 0 && p > p2))
1507 type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
1508 if (type && !strcmp(type, "optional"))
1510 r = solvable_trivial_installable_map(s, &installedmap, 0);
1513 if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
1520 queue_push2(&job, SOLVER_SOLVABLE, p);
1522 map_free(&installedmap);
1526 for (i = 0; i < job.count; i += 2)
1527 job.elements[i] |= mode;
1529 // multiversion test
1530 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
1531 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
1532 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
1537 Id problem, solution;
1540 solv = solver_create(pool);
1541 solv->ignorealreadyrecommended = 1;
1542 solv->updatesystem = updateall;
1543 solv->dosplitprovides = updateall;
1544 if (updateall && distupgrade)
1546 solv->distupgrade = 1;
1547 solv->allowdowngrade = 1;
1548 solv->allowarchchange = 1;
1549 solv->allowvendorchange = 1;
1551 // queue_push2(&job, SOLVER_DISTUPGRADE, 3);
1552 solver_solve(solv, &job);
1553 if (!solv->problems.count)
1555 pcnt = solver_problem_count(solv);
1556 printf("Found %d problems:\n", pcnt);
1557 for (problem = 1; problem <= pcnt; problem++)
1560 printf("Problem %d:\n", problem);
1561 solver_printprobleminfo(solv, problem);
1563 scnt = solver_solution_count(solv, problem);
1564 for (solution = 1; solution <= scnt; solution++)
1566 printf("Solution %d:\n", solution);
1567 solver_printsolution(solv, problem, solution);
1572 printf("Please choose a solution: ");
1575 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
1580 while (*ip == ' ' || *ip == '\t')
1582 if (*ip >= '0' && *ip <= '9')
1585 if (take >= 1 && take <= scnt)
1601 solver_take_solution(solv, problem, take, &job);
1607 trans = &solv->trans;
1608 if (!trans->steps.count)
1610 printf("Nothing to do.\n");
1614 printf("Transaction summary:\n\n");
1615 solver_printtransaction(solv);
1624 duc[1].path = "/usr/share/man";
1625 duc[2].path = "/sbin";
1626 duc[3].path = "/etc";
1627 transaction_calc_duchanges(trans, duc, 4);
1628 for (i = 0; i < 4; i++)
1629 printf("duchanges %s: %d K %d\n", duc[i].path, duc[i].kbytes, duc[i].files);
1630 printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
1634 if (!yesno("OK to continue (y/n)? "))
1640 queue_init(&checkq);
1641 newpkgs = transaction_installedresult(trans, &checkq);
1646 printf("Downloading %d packages\n", newpkgs);
1647 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
1648 for (i = 0; i < newpkgs; i++)
1650 unsigned int medianr;
1653 struct repoinfo *cinfo;
1654 const unsigned char *chksum;
1658 p = checkq.elements[i];
1659 s = pool_id2solvable(pool, p);
1660 cinfo = s->repo->appdata;
1663 printf("%s: no repository information\n", s->repo->name);
1666 loc = solvable_get_location(s, &medianr);
1670 if (pool->installed && pool->installed->nsolvables)
1672 /* try a delta first */
1673 dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, id2str(pool, s->name), SEARCH_STRING);
1674 dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
1675 while (dataiterator_step(&di))
1679 dataiterator_setpos_parent(&di);
1680 if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
1681 pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
1683 baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
1684 FOR_PROVIDES(op, pp, s->name)
1686 Solvable *os = pool->solvables + op;
1687 if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
1690 if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
1692 /* base is installed, run sequence check */
1693 const char *seqname;
1702 seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
1703 seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
1704 seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
1705 seq = pool_tmpjoin(pool, seqname, "-", seqevr);
1706 seq = pool_tmpjoin(pool, seq, "-", seqnum);
1707 if (system(pool_tmpjoin(pool, "/usr/bin/applydeltarpm -c -s ", seq, 0)) != 0)
1708 continue; /* didn't match */
1709 /* looks good, download delta */
1711 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
1713 continue; /* no way! */
1714 dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
1715 dloc = pool_tmpjoin(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
1716 dloc = pool_tmpjoin(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
1717 dloc = pool_tmpjoin(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
1718 if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype)) == 0)
1720 /* got it, now reconstruct */
1721 newfd = opentmpfile();
1722 sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
1723 fcntl(fileno(fp), F_SETFD, 0);
1730 lseek(newfd, 0, SEEK_SET);
1732 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
1733 if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
1739 newpkgsfps[i] = fdopen(newfd, "r");
1744 dataiterator_free(&di);
1751 continue; /* delta worked! */
1753 if (cinfo->type == TYPE_SUSETAGS)
1755 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
1756 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
1759 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
1760 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype)) == 0)
1762 printf("\n%s: %s not found in repository\n", s->repo->name, loc);
1775 printf("Searching for file conflicts\n");
1776 queue_init(&conflicts);
1777 fcstate.rpmdbstate = 0;
1778 fcstate.newpkgscnt = newpkgs;
1779 fcstate.checkq = &checkq;
1780 fcstate.newpkgsfps = newpkgsfps;
1781 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fc_cb, &fcstate);
1782 if (conflicts.count)
1785 for (i = 0; i < conflicts.count; i += 5)
1786 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]));
1788 if (yesno("Re-run solver (y/n/q)? "))
1790 for (i = 0; i < newpkgs; i++)
1792 fclose(newpkgsfps[i]);
1793 newpkgsfps = sat_free(newpkgsfps);
1795 pool_add_fileconflicts_deps(pool, &conflicts);
1796 pool_createwhatprovides(pool); /* Hmm... */
1800 queue_free(&conflicts);
1803 printf("Committing transaction:\n\n");
1804 transaction_order(trans, 0);
1805 for (i = 0; i < trans->steps.count; i++)
1807 const char *evr, *evrp, *nvra;
1812 p = trans->steps.elements[i];
1813 s = pool_id2solvable(pool, p);
1814 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
1817 case SOLVER_TRANSACTION_ERASE:
1818 printf("erase %s\n", solvid2str(pool, p));
1819 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
1821 /* strip epoch from evr */
1822 evr = evrp = id2str(pool, s->evr);
1823 while (*evrp >= '0' && *evrp <= '9')
1825 if (evrp > evr && evrp[0] == ':' && evrp[1])
1827 nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
1828 nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
1829 runrpm("-e", nvra, -1); /* to bad that --querybynumber doesn't work */
1831 case SOLVER_TRANSACTION_INSTALL:
1832 case SOLVER_TRANSACTION_MULTIINSTALL:
1833 printf("install %s\n", solvid2str(pool, p));
1834 for (j = 0; j < newpkgs; j++)
1835 if (checkq.elements[j] == p)
1837 fp = j < newpkgs ? newpkgsfps[j] : 0;
1841 lseek(fileno(fp), 0, SEEK_SET);
1842 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
1851 for (i = 0; i < newpkgs; i++)
1853 fclose(newpkgsfps[i]);
1854 sat_free(newpkgsfps);
1855 queue_free(&checkq);