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 */
10 /* things available in the library but missing from solv:
11 * - vendor policy loading
12 * - soft locks file handling
13 * - multi version handling
26 #include <sys/utsname.h>
27 #include <sys/types.h>
32 #include <sys/socket.h>
44 #include "solverdebug.h"
46 #include "repo_solv.h"
48 #include "repo_write.h"
49 #include "repo_rpmdb.h"
50 #include "repo_products.h"
51 #include "repo_rpmmd.h"
52 #include "repo_susetags.h"
53 #include "repo_repomdxml.h"
54 #include "repo_updateinfoxml.h"
55 #include "repo_deltainfoxml.h"
56 #include "repo_content.h"
57 #include "pool_fileconflicts.h"
61 # define REPOINFO_PATH "/etc/yum.repos.d"
63 # define REPOINFO_PATH "/etc/zypp/repos.d"
64 # define PRODUCTS_PATH "/etc/products.d"
65 # define SOFTLOCKS_PATH "/var/lib/zypp/SoftLocks"
68 #define SOLVCACHE_PATH "/var/cache/solv"
70 #define METADATA_EXPIRE (60 * 90)
89 unsigned char cookie[32];
90 unsigned char extcookie[32];
95 yum_substitute(Pool *pool, char *line)
100 while ((p2 = strchr(p, '$')) != 0)
102 if (!strncmp(p2, "$releasever", 11))
104 static char *releaseevr;
110 rpm_installedrpmdbids(0, "Providename", "redhat-release", &q);
113 void *handle, *state = 0;
115 handle = rpm_byrpmdbid(q.elements[0], 0, &state);
116 releaseevr = rpm_query(handle, SOLVABLE_EVR);
117 rpm_byrpmdbid(0, 0, &state);
118 if ((p = strchr(releaseevr, '-')) != 0)
123 releaseevr = strdup("?");
126 p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
127 p2 = p + (p2 - line);
129 p = p2 + strlen(releaseevr);
132 if (!strncmp(p2, "$basearch", 9))
134 static char *basearch;
143 basearch = strdup(un.machine);
144 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
148 p = pool_tmpjoin(pool, line, basearch, p2 + 9);
149 p2 = p + (p2 - line);
151 p = p2 + strlen(basearch);
160 #define TYPE_UNKNOWN 0
161 #define TYPE_SUSETAGS 1
163 #define TYPE_PLAINDIR 3
166 read_repoinfos_sort(const void *ap, const void *bp)
168 const struct repoinfo *a = ap;
169 const struct repoinfo *b = bp;
170 return strcmp(a->alias, b->alias);
174 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
177 char buf2[4096], *kp, *vp, *kpe;
182 struct repoinfo *repoinfos = 0, *cinfo;
185 rdlen = strlen(reposdir);
186 dir = opendir(reposdir);
192 while ((ent = readdir(dir)) != 0)
194 l = strlen(ent->d_name);
195 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
197 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
198 if ((fp = fopen(buf, "r")) == 0)
204 while(fgets(buf2, sizeof(buf2), fp))
209 while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
212 while (*kp == ' ' || *kp == '\t')
214 if (!*kp || *kp == '#')
218 kp = yum_substitute(pool, kp);
222 vp = strrchr(kp, ']');
226 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
227 cinfo = repoinfos + nrepoinfos++;
228 memset(cinfo, 0, sizeof(*cinfo));
229 cinfo->alias = strdup(kp + 1);
230 cinfo->type = TYPE_RPMMD;
231 cinfo->autorefresh = 1;
233 cinfo->repo_gpgcheck = 1;
235 cinfo->metadata_expire = METADATA_EXPIRE;
240 vp = strchr(kp, '=');
243 for (kpe = vp - 1; kpe >= kp; kpe--)
244 if (*kpe != ' ' && *kpe != '\t')
249 while (*vp == ' ' || *vp == '\t')
252 if (!strcmp(kp, "name"))
253 cinfo->name = strdup(vp);
254 else if (!strcmp(kp, "enabled"))
255 cinfo->enabled = *vp == '0' ? 0 : 1;
256 else if (!strcmp(kp, "autorefresh"))
257 cinfo->autorefresh = *vp == '0' ? 0 : 1;
258 else if (!strcmp(kp, "gpgcheck"))
259 cinfo->pkgs_gpgcheck = *vp == '0' ? 0 : 1;
260 else if (!strcmp(kp, "repo_gpgcheck"))
261 cinfo->repo_gpgcheck = *vp == '0' ? 0 : 1;
262 else if (!strcmp(kp, "baseurl"))
263 cinfo->baseurl = strdup(vp);
264 else if (!strcmp(kp, "mirrorlist"))
266 if (strstr(vp, "metalink"))
267 cinfo->metalink = strdup(vp);
269 else if (!strcmp(kp, "path"))
271 if (vp && strcmp(vp, "/") != 0)
272 cinfo->path = strdup(vp);
274 else if (!strcmp(kp, "type"))
276 if (!strcmp(vp, "yast2"))
277 cinfo->type = TYPE_SUSETAGS;
278 else if (!strcmp(vp, "rpm-md"))
279 cinfo->type = TYPE_RPMMD;
280 else if (!strcmp(vp, "plaindir"))
281 cinfo->type = TYPE_PLAINDIR;
283 cinfo->type = TYPE_UNKNOWN;
285 else if (!strcmp(kp, "priority"))
286 cinfo->priority = atoi(vp);
287 else if (!strcmp(kp, "keeppackages"))
288 cinfo->keeppackages = *vp == '0' ? 0 : 1;
294 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
295 *nrepoinfosp = nrepoinfos;
300 free_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
303 for (i = 0; i < nrepoinfos; i++)
305 struct repoinfo *cinfo = repoinfos + i;
306 sat_free(cinfo->name);
307 sat_free(cinfo->alias);
308 sat_free(cinfo->path);
309 sat_free(cinfo->metalink);
310 sat_free(cinfo->baseurl);
321 strcpy(tmpl, "/var/tmp/solvXXXXXX");
333 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
340 h = sat_chksum_create(chksumtype);
343 printf("%s: unknown checksum type\n", file);
346 while ((l = read(fd, buf, sizeof(buf))) > 0)
347 sat_chksum_add(h, buf, l);
348 lseek(fd, 0, SEEK_SET);
350 sum = sat_chksum_get(h, &l);
351 if (memcmp(sum, chksum, l))
353 printf("%s: checksum mismatch\n", file);
360 findfastest(char **urls, int nurls)
367 struct addrinfo hints, *result;;
369 fds = sat_calloc(nurls, sizeof(*fds));
370 socks = sat_calloc(nurls, sizeof(*socks));
371 for (i = 0; i < nurls; i++)
374 p = strchr(urls[i], '/');
387 if ((p2 = strchr(p, '@')) != 0)
390 if (!strncmp("https:", urls[i], 6))
392 else if (!strncmp("ftp:", urls[i], 4))
394 if ((p2 = strrchr(p, ':')) != 0)
403 sprintf(portstr, "%d", port);
404 memset(&hints, 0, sizeof(struct addrinfo));
405 hints.ai_family = AF_UNSPEC;
406 hints.ai_socktype = SOCK_STREAM;
407 hints.ai_flags = AI_NUMERICSERV;
409 if (!getaddrinfo(p, portstr, &hints, &result))
411 socks[i] = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
414 fcntl(socks[i], F_SETFL, O_NONBLOCK);
415 if (connect(socks[i], result->ai_addr, result->ai_addrlen) == -1)
417 if (errno != EINPROGRESS)
424 freeaddrinfo(result);
431 for (i = j = 0; i < nurls; i++)
435 fds[j].fd = socks[i];
436 fds[j].events = POLLOUT;
444 if (poll(fds, j, 10000) <= 0)
446 i = -1; /* something is wrong */
449 for (i = 0; i < j; i++)
450 if ((fds[i].revents & POLLOUT) != 0)
453 socklen_t soel = sizeof(int);
454 if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, &soe, &soel) == -1 || soe != 0)
456 /* connect failed, kill socket */
457 for (j = 0; j < nurls; j++)
458 if (socks[j] == fds[i].fd)
471 i = -1; /* something is wrong, no bit was set */
474 /* now i contains the fastest fd index */
477 for (j = 0; j < nurls; j++)
478 if (socks[j] == fds[i].fd)
482 char *url0 = urls[0];
487 for (i = j = 0; i < nurls; i++)
495 findmetalinkurl(FILE *fp, unsigned char *chksump, Id *chksumtypep)
497 char buf[4096], *bp, *ep;
504 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
506 while (*bp == ' ' || *bp == '\t')
508 if (chksumtypep && !*chksumtypep && !strncmp(bp, "<hash type=\"sha256\">", 20))
513 memset(chksump, 0, 32);
514 for (i = 0; i < 64; i++)
517 if (c >= '0' && c <= '9')
518 chksump[i / 2] = chksump[i / 2] * 16 + (c - '0');
519 else if (c >= 'a' && c <= 'f')
520 chksump[i / 2] = chksump[i / 2] * 16 + (c - ('a' - 10));
521 else if (c >= 'A' && c <= 'F')
522 chksump[i / 2] = chksump[i / 2] * 16 + (c - ('A' - 10));
527 *chksumtypep = REPOKEY_TYPE_SHA256;
530 if (strncmp(bp, "<url", 4))
532 bp = strchr(bp, '>');
536 ep = strstr(bp, "repodata/repomd.xml</url>");
540 if (strncmp(bp, "http", 4))
542 urls = sat_extend(urls, nurls, 1, sizeof(*urls), 15);
543 urls[nurls++] = strdup(bp);
548 findfastest(urls, nurls > 5 ? 5 : nurls);
551 for (i = 0; i < nurls; i++)
554 ep = strchr(bp, '/');
555 if ((ep = strchr(ep + 2, '/')) != 0)
558 printf("[using mirror %s]\n", bp);
567 cookie_gzread(void *cookie, char *buf, size_t nbytes)
569 return gzread((gzFile *)cookie, buf, nbytes);
573 cookie_gzclose(void *cookie)
575 return gzclose((gzFile *)cookie);
579 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype, int *badchecksump)
585 const char *baseurl = cinfo->baseurl;
589 if (!cinfo->metalink)
591 if (file != cinfo->metalink)
593 FILE *fp = curlfopen(cinfo, cinfo->metalink, 0, 0, 0, 0);
594 unsigned char mlchksum[32];
598 cinfo->baseurl = findmetalinkurl(fp, mlchksum, &mlchksumtype);
602 if (!chksumtype && mlchksumtype && !strcmp(file, "repodata/repomd.xml"))
604 chksumtype = mlchksumtype;
607 return curlfopen(cinfo, file, uncompress, chksum, chksumtype, badchecksump);
609 snprintf(url, sizeof(url), "%s", file);
614 if (l && baseurl[l - 1] == '/')
615 snprintf(url, sizeof(url), "%s%s", baseurl, file);
617 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
620 // printf("url: %s\n", url);
621 if ((pid = fork()) == (pid_t)-1)
633 execlp("curl", "curl", "-f", "-s", "-L", url, (char *)0);
638 while (waitpid(pid, &status, 0) != pid)
640 if (lseek(fd, 0, SEEK_END) == 0 && (!status || !chksumtype))
646 lseek(fd, 0, SEEK_SET);
649 printf("%s: download error %d\n", file, status >> 8 ? status >> 8 : status);
655 if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
665 cookie_io_functions_t cio;
668 sprintf(tmpl, "/dev/fd/%d", fd);
669 gzf = gzopen(tmpl, "r");
673 fprintf(stderr, "could not open /dev/fd/%d, /proc not mounted?\n", fd);
676 memset(&cio, 0, sizeof(cio));
677 cio.read = cookie_gzread;
678 cio.close = cookie_gzclose;
679 return fopencookie(gzf, "r", cio);
681 fcntl(fd, F_SETFD, FD_CLOEXEC);
682 return fdopen(fd, "r");
686 cleanupgpg(char *gpgdir)
689 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
691 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
693 snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
695 snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
697 snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
703 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
712 off_t posfp, possigfp;
715 gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
718 keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
719 if (!(kfp = fopen(keysfile, "w")) )
725 for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
729 pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
730 if (!pubkey || !*pubkey)
732 if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
734 if (fputc('\n', kfp) == EOF) /* Just in case... */
738 if (fclose(kfp) || !nkeys)
743 snprintf(cmd, sizeof(cmd), "gpg2 -q --homedir %s --import %s", gpgdir, keysfile);
746 fprintf(stderr, "key import error\n");
751 posfp = lseek(fileno(fp), 0, SEEK_CUR);
752 lseek(fileno(fp), 0, SEEK_SET);
753 possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
754 lseek(fileno(sigfp), 0, SEEK_SET);
755 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
756 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
757 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
759 lseek(fileno(sigfp), possigfp, SEEK_SET);
760 lseek(fileno(fp), posfp, SEEK_SET);
761 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
762 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
764 return r == 0 ? 1 : 0;
767 #define CHKSUM_IDENT "1.1"
770 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
773 void *h = sat_chksum_create(chktype);
776 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
777 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
778 sat_chksum_add(h, buf, l);
780 sat_chksum_free(h, out);
784 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
786 void *h = sat_chksum_create(chktype);
787 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
788 sat_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
789 sat_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
790 sat_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
791 sat_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
792 sat_chksum_free(h, out);
804 pool_setarch(pool, un.machine);
807 char *calccachepath(Repo *repo, const char *repoext)
809 char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
812 p = pool_tmpjoin(repo->pool, p, "_", repoext);
813 p = pool_tmpjoin(repo->pool, p, ".solvx", 0);
816 p = pool_tmpjoin(repo->pool, p, ".solv", 0);
817 q = p + strlen(SOLVCACHE_PATH) + 1;
827 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie, int mark)
830 unsigned char mycookie[32];
831 struct repoinfo *cinfo;
834 cinfo = repo->appdata;
835 if (!(fp = fopen(calccachepath(repo, repoext), "r")))
837 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
842 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
851 flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
852 if (repoext && strcmp(repoext, "DL") != 0)
853 flags |= REPO_LOCALPOOL; /* no local pool for DL so that we can compare IDs */
855 if (repo_add_solv_flags(repo, fp, flags))
860 if (cinfo && !repoext)
862 /* set the checksum so that we can use it with the stub loads */
864 if (!fstat(fileno(fp), &stb))
869 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
870 for (i = 0; i < 32; i++)
871 cinfo->extcookie[i] ^= cinfo->cookie[i];
875 futimes(fileno(fp), 0); /* try to set modification time */
881 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
883 Id *addedfileprovides = 0;
888 struct repoinfo *cinfo;
891 cinfo = repo->appdata;
892 mkdir(SOLVCACHE_PATH, 0755);
893 tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
901 if (!(fp = fdopen(fd, "w")))
910 for (i = repo->start; i < repo->end; i++)
911 if (repo->pool->solvables[i].repo != repo)
920 info = repo_add_repodata(repo, 0);
923 pool_addfileprovides_ids(repo->pool, 0, &addedfileprovides);
924 if (addedfileprovides && *addedfileprovides)
926 for (i = 0; addedfileprovides[i]; i++)
927 repodata_add_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[i]);
929 sat_free(addedfileprovides);
930 repodata_internalize(info);
931 repo_write(repo, fp, repo_write_stdkeyfilter, 0, 0);
934 repodata_write(info, fp, repo_write_stdkeyfilter, 0);
937 if (fwrite(cookie, 32, 1, fp) != 1)
950 if (!repoext && cinfo)
952 /* set the checksum so that we can use it with the stub loads */
954 if (!stat(tmpl, &stb))
959 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
960 for (i = 0; i < 32; i++)
961 cinfo->extcookie[i] ^= cinfo->cookie[i];
966 /* switch to just saved repo to activate paging and save memory */
967 FILE *fp = fopen(tmpl, "r");
974 if (repo_add_solv_flags(repo, fp, SOLV_ADD_NO_STUBS))
976 /* oops, no way to recover from here */
977 fprintf(stderr, "internal error\n");
983 /* make sure repodata contains complete repo */
984 /* (this is how repodata_write saves it) */
985 repodata_extend_block(info, repo->start, repo->end - repo->start);
986 info->state = REPODATA_LOADING;
987 /* no need for LOCALPOOL as pool already contains ids */
988 repo_add_solv_flags(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
989 info->state = REPODATA_AVAILABLE; /* in case the load failed */
994 if (!rename(tmpl, calccachepath(repo, repoext)))
1003 Pool *sigpool = pool_create();
1004 Repo *repo = repo_create(sigpool, "rpmdbkeys");
1005 repo_add_rpmdb_pubkeys(repo, 0, 0);
1010 /* repomd helpers */
1012 static inline const char *
1013 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1015 Pool *pool = repo->pool;
1017 const char *filename;
1022 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1023 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1024 if (dataiterator_step(&di))
1026 dataiterator_setpos_parent(&di);
1027 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1028 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
1030 dataiterator_free(&di);
1031 if (filename && !*chksumtypep)
1033 printf("no %s file checksum!\n", what);
1040 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
1042 Pool *pool = repo->pool;
1044 Id chksumtype, handle;
1045 const unsigned char *chksum;
1046 const char *filename;
1048 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1049 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1050 if (!dataiterator_step(&di))
1052 dataiterator_free(&di);
1055 if (!strcmp(what, "prestodelta"))
1057 dataiterator_setpos_parent(&di);
1058 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1059 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, &chksumtype);
1060 if (!filename || !chksum)
1062 dataiterator_free(&di);
1065 handle = repodata_new_handle(data);
1066 repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
1067 repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
1069 repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
1070 if (!strcmp(what, "deltainfo"))
1072 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
1073 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
1075 if (!strcmp(what, "filelists"))
1077 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1078 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1080 dataiterator_free(&di);
1081 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1086 /* susetags helpers */
1088 static inline const char *
1089 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1091 Pool *pool = repo->pool;
1093 const char *filename;
1098 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
1099 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1100 if (dataiterator_step(&di))
1102 dataiterator_setpos_parent(&di);
1103 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
1106 dataiterator_free(&di);
1107 if (filename && !*chksumtypep)
1109 printf("no %s file checksum!\n", what);
1115 static Id susetags_langtags[] = {
1116 SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
1117 SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
1118 SOLVABLE_EULA, REPOKEY_TYPE_STR,
1119 SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
1120 SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
1121 SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
1126 susetags_add_ext(Repo *repo, Repodata *data)
1128 Pool *pool = repo->pool;
1131 Id handle, filechksumtype;
1132 const unsigned char *filechksum;
1135 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
1136 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1137 while (dataiterator_step(&di))
1139 if (strncmp(di.kv.str, "packages.", 9) != 0)
1141 if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
1143 ext[0] = di.kv.str[9];
1144 ext[1] = di.kv.str[10];
1146 if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
1148 handle = repodata_new_handle(data);
1149 repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
1151 repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
1152 if (!strcmp(ext, "DU"))
1154 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
1155 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
1157 if (!strcmp(ext, "FL"))
1159 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1160 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1164 for (i = 0; susetags_langtags[i]; i += 2)
1166 repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
1167 repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
1170 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1172 dataiterator_free(&di);
1177 iscompressed(const char *name)
1179 int l = strlen(name);
1180 return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
1187 load_stub(Pool *pool, Repodata *data, void *dp)
1189 const char *filename, *descrdir, *repomdtype;
1190 const unsigned char *filechksum;
1192 struct repoinfo *cinfo;
1197 cinfo = data->repo->appdata;
1199 filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
1203 ext[0] = filename[9];
1204 ext[1] = filename[10];
1207 printf("[%s:%s", data->repo->name, ext);
1209 if (usecachedrepo(data->repo, ext, cinfo->extcookie, 0))
1211 printf(" cached]\n"); fflush(stdout);
1215 printf(" loading]\n"); fflush(stdout);
1217 defvendor = repo_lookup_id(data->repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1218 descrdir = repo_lookup_str(data->repo, SOLVID_META, SUSETAGS_DESCRDIR);
1220 descrdir = "suse/setup/descr";
1222 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
1223 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1225 repo_add_susetags(data->repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1227 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
1231 repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
1234 if (!strcmp(repomdtype, "filelists"))
1236 else if (!strcmp(repomdtype, "deltainfo"))
1241 printf("[%s:%s", data->repo->name, ext);
1243 if (usecachedrepo(data->repo, ext, cinfo->extcookie, 0))
1245 printf(" cached]\n");fflush(stdout);
1248 printf(" loading]\n"); fflush(stdout);
1249 filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
1251 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, REPOSITORY_REPOMD_CHECKSUM, &filechksumtype);
1252 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1254 if (!strcmp(ext, "FL"))
1255 repo_add_rpmmd(data->repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1256 else if (!strcmp(ext, "DL"))
1257 repo_add_deltainfoxml(data->repo, fp, REPO_USE_LOADING);
1259 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
1267 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
1270 struct repoinfo *cinfo;
1274 const char *filename;
1275 const unsigned char *filechksum;
1277 const char *descrdir;
1280 unsigned char cookie[32];
1286 repo = repo_create(pool, "@System");
1287 printf("rpm database:");
1288 if (stat("/var/lib/rpm/Packages", &stb))
1289 memset(&stb, 0, sizeof(&stb));
1290 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cookie);
1291 if (usecachedrepo(repo, 0, cookie, 0))
1292 printf(" cached\n");
1296 printf(" reading\n");
1299 #ifdef PRODUCTS_PATH
1300 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
1302 if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1304 Repo *ref = repo_create(pool, "@System.old");
1305 if (!repo_add_solv(ref, ofp))
1307 repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
1314 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
1315 writecachedrepo(repo, 0, 0, cookie);
1317 pool_set_installed(pool, repo);
1319 for (i = 0; i < nrepoinfos; i++)
1321 cinfo = repoinfos + i;
1322 if (!cinfo->enabled)
1325 repo = repo_create(pool, cinfo->alias);
1327 repo->appdata = cinfo;
1328 repo->priority = 99 - cinfo->priority;
1330 dorefresh = cinfo->autorefresh;
1331 if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0), &stb) == 0)
1333 if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1336 if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1338 printf("repo '%s':", cinfo->alias);
1339 printf(" cached\n");
1343 switch (cinfo->type)
1346 printf("rpmmd repo '%s':", cinfo->alias);
1348 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1350 printf(" no repomd.xml file, skipped\n");
1355 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1356 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1358 printf(" cached\n");
1362 if (cinfo->repo_gpgcheck)
1364 sigfp = curlfopen(cinfo, "repodata/repomd.xml.asc", 0, 0, 0, 0);
1367 printf(" unsigned, skipped\n");
1372 sigpool = read_sigs();
1373 if (!checksig(sigpool, fp, sigfp))
1375 printf(" checksig failed, skipped\n");
1382 repo_add_repomdxml(repo, fp, 0);
1384 printf(" reading\n");
1385 filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1386 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1388 repo_add_rpmmd(repo, fp, 0, 0);
1392 break; /* hopeless */
1394 filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1395 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1397 repo_add_updateinfoxml(repo, fp, 0);
1401 data = repo_add_repodata(repo, 0);
1402 if (!repomd_add_ext(repo, data, "deltainfo"))
1403 repomd_add_ext(repo, data, "prestodelta");
1404 repomd_add_ext(repo, data, "filelists");
1405 repodata_internalize(data);
1407 writecachedrepo(repo, data, 0, cinfo->cookie);
1408 repodata_create_stubs(repo_last_repodata(repo));
1412 printf("susetags repo '%s':", cinfo->alias);
1416 if ((fp = curlfopen(cinfo, "content", 0, 0, 0, 0)) == 0)
1418 printf(" no content file, skipped\n");
1423 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1424 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1426 printf(" cached\n");
1430 if (cinfo->repo_gpgcheck)
1432 sigfp = curlfopen(cinfo, "content.asc", 0, 0, 0, 0);
1435 printf(" unsigned, skipped\n");
1442 sigpool = read_sigs();
1443 if (!checksig(sigpool, fp, sigfp))
1445 printf(" checksig failed, skipped\n");
1453 repo_add_content(repo, fp, 0);
1455 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1456 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1458 descrdir = "suse/setup/descr";
1459 filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1461 filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1464 printf(" no packages file entry, skipped\n");
1467 printf(" reading\n");
1468 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
1469 break; /* hopeless */
1470 repo_add_susetags(repo, fp, defvendor, 0, 0);
1472 data = repo_add_repodata(repo, 0);
1473 susetags_add_ext(repo, data);
1474 repodata_internalize(data);
1476 writecachedrepo(repo, data, 0, cinfo->cookie);
1477 repodata_create_stubs(repo_last_repodata(repo));
1480 printf("unsupported repo '%s': skipped\n", cinfo->alias);
1492 str2archid(Pool *pool, char *arch)
1497 id = str2id(pool, arch, 0);
1498 if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
1500 if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
1506 depglob(Pool *pool, char *name, Queue *job)
1509 Id id = str2id(pool, name, 0);
1514 FOR_PROVIDES(p, pp, id)
1516 Solvable *s = pool->solvables + p;
1520 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
1526 printf("[using capability match for '%s']\n", name);
1527 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
1532 if (strpbrk(name, "[*?") == 0)
1535 /* looks like a name glob. hard work. */
1536 for (p = 1; p < pool->nsolvables; p++)
1538 Solvable *s = pool->solvables + p;
1539 if (!s->repo || !pool_installable(pool, s))
1542 if (fnmatch(name, id2str(pool, id), 0) == 0)
1544 for (i = 0; i < job->count; i += 2)
1545 if (job->elements[i] == SOLVER_SOLVABLE_NAME && job->elements[i + 1] == id)
1547 if (i == job->count)
1548 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
1554 /* looks like a dep glob. really hard work. */
1555 for (id = 1; id < pool->ss.nstrings; id++)
1557 if (!pool->whatprovides[id])
1559 if (fnmatch(name, id2str(pool, id), 0) == 0)
1562 printf("[using capability match for '%s']\n", name);
1563 for (i = 0; i < job->count; i += 2)
1564 if (job->elements[i] == SOLVER_SOLVABLE_PROVIDES && job->elements[i + 1] == id)
1566 if (i == job->count)
1567 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
1577 addrelation(Pool *pool, Queue *job, int flags, Id evr)
1580 for (i = 0; i < job->count; i += 2)
1582 if (job->elements[i] != SOLVER_SOLVABLE_NAME && job->elements[i] != SOLVER_SOLVABLE_PROVIDES)
1584 job->elements[i + 1] = rel2id(pool, job->elements[i + 1], evr, flags, 1);
1589 limitevr(Pool *pool, char *evr, Queue *job, Id archid)
1598 for (i = 0; i < job->count; i += 2)
1601 FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
1603 s = pool_id2solvable(pool, p);
1604 if (archid && s->arch != archid)
1606 if (evrcmp_str(pool, id2str(pool, s->evr), evr, EVRCMP_MATCH) == 0)
1613 queue_deleten(job, 0, i);
1617 /* if all solvables have the same evr */
1618 s = pool_id2solvable(pool, mq.elements[0]);
1620 for (j = 0; j < mq.count; j++)
1622 s = pool_id2solvable(pool, mq.elements[j]);
1623 if (s->evr != evrid)
1626 if (j == mq.count && j > 1)
1628 prune_to_best_arch(pool, &mq);
1629 // prune_to_highest_prio(pool, &mq);
1634 job->elements[i] = SOLVER_SOLVABLE_ONE_OF;
1635 job->elements[i + 1] = pool_queuetowhatprovides(pool, &mq);
1639 job->elements[i] = SOLVER_SOLVABLE;
1640 job->elements[i + 1] = mq.elements[0];
1645 queue_deleten(job, i, 2);
1655 if ((r = strrchr(evr, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
1658 if (limitevr(pool, evr, job, archid))
1670 mkselect(Pool *pool, int mode, char *name, Queue *job)
1682 dataiterator_init(&di, pool, mode == SOLVER_ERASE ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, SEARCH_STRING|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
1683 while (dataiterator_step(&di))
1685 Solvable *s = pool->solvables + di.solvid;
1686 if (!s->repo || !pool_installable(pool, s))
1688 queue_push(&q, di.solvid);
1689 dataiterator_skip_solvable(&di);
1691 dataiterator_free(&di);
1694 printf("[using file list match for '%s']\n", name);
1697 queue_push2(job, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
1699 queue_push2(job, SOLVER_SOLVABLE, q.elements[0]);
1705 if ((r = strpbrk(name, "<=>")) != 0)
1707 /* relation case, support:
1712 int nend = r - name;
1724 while (*r && *r == ' ' && *r == '\t')
1726 while (nend && (name[nend - 1] == ' ' || name[nend -1 ] == '\t'))
1731 fprintf(stderr, "bad relation\n");
1734 if (depglob(pool, name, job))
1736 addrelation(pool, job, rflags, str2id(pool, r, 1));
1739 if ((r2 = strrchr(name, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
1742 if (depglob(pool, name, job))
1745 addrelation(pool, job, REL_ARCH, archid);
1746 addrelation(pool, job, rflags, str2id(pool, r, 1));
1754 /* no relation case, support:
1757 * depglob-version-release
1758 * depglob-version-release.arch
1760 if (depglob(pool, name, job))
1763 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
1766 if (depglob(pool, name, job))
1769 addrelation(pool, job, REL_ARCH, archid);
1774 if ((r = strrchr(name, '-')) != 0)
1777 if (depglob(pool, name, job))
1779 /* have just the version */
1781 if (limitevr(pool, r + 1, job, 0))
1784 if ((r2 = strrchr(name, '-')) != 0)
1788 if (depglob(pool, name, job))
1791 if (limitevr(pool, r2 + 1, job, 0))
1799 fprintf(stderr, "nothing matches '%s'\n", name);
1805 yesno(const char *str)
1807 char inbuf[128], *ip;
1814 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
1819 while (*ip == ' ' || *ip == '\t')
1826 if (*ip == 'y' || *ip == 'n')
1827 return *ip == 'y' ? 1 : 0;
1839 fileconflict_cb(Pool *pool, Id p, void *cbdata)
1841 struct fcstate *fcstate = cbdata;
1849 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
1852 s = pool_id2solvable(pool, p);
1853 if (pool->installed && s->repo == pool->installed)
1855 if (!s->repo->rpmdbid)
1857 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
1860 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
1862 for (i = 0; i < fcstate->newpkgscnt; i++)
1863 if (fcstate->checkq->elements[i] == p)
1865 if (i == fcstate->newpkgscnt)
1867 fp = fcstate->newpkgsfps[i];
1871 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
1875 runrpm(const char *arg, const char *name, int dupfd3)
1880 if ((pid = fork()) == (pid_t)-1)
1887 if (dupfd3 != -1 && dupfd3 != 3)
1893 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
1894 if (strcmp(arg, "-e") == 0)
1895 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1897 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1901 while (waitpid(pid, &status, 0) != pid)
1905 printf("rpm failed\n");
1911 nscallback(Pool *pool, void *data, Id name, Id evr)
1913 if (name == NAMESPACE_PRODUCTBUDDY)
1915 /* SUSE specific hack: each product has an associated rpm */
1916 Solvable *s = pool->solvables + evr;
1919 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
1922 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
1925 FOR_PROVIDES(p, pp, cap)
1927 Solvable *ps = pool->solvables + p;
1928 if (ps->repo == s->repo && ps->arch == s->arch)
1936 #ifdef SOFTLOCKS_PATH
1939 addsoftlocks(Pool *pool, Queue *job)
1943 char *bp, *ep, buf[4096];
1945 if ((fp = fopen(SOFTLOCKS_PATH, "r")) == 0)
1947 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
1949 while (*bp == ' ' || *bp == '\t')
1951 if (!*bp || *bp == '#')
1953 for (ep = bp; *ep; ep++)
1954 if (*ep == ' ' || *ep == '\t' || *ep == '\n')
1957 type = SOLVER_SOLVABLE_NAME;
1958 if (!strncmp(bp, "provides:", 9) && bp[9])
1960 type = SOLVER_SOLVABLE_PROVIDES;
1963 id = str2id(pool, bp, 1);
1964 if (pool->installed)
1966 FOR_JOB_SELECT(p, pp, type, id)
1967 if (pool->solvables[p].repo == pool->installed)
1970 continue; /* ignore, as it is already installed */
1972 queue_push2(job, SOLVER_LOCK|SOLVER_WEAK|type, id);
1980 #define MODE_INSTALL 1
1981 #define MODE_ERASE 2
1982 #define MODE_UPDATE 3
1983 #define MODE_DISTUPGRADE 4
1984 #define MODE_VERIFY 5
1985 #define MODE_PATCH 6
1987 #define MODE_REPOLIST 8
1988 #define MODE_SEARCH 9
1993 fprintf(stderr, "Usage: solv COMMAND <select>\n");
1994 fprintf(stderr, "\n");
1995 fprintf(stderr, " distupgrade: replace installed packages with\n");
1996 fprintf(stderr, " versions from the repositories\n");
1997 fprintf(stderr, " erase: erase installed packages\n");
1998 fprintf(stderr, " info: display package information\n");
1999 fprintf(stderr, " install: install packages\n");
2000 fprintf(stderr, " list: list packages\n");
2001 fprintf(stderr, " repos: list enabled repositories\n");
2002 fprintf(stderr, " search: search name/summary/description\n");
2003 fprintf(stderr, " update: update installed packages\n");
2004 fprintf(stderr, "\n");
2009 main(int argc, char **argv)
2012 Repo *commandlinerepo = 0;
2013 Id *commandlinepkgs = 0;
2015 struct repoinfo *repoinfos;
2017 int mainmode = 0, mode = 0;
2022 char inbuf[128], *ip;
2025 struct fcstate fcstate;
2031 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2033 mainmode = MODE_INSTALL;
2034 mode = SOLVER_INSTALL;
2036 else if (!strcmp(argv[0], "patch"))
2038 mainmode = MODE_PATCH;
2039 mode = SOLVER_INSTALL;
2041 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2043 mainmode = MODE_ERASE;
2044 mode = SOLVER_ERASE;
2046 else if (!strcmp(argv[0], "list"))
2048 mainmode = MODE_LIST;
2051 else if (!strcmp(argv[0], "info"))
2053 mainmode = MODE_INFO;
2056 else if (!strcmp(argv[0], "search"))
2058 mainmode = MODE_SEARCH;
2061 else if (!strcmp(argv[0], "verify"))
2063 mainmode = MODE_VERIFY;
2064 mode = SOLVER_VERIFY;
2066 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2068 mainmode = MODE_UPDATE;
2069 mode = SOLVER_UPDATE;
2071 else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2073 mainmode = MODE_DISTUPGRADE;
2074 mode = SOLVER_UPDATE;
2076 else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2078 mainmode = MODE_REPOLIST;
2084 pool = pool_create();
2086 pool->obsoleteusescolors = 1;
2088 pool_setloadcallback(pool, load_stub, 0);
2089 pool->nscallback = nscallback;
2090 // pool_setdebuglevel(pool, 2);
2092 repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
2094 if (mainmode == MODE_REPOLIST)
2097 for (i = 0; i < nrepoinfos; i++)
2099 struct repoinfo *cinfo = repoinfos + i;
2100 if (!cinfo->enabled)
2102 printf("%d: %-20s %s\n", j++, cinfo->alias, cinfo->name);
2107 read_repos(pool, repoinfos, nrepoinfos);
2109 if (mainmode == MODE_SEARCH)
2115 map_init(&m, pool->nsolvables);
2116 dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2117 dataiterator_set_keyname(&di, SOLVABLE_NAME);
2118 dataiterator_set_search(&di, 0, 0);
2119 while (dataiterator_step(&di))
2120 MAPSET(&m, di.solvid);
2121 dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2122 dataiterator_set_search(&di, 0, 0);
2123 while (dataiterator_step(&di))
2124 MAPSET(&m, di.solvid);
2125 dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2126 dataiterator_set_search(&di, 0, 0);
2127 while (dataiterator_step(&di))
2128 MAPSET(&m, di.solvid);
2129 dataiterator_free(&di);
2131 for (p = 1; p < pool->nsolvables; p++)
2133 Solvable *s = pool_id2solvable(pool, p);
2136 printf(" - %s: %s\n", solvable2str(pool, s), solvable_lookup_str(s, SOLVABLE_SUMMARY));
2143 if (mainmode == MODE_LIST || mainmode == MODE_INSTALL)
2145 for (i = 1; i < argc; i++)
2148 l = strlen(argv[i]);
2149 if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2151 if (access(argv[i], R_OK))
2156 if (!commandlinepkgs)
2157 commandlinepkgs = sat_calloc(argc, sizeof(Id));
2158 if (!commandlinerepo)
2159 commandlinerepo = repo_create(pool, "@commandline");
2160 repo_add_rpms(commandlinerepo, (const char **)argv + i, 1, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2161 commandlinepkgs[i] = commandlinerepo->end - 1;
2163 if (commandlinerepo)
2164 repo_internalize(commandlinerepo);
2167 // FOR_REPOS(i, repo)
2168 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2169 pool_addfileprovides(pool);
2170 pool_createwhatprovides(pool);
2173 for (i = 1; i < argc; i++)
2178 if (commandlinepkgs && commandlinepkgs[i])
2180 queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
2184 mkselect(pool, mode, argv[i], &job2);
2185 for (j = 0; j < job2.count; j++)
2186 queue_push(&job, job2.elements[j]);
2190 if (!job.count && mainmode != MODE_UPDATE && mainmode != MODE_DISTUPGRADE && mainmode != MODE_VERIFY && mainmode != MODE_PATCH)
2192 printf("no package matched\n");
2199 if (mainmode == MODE_LIST || mainmode == MODE_INFO)
2201 /* list mode, no solver needed */
2202 for (i = 0; i < job.count; i += 2)
2204 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
2206 Solvable *s = pool_id2solvable(pool, p);
2207 if (mainmode == MODE_INFO)
2209 printf("Name: %s\n", solvable2str(pool, s));
2210 printf("Repo: %s\n", s->repo->name);
2211 printf("Summary: %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
2212 printf("Url: %s\n", solvable_lookup_str(s, SOLVABLE_URL));
2213 printf("License: %s\n", solvable_lookup_str(s, SOLVABLE_LICENSE));
2214 printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
2219 const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de");
2220 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
2222 printf(" %s\n", sum);
2228 free_repoinfos(repoinfos, nrepoinfos);
2229 sat_free(commandlinepkgs);
2233 if (mainmode == MODE_PATCH)
2239 map_init(&installedmap, pool->nsolvables);
2240 if (pool->installed)
2241 FOR_REPO_SOLVABLES(pool->installed, p, s)
2242 MAPSET(&installedmap, p);
2244 /* install all patches */
2245 for (p = 1; p < pool->nsolvables; p++)
2251 s = pool->solvables + p;
2252 if (strncmp(id2str(pool, s->name), "patch:", 6) != 0)
2254 FOR_PROVIDES(p2, pp, s->name)
2256 Solvable *s2 = pool->solvables + p2;
2257 if (s2->name != s->name)
2259 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2260 if (r < 0 || (r == 0 && p > p2))
2265 type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2266 if (type && !strcmp(type, "optional"))
2268 r = solvable_trivial_installable_map(s, &installedmap, 0);
2271 if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2278 queue_push2(&job, SOLVER_SOLVABLE, p);
2280 map_free(&installedmap);
2284 for (i = 0; i < job.count; i += 2)
2286 if (mode == SOLVER_UPDATE)
2288 /* make update of not installed packages an install */
2289 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
2290 if (pool->installed && pool->solvables[p].repo == pool->installed)
2294 job.elements[i] |= SOLVER_INSTALL;
2298 job.elements[i] |= mode;
2301 // multiversion test
2302 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
2303 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
2304 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
2306 #ifdef SOFTLOCKS_PATH
2307 addsoftlocks(pool, &job);
2313 Id problem, solution;
2316 solv = solver_create(pool);
2317 solv->ignorealreadyrecommended = 1;
2318 solv->updatesystem = allpkgs && (mainmode == MODE_UPDATE || mainmode == MODE_DISTUPGRADE);
2319 solv->dosplitprovides = solv->updatesystem;
2320 solv->fixsystem = allpkgs && (mainmode == MODE_VERIFY);
2321 if (mainmode == MODE_DISTUPGRADE)
2323 solv->distupgrade = 1;
2324 solv->allowdowngrade = 1;
2325 solv->allowarchchange = 1;
2326 solv->allowvendorchange = 1;
2328 if (mainmode == MODE_ERASE)
2329 solv->allowuninstall = 1; /* don't nag */
2331 // queue_push2(&job, SOLVER_DISTUPGRADE, 3);
2332 solver_solve(solv, &job);
2333 if (!solv->problems.count)
2335 pcnt = solver_problem_count(solv);
2336 printf("Found %d problems:\n", pcnt);
2337 for (problem = 1; problem <= pcnt; problem++)
2340 printf("Problem %d:\n", problem);
2341 solver_printprobleminfo(solv, problem);
2343 scnt = solver_solution_count(solv, problem);
2344 for (solution = 1; solution <= scnt; solution++)
2346 printf("Solution %d:\n", solution);
2347 solver_printsolution(solv, problem, solution);
2352 printf("Please choose a solution: ");
2355 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2360 while (*ip == ' ' || *ip == '\t')
2362 if (*ip >= '0' && *ip <= '9')
2365 if (take >= 1 && take <= scnt)
2381 solver_take_solution(solv, problem, take, &job);
2387 trans = &solv->trans;
2388 if (!trans->steps.count)
2390 printf("Nothing to do.\n");
2394 printf("Transaction summary:\n\n");
2395 solver_printtransaction(solv);
2404 duc[1].path = "/usr/share/man";
2405 duc[2].path = "/sbin";
2406 duc[3].path = "/etc";
2407 transaction_calc_duchanges(trans, duc, 4);
2408 for (i = 0; i < 4; i++)
2409 printf("duchanges %s: %d K %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
2412 printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
2415 if (!yesno("OK to continue (y/n)? "))
2421 queue_init(&checkq);
2422 newpkgs = transaction_installedresult(trans, &checkq);
2427 int downloadsize = 0;
2428 for (i = 0; i < newpkgs; i++)
2432 p = checkq.elements[i];
2433 s = pool_id2solvable(pool, p);
2434 downloadsize += solvable_lookup_num(s, SOLVABLE_DOWNLOADSIZE, 0);
2436 printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
2437 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
2438 for (i = 0; i < newpkgs; i++)
2440 unsigned int medianr;
2443 struct repoinfo *cinfo;
2444 const unsigned char *chksum;
2448 p = checkq.elements[i];
2449 s = pool_id2solvable(pool, p);
2450 if (s->repo == commandlinerepo)
2452 loc = solvable_get_location(s, &medianr);
2453 if (!(newpkgsfps[i] = fopen(loc, "r")))
2461 cinfo = s->repo->appdata;
2464 printf("%s: no repository information\n", s->repo->name);
2467 loc = solvable_get_location(s, &medianr);
2471 if (pool->installed && pool->installed->nsolvables)
2473 /* try a delta first */
2474 char *matchname = strdup(id2str(pool, s->name));
2475 dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
2476 dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
2477 while (dataiterator_step(&di))
2481 dataiterator_setpos_parent(&di);
2482 if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
2483 pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
2485 baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
2486 FOR_PROVIDES(op, pp, s->name)
2488 Solvable *os = pool->solvables + op;
2489 if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
2492 if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
2494 /* base is installed, run sequence check */
2495 const char *seqname;
2504 seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
2505 seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
2506 seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
2507 seq = pool_tmpjoin(pool, seqname, "-", seqevr);
2508 seq = pool_tmpjoin(pool, seq, "-", seqnum);
2510 sprintf(cmd, "/usr/bin/applydeltarpm -a %s -c -s ", id2str(pool, s->arch));
2512 sprintf(cmd, "/usr/bin/applydeltarpm -c -s ");
2514 if (system(pool_tmpjoin(pool, cmd, seq, 0)) != 0)
2515 continue; /* didn't match */
2516 /* looks good, download delta */
2518 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
2520 continue; /* no way! */
2521 dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
2522 dloc = pool_tmpjoin(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
2523 dloc = pool_tmpjoin(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
2524 dloc = pool_tmpjoin(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
2525 if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
2527 /* got it, now reconstruct */
2528 newfd = opentmpfile();
2530 sprintf(cmd, "applydeltarpm -a %s /dev/fd/%d /dev/fd/%d", id2str(pool, s->arch), fileno(fp), newfd);
2532 sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
2534 fcntl(fileno(fp), F_SETFD, 0);
2541 lseek(newfd, 0, SEEK_SET);
2543 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2544 if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
2550 newpkgsfps[i] = fdopen(newfd, "r");
2555 dataiterator_free(&di);
2556 sat_free(matchname);
2563 continue; /* delta worked! */
2565 if (cinfo->type == TYPE_SUSETAGS)
2567 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
2568 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
2571 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2572 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
2574 printf("\n%s: %s not found in repository\n", s->repo->name, loc);
2587 printf("Searching for file conflicts\n");
2588 queue_init(&conflicts);
2589 fcstate.rpmdbstate = 0;
2590 fcstate.newpkgscnt = newpkgs;
2591 fcstate.checkq = &checkq;
2592 fcstate.newpkgsfps = newpkgsfps;
2593 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fileconflict_cb, &fcstate);
2594 if (conflicts.count)
2597 for (i = 0; i < conflicts.count; i += 5)
2598 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]));
2600 if (yesno("Re-run solver (y/n/q)? "))
2602 for (i = 0; i < newpkgs; i++)
2604 fclose(newpkgsfps[i]);
2605 newpkgsfps = sat_free(newpkgsfps);
2607 pool_add_fileconflicts_deps(pool, &conflicts);
2608 pool_createwhatprovides(pool); /* Hmm... */
2612 queue_free(&conflicts);
2615 printf("Committing transaction:\n\n");
2616 transaction_order(trans, 0);
2617 for (i = 0; i < trans->steps.count; i++)
2619 const char *evr, *evrp, *nvra;
2624 p = trans->steps.elements[i];
2625 s = pool_id2solvable(pool, p);
2626 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
2629 case SOLVER_TRANSACTION_ERASE:
2630 printf("erase %s\n", solvid2str(pool, p));
2631 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
2633 /* strip epoch from evr */
2634 evr = evrp = id2str(pool, s->evr);
2635 while (*evrp >= '0' && *evrp <= '9')
2637 if (evrp > evr && evrp[0] == ':' && evrp[1])
2639 nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
2640 nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
2641 runrpm("-e", nvra, -1); /* to bad that --querybynumber doesn't work */
2643 case SOLVER_TRANSACTION_INSTALL:
2644 case SOLVER_TRANSACTION_MULTIINSTALL:
2645 printf("install %s\n", solvid2str(pool, p));
2646 for (j = 0; j < newpkgs; j++)
2647 if (checkq.elements[j] == p)
2649 fp = j < newpkgs ? newpkgsfps[j] : 0;
2653 lseek(fileno(fp), 0, SEEK_SET);
2654 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
2663 for (i = 0; i < newpkgs; i++)
2665 fclose(newpkgsfps[i]);
2666 sat_free(newpkgsfps);
2667 queue_free(&checkq);
2671 free_repoinfos(repoinfos, nrepoinfos);
2672 sat_free(commandlinepkgs);