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)
90 unsigned char cookie[32];
91 unsigned char extcookie[32];
96 yum_substitute(Pool *pool, char *line)
99 static char *releaseevr;
100 static char *basearch;
104 sat_free(releaseevr);
111 while ((p2 = strchr(p, '$')) != 0)
113 if (!strncmp(p2, "$releasever", 11))
120 rpm_installedrpmdbids(0, "Providename", "redhat-release", &q);
123 void *handle, *state = 0;
125 handle = rpm_byrpmdbid(q.elements[0], 0, &state);
126 releaseevr = rpm_query(handle, SOLVABLE_EVR);
127 rpm_byrpmdbid(0, 0, &state);
128 if ((p = strchr(releaseevr, '-')) != 0)
133 releaseevr = strdup("?");
136 p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
137 p2 = p + (p2 - line);
139 p = p2 + strlen(releaseevr);
142 if (!strncmp(p2, "$basearch", 9))
152 basearch = strdup(un.machine);
153 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
157 p = pool_tmpjoin(pool, line, basearch, p2 + 9);
158 p2 = p + (p2 - line);
160 p = p2 + strlen(basearch);
169 #define TYPE_UNKNOWN 0
170 #define TYPE_SUSETAGS 1
172 #define TYPE_PLAINDIR 3
175 read_repoinfos_sort(const void *ap, const void *bp)
177 const struct repoinfo *a = ap;
178 const struct repoinfo *b = bp;
179 return strcmp(a->alias, b->alias);
183 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
186 char buf2[4096], *kp, *vp, *kpe;
191 struct repoinfo *repoinfos = 0, *cinfo;
194 rdlen = strlen(reposdir);
195 dir = opendir(reposdir);
201 while ((ent = readdir(dir)) != 0)
203 l = strlen(ent->d_name);
204 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
206 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
207 if ((fp = fopen(buf, "r")) == 0)
213 while(fgets(buf2, sizeof(buf2), fp))
218 while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
221 while (*kp == ' ' || *kp == '\t')
223 if (!*kp || *kp == '#')
227 kp = yum_substitute(pool, kp);
231 vp = strrchr(kp, ']');
235 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
236 cinfo = repoinfos + nrepoinfos++;
237 memset(cinfo, 0, sizeof(*cinfo));
238 cinfo->alias = strdup(kp + 1);
239 cinfo->type = TYPE_RPMMD;
240 cinfo->autorefresh = 1;
242 cinfo->repo_gpgcheck = 1;
244 cinfo->metadata_expire = METADATA_EXPIRE;
249 vp = strchr(kp, '=');
252 for (kpe = vp - 1; kpe >= kp; kpe--)
253 if (*kpe != ' ' && *kpe != '\t')
258 while (*vp == ' ' || *vp == '\t')
261 if (!strcmp(kp, "name"))
262 cinfo->name = strdup(vp);
263 else if (!strcmp(kp, "enabled"))
264 cinfo->enabled = *vp == '0' ? 0 : 1;
265 else if (!strcmp(kp, "autorefresh"))
266 cinfo->autorefresh = *vp == '0' ? 0 : 1;
267 else if (!strcmp(kp, "gpgcheck"))
268 cinfo->pkgs_gpgcheck = *vp == '0' ? 0 : 1;
269 else if (!strcmp(kp, "repo_gpgcheck"))
270 cinfo->repo_gpgcheck = *vp == '0' ? 0 : 1;
271 else if (!strcmp(kp, "baseurl"))
272 cinfo->baseurl = strdup(vp);
273 else if (!strcmp(kp, "mirrorlist"))
275 if (strstr(vp, "metalink"))
276 cinfo->metalink = strdup(vp);
278 cinfo->mirrorlist = strdup(vp);
280 else if (!strcmp(kp, "path"))
282 if (vp && strcmp(vp, "/") != 0)
283 cinfo->path = strdup(vp);
285 else if (!strcmp(kp, "type"))
287 if (!strcmp(vp, "yast2"))
288 cinfo->type = TYPE_SUSETAGS;
289 else if (!strcmp(vp, "rpm-md"))
290 cinfo->type = TYPE_RPMMD;
291 else if (!strcmp(vp, "plaindir"))
292 cinfo->type = TYPE_PLAINDIR;
294 cinfo->type = TYPE_UNKNOWN;
296 else if (!strcmp(kp, "priority"))
297 cinfo->priority = atoi(vp);
298 else if (!strcmp(kp, "keeppackages"))
299 cinfo->keeppackages = *vp == '0' ? 0 : 1;
305 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
306 *nrepoinfosp = nrepoinfos;
311 free_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
314 for (i = 0; i < nrepoinfos; i++)
316 struct repoinfo *cinfo = repoinfos + i;
317 sat_free(cinfo->name);
318 sat_free(cinfo->alias);
319 sat_free(cinfo->path);
320 sat_free(cinfo->metalink);
321 sat_free(cinfo->mirrorlist);
322 sat_free(cinfo->baseurl);
333 strcpy(tmpl, "/var/tmp/solvXXXXXX");
345 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
352 h = sat_chksum_create(chksumtype);
355 printf("%s: unknown checksum type\n", file);
358 while ((l = read(fd, buf, sizeof(buf))) > 0)
359 sat_chksum_add(h, buf, l);
360 lseek(fd, 0, SEEK_SET);
362 sum = sat_chksum_get(h, &l);
363 if (memcmp(sum, chksum, l))
365 printf("%s: checksum mismatch\n", file);
366 sat_chksum_free(h, 0);
369 sat_chksum_free(h, 0);
374 findfastest(char **urls, int nurls)
381 struct addrinfo hints, *result;;
383 fds = sat_calloc(nurls, sizeof(*fds));
384 socks = sat_calloc(nurls, sizeof(*socks));
385 for (i = 0; i < nurls; i++)
388 p = strchr(urls[i], '/');
401 if ((p2 = strchr(p, '@')) != 0)
404 if (!strncmp("https:", urls[i], 6))
406 else if (!strncmp("ftp:", urls[i], 4))
408 if ((p2 = strrchr(p, ':')) != 0)
417 sprintf(portstr, "%d", port);
418 memset(&hints, 0, sizeof(struct addrinfo));
419 hints.ai_family = AF_UNSPEC;
420 hints.ai_socktype = SOCK_STREAM;
421 hints.ai_flags = AI_NUMERICSERV;
423 if (!getaddrinfo(p, portstr, &hints, &result))
425 socks[i] = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
428 fcntl(socks[i], F_SETFL, O_NONBLOCK);
429 if (connect(socks[i], result->ai_addr, result->ai_addrlen) == -1)
431 if (errno != EINPROGRESS)
438 freeaddrinfo(result);
445 for (i = j = 0; i < nurls; i++)
449 fds[j].fd = socks[i];
450 fds[j].events = POLLOUT;
458 if (poll(fds, j, 10000) <= 0)
460 i = -1; /* something is wrong */
463 for (i = 0; i < j; i++)
464 if ((fds[i].revents & POLLOUT) != 0)
467 socklen_t soel = sizeof(int);
468 if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, &soe, &soel) == -1 || soe != 0)
470 /* connect failed, kill socket */
471 for (j = 0; j < nurls; j++)
472 if (socks[j] == fds[i].fd)
485 i = -1; /* something is wrong, no bit was set */
488 /* now i contains the fastest fd index */
491 for (j = 0; j < nurls; j++)
492 if (socks[j] == fds[i].fd)
496 char *url0 = urls[0];
501 for (i = j = 0; i < nurls; i++)
509 findmetalinkurl(FILE *fp, unsigned char *chksump, Id *chksumtypep)
511 char buf[4096], *bp, *ep;
518 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
520 while (*bp == ' ' || *bp == '\t')
522 if (chksumtypep && !*chksumtypep && !strncmp(bp, "<hash type=\"sha256\">", 20))
527 memset(chksump, 0, 32);
528 for (i = 0; i < 64; i++)
531 if (c >= '0' && c <= '9')
532 chksump[i / 2] = chksump[i / 2] * 16 + (c - '0');
533 else if (c >= 'a' && c <= 'f')
534 chksump[i / 2] = chksump[i / 2] * 16 + (c - ('a' - 10));
535 else if (c >= 'A' && c <= 'F')
536 chksump[i / 2] = chksump[i / 2] * 16 + (c - ('A' - 10));
541 *chksumtypep = REPOKEY_TYPE_SHA256;
544 if (strncmp(bp, "<url", 4))
546 bp = strchr(bp, '>');
550 ep = strstr(bp, "repodata/repomd.xml</url>");
554 if (strncmp(bp, "http", 4))
556 urls = sat_extend(urls, nurls, 1, sizeof(*urls), 15);
557 urls[nurls++] = strdup(bp);
562 findfastest(urls, nurls > 5 ? 5 : nurls);
565 for (i = 0; i < nurls; i++)
568 ep = strchr(bp, '/');
569 if ((ep = strchr(ep + 2, '/')) != 0)
572 printf("[using mirror %s]\n", bp);
581 findmirrorlisturl(FILE *fp)
583 char buf[4096], *bp, *ep;
588 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
590 while (*bp == ' ' || *bp == '\t')
592 if (!*bp || *bp == '#')
595 while (l > 0 && (bp[l - 1] == ' ' || bp[l - 1] == '\t' || bp[l - 1] == '\n'))
597 urls = sat_extend(urls, nurls, 1, sizeof(*urls), 15);
598 urls[nurls++] = strdup(bp);
603 findfastest(urls, nurls > 5 ? 5 : nurls);
606 for (i = 0; i < nurls; i++)
609 ep = strchr(bp, '/');
610 if ((ep = strchr(ep + 2, '/')) != 0)
613 printf("[using mirror %s]\n", bp);
622 cookie_gzread(void *cookie, char *buf, size_t nbytes)
624 return gzread((gzFile *)cookie, buf, nbytes);
628 cookie_gzclose(void *cookie)
630 return gzclose((gzFile *)cookie);
634 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype, int *badchecksump)
640 const char *baseurl = cinfo->baseurl;
644 if (!cinfo->metalink && !cinfo->mirrorlist)
646 if (file != cinfo->metalink && file != cinfo->mirrorlist)
648 FILE *fp = curlfopen(cinfo, cinfo->metalink ? cinfo->metalink : cinfo->mirrorlist, 0, 0, 0, 0);
649 unsigned char mlchksum[32];
654 cinfo->baseurl = findmetalinkurl(fp, mlchksum, &mlchksumtype);
656 cinfo->baseurl = findmirrorlisturl(fp);
661 if (strchr(cinfo->baseurl, '$'))
663 char *b = yum_substitute(cinfo->repo->pool, cinfo->baseurl);
664 free(cinfo->baseurl);
665 cinfo->baseurl = strdup(b);
668 if (!chksumtype && mlchksumtype && !strcmp(file, "repodata/repomd.xml"))
670 chksumtype = mlchksumtype;
673 return curlfopen(cinfo, file, uncompress, chksum, chksumtype, badchecksump);
675 snprintf(url, sizeof(url), "%s", file);
680 if (l && baseurl[l - 1] == '/')
681 snprintf(url, sizeof(url), "%s%s", baseurl, file);
683 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
686 // printf("url: %s\n", url);
687 if ((pid = fork()) == (pid_t)-1)
699 execlp("curl", "curl", "-f", "-s", "-L", url, (char *)0);
704 while (waitpid(pid, &status, 0) != pid)
706 if (lseek(fd, 0, SEEK_END) == 0 && (!status || !chksumtype))
712 lseek(fd, 0, SEEK_SET);
715 printf("%s: download error %d\n", file, status >> 8 ? status >> 8 : status);
721 if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
731 cookie_io_functions_t cio;
734 sprintf(tmpl, "/dev/fd/%d", fd);
735 gzf = gzopen(tmpl, "r");
739 fprintf(stderr, "could not open /dev/fd/%d, /proc not mounted?\n", fd);
742 memset(&cio, 0, sizeof(cio));
743 cio.read = cookie_gzread;
744 cio.close = cookie_gzclose;
745 return fopencookie(gzf, "r", cio);
747 fcntl(fd, F_SETFD, FD_CLOEXEC);
748 return fdopen(fd, "r");
752 cleanupgpg(char *gpgdir)
755 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
757 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
759 snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
761 snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
763 snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
769 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
778 off_t posfp, possigfp;
781 gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
784 keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
785 if (!(kfp = fopen(keysfile, "w")) )
791 for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
795 pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
796 if (!pubkey || !*pubkey)
798 if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
800 if (fputc('\n', kfp) == EOF) /* Just in case... */
804 if (fclose(kfp) || !nkeys)
809 snprintf(cmd, sizeof(cmd), "gpg2 -q --homedir %s --import %s", gpgdir, keysfile);
812 fprintf(stderr, "key import error\n");
817 posfp = lseek(fileno(fp), 0, SEEK_CUR);
818 lseek(fileno(fp), 0, SEEK_SET);
819 possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
820 lseek(fileno(sigfp), 0, SEEK_SET);
821 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
822 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
823 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
825 lseek(fileno(sigfp), possigfp, SEEK_SET);
826 lseek(fileno(fp), posfp, SEEK_SET);
827 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
828 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
830 return r == 0 ? 1 : 0;
833 #define CHKSUM_IDENT "1.1"
836 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
839 void *h = sat_chksum_create(chktype);
842 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
843 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
844 sat_chksum_add(h, buf, l);
846 sat_chksum_free(h, out);
850 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
852 void *h = sat_chksum_create(chktype);
853 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
854 sat_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
855 sat_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
856 sat_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
857 sat_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
858 sat_chksum_free(h, out);
870 pool_setarch(pool, un.machine);
873 char *calccachepath(Repo *repo, const char *repoext)
875 char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
878 p = pool_tmpjoin(repo->pool, p, "_", repoext);
879 p = pool_tmpjoin(repo->pool, p, ".solvx", 0);
882 p = pool_tmpjoin(repo->pool, p, ".solv", 0);
883 q = p + strlen(SOLVCACHE_PATH) + 1;
893 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie, int mark)
896 unsigned char mycookie[32];
897 unsigned char myextcookie[32];
898 struct repoinfo *cinfo;
901 cinfo = repo->appdata;
902 if (!(fp = fopen(calccachepath(repo, repoext), "r")))
904 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
909 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
914 if (cinfo && !repoext)
916 if (fseek(fp, -sizeof(mycookie) * 2, SEEK_END) || fread(myextcookie, sizeof(myextcookie), 1, fp) != 1)
926 flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
927 if (repoext && strcmp(repoext, "DL") != 0)
928 flags |= REPO_LOCALPOOL; /* no local pool for DL so that we can compare IDs */
930 if (repo_add_solv_flags(repo, fp, flags))
935 if (cinfo && !repoext)
937 memcpy(cinfo->cookie, mycookie, sizeof(mycookie));
938 memcpy(cinfo->extcookie, myextcookie, sizeof(myextcookie));
941 futimes(fileno(fp), 0); /* try to set modification time */
947 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
952 struct repoinfo *cinfo;
955 cinfo = repo->appdata;
956 mkdir(SOLVCACHE_PATH, 0755);
957 tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
965 if (!(fp = fdopen(fd, "w")))
974 for (i = repo->start; i < repo->end; i++)
975 if (repo->pool->solvables[i].repo != repo)
981 repo_write(repo, fp, repo_write_stdkeyfilter, 0, 0);
983 repodata_write(info, fp, repo_write_stdkeyfilter, 0);
986 int oldnrepodata = repo->nrepodata;
987 repo->nrepodata = 1; /* XXX: do this right */
988 repo_write(repo, fp, repo_write_stdkeyfilter, 0, 0);
989 repo->nrepodata = oldnrepodata;
993 if (!repoext && cinfo)
995 if (!cinfo->extcookie[0])
997 /* create the ext cookie and append it */
998 /* we just need some unique ID */
1000 if (!fstat(fileno(fp), &stb))
1004 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
1005 for (i = 0; i < 32; i++)
1006 cinfo->extcookie[i] ^= cookie[i];
1008 if (cinfo->extcookie[0] == 0)
1009 cinfo->extcookie[0] = 1;
1011 if (fwrite(cinfo->extcookie, 32, 1, fp) != 1)
1019 /* append our cookie describing the metadata state */
1020 if (fwrite(cookie, 32, 1, fp) != 1)
1035 /* switch to just saved repo to activate paging and save memory */
1036 FILE *fp = fopen(tmpl, "r");
1042 repo_empty(repo, 1);
1043 if (repo_add_solv_flags(repo, fp, SOLV_ADD_NO_STUBS))
1045 /* oops, no way to recover from here */
1046 fprintf(stderr, "internal error\n");
1052 /* make sure repodata contains complete repo */
1053 /* (this is how repodata_write saves it) */
1054 repodata_extend_block(info, repo->start, repo->end - repo->start);
1055 info->state = REPODATA_LOADING;
1056 /* no need for LOCALPOOL as pool already contains ids */
1057 repo_add_solv_flags(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1058 info->state = REPODATA_AVAILABLE; /* in case the load failed */
1063 if (!rename(tmpl, calccachepath(repo, repoext)))
1072 Pool *sigpool = pool_create();
1073 Repo *repo = repo_create(sigpool, "rpmdbkeys");
1074 repo_add_rpmdb_pubkeys(repo, 0, 0);
1079 /* repomd helpers */
1081 static inline const char *
1082 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1084 Pool *pool = repo->pool;
1086 const char *filename;
1091 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1092 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1093 if (dataiterator_step(&di))
1095 dataiterator_setpos_parent(&di);
1096 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1097 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
1099 dataiterator_free(&di);
1100 if (filename && !*chksumtypep)
1102 printf("no %s file checksum!\n", what);
1109 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
1111 Pool *pool = repo->pool;
1113 Id chksumtype, handle;
1114 const unsigned char *chksum;
1115 const char *filename;
1117 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1118 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1119 if (!dataiterator_step(&di))
1121 dataiterator_free(&di);
1124 if (!strcmp(what, "prestodelta"))
1126 dataiterator_setpos_parent(&di);
1127 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1128 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, &chksumtype);
1129 if (!filename || !chksum)
1131 dataiterator_free(&di);
1134 handle = repodata_new_handle(data);
1135 repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
1136 repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
1138 repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
1139 if (!strcmp(what, "deltainfo"))
1141 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
1142 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
1144 if (!strcmp(what, "filelists"))
1146 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1147 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1149 dataiterator_free(&di);
1150 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1155 /* susetags helpers */
1157 static inline const char *
1158 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1160 Pool *pool = repo->pool;
1162 const char *filename;
1167 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
1168 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1169 if (dataiterator_step(&di))
1171 dataiterator_setpos_parent(&di);
1172 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
1175 dataiterator_free(&di);
1176 if (filename && !*chksumtypep)
1178 printf("no %s file checksum!\n", what);
1184 static Id susetags_langtags[] = {
1185 SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
1186 SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
1187 SOLVABLE_EULA, REPOKEY_TYPE_STR,
1188 SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
1189 SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
1190 SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
1195 susetags_add_ext(Repo *repo, Repodata *data)
1197 Pool *pool = repo->pool;
1200 Id handle, filechksumtype;
1201 const unsigned char *filechksum;
1204 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
1205 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1206 while (dataiterator_step(&di))
1208 if (strncmp(di.kv.str, "packages.", 9) != 0)
1210 if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
1212 ext[0] = di.kv.str[9];
1213 ext[1] = di.kv.str[10];
1215 if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
1217 handle = repodata_new_handle(data);
1218 repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
1220 repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
1221 if (!strcmp(ext, "DU"))
1223 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
1224 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
1226 if (!strcmp(ext, "FL"))
1228 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1229 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1233 for (i = 0; susetags_langtags[i]; i += 2)
1235 repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
1236 repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
1239 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1241 dataiterator_free(&di);
1246 iscompressed(const char *name)
1248 int l = strlen(name);
1249 return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
1256 load_stub(Pool *pool, Repodata *data, void *dp)
1258 const char *filename, *descrdir, *repomdtype;
1259 const unsigned char *filechksum;
1261 struct repoinfo *cinfo;
1266 cinfo = data->repo->appdata;
1268 filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
1272 ext[0] = filename[9];
1273 ext[1] = filename[10];
1276 printf("[%s:%s", data->repo->name, ext);
1278 if (usecachedrepo(data->repo, ext, cinfo->extcookie, 0))
1280 printf(" cached]\n"); fflush(stdout);
1284 printf(" fetching]\n"); fflush(stdout);
1286 defvendor = repo_lookup_id(data->repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1287 descrdir = repo_lookup_str(data->repo, SOLVID_META, SUSETAGS_DESCRDIR);
1289 descrdir = "suse/setup/descr";
1291 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
1292 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1294 repo_add_susetags(data->repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1296 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
1300 repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
1303 if (!strcmp(repomdtype, "filelists"))
1305 else if (!strcmp(repomdtype, "deltainfo"))
1310 printf("[%s:%s", data->repo->name, ext);
1312 if (usecachedrepo(data->repo, ext, cinfo->extcookie, 0))
1314 printf(" cached]\n");fflush(stdout);
1317 printf(" fetching]\n"); fflush(stdout);
1318 filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
1320 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, REPOSITORY_REPOMD_CHECKSUM, &filechksumtype);
1321 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1323 if (!strcmp(ext, "FL"))
1324 repo_add_rpmmd(data->repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1325 else if (!strcmp(ext, "DL"))
1326 repo_add_deltainfoxml(data->repo, fp, REPO_USE_LOADING);
1328 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
1335 static unsigned char installedcookie[32];
1338 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
1341 struct repoinfo *cinfo;
1345 const char *filename;
1346 const unsigned char *filechksum;
1348 const char *descrdir;
1356 repo = repo_create(pool, "@System");
1357 printf("rpm database:");
1358 if (stat("/var/lib/rpm/Packages", &stb))
1359 memset(&stb, 0, sizeof(&stb));
1360 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, installedcookie);
1361 if (usecachedrepo(repo, 0, installedcookie, 0))
1362 printf(" cached\n");
1366 printf(" reading\n");
1369 #ifdef PRODUCTS_PATH
1370 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
1372 if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1374 Repo *ref = repo_create(pool, "@System.old");
1375 if (!repo_add_solv(ref, ofp))
1377 repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
1384 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
1385 writecachedrepo(repo, 0, 0, installedcookie);
1387 pool_set_installed(pool, repo);
1389 for (i = 0; i < nrepoinfos; i++)
1391 cinfo = repoinfos + i;
1392 if (!cinfo->enabled)
1395 repo = repo_create(pool, cinfo->alias);
1397 repo->appdata = cinfo;
1398 repo->priority = 99 - cinfo->priority;
1400 dorefresh = cinfo->autorefresh;
1401 if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0), &stb) == 0)
1403 if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1406 if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1408 printf("repo '%s':", cinfo->alias);
1409 printf(" cached\n");
1413 switch (cinfo->type)
1416 printf("rpmmd repo '%s':", cinfo->alias);
1418 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1420 printf(" no repomd.xml file, skipped\n");
1425 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1426 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1428 printf(" cached\n");
1432 if (cinfo->repo_gpgcheck)
1434 sigfp = curlfopen(cinfo, "repodata/repomd.xml.asc", 0, 0, 0, 0);
1437 printf(" unsigned, skipped\n");
1442 sigpool = read_sigs();
1443 if (!checksig(sigpool, fp, sigfp))
1445 printf(" checksig failed, skipped\n");
1452 repo_add_repomdxml(repo, fp, 0);
1454 printf(" fetching\n");
1455 filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1456 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1458 repo_add_rpmmd(repo, fp, 0, 0);
1462 break; /* hopeless */
1464 filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1465 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1467 repo_add_updateinfoxml(repo, fp, 0);
1471 data = repo_add_repodata(repo, 0);
1472 if (!repomd_add_ext(repo, data, "deltainfo"))
1473 repomd_add_ext(repo, data, "prestodelta");
1474 repomd_add_ext(repo, data, "filelists");
1475 repodata_internalize(data);
1477 writecachedrepo(repo, 0, 0, cinfo->cookie);
1478 repodata_create_stubs(repo_last_repodata(repo));
1482 printf("susetags repo '%s':", cinfo->alias);
1486 if ((fp = curlfopen(cinfo, "content", 0, 0, 0, 0)) == 0)
1488 printf(" no content file, skipped\n");
1493 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1494 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1496 printf(" cached\n");
1500 if (cinfo->repo_gpgcheck)
1502 sigfp = curlfopen(cinfo, "content.asc", 0, 0, 0, 0);
1505 printf(" unsigned, skipped\n");
1512 sigpool = read_sigs();
1513 if (!checksig(sigpool, fp, sigfp))
1515 printf(" checksig failed, skipped\n");
1523 repo_add_content(repo, fp, 0);
1525 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1526 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1528 descrdir = "suse/setup/descr";
1529 filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1531 filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1534 printf(" no packages file entry, skipped\n");
1537 printf(" fetching\n");
1538 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
1539 break; /* hopeless */
1540 repo_add_susetags(repo, fp, defvendor, 0, 0);
1542 data = repo_add_repodata(repo, 0);
1543 susetags_add_ext(repo, data);
1544 repodata_internalize(data);
1546 writecachedrepo(repo, 0, 0, cinfo->cookie);
1547 repodata_create_stubs(repo_last_repodata(repo));
1550 printf("unsupported repo '%s': skipped\n", cinfo->alias);
1562 str2archid(Pool *pool, char *arch)
1567 id = str2id(pool, arch, 0);
1568 if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
1570 if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
1576 depglob(Pool *pool, char *name, Queue *job)
1579 Id id = str2id(pool, name, 0);
1584 FOR_PROVIDES(p, pp, id)
1586 Solvable *s = pool->solvables + p;
1590 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
1596 printf("[using capability match for '%s']\n", name);
1597 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
1602 if (strpbrk(name, "[*?") == 0)
1605 /* looks like a name glob. hard work. */
1606 for (p = 1; p < pool->nsolvables; p++)
1608 Solvable *s = pool->solvables + p;
1609 if (!s->repo || !pool_installable(pool, s))
1612 if (fnmatch(name, id2str(pool, id), 0) == 0)
1614 for (i = 0; i < job->count; i += 2)
1615 if (job->elements[i] == SOLVER_SOLVABLE_NAME && job->elements[i + 1] == id)
1617 if (i == job->count)
1618 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
1624 /* looks like a dep glob. really hard work. */
1625 for (id = 1; id < pool->ss.nstrings; id++)
1627 if (!pool->whatprovides[id])
1629 if (fnmatch(name, id2str(pool, id), 0) == 0)
1632 printf("[using capability match for '%s']\n", name);
1633 for (i = 0; i < job->count; i += 2)
1634 if (job->elements[i] == SOLVER_SOLVABLE_PROVIDES && job->elements[i + 1] == id)
1636 if (i == job->count)
1637 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
1647 addrelation(Pool *pool, Queue *job, int flags, Id evr)
1650 for (i = 0; i < job->count; i += 2)
1652 if (job->elements[i] != SOLVER_SOLVABLE_NAME && job->elements[i] != SOLVER_SOLVABLE_PROVIDES)
1654 job->elements[i + 1] = rel2id(pool, job->elements[i + 1], evr, flags, 1);
1659 limitevr(Pool *pool, char *evr, Queue *job, Id archid)
1669 for (i = 0; i < job->count; i += 2)
1672 FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
1674 s = pool_id2solvable(pool, p);
1675 if (archid && s->arch != archid)
1677 sevr = id2str(pool, s->evr);
1678 if (!strchr(evr, ':') && strchr(sevr, ':'))
1679 sevr = strchr(sevr, ':') + 1;
1680 if (evrcmp_str(pool, sevr, evr, EVRCMP_MATCH) == 0)
1687 queue_deleten(job, 0, i);
1691 /* if all solvables have the same evr */
1692 s = pool_id2solvable(pool, mq.elements[0]);
1694 for (j = 0; j < mq.count; j++)
1696 s = pool_id2solvable(pool, mq.elements[j]);
1697 if (s->evr != evrid)
1700 if (j == mq.count && j > 1)
1702 prune_to_best_arch(pool, &mq);
1703 // prune_to_highest_prio(pool, &mq);
1708 job->elements[i] = SOLVER_SOLVABLE_ONE_OF;
1709 job->elements[i + 1] = pool_queuetowhatprovides(pool, &mq);
1713 job->elements[i] = SOLVER_SOLVABLE;
1714 job->elements[i + 1] = mq.elements[0];
1719 queue_deleten(job, i, 2);
1729 if ((r = strrchr(evr, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
1732 if (limitevr(pool, evr, job, archid))
1744 mkselect(Pool *pool, int mode, char *name, Queue *job)
1756 dataiterator_init(&di, pool, mode == SOLVER_ERASE ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, SEARCH_STRING|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
1757 while (dataiterator_step(&di))
1759 Solvable *s = pool->solvables + di.solvid;
1760 if (!s->repo || !pool_installable(pool, s))
1762 queue_push(&q, di.solvid);
1763 dataiterator_skip_solvable(&di);
1765 dataiterator_free(&di);
1768 printf("[using file list match for '%s']\n", name);
1771 queue_push2(job, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
1773 queue_push2(job, SOLVER_SOLVABLE, q.elements[0]);
1779 if ((r = strpbrk(name, "<=>")) != 0)
1781 /* relation case, support:
1786 int nend = r - name;
1798 while (*r && *r == ' ' && *r == '\t')
1800 while (nend && (name[nend - 1] == ' ' || name[nend -1 ] == '\t'))
1805 fprintf(stderr, "bad relation\n");
1808 if (depglob(pool, name, job))
1810 addrelation(pool, job, rflags, str2id(pool, r, 1));
1813 if ((r2 = strrchr(name, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
1816 if (depglob(pool, name, job))
1819 addrelation(pool, job, REL_ARCH, archid);
1820 addrelation(pool, job, rflags, str2id(pool, r, 1));
1828 /* no relation case, support:
1831 * depglob-version-release
1832 * depglob-version-release.arch
1834 if (depglob(pool, name, job))
1837 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
1840 if (depglob(pool, name, job))
1843 addrelation(pool, job, REL_ARCH, archid);
1848 if ((r = strrchr(name, '-')) != 0)
1851 if (depglob(pool, name, job))
1853 /* have just the version */
1855 if (limitevr(pool, r + 1, job, 0))
1858 if ((r2 = strrchr(name, '-')) != 0)
1862 if (depglob(pool, name, job))
1865 if (limitevr(pool, r2 + 1, job, 0))
1873 fprintf(stderr, "nothing matches '%s'\n", name);
1879 yesno(const char *str)
1881 char inbuf[128], *ip;
1888 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
1893 while (*ip == ' ' || *ip == '\t')
1900 if (*ip == 'y' || *ip == 'n')
1901 return *ip == 'y' ? 1 : 0;
1913 fileconflict_cb(Pool *pool, Id p, void *cbdata)
1915 struct fcstate *fcstate = cbdata;
1923 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
1926 s = pool_id2solvable(pool, p);
1927 if (pool->installed && s->repo == pool->installed)
1929 if (!s->repo->rpmdbid)
1931 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
1934 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
1936 for (i = 0; i < fcstate->newpkgscnt; i++)
1937 if (fcstate->checkq->elements[i] == p)
1939 if (i == fcstate->newpkgscnt)
1941 fp = fcstate->newpkgsfps[i];
1945 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
1949 runrpm(const char *arg, const char *name, int dupfd3)
1954 if ((pid = fork()) == (pid_t)-1)
1961 if (dupfd3 != -1 && dupfd3 != 3)
1967 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
1968 if (strcmp(arg, "-e") == 0)
1969 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1971 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1975 while (waitpid(pid, &status, 0) != pid)
1979 printf("rpm failed\n");
1985 nscallback(Pool *pool, void *data, Id name, Id evr)
1987 if (name == NAMESPACE_PRODUCTBUDDY)
1989 /* SUSE specific hack: each product has an associated rpm */
1990 Solvable *s = pool->solvables + evr;
1993 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
1996 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
1999 FOR_PROVIDES(p, pp, cap)
2001 Solvable *ps = pool->solvables + p;
2002 if (ps->repo == s->repo && ps->arch == s->arch)
2010 #ifdef SOFTLOCKS_PATH
2013 addsoftlocks(Pool *pool, Queue *job)
2017 char *bp, *ep, buf[4096];
2019 if ((fp = fopen(SOFTLOCKS_PATH, "r")) == 0)
2021 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
2023 while (*bp == ' ' || *bp == '\t')
2025 if (!*bp || *bp == '#')
2027 for (ep = bp; *ep; ep++)
2028 if (*ep == ' ' || *ep == '\t' || *ep == '\n')
2031 type = SOLVER_SOLVABLE_NAME;
2032 if (!strncmp(bp, "provides:", 9) && bp[9])
2034 type = SOLVER_SOLVABLE_PROVIDES;
2037 id = str2id(pool, bp, 1);
2038 if (pool->installed)
2040 FOR_JOB_SELECT(p, pp, type, id)
2041 if (pool->solvables[p].repo == pool->installed)
2044 continue; /* ignore, as it is already installed */
2046 queue_push2(job, SOLVER_LOCK|SOLVER_WEAK|type, id);
2055 rewrite_repos(Pool *pool, Id *addedfileprovides)
2060 Queue fileprovidesq;
2062 int i, j, n, nprovidedids;
2063 struct repoinfo *cinfo;
2065 map_init(&providedids, pool->ss.nstrings);
2066 queue_init(&fileprovidesq);
2067 for (nprovidedids = 0; (id = addedfileprovides[nprovidedids]) != 0; nprovidedids++)
2068 MAPSET(&providedids, id);
2071 /* make sure this repo has just one main repodata */
2072 if (!repo->nrepodata)
2074 cinfo = repo->appdata;
2075 data = repo->repodata + 0;
2076 if (data->store.pagefd == -1)
2078 if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
2081 for (j = 0; j < fileprovidesq.count; j++)
2082 if (MAPTST(&providedids, fileprovidesq.elements[j]))
2084 if (n == nprovidedids)
2085 continue; /* nothing new added */
2088 for (j = 0; addedfileprovides[j]; j++)
2089 repodata_add_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[j]);
2090 repodata_internalize(data);
2091 writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
2093 queue_free(&fileprovidesq);
2094 map_free(&providedids);
2098 #define MODE_INSTALL 1
2099 #define MODE_ERASE 2
2100 #define MODE_UPDATE 3
2101 #define MODE_DISTUPGRADE 4
2102 #define MODE_VERIFY 5
2103 #define MODE_PATCH 6
2105 #define MODE_REPOLIST 8
2106 #define MODE_SEARCH 9
2111 fprintf(stderr, "Usage: solv COMMAND <select>\n");
2112 fprintf(stderr, "\n");
2113 fprintf(stderr, " distupgrade: replace installed packages with\n");
2114 fprintf(stderr, " versions from the repositories\n");
2115 fprintf(stderr, " erase: erase installed packages\n");
2116 fprintf(stderr, " info: display package information\n");
2117 fprintf(stderr, " install: install packages\n");
2118 fprintf(stderr, " list: list packages\n");
2119 fprintf(stderr, " repos: list enabled repositories\n");
2120 fprintf(stderr, " search: search name/summary/description\n");
2121 fprintf(stderr, " update: update installed packages\n");
2122 fprintf(stderr, " verify: check dependencies of installed packages\n");
2123 fprintf(stderr, "\n");
2128 main(int argc, char **argv)
2131 Repo *commandlinerepo = 0;
2132 Id *commandlinepkgs = 0;
2134 struct repoinfo *repoinfos;
2136 int mainmode = 0, mode = 0;
2141 char inbuf[128], *ip;
2144 struct fcstate fcstate;
2145 Id *addedfileprovides = 0;
2151 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2153 mainmode = MODE_INSTALL;
2154 mode = SOLVER_INSTALL;
2156 else if (!strcmp(argv[0], "patch"))
2158 mainmode = MODE_PATCH;
2159 mode = SOLVER_INSTALL;
2161 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2163 mainmode = MODE_ERASE;
2164 mode = SOLVER_ERASE;
2166 else if (!strcmp(argv[0], "list"))
2168 mainmode = MODE_LIST;
2171 else if (!strcmp(argv[0], "info"))
2173 mainmode = MODE_INFO;
2176 else if (!strcmp(argv[0], "search"))
2178 mainmode = MODE_SEARCH;
2181 else if (!strcmp(argv[0], "verify"))
2183 mainmode = MODE_VERIFY;
2184 mode = SOLVER_VERIFY;
2186 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2188 mainmode = MODE_UPDATE;
2189 mode = SOLVER_UPDATE;
2191 else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2193 mainmode = MODE_DISTUPGRADE;
2194 mode = SOLVER_UPDATE;
2196 else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2198 mainmode = MODE_REPOLIST;
2204 pool = pool_create();
2206 pool->obsoleteusescolors = 1;
2208 pool_setloadcallback(pool, load_stub, 0);
2209 pool->nscallback = nscallback;
2210 // pool_setdebuglevel(pool, 2);
2212 repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
2214 if (mainmode == MODE_REPOLIST)
2217 for (i = 0; i < nrepoinfos; i++)
2219 struct repoinfo *cinfo = repoinfos + i;
2220 if (!cinfo->enabled)
2222 printf("%d: %-20s %s\n", j++, cinfo->alias, cinfo->name);
2227 read_repos(pool, repoinfos, nrepoinfos);
2229 if (mainmode == MODE_SEARCH)
2235 map_init(&m, pool->nsolvables);
2236 dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2237 dataiterator_set_keyname(&di, SOLVABLE_NAME);
2238 dataiterator_set_search(&di, 0, 0);
2239 while (dataiterator_step(&di))
2240 MAPSET(&m, di.solvid);
2241 dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2242 dataiterator_set_search(&di, 0, 0);
2243 while (dataiterator_step(&di))
2244 MAPSET(&m, di.solvid);
2245 dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2246 dataiterator_set_search(&di, 0, 0);
2247 while (dataiterator_step(&di))
2248 MAPSET(&m, di.solvid);
2249 dataiterator_free(&di);
2251 for (p = 1; p < pool->nsolvables; p++)
2253 Solvable *s = pool_id2solvable(pool, p);
2256 printf(" - %s: %s\n", solvable2str(pool, s), solvable_lookup_str(s, SOLVABLE_SUMMARY));
2263 if (mainmode == MODE_LIST || mainmode == MODE_INSTALL)
2265 for (i = 1; i < argc; i++)
2268 l = strlen(argv[i]);
2269 if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2271 if (access(argv[i], R_OK))
2276 if (!commandlinepkgs)
2277 commandlinepkgs = sat_calloc(argc, sizeof(Id));
2278 if (!commandlinerepo)
2279 commandlinerepo = repo_create(pool, "@commandline");
2280 repo_add_rpms(commandlinerepo, (const char **)argv + i, 1, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2281 commandlinepkgs[i] = commandlinerepo->end - 1;
2283 if (commandlinerepo)
2284 repo_internalize(commandlinerepo);
2287 // FOR_REPOS(i, repo)
2288 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2289 addedfileprovides = 0;
2290 pool_addfileprovides_ids(pool, 0, &addedfileprovides);
2291 if (addedfileprovides && *addedfileprovides)
2292 rewrite_repos(pool, addedfileprovides);
2293 sat_free(addedfileprovides);
2294 pool_createwhatprovides(pool);
2297 for (i = 1; i < argc; i++)
2302 if (commandlinepkgs && commandlinepkgs[i])
2304 queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
2308 mkselect(pool, mode, argv[i], &job2);
2309 for (j = 0; j < job2.count; j++)
2310 queue_push(&job, job2.elements[j]);
2314 if (!job.count && mainmode != MODE_UPDATE && mainmode != MODE_DISTUPGRADE && mainmode != MODE_VERIFY && mainmode != MODE_PATCH)
2316 printf("no package matched\n");
2323 if (mainmode == MODE_LIST || mainmode == MODE_INFO)
2325 /* list mode, no solver needed */
2326 for (i = 0; i < job.count; i += 2)
2328 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
2330 Solvable *s = pool_id2solvable(pool, p);
2331 if (mainmode == MODE_INFO)
2333 printf("Name: %s\n", solvable2str(pool, s));
2334 printf("Repo: %s\n", s->repo->name);
2335 printf("Summary: %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
2336 printf("Url: %s\n", solvable_lookup_str(s, SOLVABLE_URL));
2337 printf("License: %s\n", solvable_lookup_str(s, SOLVABLE_LICENSE));
2338 printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
2343 const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de");
2344 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
2346 printf(" %s\n", sum);
2352 free_repoinfos(repoinfos, nrepoinfos);
2353 sat_free(commandlinepkgs);
2355 yum_substitute(pool, 0);
2360 if (mainmode == MODE_PATCH)
2366 map_init(&installedmap, pool->nsolvables);
2367 if (pool->installed)
2368 FOR_REPO_SOLVABLES(pool->installed, p, s)
2369 MAPSET(&installedmap, p);
2371 /* install all patches */
2372 for (p = 1; p < pool->nsolvables; p++)
2378 s = pool->solvables + p;
2379 if (strncmp(id2str(pool, s->name), "patch:", 6) != 0)
2381 FOR_PROVIDES(p2, pp, s->name)
2383 Solvable *s2 = pool->solvables + p2;
2384 if (s2->name != s->name)
2386 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2387 if (r < 0 || (r == 0 && p > p2))
2392 type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2393 if (type && !strcmp(type, "optional"))
2395 r = solvable_trivial_installable_map(s, &installedmap, 0);
2398 if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2405 queue_push2(&job, SOLVER_SOLVABLE, p);
2407 map_free(&installedmap);
2411 for (i = 0; i < job.count; i += 2)
2413 if (mode == SOLVER_UPDATE)
2415 /* make update of not installed packages an install */
2416 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
2417 if (pool->installed && pool->solvables[p].repo == pool->installed)
2421 job.elements[i] |= SOLVER_INSTALL;
2425 job.elements[i] |= mode;
2428 // multiversion test
2429 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
2430 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
2431 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
2433 #ifdef SOFTLOCKS_PATH
2434 addsoftlocks(pool, &job);
2440 Id problem, solution;
2443 solv = solver_create(pool);
2444 solv->ignorealreadyrecommended = 1;
2445 solv->updatesystem = allpkgs && (mainmode == MODE_UPDATE || mainmode == MODE_DISTUPGRADE);
2446 solv->dosplitprovides = solv->updatesystem;
2447 solv->fixsystem = allpkgs && (mainmode == MODE_VERIFY);
2448 if (mainmode == MODE_DISTUPGRADE)
2450 solv->distupgrade = 1;
2451 solv->allowdowngrade = 1;
2452 solv->allowarchchange = 1;
2453 solv->allowvendorchange = 1;
2455 if (mainmode == MODE_ERASE)
2456 solv->allowuninstall = 1; /* don't nag */
2458 // queue_push2(&job, SOLVER_DISTUPGRADE, 3);
2459 solver_solve(solv, &job);
2460 if (!solv->problems.count)
2462 pcnt = solver_problem_count(solv);
2463 printf("Found %d problems:\n", pcnt);
2464 for (problem = 1; problem <= pcnt; problem++)
2467 printf("Problem %d:\n", problem);
2468 solver_printprobleminfo(solv, problem);
2470 scnt = solver_solution_count(solv, problem);
2471 for (solution = 1; solution <= scnt; solution++)
2473 printf("Solution %d:\n", solution);
2474 solver_printsolution(solv, problem, solution);
2479 printf("Please choose a solution: ");
2482 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2487 while (*ip == ' ' || *ip == '\t')
2489 if (*ip >= '0' && *ip <= '9')
2492 if (take >= 1 && take <= scnt)
2508 solver_take_solution(solv, problem, take, &job);
2514 trans = &solv->trans;
2515 if (!trans->steps.count)
2517 printf("Nothing to do.\n");
2521 printf("Transaction summary:\n\n");
2522 solver_printtransaction(solv);
2531 duc[1].path = "/usr/share/man";
2532 duc[2].path = "/sbin";
2533 duc[3].path = "/etc";
2534 transaction_calc_duchanges(trans, duc, 4);
2535 for (i = 0; i < 4; i++)
2536 printf("duchanges %s: %d K %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
2539 printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
2542 if (!yesno("OK to continue (y/n)? "))
2548 queue_init(&checkq);
2549 newpkgs = transaction_installedresult(trans, &checkq);
2554 int downloadsize = 0;
2555 for (i = 0; i < newpkgs; i++)
2559 p = checkq.elements[i];
2560 s = pool_id2solvable(pool, p);
2561 downloadsize += solvable_lookup_num(s, SOLVABLE_DOWNLOADSIZE, 0);
2563 printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
2564 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
2565 for (i = 0; i < newpkgs; i++)
2567 unsigned int medianr;
2570 struct repoinfo *cinfo;
2571 const unsigned char *chksum;
2575 p = checkq.elements[i];
2576 s = pool_id2solvable(pool, p);
2577 if (s->repo == commandlinerepo)
2579 loc = solvable_get_location(s, &medianr);
2580 if (!(newpkgsfps[i] = fopen(loc, "r")))
2588 cinfo = s->repo->appdata;
2591 printf("%s: no repository information\n", s->repo->name);
2594 loc = solvable_get_location(s, &medianr);
2598 if (pool->installed && pool->installed->nsolvables)
2600 /* try a delta first */
2601 char *matchname = strdup(id2str(pool, s->name));
2602 dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
2603 dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
2604 while (dataiterator_step(&di))
2608 dataiterator_setpos_parent(&di);
2609 if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
2610 pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
2612 baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
2613 FOR_PROVIDES(op, pp, s->name)
2615 Solvable *os = pool->solvables + op;
2616 if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
2619 if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
2621 /* base is installed, run sequence check */
2622 const char *seqname;
2631 seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
2632 seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
2633 seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
2634 seq = pool_tmpjoin(pool, seqname, "-", seqevr);
2635 seq = pool_tmpjoin(pool, seq, "-", seqnum);
2637 sprintf(cmd, "/usr/bin/applydeltarpm -a %s -c -s ", id2str(pool, s->arch));
2639 sprintf(cmd, "/usr/bin/applydeltarpm -c -s ");
2641 if (system(pool_tmpjoin(pool, cmd, seq, 0)) != 0)
2642 continue; /* didn't match */
2643 /* looks good, download delta */
2645 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
2647 continue; /* no way! */
2648 dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
2649 dloc = pool_tmpjoin(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
2650 dloc = pool_tmpjoin(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
2651 dloc = pool_tmpjoin(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
2652 if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
2654 /* got it, now reconstruct */
2655 newfd = opentmpfile();
2657 sprintf(cmd, "applydeltarpm -a %s /dev/fd/%d /dev/fd/%d", id2str(pool, s->arch), fileno(fp), newfd);
2659 sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
2661 fcntl(fileno(fp), F_SETFD, 0);
2668 lseek(newfd, 0, SEEK_SET);
2670 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2671 if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
2677 newpkgsfps[i] = fdopen(newfd, "r");
2682 dataiterator_free(&di);
2683 sat_free(matchname);
2690 continue; /* delta worked! */
2692 if (cinfo->type == TYPE_SUSETAGS)
2694 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
2695 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
2698 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2699 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
2701 printf("\n%s: %s not found in repository\n", s->repo->name, loc);
2714 printf("Searching for file conflicts\n");
2715 queue_init(&conflicts);
2716 fcstate.rpmdbstate = 0;
2717 fcstate.newpkgscnt = newpkgs;
2718 fcstate.checkq = &checkq;
2719 fcstate.newpkgsfps = newpkgsfps;
2720 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fileconflict_cb, &fcstate);
2721 if (conflicts.count)
2724 for (i = 0; i < conflicts.count; i += 5)
2725 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]));
2727 if (yesno("Re-run solver (y/n/q)? "))
2729 for (i = 0; i < newpkgs; i++)
2731 fclose(newpkgsfps[i]);
2732 newpkgsfps = sat_free(newpkgsfps);
2734 pool_add_fileconflicts_deps(pool, &conflicts);
2735 pool_createwhatprovides(pool); /* Hmm... */
2739 queue_free(&conflicts);
2742 printf("Committing transaction:\n\n");
2743 transaction_order(trans, 0);
2744 for (i = 0; i < trans->steps.count; i++)
2746 const char *evr, *evrp, *nvra;
2751 p = trans->steps.elements[i];
2752 s = pool_id2solvable(pool, p);
2753 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
2756 case SOLVER_TRANSACTION_ERASE:
2757 printf("erase %s\n", solvid2str(pool, p));
2758 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
2760 /* strip epoch from evr */
2761 evr = evrp = id2str(pool, s->evr);
2762 while (*evrp >= '0' && *evrp <= '9')
2764 if (evrp > evr && evrp[0] == ':' && evrp[1])
2766 nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
2767 nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
2768 runrpm("-e", nvra, -1); /* to bad that --querybynumber doesn't work */
2770 case SOLVER_TRANSACTION_INSTALL:
2771 case SOLVER_TRANSACTION_MULTIINSTALL:
2772 printf("install %s\n", solvid2str(pool, p));
2773 for (j = 0; j < newpkgs; j++)
2774 if (checkq.elements[j] == p)
2776 fp = j < newpkgs ? newpkgsfps[j] : 0;
2780 lseek(fileno(fp), 0, SEEK_SET);
2781 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
2790 for (i = 0; i < newpkgs; i++)
2792 fclose(newpkgsfps[i]);
2793 sat_free(newpkgsfps);
2794 queue_free(&checkq);
2798 free_repoinfos(repoinfos, nrepoinfos);
2799 sat_free(commandlinepkgs);
2801 yum_substitute(pool, 0);