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"
67 #define SOLVCACHE_PATH "/var/cache/solv"
69 #define METADATA_EXPIRE (60 * 90)
88 unsigned char cookie[32];
89 unsigned char extcookie[32];
94 yum_substitute(Pool *pool, char *line)
99 while ((p2 = strchr(p, '$')) != 0)
101 if (!strncmp(p2, "$releasever", 11))
103 static char *releaseevr;
109 rpm_installedrpmdbids(0, "Providename", "redhat-release", &q);
112 void *handle, *state = 0;
114 handle = rpm_byrpmdbid(q.elements[0], 0, &state);
115 releaseevr = rpm_query(handle, SOLVABLE_EVR);
116 rpm_byrpmdbid(0, 0, &state);
117 if ((p = strchr(releaseevr, '-')) != 0)
122 releaseevr = strdup("?");
125 p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
126 p2 = p + (p2 - line);
128 p = p2 + strlen(releaseevr);
131 if (!strncmp(p2, "$basearch", 9))
133 static char *basearch;
142 basearch = strdup(un.machine);
143 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
147 p = pool_tmpjoin(pool, line, basearch, p2 + 9);
148 p2 = p + (p2 - line);
150 p = p2 + strlen(basearch);
159 #define TYPE_UNKNOWN 0
160 #define TYPE_SUSETAGS 1
162 #define TYPE_PLAINDIR 3
165 read_repoinfos_sort(const void *ap, const void *bp)
167 const struct repoinfo *a = ap;
168 const struct repoinfo *b = bp;
169 return strcmp(a->alias, b->alias);
173 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
176 char buf2[4096], *kp, *vp, *kpe;
181 struct repoinfo *repoinfos = 0, *cinfo;
184 rdlen = strlen(reposdir);
185 dir = opendir(reposdir);
191 while ((ent = readdir(dir)) != 0)
193 l = strlen(ent->d_name);
194 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
196 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
197 if ((fp = fopen(buf, "r")) == 0)
203 while(fgets(buf2, sizeof(buf2), fp))
208 while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
211 while (*kp == ' ' || *kp == '\t')
213 if (!*kp || *kp == '#')
217 kp = yum_substitute(pool, kp);
221 vp = strrchr(kp, ']');
225 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
226 cinfo = repoinfos + nrepoinfos++;
227 memset(cinfo, 0, sizeof(*cinfo));
228 cinfo->alias = strdup(kp + 1);
229 cinfo->type = TYPE_RPMMD;
230 cinfo->autorefresh = 1;
232 cinfo->repo_gpgcheck = 1;
234 cinfo->metadata_expire = METADATA_EXPIRE;
239 vp = strchr(kp, '=');
242 for (kpe = vp - 1; kpe >= kp; kpe--)
243 if (*kpe != ' ' && *kpe != '\t')
248 while (*vp == ' ' || *vp == '\t')
251 if (!strcmp(kp, "name"))
252 cinfo->name = strdup(vp);
253 else if (!strcmp(kp, "enabled"))
254 cinfo->enabled = *vp == '0' ? 0 : 1;
255 else if (!strcmp(kp, "autorefresh"))
256 cinfo->autorefresh = *vp == '0' ? 0 : 1;
257 else if (!strcmp(kp, "gpgcheck"))
258 cinfo->pkgs_gpgcheck = *vp == '0' ? 0 : 1;
259 else if (!strcmp(kp, "repo_gpgcheck"))
260 cinfo->repo_gpgcheck = *vp == '0' ? 0 : 1;
261 else if (!strcmp(kp, "baseurl"))
262 cinfo->baseurl = strdup(vp);
263 else if (!strcmp(kp, "mirrorlist"))
265 if (strstr(vp, "metalink"))
266 cinfo->metalink = strdup(vp);
268 else if (!strcmp(kp, "path"))
270 if (vp && strcmp(vp, "/") != 0)
271 cinfo->path = strdup(vp);
273 else if (!strcmp(kp, "type"))
275 if (!strcmp(vp, "yast2"))
276 cinfo->type = TYPE_SUSETAGS;
277 else if (!strcmp(vp, "rpm-md"))
278 cinfo->type = TYPE_RPMMD;
279 else if (!strcmp(vp, "plaindir"))
280 cinfo->type = TYPE_PLAINDIR;
282 cinfo->type = TYPE_UNKNOWN;
284 else if (!strcmp(kp, "priority"))
285 cinfo->priority = atoi(vp);
286 else if (!strcmp(kp, "keeppackages"))
287 cinfo->keeppackages = *vp == '0' ? 0 : 1;
293 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
294 *nrepoinfosp = nrepoinfos;
299 free_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
302 for (i = 0; i < nrepoinfos; i++)
304 struct repoinfo *cinfo = repoinfos + i;
305 sat_free(cinfo->name);
306 sat_free(cinfo->alias);
307 sat_free(cinfo->path);
308 sat_free(cinfo->metalink);
309 sat_free(cinfo->baseurl);
320 strcpy(tmpl, "/var/tmp/solvXXXXXX");
332 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
339 h = sat_chksum_create(chksumtype);
342 printf("%s: unknown checksum type\n", file);
345 while ((l = read(fd, buf, sizeof(buf))) > 0)
346 sat_chksum_add(h, buf, l);
347 lseek(fd, 0, SEEK_SET);
349 sum = sat_chksum_get(h, &l);
350 if (memcmp(sum, chksum, l))
352 printf("%s: checksum mismatch\n", file);
359 findfastest(char **urls, int nurls)
366 struct addrinfo hints, *result;;
368 fds = sat_calloc(nurls, sizeof(*fds));
369 socks = sat_calloc(nurls, sizeof(*socks));
370 for (i = 0; i < nurls; i++)
373 p = strchr(urls[i], '/');
386 if ((p2 = strchr(p, '@')) != 0)
389 if (!strncmp("https:", urls[i], 6))
391 else if (!strncmp("ftp:", urls[i], 4))
393 if ((p2 = strrchr(p, ':')) != 0)
402 sprintf(portstr, "%d", port);
403 memset(&hints, 0, sizeof(struct addrinfo));
404 hints.ai_family = AF_UNSPEC;
405 hints.ai_socktype = SOCK_STREAM;
406 hints.ai_flags = AI_NUMERICSERV;
408 if (!getaddrinfo(p, portstr, &hints, &result))
410 socks[i] = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
413 fcntl(socks[i], F_SETFL, O_NONBLOCK);
414 if (connect(socks[i], result->ai_addr, result->ai_addrlen) == -1)
416 if (errno != EINPROGRESS)
423 freeaddrinfo(result);
430 for (i = j = 0; i < nurls; i++)
434 fds[j].fd = socks[i];
435 fds[j].events = POLLOUT;
443 if (poll(fds, j, 10000) <= 0)
445 i = -1; /* something is wrong */
448 for (i = 0; i < j; i++)
449 if ((fds[i].revents & POLLOUT) != 0)
452 socklen_t soel = sizeof(int);
453 if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, &soe, &soel) == -1 || soe != 0)
455 /* connect failed, kill socket */
456 for (j = 0; j < nurls; j++)
457 if (socks[j] == fds[i].fd)
470 i = -1; /* something is wrong, no bit was set */
473 /* now i contains the fastest fd index */
476 for (j = 0; j < nurls; j++)
477 if (socks[j] == fds[i].fd)
481 char *url0 = urls[0];
486 for (i = j = 0; i < nurls; i++)
494 findmetalinkurl(FILE *fp, unsigned char *chksump, Id *chksumtypep)
496 char buf[4096], *bp, *ep;
503 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
505 while (*bp == ' ' || *bp == '\t')
507 if (chksumtypep && !*chksumtypep && !strncmp(bp, "<hash type=\"sha256\">", 20))
512 memset(chksump, 0, 32);
513 for (i = 0; i < 64; i++)
516 if (c >= '0' && c <= '9')
517 chksump[i / 2] = chksump[i / 2] * 16 + (c - '0');
518 else if (c >= 'a' && c <= 'f')
519 chksump[i / 2] = chksump[i / 2] * 16 + (c - ('a' - 10));
520 else if (c >= 'A' && c <= 'F')
521 chksump[i / 2] = chksump[i / 2] * 16 + (c - ('A' - 10));
526 *chksumtypep = REPOKEY_TYPE_SHA256;
529 if (strncmp(bp, "<url", 4))
531 bp = strchr(bp, '>');
535 ep = strstr(bp, "repodata/repomd.xml</url>");
539 if (strncmp(bp, "http", 4))
541 urls = sat_extend(urls, nurls, 1, sizeof(*urls), 15);
542 urls[nurls++] = strdup(bp);
547 findfastest(urls, nurls > 5 ? 5 : nurls);
550 for (i = 0; i < nurls; i++)
553 ep = strchr(bp, '/');
554 if ((ep = strchr(ep + 2, '/')) != 0)
557 printf("[using mirror %s]\n", bp);
566 cookie_gzread(void *cookie, char *buf, size_t nbytes)
568 return gzread((gzFile *)cookie, buf, nbytes);
572 cookie_gzclose(void *cookie)
574 return gzclose((gzFile *)cookie);
578 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype, int *badchecksump)
584 const char *baseurl = cinfo->baseurl;
588 if (!cinfo->metalink)
590 if (file != cinfo->metalink)
592 FILE *fp = curlfopen(cinfo, cinfo->metalink, 0, 0, 0, 0);
593 unsigned char mlchksum[32];
597 cinfo->baseurl = findmetalinkurl(fp, mlchksum, &mlchksumtype);
601 if (!chksumtype && mlchksumtype && !strcmp(file, "repodata/repomd.xml"))
603 chksumtype = mlchksumtype;
606 return curlfopen(cinfo, file, uncompress, chksum, chksumtype, badchecksump);
608 snprintf(url, sizeof(url), "%s", file);
613 if (l && baseurl[l - 1] == '/')
614 snprintf(url, sizeof(url), "%s%s", baseurl, file);
616 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
619 // printf("url: %s\n", url);
620 if ((pid = fork()) == (pid_t)-1)
632 execlp("curl", "curl", "-f", "-s", "-L", url, (char *)0);
637 while (waitpid(pid, &status, 0) != pid)
639 if (lseek(fd, 0, SEEK_END) == 0 && (!status || !chksumtype))
645 lseek(fd, 0, SEEK_SET);
648 printf("%s: download error %d\n", file, status >> 8 ? status >> 8 : status);
654 if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
664 cookie_io_functions_t cio;
667 sprintf(tmpl, "/dev/fd/%d", fd);
668 gzf = gzopen(tmpl, "r");
672 fprintf(stderr, "could not open /dev/fd/%d, /proc not mounted?\n", fd);
675 memset(&cio, 0, sizeof(cio));
676 cio.read = cookie_gzread;
677 cio.close = cookie_gzclose;
678 return fopencookie(gzf, "r", cio);
680 fcntl(fd, F_SETFD, FD_CLOEXEC);
681 return fdopen(fd, "r");
685 cleanupgpg(char *gpgdir)
688 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
690 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
692 snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
694 snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
696 snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
702 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
711 off_t posfp, possigfp;
714 gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
717 keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
718 if (!(kfp = fopen(keysfile, "w")) )
724 for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
728 pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
729 if (!pubkey || !*pubkey)
731 if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
733 if (fputc('\n', kfp) == EOF) /* Just in case... */
737 if (fclose(kfp) || !nkeys)
742 snprintf(cmd, sizeof(cmd), "gpg2 -q --homedir %s --import %s", gpgdir, keysfile);
745 fprintf(stderr, "key import error\n");
750 posfp = lseek(fileno(fp), 0, SEEK_CUR);
751 lseek(fileno(fp), 0, SEEK_SET);
752 possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
753 lseek(fileno(sigfp), 0, SEEK_SET);
754 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
755 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
756 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
758 lseek(fileno(sigfp), possigfp, SEEK_SET);
759 lseek(fileno(fp), posfp, SEEK_SET);
760 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
761 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
763 return r == 0 ? 1 : 0;
766 #define CHKSUM_IDENT "1.1"
769 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
772 void *h = sat_chksum_create(chktype);
775 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
776 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
777 sat_chksum_add(h, buf, l);
779 sat_chksum_free(h, out);
783 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
785 void *h = sat_chksum_create(chktype);
786 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
787 sat_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
788 sat_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
789 sat_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
790 sat_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
791 sat_chksum_free(h, out);
803 pool_setarch(pool, un.machine);
806 char *calccachepath(Repo *repo, const char *repoext)
808 char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
811 p = pool_tmpjoin(repo->pool, p, "_", repoext);
812 p = pool_tmpjoin(repo->pool, p, ".solvx", 0);
815 p = pool_tmpjoin(repo->pool, p, ".solv", 0);
816 q = p + strlen(SOLVCACHE_PATH) + 1;
826 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie, int mark)
829 unsigned char mycookie[32];
830 struct repoinfo *cinfo;
833 cinfo = repo->appdata;
834 if (!(fp = fopen(calccachepath(repo, repoext), "r")))
836 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
841 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
850 flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
851 if (repoext && strcmp(repoext, "DL") != 0)
852 flags |= REPO_LOCALPOOL; /* no local pool for DL so that we can compare IDs */
854 if (repo_add_solv_flags(repo, fp, flags))
859 if (cinfo && !repoext)
861 /* set the checksum so that we can use it with the stub loads */
863 if (!fstat(fileno(fp), &stb))
868 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
869 for (i = 0; i < 32; i++)
870 cinfo->extcookie[i] ^= cinfo->cookie[i];
874 futimes(fileno(fp), 0); /* try to set modification time */
880 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
882 Id *addedfileprovides = 0;
887 struct repoinfo *cinfo;
890 cinfo = repo->appdata;
891 mkdir(SOLVCACHE_PATH, 0755);
892 tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
900 if (!(fp = fdopen(fd, "w")))
909 for (i = repo->start; i < repo->end; i++)
910 if (repo->pool->solvables[i].repo != repo)
919 info = repo_add_repodata(repo, 0);
922 pool_addfileprovides_ids(repo->pool, 0, &addedfileprovides);
923 if (addedfileprovides && *addedfileprovides)
925 for (i = 0; addedfileprovides[i]; i++)
926 repodata_add_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[i]);
928 sat_free(addedfileprovides);
929 repodata_internalize(info);
930 repo_write(repo, fp, repo_write_stdkeyfilter, 0, 0);
933 repodata_write(info, fp, repo_write_stdkeyfilter, 0);
936 if (fwrite(cookie, 32, 1, fp) != 1)
949 if (!repoext && cinfo)
951 /* set the checksum so that we can use it with the stub loads */
953 if (!stat(tmpl, &stb))
958 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
959 for (i = 0; i < 32; i++)
960 cinfo->extcookie[i] ^= cinfo->cookie[i];
965 /* switch to just saved repo to activate paging and save memory */
966 FILE *fp = fopen(tmpl, "r");
973 if (repo_add_solv_flags(repo, fp, SOLV_ADD_NO_STUBS))
975 /* oops, no way to recover from here */
976 fprintf(stderr, "internal error\n");
982 /* make sure repodata contains complete repo */
983 /* (this is how repodata_write saves it) */
984 repodata_extend_block(info, repo->start, repo->end - repo->start);
985 info->state = REPODATA_LOADING;
986 /* no need for LOCALPOOL as pool already contains ids */
987 repo_add_solv_flags(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
988 info->state = REPODATA_AVAILABLE; /* in case the load failed */
993 if (!rename(tmpl, calccachepath(repo, repoext)))
1002 Pool *sigpool = pool_create();
1003 Repo *repo = repo_create(sigpool, "rpmdbkeys");
1004 repo_add_rpmdb_pubkeys(repo, 0, 0);
1009 /* repomd helpers */
1011 static inline const char *
1012 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1014 Pool *pool = repo->pool;
1016 const char *filename;
1021 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1022 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1023 if (dataiterator_step(&di))
1025 dataiterator_setpos_parent(&di);
1026 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1027 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
1029 dataiterator_free(&di);
1030 if (filename && !*chksumtypep)
1032 printf("no %s file checksum!\n", what);
1039 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
1041 Pool *pool = repo->pool;
1043 Id chksumtype, handle;
1044 const unsigned char *chksum;
1045 const char *filename;
1047 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1048 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1049 if (!dataiterator_step(&di))
1051 dataiterator_free(&di);
1054 if (!strcmp(what, "prestodelta"))
1056 dataiterator_setpos_parent(&di);
1057 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1058 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, &chksumtype);
1059 if (!filename || !chksum)
1061 dataiterator_free(&di);
1064 handle = repodata_new_handle(data);
1065 repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
1066 repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
1068 repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
1069 if (!strcmp(what, "deltainfo"))
1071 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
1072 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
1074 if (!strcmp(what, "filelists"))
1076 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1077 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1079 dataiterator_free(&di);
1080 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1085 /* susetags helpers */
1087 static inline const char *
1088 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1090 Pool *pool = repo->pool;
1092 const char *filename;
1097 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
1098 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1099 if (dataiterator_step(&di))
1101 dataiterator_setpos_parent(&di);
1102 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
1105 dataiterator_free(&di);
1106 if (filename && !*chksumtypep)
1108 printf("no %s file checksum!\n", what);
1114 static Id susetags_langtags[] = {
1115 SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
1116 SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
1117 SOLVABLE_EULA, REPOKEY_TYPE_STR,
1118 SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
1119 SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
1120 SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
1125 susetags_add_ext(Repo *repo, Repodata *data)
1127 Pool *pool = repo->pool;
1130 Id handle, filechksumtype;
1131 const unsigned char *filechksum;
1134 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
1135 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1136 while (dataiterator_step(&di))
1138 if (strncmp(di.kv.str, "packages.", 9) != 0)
1140 if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
1142 ext[0] = di.kv.str[9];
1143 ext[1] = di.kv.str[10];
1145 if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
1147 handle = repodata_new_handle(data);
1148 repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
1150 repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
1151 if (!strcmp(ext, "DU"))
1153 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
1154 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
1156 if (!strcmp(ext, "FL"))
1158 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1159 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1163 for (i = 0; susetags_langtags[i]; i += 2)
1165 repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
1166 repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
1169 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1171 dataiterator_free(&di);
1176 iscompressed(const char *name)
1178 int l = strlen(name);
1179 return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
1186 load_stub(Pool *pool, Repodata *data, void *dp)
1188 const char *filename, *descrdir, *repomdtype;
1189 const unsigned char *filechksum;
1191 struct repoinfo *cinfo;
1196 cinfo = data->repo->appdata;
1198 filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
1202 ext[0] = filename[9];
1203 ext[1] = filename[10];
1206 printf("[%s:%s", data->repo->name, ext);
1208 if (usecachedrepo(data->repo, ext, cinfo->extcookie, 0))
1210 printf(" cached]\n"); fflush(stdout);
1214 printf(" loading]\n"); fflush(stdout);
1216 defvendor = repo_lookup_id(data->repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1217 descrdir = repo_lookup_str(data->repo, SOLVID_META, SUSETAGS_DESCRDIR);
1219 descrdir = "suse/setup/descr";
1221 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
1222 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1224 repo_add_susetags(data->repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1226 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
1230 repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
1233 if (!strcmp(repomdtype, "filelists"))
1235 else if (!strcmp(repomdtype, "deltainfo"))
1240 printf("[%s:%s", data->repo->name, ext);
1242 if (usecachedrepo(data->repo, ext, cinfo->extcookie, 0))
1244 printf(" cached]\n");fflush(stdout);
1247 printf(" loading]\n"); fflush(stdout);
1248 filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
1250 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, REPOSITORY_REPOMD_CHECKSUM, &filechksumtype);
1251 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1253 if (!strcmp(ext, "FL"))
1254 repo_add_rpmmd(data->repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1255 else if (!strcmp(ext, "DL"))
1256 repo_add_deltainfoxml(data->repo, fp, REPO_USE_LOADING);
1258 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
1266 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
1269 struct repoinfo *cinfo;
1273 const char *filename;
1274 const unsigned char *filechksum;
1276 const char *descrdir;
1279 unsigned char cookie[32];
1285 repo = repo_create(pool, "@System");
1286 printf("rpm database:");
1287 if (stat("/var/lib/rpm/Packages", &stb))
1288 memset(&stb, 0, sizeof(&stb));
1289 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cookie);
1290 if (usecachedrepo(repo, 0, cookie, 0))
1291 printf(" cached\n");
1295 printf(" reading\n");
1298 #ifdef PRODUCTS_PATH
1299 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
1301 if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1303 Repo *ref = repo_create(pool, "@System.old");
1304 if (!repo_add_solv(ref, ofp))
1306 repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
1313 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
1314 writecachedrepo(repo, 0, 0, cookie);
1316 pool_set_installed(pool, repo);
1318 for (i = 0; i < nrepoinfos; i++)
1320 cinfo = repoinfos + i;
1321 if (!cinfo->enabled)
1324 repo = repo_create(pool, cinfo->alias);
1326 repo->appdata = cinfo;
1327 repo->priority = 99 - cinfo->priority;
1329 dorefresh = cinfo->autorefresh;
1330 if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0), &stb) == 0)
1332 if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1335 if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1337 printf("repo '%s':", cinfo->alias);
1338 printf(" cached\n");
1342 switch (cinfo->type)
1345 printf("rpmmd repo '%s':", cinfo->alias);
1347 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1349 printf(" no repomd.xml file, skipped\n");
1354 printf("cinfo: %p\n", cinfo);
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)
1937 #define MODE_INSTALL 1
1938 #define MODE_ERASE 2
1939 #define MODE_UPDATE 3
1940 #define MODE_DISTUPGRADE 4
1941 #define MODE_VERIFY 5
1942 #define MODE_PATCH 6
1944 #define MODE_REPOLIST 8
1945 #define MODE_SEARCH 9
1950 fprintf(stderr, "Usage: solv COMMAND <select>\n");
1951 fprintf(stderr, "\n");
1952 fprintf(stderr, " distupgrade: replace installed packages with\n");
1953 fprintf(stderr, " versions from the repositories\n");
1954 fprintf(stderr, " erase: erase installed packages\n");
1955 fprintf(stderr, " info: display package information\n");
1956 fprintf(stderr, " install: install packages\n");
1957 fprintf(stderr, " list: list packages\n");
1958 fprintf(stderr, " repos: list enabled repositories\n");
1959 fprintf(stderr, " search: search name/summary/description\n");
1960 fprintf(stderr, " update: update installed packages\n");
1961 fprintf(stderr, "\n");
1966 main(int argc, char **argv)
1969 Repo *commandlinerepo = 0;
1970 Id *commandlinepkgs = 0;
1972 struct repoinfo *repoinfos;
1974 int mainmode = 0, mode = 0;
1979 char inbuf[128], *ip;
1982 struct fcstate fcstate;
1988 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
1990 mainmode = MODE_INSTALL;
1991 mode = SOLVER_INSTALL;
1993 else if (!strcmp(argv[0], "patch"))
1995 mainmode = MODE_PATCH;
1996 mode = SOLVER_INSTALL;
1998 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2000 mainmode = MODE_ERASE;
2001 mode = SOLVER_ERASE;
2003 else if (!strcmp(argv[0], "list"))
2005 mainmode = MODE_LIST;
2008 else if (!strcmp(argv[0], "info"))
2010 mainmode = MODE_INFO;
2013 else if (!strcmp(argv[0], "search"))
2015 mainmode = MODE_SEARCH;
2018 else if (!strcmp(argv[0], "verify"))
2020 mainmode = MODE_VERIFY;
2021 mode = SOLVER_VERIFY;
2023 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2025 mainmode = MODE_UPDATE;
2026 mode = SOLVER_UPDATE;
2028 else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2030 mainmode = MODE_DISTUPGRADE;
2031 mode = SOLVER_UPDATE;
2033 else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2035 mainmode = MODE_REPOLIST;
2041 pool = pool_create();
2043 pool->obsoleteusescolors = 1;
2045 pool_setloadcallback(pool, load_stub, 0);
2046 pool->nscallback = nscallback;
2047 // pool_setdebuglevel(pool, 2);
2049 repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
2051 if (mainmode == MODE_REPOLIST)
2054 for (i = 0; i < nrepoinfos; i++)
2056 struct repoinfo *cinfo = repoinfos + i;
2057 if (!cinfo->enabled)
2059 printf("%d: %-20s %s\n", j++, cinfo->alias, cinfo->name);
2064 read_repos(pool, repoinfos, nrepoinfos);
2066 if (mainmode == MODE_SEARCH)
2072 map_init(&m, pool->nsolvables);
2073 dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2074 dataiterator_set_keyname(&di, SOLVABLE_NAME);
2075 dataiterator_set_search(&di, 0, 0);
2076 while (dataiterator_step(&di))
2077 MAPSET(&m, di.solvid);
2078 dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2079 dataiterator_set_search(&di, 0, 0);
2080 while (dataiterator_step(&di))
2081 MAPSET(&m, di.solvid);
2082 dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2083 dataiterator_set_search(&di, 0, 0);
2084 while (dataiterator_step(&di))
2085 MAPSET(&m, di.solvid);
2086 dataiterator_free(&di);
2088 for (p = 1; p < pool->nsolvables; p++)
2090 Solvable *s = pool_id2solvable(pool, p);
2093 printf(" - %s: %s\n", solvable2str(pool, s), solvable_lookup_str(s, SOLVABLE_SUMMARY));
2100 if (mainmode == MODE_LIST || mainmode == MODE_INSTALL)
2102 for (i = 1; i < argc; i++)
2105 l = strlen(argv[i]);
2106 if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2108 if (access(argv[i], R_OK))
2113 if (!commandlinepkgs)
2114 commandlinepkgs = sat_calloc(argc, sizeof(Id));
2115 if (!commandlinerepo)
2116 commandlinerepo = repo_create(pool, "@commandline");
2117 repo_add_rpms(commandlinerepo, (const char **)argv + i, 1, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2118 commandlinepkgs[i] = commandlinerepo->end - 1;
2120 if (commandlinerepo)
2121 repo_internalize(commandlinerepo);
2124 // FOR_REPOS(i, repo)
2125 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2126 pool_addfileprovides(pool);
2127 pool_createwhatprovides(pool);
2130 for (i = 1; i < argc; i++)
2135 if (commandlinepkgs && commandlinepkgs[i])
2137 queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
2141 mkselect(pool, mode, argv[i], &job2);
2142 for (j = 0; j < job2.count; j++)
2143 queue_push(&job, job2.elements[j]);
2147 if (!job.count && mainmode != MODE_UPDATE && mainmode != MODE_DISTUPGRADE && mainmode != MODE_VERIFY && mainmode != MODE_PATCH)
2149 printf("no package matched\n");
2156 if (mainmode == MODE_LIST || mainmode == MODE_INFO)
2158 /* list mode, no solver needed */
2159 for (i = 0; i < job.count; i += 2)
2161 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
2163 Solvable *s = pool_id2solvable(pool, p);
2164 if (mainmode == MODE_INFO)
2166 printf("Name: %s\n", solvable2str(pool, s));
2167 printf("Repo: %s\n", s->repo->name);
2168 printf("Summary: %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
2169 printf("Url: %s\n", solvable_lookup_str(s, SOLVABLE_URL));
2170 printf("License: %s\n", solvable_lookup_str(s, SOLVABLE_LICENSE));
2171 printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
2176 const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de");
2177 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
2179 printf(" %s\n", sum);
2185 free_repoinfos(repoinfos, nrepoinfos);
2186 sat_free(commandlinepkgs);
2190 if (mainmode == MODE_PATCH)
2196 map_init(&installedmap, pool->nsolvables);
2197 if (pool->installed)
2198 FOR_REPO_SOLVABLES(pool->installed, p, s)
2199 MAPSET(&installedmap, p);
2201 /* install all patches */
2202 for (p = 1; p < pool->nsolvables; p++)
2208 s = pool->solvables + p;
2209 if (strncmp(id2str(pool, s->name), "patch:", 6) != 0)
2211 FOR_PROVIDES(p2, pp, s->name)
2213 Solvable *s2 = pool->solvables + p2;
2214 if (s2->name != s->name)
2216 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2217 if (r < 0 || (r == 0 && p > p2))
2222 type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2223 if (type && !strcmp(type, "optional"))
2225 r = solvable_trivial_installable_map(s, &installedmap, 0);
2228 if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2235 queue_push2(&job, SOLVER_SOLVABLE, p);
2237 map_free(&installedmap);
2241 for (i = 0; i < job.count; i += 2)
2243 if (mode == SOLVER_UPDATE)
2245 /* make update of not installed packages an install */
2246 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
2247 if (pool->installed && pool->solvables[p].repo == pool->installed)
2251 job.elements[i] |= SOLVER_INSTALL;
2255 job.elements[i] |= mode;
2258 // multiversion test
2259 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
2260 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
2261 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
2266 Id problem, solution;
2269 solv = solver_create(pool);
2270 solv->ignorealreadyrecommended = 1;
2271 solv->updatesystem = allpkgs && (mainmode == MODE_UPDATE || mainmode == MODE_DISTUPGRADE);
2272 solv->dosplitprovides = solv->updatesystem;
2273 solv->fixsystem = allpkgs && (mainmode == MODE_VERIFY);
2274 if (mainmode == MODE_DISTUPGRADE)
2276 solv->distupgrade = 1;
2277 solv->allowdowngrade = 1;
2278 solv->allowarchchange = 1;
2279 solv->allowvendorchange = 1;
2281 /* infarch check currently doesn't work with colors */
2282 solv->noinfarchcheck = pool->obsoleteusescolors;
2284 // queue_push2(&job, SOLVER_DISTUPGRADE, 3);
2285 solver_solve(solv, &job);
2286 if (!solv->problems.count)
2288 pcnt = solver_problem_count(solv);
2289 printf("Found %d problems:\n", pcnt);
2290 for (problem = 1; problem <= pcnt; problem++)
2293 printf("Problem %d:\n", problem);
2294 solver_printprobleminfo(solv, problem);
2296 scnt = solver_solution_count(solv, problem);
2297 for (solution = 1; solution <= scnt; solution++)
2299 printf("Solution %d:\n", solution);
2300 solver_printsolution(solv, problem, solution);
2305 printf("Please choose a solution: ");
2308 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2313 while (*ip == ' ' || *ip == '\t')
2315 if (*ip >= '0' && *ip <= '9')
2318 if (take >= 1 && take <= scnt)
2334 solver_take_solution(solv, problem, take, &job);
2340 trans = &solv->trans;
2341 if (!trans->steps.count)
2343 printf("Nothing to do.\n");
2347 printf("Transaction summary:\n\n");
2348 solver_printtransaction(solv);
2357 duc[1].path = "/usr/share/man";
2358 duc[2].path = "/sbin";
2359 duc[3].path = "/etc";
2360 transaction_calc_duchanges(trans, duc, 4);
2361 for (i = 0; i < 4; i++)
2362 printf("duchanges %s: %d K %d\n", duc[i].path, duc[i].kbytes, duc[i].files);
2363 printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
2367 if (!yesno("OK to continue (y/n)? "))
2373 queue_init(&checkq);
2374 newpkgs = transaction_installedresult(trans, &checkq);
2379 printf("Downloading %d packages\n", newpkgs);
2380 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
2381 for (i = 0; i < newpkgs; i++)
2383 unsigned int medianr;
2386 struct repoinfo *cinfo;
2387 const unsigned char *chksum;
2391 p = checkq.elements[i];
2392 s = pool_id2solvable(pool, p);
2393 if (s->repo == commandlinerepo)
2395 loc = solvable_get_location(s, &medianr);
2396 if (!(newpkgsfps[i] = fopen(loc, "r")))
2404 cinfo = s->repo->appdata;
2407 printf("%s: no repository information\n", s->repo->name);
2410 loc = solvable_get_location(s, &medianr);
2414 if (pool->installed && pool->installed->nsolvables)
2416 /* try a delta first */
2417 char *matchname = strdup(id2str(pool, s->name));
2418 dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
2419 dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
2420 while (dataiterator_step(&di))
2424 dataiterator_setpos_parent(&di);
2425 if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
2426 pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
2428 baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
2429 FOR_PROVIDES(op, pp, s->name)
2431 Solvable *os = pool->solvables + op;
2432 if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
2435 if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
2437 /* base is installed, run sequence check */
2438 const char *seqname;
2447 seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
2448 seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
2449 seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
2450 seq = pool_tmpjoin(pool, seqname, "-", seqevr);
2451 seq = pool_tmpjoin(pool, seq, "-", seqnum);
2453 sprintf(cmd, "/usr/bin/applydeltarpm -a %s -c -s ", id2str(pool, s->arch));
2455 sprintf(cmd, "/usr/bin/applydeltarpm -c -s ");
2457 if (system(pool_tmpjoin(pool, cmd, seq, 0)) != 0)
2458 continue; /* didn't match */
2459 /* looks good, download delta */
2461 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
2463 continue; /* no way! */
2464 dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
2465 dloc = pool_tmpjoin(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
2466 dloc = pool_tmpjoin(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
2467 dloc = pool_tmpjoin(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
2468 if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
2470 /* got it, now reconstruct */
2471 newfd = opentmpfile();
2473 sprintf(cmd, "applydeltarpm -a %s /dev/fd/%d /dev/fd/%d", id2str(pool, s->arch), fileno(fp), newfd);
2475 sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
2477 fcntl(fileno(fp), F_SETFD, 0);
2484 lseek(newfd, 0, SEEK_SET);
2486 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2487 if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
2493 newpkgsfps[i] = fdopen(newfd, "r");
2498 dataiterator_free(&di);
2499 sat_free(matchname);
2506 continue; /* delta worked! */
2508 if (cinfo->type == TYPE_SUSETAGS)
2510 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
2511 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
2514 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2515 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
2517 printf("\n%s: %s not found in repository\n", s->repo->name, loc);
2530 printf("Searching for file conflicts\n");
2531 queue_init(&conflicts);
2532 fcstate.rpmdbstate = 0;
2533 fcstate.newpkgscnt = newpkgs;
2534 fcstate.checkq = &checkq;
2535 fcstate.newpkgsfps = newpkgsfps;
2536 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fileconflict_cb, &fcstate);
2537 if (conflicts.count)
2540 for (i = 0; i < conflicts.count; i += 5)
2541 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]));
2543 if (yesno("Re-run solver (y/n/q)? "))
2545 for (i = 0; i < newpkgs; i++)
2547 fclose(newpkgsfps[i]);
2548 newpkgsfps = sat_free(newpkgsfps);
2550 pool_add_fileconflicts_deps(pool, &conflicts);
2551 pool_createwhatprovides(pool); /* Hmm... */
2555 queue_free(&conflicts);
2558 printf("Committing transaction:\n\n");
2559 transaction_order(trans, 0);
2560 for (i = 0; i < trans->steps.count; i++)
2562 const char *evr, *evrp, *nvra;
2567 p = trans->steps.elements[i];
2568 s = pool_id2solvable(pool, p);
2569 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
2572 case SOLVER_TRANSACTION_ERASE:
2573 printf("erase %s\n", solvid2str(pool, p));
2574 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
2576 /* strip epoch from evr */
2577 evr = evrp = id2str(pool, s->evr);
2578 while (*evrp >= '0' && *evrp <= '9')
2580 if (evrp > evr && evrp[0] == ':' && evrp[1])
2582 nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
2583 nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
2584 runrpm("-e", nvra, -1); /* to bad that --querybynumber doesn't work */
2586 case SOLVER_TRANSACTION_INSTALL:
2587 case SOLVER_TRANSACTION_MULTIINSTALL:
2588 printf("install %s\n", solvid2str(pool, p));
2589 for (j = 0; j < newpkgs; j++)
2590 if (checkq.elements[j] == p)
2592 fp = j < newpkgs ? newpkgsfps[j] : 0;
2596 lseek(fileno(fp), 0, SEEK_SET);
2597 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
2606 for (i = 0; i < newpkgs; i++)
2608 fclose(newpkgsfps[i]);
2609 sat_free(newpkgsfps);
2610 queue_free(&checkq);
2614 free_repoinfos(repoinfos, nrepoinfos);
2615 sat_free(commandlinepkgs);