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)
101 while ((p2 = strchr(p, '$')) != 0)
103 if (!strncmp(p2, "$releasever", 11))
105 static char *releaseevr;
111 rpm_installedrpmdbids(0, "Providename", "redhat-release", &q);
114 void *handle, *state = 0;
116 handle = rpm_byrpmdbid(q.elements[0], 0, &state);
117 releaseevr = rpm_query(handle, SOLVABLE_EVR);
118 rpm_byrpmdbid(0, 0, &state);
119 if ((p = strchr(releaseevr, '-')) != 0)
124 releaseevr = strdup("?");
127 p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
128 p2 = p + (p2 - line);
130 p = p2 + strlen(releaseevr);
133 if (!strncmp(p2, "$basearch", 9))
135 static char *basearch;
144 basearch = strdup(un.machine);
145 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
149 p = pool_tmpjoin(pool, line, basearch, p2 + 9);
150 p2 = p + (p2 - line);
152 p = p2 + strlen(basearch);
161 #define TYPE_UNKNOWN 0
162 #define TYPE_SUSETAGS 1
164 #define TYPE_PLAINDIR 3
167 read_repoinfos_sort(const void *ap, const void *bp)
169 const struct repoinfo *a = ap;
170 const struct repoinfo *b = bp;
171 return strcmp(a->alias, b->alias);
175 read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
178 char buf2[4096], *kp, *vp, *kpe;
183 struct repoinfo *repoinfos = 0, *cinfo;
186 rdlen = strlen(reposdir);
187 dir = opendir(reposdir);
193 while ((ent = readdir(dir)) != 0)
195 l = strlen(ent->d_name);
196 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
198 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
199 if ((fp = fopen(buf, "r")) == 0)
205 while(fgets(buf2, sizeof(buf2), fp))
210 while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
213 while (*kp == ' ' || *kp == '\t')
215 if (!*kp || *kp == '#')
219 kp = yum_substitute(pool, kp);
223 vp = strrchr(kp, ']');
227 repoinfos = sat_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
228 cinfo = repoinfos + nrepoinfos++;
229 memset(cinfo, 0, sizeof(*cinfo));
230 cinfo->alias = strdup(kp + 1);
231 cinfo->type = TYPE_RPMMD;
232 cinfo->autorefresh = 1;
234 cinfo->repo_gpgcheck = 1;
236 cinfo->metadata_expire = METADATA_EXPIRE;
241 vp = strchr(kp, '=');
244 for (kpe = vp - 1; kpe >= kp; kpe--)
245 if (*kpe != ' ' && *kpe != '\t')
250 while (*vp == ' ' || *vp == '\t')
253 if (!strcmp(kp, "name"))
254 cinfo->name = strdup(vp);
255 else if (!strcmp(kp, "enabled"))
256 cinfo->enabled = *vp == '0' ? 0 : 1;
257 else if (!strcmp(kp, "autorefresh"))
258 cinfo->autorefresh = *vp == '0' ? 0 : 1;
259 else if (!strcmp(kp, "gpgcheck"))
260 cinfo->pkgs_gpgcheck = *vp == '0' ? 0 : 1;
261 else if (!strcmp(kp, "repo_gpgcheck"))
262 cinfo->repo_gpgcheck = *vp == '0' ? 0 : 1;
263 else if (!strcmp(kp, "baseurl"))
264 cinfo->baseurl = strdup(vp);
265 else if (!strcmp(kp, "mirrorlist"))
267 if (strstr(vp, "metalink"))
268 cinfo->metalink = strdup(vp);
270 cinfo->mirrorlist = strdup(vp);
272 else if (!strcmp(kp, "path"))
274 if (vp && strcmp(vp, "/") != 0)
275 cinfo->path = strdup(vp);
277 else if (!strcmp(kp, "type"))
279 if (!strcmp(vp, "yast2"))
280 cinfo->type = TYPE_SUSETAGS;
281 else if (!strcmp(vp, "rpm-md"))
282 cinfo->type = TYPE_RPMMD;
283 else if (!strcmp(vp, "plaindir"))
284 cinfo->type = TYPE_PLAINDIR;
286 cinfo->type = TYPE_UNKNOWN;
288 else if (!strcmp(kp, "priority"))
289 cinfo->priority = atoi(vp);
290 else if (!strcmp(kp, "keeppackages"))
291 cinfo->keeppackages = *vp == '0' ? 0 : 1;
297 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
298 *nrepoinfosp = nrepoinfos;
303 free_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
306 for (i = 0; i < nrepoinfos; i++)
308 struct repoinfo *cinfo = repoinfos + i;
309 sat_free(cinfo->name);
310 sat_free(cinfo->alias);
311 sat_free(cinfo->path);
312 sat_free(cinfo->metalink);
313 sat_free(cinfo->mirrorlist);
314 sat_free(cinfo->baseurl);
325 strcpy(tmpl, "/var/tmp/solvXXXXXX");
337 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
344 h = sat_chksum_create(chksumtype);
347 printf("%s: unknown checksum type\n", file);
350 while ((l = read(fd, buf, sizeof(buf))) > 0)
351 sat_chksum_add(h, buf, l);
352 lseek(fd, 0, SEEK_SET);
354 sum = sat_chksum_get(h, &l);
355 if (memcmp(sum, chksum, l))
357 printf("%s: checksum mismatch\n", file);
364 findfastest(char **urls, int nurls)
371 struct addrinfo hints, *result;;
373 fds = sat_calloc(nurls, sizeof(*fds));
374 socks = sat_calloc(nurls, sizeof(*socks));
375 for (i = 0; i < nurls; i++)
378 p = strchr(urls[i], '/');
391 if ((p2 = strchr(p, '@')) != 0)
394 if (!strncmp("https:", urls[i], 6))
396 else if (!strncmp("ftp:", urls[i], 4))
398 if ((p2 = strrchr(p, ':')) != 0)
407 sprintf(portstr, "%d", port);
408 memset(&hints, 0, sizeof(struct addrinfo));
409 hints.ai_family = AF_UNSPEC;
410 hints.ai_socktype = SOCK_STREAM;
411 hints.ai_flags = AI_NUMERICSERV;
413 if (!getaddrinfo(p, portstr, &hints, &result))
415 socks[i] = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
418 fcntl(socks[i], F_SETFL, O_NONBLOCK);
419 if (connect(socks[i], result->ai_addr, result->ai_addrlen) == -1)
421 if (errno != EINPROGRESS)
428 freeaddrinfo(result);
435 for (i = j = 0; i < nurls; i++)
439 fds[j].fd = socks[i];
440 fds[j].events = POLLOUT;
448 if (poll(fds, j, 10000) <= 0)
450 i = -1; /* something is wrong */
453 for (i = 0; i < j; i++)
454 if ((fds[i].revents & POLLOUT) != 0)
457 socklen_t soel = sizeof(int);
458 if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, &soe, &soel) == -1 || soe != 0)
460 /* connect failed, kill socket */
461 for (j = 0; j < nurls; j++)
462 if (socks[j] == fds[i].fd)
475 i = -1; /* something is wrong, no bit was set */
478 /* now i contains the fastest fd index */
481 for (j = 0; j < nurls; j++)
482 if (socks[j] == fds[i].fd)
486 char *url0 = urls[0];
491 for (i = j = 0; i < nurls; i++)
499 findmetalinkurl(FILE *fp, unsigned char *chksump, Id *chksumtypep)
501 char buf[4096], *bp, *ep;
508 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
510 while (*bp == ' ' || *bp == '\t')
512 if (chksumtypep && !*chksumtypep && !strncmp(bp, "<hash type=\"sha256\">", 20))
517 memset(chksump, 0, 32);
518 for (i = 0; i < 64; i++)
521 if (c >= '0' && c <= '9')
522 chksump[i / 2] = chksump[i / 2] * 16 + (c - '0');
523 else if (c >= 'a' && c <= 'f')
524 chksump[i / 2] = chksump[i / 2] * 16 + (c - ('a' - 10));
525 else if (c >= 'A' && c <= 'F')
526 chksump[i / 2] = chksump[i / 2] * 16 + (c - ('A' - 10));
531 *chksumtypep = REPOKEY_TYPE_SHA256;
534 if (strncmp(bp, "<url", 4))
536 bp = strchr(bp, '>');
540 ep = strstr(bp, "repodata/repomd.xml</url>");
544 if (strncmp(bp, "http", 4))
546 urls = sat_extend(urls, nurls, 1, sizeof(*urls), 15);
547 urls[nurls++] = strdup(bp);
552 findfastest(urls, nurls > 5 ? 5 : nurls);
555 for (i = 0; i < nurls; i++)
558 ep = strchr(bp, '/');
559 if ((ep = strchr(ep + 2, '/')) != 0)
562 printf("[using mirror %s]\n", bp);
571 findmirrorlisturl(FILE *fp)
573 char buf[4096], *bp, *ep;
578 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
580 while (*bp == ' ' || *bp == '\t')
582 if (!*bp || *bp == '#')
585 while (l > 0 && (bp[l - 1] == ' ' || bp[l - 1] == '\t' || bp[l - 1] == '\n'))
587 urls = sat_extend(urls, nurls, 1, sizeof(*urls), 15);
588 urls[nurls++] = strdup(bp);
593 findfastest(urls, nurls > 5 ? 5 : nurls);
596 for (i = 0; i < nurls; i++)
599 ep = strchr(bp, '/');
600 if ((ep = strchr(ep + 2, '/')) != 0)
603 printf("[using mirror %s]\n", bp);
612 cookie_gzread(void *cookie, char *buf, size_t nbytes)
614 return gzread((gzFile *)cookie, buf, nbytes);
618 cookie_gzclose(void *cookie)
620 return gzclose((gzFile *)cookie);
624 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype, int *badchecksump)
630 const char *baseurl = cinfo->baseurl;
634 if (!cinfo->metalink && !cinfo->mirrorlist)
636 if (file != cinfo->metalink && file != cinfo->mirrorlist)
638 FILE *fp = curlfopen(cinfo, cinfo->metalink ? cinfo->metalink : cinfo->mirrorlist, 0, 0, 0, 0);
639 unsigned char mlchksum[32];
644 cinfo->baseurl = findmetalinkurl(fp, mlchksum, &mlchksumtype);
646 cinfo->baseurl = findmirrorlisturl(fp);
650 if (!chksumtype && mlchksumtype && !strcmp(file, "repodata/repomd.xml"))
652 chksumtype = mlchksumtype;
655 return curlfopen(cinfo, file, uncompress, chksum, chksumtype, badchecksump);
657 snprintf(url, sizeof(url), "%s", file);
662 if (l && baseurl[l - 1] == '/')
663 snprintf(url, sizeof(url), "%s%s", baseurl, file);
665 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
668 // printf("url: %s\n", url);
669 if ((pid = fork()) == (pid_t)-1)
681 execlp("curl", "curl", "-f", "-s", "-L", url, (char *)0);
686 while (waitpid(pid, &status, 0) != pid)
688 if (lseek(fd, 0, SEEK_END) == 0 && (!status || !chksumtype))
694 lseek(fd, 0, SEEK_SET);
697 printf("%s: download error %d\n", file, status >> 8 ? status >> 8 : status);
703 if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
713 cookie_io_functions_t cio;
716 sprintf(tmpl, "/dev/fd/%d", fd);
717 gzf = gzopen(tmpl, "r");
721 fprintf(stderr, "could not open /dev/fd/%d, /proc not mounted?\n", fd);
724 memset(&cio, 0, sizeof(cio));
725 cio.read = cookie_gzread;
726 cio.close = cookie_gzclose;
727 return fopencookie(gzf, "r", cio);
729 fcntl(fd, F_SETFD, FD_CLOEXEC);
730 return fdopen(fd, "r");
734 cleanupgpg(char *gpgdir)
737 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
739 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
741 snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
743 snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
745 snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
751 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
760 off_t posfp, possigfp;
763 gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
766 keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
767 if (!(kfp = fopen(keysfile, "w")) )
773 for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
777 pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
778 if (!pubkey || !*pubkey)
780 if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
782 if (fputc('\n', kfp) == EOF) /* Just in case... */
786 if (fclose(kfp) || !nkeys)
791 snprintf(cmd, sizeof(cmd), "gpg2 -q --homedir %s --import %s", gpgdir, keysfile);
794 fprintf(stderr, "key import error\n");
799 posfp = lseek(fileno(fp), 0, SEEK_CUR);
800 lseek(fileno(fp), 0, SEEK_SET);
801 possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
802 lseek(fileno(sigfp), 0, SEEK_SET);
803 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
804 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
805 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
807 lseek(fileno(sigfp), possigfp, SEEK_SET);
808 lseek(fileno(fp), posfp, SEEK_SET);
809 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
810 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
812 return r == 0 ? 1 : 0;
815 #define CHKSUM_IDENT "1.1"
818 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
821 void *h = sat_chksum_create(chktype);
824 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
825 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
826 sat_chksum_add(h, buf, l);
828 sat_chksum_free(h, out);
832 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
834 void *h = sat_chksum_create(chktype);
835 sat_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
836 sat_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
837 sat_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
838 sat_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
839 sat_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
840 sat_chksum_free(h, out);
852 pool_setarch(pool, un.machine);
855 char *calccachepath(Repo *repo, const char *repoext)
857 char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
860 p = pool_tmpjoin(repo->pool, p, "_", repoext);
861 p = pool_tmpjoin(repo->pool, p, ".solvx", 0);
864 p = pool_tmpjoin(repo->pool, p, ".solv", 0);
865 q = p + strlen(SOLVCACHE_PATH) + 1;
875 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie, int mark)
878 unsigned char mycookie[32];
879 unsigned char myextcookie[32];
880 struct repoinfo *cinfo;
883 cinfo = repo->appdata;
884 if (!(fp = fopen(calccachepath(repo, repoext), "r")))
886 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
891 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
896 if (cinfo && !repoext)
898 if (fseek(fp, -sizeof(mycookie) * 2, SEEK_END) || fread(myextcookie, sizeof(myextcookie), 1, fp) != 1)
908 flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
909 if (repoext && strcmp(repoext, "DL") != 0)
910 flags |= REPO_LOCALPOOL; /* no local pool for DL so that we can compare IDs */
912 if (repo_add_solv_flags(repo, fp, flags))
917 if (cinfo && !repoext)
919 memcpy(cinfo->cookie, mycookie, sizeof(mycookie));
920 memcpy(cinfo->extcookie, myextcookie, sizeof(myextcookie));
923 futimes(fileno(fp), 0); /* try to set modification time */
929 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
934 struct repoinfo *cinfo;
937 cinfo = repo->appdata;
938 mkdir(SOLVCACHE_PATH, 0755);
939 tmpl = sat_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
947 if (!(fp = fdopen(fd, "w")))
956 for (i = repo->start; i < repo->end; i++)
957 if (repo->pool->solvables[i].repo != repo)
963 repo_write(repo, fp, repo_write_stdkeyfilter, 0, 0);
965 repodata_write(info, fp, repo_write_stdkeyfilter, 0);
968 int oldnrepodata = repo->nrepodata;
969 repo->nrepodata = 1; /* XXX: do this right */
970 repo_write(repo, fp, repo_write_stdkeyfilter, 0, 0);
971 repo->nrepodata = oldnrepodata;
975 if (!repoext && cinfo)
977 if (!cinfo->extcookie[0])
979 /* create the ext cookie and append it */
980 /* we just need some unique ID */
982 if (!fstat(fileno(fp), &stb))
986 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
987 for (i = 0; i < 32; i++)
988 cinfo->extcookie[i] ^= cookie[i];
990 if (cinfo->extcookie[0] == 0)
991 cinfo->extcookie[0] = 1;
993 if (fwrite(cinfo->extcookie, 32, 1, fp) != 1)
1001 /* append our cookie describing the metadata state */
1002 if (fwrite(cookie, 32, 1, fp) != 1)
1017 /* switch to just saved repo to activate paging and save memory */
1018 FILE *fp = fopen(tmpl, "r");
1024 repo_empty(repo, 1);
1025 if (repo_add_solv_flags(repo, fp, SOLV_ADD_NO_STUBS))
1027 /* oops, no way to recover from here */
1028 fprintf(stderr, "internal error\n");
1034 /* make sure repodata contains complete repo */
1035 /* (this is how repodata_write saves it) */
1036 repodata_extend_block(info, repo->start, repo->end - repo->start);
1037 info->state = REPODATA_LOADING;
1038 /* no need for LOCALPOOL as pool already contains ids */
1039 repo_add_solv_flags(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1040 info->state = REPODATA_AVAILABLE; /* in case the load failed */
1045 if (!rename(tmpl, calccachepath(repo, repoext)))
1054 Pool *sigpool = pool_create();
1055 Repo *repo = repo_create(sigpool, "rpmdbkeys");
1056 repo_add_rpmdb_pubkeys(repo, 0, 0);
1061 /* repomd helpers */
1063 static inline const char *
1064 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1066 Pool *pool = repo->pool;
1068 const char *filename;
1073 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1074 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1075 if (dataiterator_step(&di))
1077 dataiterator_setpos_parent(&di);
1078 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1079 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
1081 dataiterator_free(&di);
1082 if (filename && !*chksumtypep)
1084 printf("no %s file checksum!\n", what);
1091 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
1093 Pool *pool = repo->pool;
1095 Id chksumtype, handle;
1096 const unsigned char *chksum;
1097 const char *filename;
1099 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1100 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1101 if (!dataiterator_step(&di))
1103 dataiterator_free(&di);
1106 if (!strcmp(what, "prestodelta"))
1108 dataiterator_setpos_parent(&di);
1109 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1110 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, &chksumtype);
1111 if (!filename || !chksum)
1113 dataiterator_free(&di);
1116 handle = repodata_new_handle(data);
1117 repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
1118 repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
1120 repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
1121 if (!strcmp(what, "deltainfo"))
1123 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
1124 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
1126 if (!strcmp(what, "filelists"))
1128 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1129 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1131 dataiterator_free(&di);
1132 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1137 /* susetags helpers */
1139 static inline const char *
1140 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1142 Pool *pool = repo->pool;
1144 const char *filename;
1149 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
1150 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1151 if (dataiterator_step(&di))
1153 dataiterator_setpos_parent(&di);
1154 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
1157 dataiterator_free(&di);
1158 if (filename && !*chksumtypep)
1160 printf("no %s file checksum!\n", what);
1166 static Id susetags_langtags[] = {
1167 SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
1168 SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
1169 SOLVABLE_EULA, REPOKEY_TYPE_STR,
1170 SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
1171 SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
1172 SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
1177 susetags_add_ext(Repo *repo, Repodata *data)
1179 Pool *pool = repo->pool;
1182 Id handle, filechksumtype;
1183 const unsigned char *filechksum;
1186 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
1187 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1188 while (dataiterator_step(&di))
1190 if (strncmp(di.kv.str, "packages.", 9) != 0)
1192 if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
1194 ext[0] = di.kv.str[9];
1195 ext[1] = di.kv.str[10];
1197 if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
1199 handle = repodata_new_handle(data);
1200 repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
1202 repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
1203 if (!strcmp(ext, "DU"))
1205 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
1206 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
1208 if (!strcmp(ext, "FL"))
1210 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1211 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1215 for (i = 0; susetags_langtags[i]; i += 2)
1217 repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
1218 repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
1221 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1223 dataiterator_free(&di);
1228 iscompressed(const char *name)
1230 int l = strlen(name);
1231 return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
1238 load_stub(Pool *pool, Repodata *data, void *dp)
1240 const char *filename, *descrdir, *repomdtype;
1241 const unsigned char *filechksum;
1243 struct repoinfo *cinfo;
1248 cinfo = data->repo->appdata;
1250 filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
1254 ext[0] = filename[9];
1255 ext[1] = filename[10];
1258 printf("[%s:%s", data->repo->name, ext);
1260 if (usecachedrepo(data->repo, ext, cinfo->extcookie, 0))
1262 printf(" cached]\n"); fflush(stdout);
1266 printf(" fetching]\n"); fflush(stdout);
1268 defvendor = repo_lookup_id(data->repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1269 descrdir = repo_lookup_str(data->repo, SOLVID_META, SUSETAGS_DESCRDIR);
1271 descrdir = "suse/setup/descr";
1273 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
1274 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1276 repo_add_susetags(data->repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1278 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
1282 repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
1285 if (!strcmp(repomdtype, "filelists"))
1287 else if (!strcmp(repomdtype, "deltainfo"))
1292 printf("[%s:%s", data->repo->name, ext);
1294 if (usecachedrepo(data->repo, ext, cinfo->extcookie, 0))
1296 printf(" cached]\n");fflush(stdout);
1299 printf(" fetching]\n"); fflush(stdout);
1300 filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
1302 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, REPOSITORY_REPOMD_CHECKSUM, &filechksumtype);
1303 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1305 if (!strcmp(ext, "FL"))
1306 repo_add_rpmmd(data->repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1307 else if (!strcmp(ext, "DL"))
1308 repo_add_deltainfoxml(data->repo, fp, REPO_USE_LOADING);
1310 writecachedrepo(data->repo, data, ext, cinfo->extcookie);
1317 static unsigned char installedcookie[32];
1320 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
1323 struct repoinfo *cinfo;
1327 const char *filename;
1328 const unsigned char *filechksum;
1330 const char *descrdir;
1338 repo = repo_create(pool, "@System");
1339 printf("rpm database:");
1340 if (stat("/var/lib/rpm/Packages", &stb))
1341 memset(&stb, 0, sizeof(&stb));
1342 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, installedcookie);
1343 if (usecachedrepo(repo, 0, installedcookie, 0))
1344 printf(" cached\n");
1348 printf(" reading\n");
1351 #ifdef PRODUCTS_PATH
1352 repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
1354 if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1356 Repo *ref = repo_create(pool, "@System.old");
1357 if (!repo_add_solv(ref, ofp))
1359 repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
1366 repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
1367 writecachedrepo(repo, 0, 0, installedcookie);
1369 pool_set_installed(pool, repo);
1371 for (i = 0; i < nrepoinfos; i++)
1373 cinfo = repoinfos + i;
1374 if (!cinfo->enabled)
1377 repo = repo_create(pool, cinfo->alias);
1379 repo->appdata = cinfo;
1380 repo->priority = 99 - cinfo->priority;
1382 dorefresh = cinfo->autorefresh;
1383 if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0), &stb) == 0)
1385 if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1388 if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1390 printf("repo '%s':", cinfo->alias);
1391 printf(" cached\n");
1395 switch (cinfo->type)
1398 printf("rpmmd repo '%s':", cinfo->alias);
1400 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1402 printf(" no repomd.xml file, skipped\n");
1407 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1408 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1410 printf(" cached\n");
1414 if (cinfo->repo_gpgcheck)
1416 sigfp = curlfopen(cinfo, "repodata/repomd.xml.asc", 0, 0, 0, 0);
1419 printf(" unsigned, skipped\n");
1424 sigpool = read_sigs();
1425 if (!checksig(sigpool, fp, sigfp))
1427 printf(" checksig failed, skipped\n");
1434 repo_add_repomdxml(repo, fp, 0);
1436 printf(" fetching\n");
1437 filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1438 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1440 repo_add_rpmmd(repo, fp, 0, 0);
1444 break; /* hopeless */
1446 filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1447 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1449 repo_add_updateinfoxml(repo, fp, 0);
1453 data = repo_add_repodata(repo, 0);
1454 if (!repomd_add_ext(repo, data, "deltainfo"))
1455 repomd_add_ext(repo, data, "prestodelta");
1456 repomd_add_ext(repo, data, "filelists");
1457 repodata_internalize(data);
1459 writecachedrepo(repo, 0, 0, cinfo->cookie);
1460 repodata_create_stubs(repo_last_repodata(repo));
1464 printf("susetags repo '%s':", cinfo->alias);
1468 if ((fp = curlfopen(cinfo, "content", 0, 0, 0, 0)) == 0)
1470 printf(" no content file, skipped\n");
1475 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1476 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1478 printf(" cached\n");
1482 if (cinfo->repo_gpgcheck)
1484 sigfp = curlfopen(cinfo, "content.asc", 0, 0, 0, 0);
1487 printf(" unsigned, skipped\n");
1494 sigpool = read_sigs();
1495 if (!checksig(sigpool, fp, sigfp))
1497 printf(" checksig failed, skipped\n");
1505 repo_add_content(repo, fp, 0);
1507 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1508 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1510 descrdir = "suse/setup/descr";
1511 filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1513 filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1516 printf(" no packages file entry, skipped\n");
1519 printf(" fetching\n");
1520 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
1521 break; /* hopeless */
1522 repo_add_susetags(repo, fp, defvendor, 0, 0);
1524 data = repo_add_repodata(repo, 0);
1525 susetags_add_ext(repo, data);
1526 repodata_internalize(data);
1528 writecachedrepo(repo, 0, 0, cinfo->cookie);
1529 repodata_create_stubs(repo_last_repodata(repo));
1532 printf("unsupported repo '%s': skipped\n", cinfo->alias);
1544 str2archid(Pool *pool, char *arch)
1549 id = str2id(pool, arch, 0);
1550 if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
1552 if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
1558 depglob(Pool *pool, char *name, Queue *job)
1561 Id id = str2id(pool, name, 0);
1566 FOR_PROVIDES(p, pp, id)
1568 Solvable *s = pool->solvables + p;
1572 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
1578 printf("[using capability match for '%s']\n", name);
1579 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
1584 if (strpbrk(name, "[*?") == 0)
1587 /* looks like a name glob. hard work. */
1588 for (p = 1; p < pool->nsolvables; p++)
1590 Solvable *s = pool->solvables + p;
1591 if (!s->repo || !pool_installable(pool, s))
1594 if (fnmatch(name, id2str(pool, id), 0) == 0)
1596 for (i = 0; i < job->count; i += 2)
1597 if (job->elements[i] == SOLVER_SOLVABLE_NAME && job->elements[i + 1] == id)
1599 if (i == job->count)
1600 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
1606 /* looks like a dep glob. really hard work. */
1607 for (id = 1; id < pool->ss.nstrings; id++)
1609 if (!pool->whatprovides[id])
1611 if (fnmatch(name, id2str(pool, id), 0) == 0)
1614 printf("[using capability match for '%s']\n", name);
1615 for (i = 0; i < job->count; i += 2)
1616 if (job->elements[i] == SOLVER_SOLVABLE_PROVIDES && job->elements[i + 1] == id)
1618 if (i == job->count)
1619 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
1629 addrelation(Pool *pool, Queue *job, int flags, Id evr)
1632 for (i = 0; i < job->count; i += 2)
1634 if (job->elements[i] != SOLVER_SOLVABLE_NAME && job->elements[i] != SOLVER_SOLVABLE_PROVIDES)
1636 job->elements[i + 1] = rel2id(pool, job->elements[i + 1], evr, flags, 1);
1641 limitevr(Pool *pool, char *evr, Queue *job, Id archid)
1650 for (i = 0; i < job->count; i += 2)
1653 FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
1655 s = pool_id2solvable(pool, p);
1656 if (archid && s->arch != archid)
1658 if (evrcmp_str(pool, id2str(pool, s->evr), evr, EVRCMP_MATCH) == 0)
1665 queue_deleten(job, 0, i);
1669 /* if all solvables have the same evr */
1670 s = pool_id2solvable(pool, mq.elements[0]);
1672 for (j = 0; j < mq.count; j++)
1674 s = pool_id2solvable(pool, mq.elements[j]);
1675 if (s->evr != evrid)
1678 if (j == mq.count && j > 1)
1680 prune_to_best_arch(pool, &mq);
1681 // prune_to_highest_prio(pool, &mq);
1686 job->elements[i] = SOLVER_SOLVABLE_ONE_OF;
1687 job->elements[i + 1] = pool_queuetowhatprovides(pool, &mq);
1691 job->elements[i] = SOLVER_SOLVABLE;
1692 job->elements[i + 1] = mq.elements[0];
1697 queue_deleten(job, i, 2);
1707 if ((r = strrchr(evr, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
1710 if (limitevr(pool, evr, job, archid))
1722 mkselect(Pool *pool, int mode, char *name, Queue *job)
1734 dataiterator_init(&di, pool, mode == SOLVER_ERASE ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, SEARCH_STRING|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
1735 while (dataiterator_step(&di))
1737 Solvable *s = pool->solvables + di.solvid;
1738 if (!s->repo || !pool_installable(pool, s))
1740 queue_push(&q, di.solvid);
1741 dataiterator_skip_solvable(&di);
1743 dataiterator_free(&di);
1746 printf("[using file list match for '%s']\n", name);
1749 queue_push2(job, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
1751 queue_push2(job, SOLVER_SOLVABLE, q.elements[0]);
1757 if ((r = strpbrk(name, "<=>")) != 0)
1759 /* relation case, support:
1764 int nend = r - name;
1776 while (*r && *r == ' ' && *r == '\t')
1778 while (nend && (name[nend - 1] == ' ' || name[nend -1 ] == '\t'))
1783 fprintf(stderr, "bad relation\n");
1786 if (depglob(pool, name, job))
1788 addrelation(pool, job, rflags, str2id(pool, r, 1));
1791 if ((r2 = strrchr(name, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
1794 if (depglob(pool, name, job))
1797 addrelation(pool, job, REL_ARCH, archid);
1798 addrelation(pool, job, rflags, str2id(pool, r, 1));
1806 /* no relation case, support:
1809 * depglob-version-release
1810 * depglob-version-release.arch
1812 if (depglob(pool, name, job))
1815 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
1818 if (depglob(pool, name, job))
1821 addrelation(pool, job, REL_ARCH, archid);
1826 if ((r = strrchr(name, '-')) != 0)
1829 if (depglob(pool, name, job))
1831 /* have just the version */
1833 if (limitevr(pool, r + 1, job, 0))
1836 if ((r2 = strrchr(name, '-')) != 0)
1840 if (depglob(pool, name, job))
1843 if (limitevr(pool, r2 + 1, job, 0))
1851 fprintf(stderr, "nothing matches '%s'\n", name);
1857 yesno(const char *str)
1859 char inbuf[128], *ip;
1866 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
1871 while (*ip == ' ' || *ip == '\t')
1878 if (*ip == 'y' || *ip == 'n')
1879 return *ip == 'y' ? 1 : 0;
1891 fileconflict_cb(Pool *pool, Id p, void *cbdata)
1893 struct fcstate *fcstate = cbdata;
1901 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
1904 s = pool_id2solvable(pool, p);
1905 if (pool->installed && s->repo == pool->installed)
1907 if (!s->repo->rpmdbid)
1909 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
1912 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
1914 for (i = 0; i < fcstate->newpkgscnt; i++)
1915 if (fcstate->checkq->elements[i] == p)
1917 if (i == fcstate->newpkgscnt)
1919 fp = fcstate->newpkgsfps[i];
1923 return rpm_byfp(fp, solvable2str(pool, s), &fcstate->rpmdbstate);
1927 runrpm(const char *arg, const char *name, int dupfd3)
1932 if ((pid = fork()) == (pid_t)-1)
1939 if (dupfd3 != -1 && dupfd3 != 3)
1945 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
1946 if (strcmp(arg, "-e") == 0)
1947 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1949 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
1953 while (waitpid(pid, &status, 0) != pid)
1957 printf("rpm failed\n");
1963 nscallback(Pool *pool, void *data, Id name, Id evr)
1965 if (name == NAMESPACE_PRODUCTBUDDY)
1967 /* SUSE specific hack: each product has an associated rpm */
1968 Solvable *s = pool->solvables + evr;
1971 cap = str2id(pool, pool_tmpjoin(pool, "product(", id2str(pool, s->name) + 8, ")"), 0);
1974 cap = rel2id(pool, cap, s->evr, REL_EQ, 0);
1977 FOR_PROVIDES(p, pp, cap)
1979 Solvable *ps = pool->solvables + p;
1980 if (ps->repo == s->repo && ps->arch == s->arch)
1988 #ifdef SOFTLOCKS_PATH
1991 addsoftlocks(Pool *pool, Queue *job)
1995 char *bp, *ep, buf[4096];
1997 if ((fp = fopen(SOFTLOCKS_PATH, "r")) == 0)
1999 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
2001 while (*bp == ' ' || *bp == '\t')
2003 if (!*bp || *bp == '#')
2005 for (ep = bp; *ep; ep++)
2006 if (*ep == ' ' || *ep == '\t' || *ep == '\n')
2009 type = SOLVER_SOLVABLE_NAME;
2010 if (!strncmp(bp, "provides:", 9) && bp[9])
2012 type = SOLVER_SOLVABLE_PROVIDES;
2015 id = str2id(pool, bp, 1);
2016 if (pool->installed)
2018 FOR_JOB_SELECT(p, pp, type, id)
2019 if (pool->solvables[p].repo == pool->installed)
2022 continue; /* ignore, as it is already installed */
2024 queue_push2(job, SOLVER_LOCK|SOLVER_WEAK|type, id);
2033 rewrite_repos(Pool *pool, Id *addedfileprovides)
2038 Queue fileprovidesq;
2040 int i, j, n, nprovidedids;
2041 struct repoinfo *cinfo;
2043 map_init(&providedids, pool->ss.nstrings);
2044 queue_init(&fileprovidesq);
2045 for (nprovidedids = 0; (id = addedfileprovides[nprovidedids]) != 0; nprovidedids++)
2046 MAPSET(&providedids, id);
2049 /* make sure this repo has just one main repodata */
2050 if (!repo->nrepodata)
2052 cinfo = repo->appdata;
2053 data = repo->repodata + 0;
2054 if (data->store.pagefd == -1)
2056 if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
2059 for (j = 0; j < fileprovidesq.count; j++)
2060 if (MAPTST(&providedids, fileprovidesq.elements[j]))
2062 if (n == nprovidedids)
2063 continue; /* nothing new added */
2066 for (j = 0; addedfileprovides[j]; j++)
2067 repodata_add_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[j]);
2068 repodata_internalize(data);
2069 writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
2071 queue_free(&fileprovidesq);
2072 map_free(&providedids);
2076 #define MODE_INSTALL 1
2077 #define MODE_ERASE 2
2078 #define MODE_UPDATE 3
2079 #define MODE_DISTUPGRADE 4
2080 #define MODE_VERIFY 5
2081 #define MODE_PATCH 6
2083 #define MODE_REPOLIST 8
2084 #define MODE_SEARCH 9
2089 fprintf(stderr, "Usage: solv COMMAND <select>\n");
2090 fprintf(stderr, "\n");
2091 fprintf(stderr, " distupgrade: replace installed packages with\n");
2092 fprintf(stderr, " versions from the repositories\n");
2093 fprintf(stderr, " erase: erase installed packages\n");
2094 fprintf(stderr, " info: display package information\n");
2095 fprintf(stderr, " install: install packages\n");
2096 fprintf(stderr, " list: list packages\n");
2097 fprintf(stderr, " repos: list enabled repositories\n");
2098 fprintf(stderr, " search: search name/summary/description\n");
2099 fprintf(stderr, " update: update installed packages\n");
2100 fprintf(stderr, " verify: check dependencies of installed packages\n");
2101 fprintf(stderr, "\n");
2106 main(int argc, char **argv)
2109 Repo *commandlinerepo = 0;
2110 Id *commandlinepkgs = 0;
2112 struct repoinfo *repoinfos;
2114 int mainmode = 0, mode = 0;
2119 char inbuf[128], *ip;
2122 struct fcstate fcstate;
2123 Id *addedfileprovides = 0;
2129 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2131 mainmode = MODE_INSTALL;
2132 mode = SOLVER_INSTALL;
2134 else if (!strcmp(argv[0], "patch"))
2136 mainmode = MODE_PATCH;
2137 mode = SOLVER_INSTALL;
2139 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2141 mainmode = MODE_ERASE;
2142 mode = SOLVER_ERASE;
2144 else if (!strcmp(argv[0], "list"))
2146 mainmode = MODE_LIST;
2149 else if (!strcmp(argv[0], "info"))
2151 mainmode = MODE_INFO;
2154 else if (!strcmp(argv[0], "search"))
2156 mainmode = MODE_SEARCH;
2159 else if (!strcmp(argv[0], "verify"))
2161 mainmode = MODE_VERIFY;
2162 mode = SOLVER_VERIFY;
2164 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2166 mainmode = MODE_UPDATE;
2167 mode = SOLVER_UPDATE;
2169 else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2171 mainmode = MODE_DISTUPGRADE;
2172 mode = SOLVER_UPDATE;
2174 else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2176 mainmode = MODE_REPOLIST;
2182 pool = pool_create();
2184 pool->obsoleteusescolors = 1;
2186 pool_setloadcallback(pool, load_stub, 0);
2187 pool->nscallback = nscallback;
2188 // pool_setdebuglevel(pool, 2);
2190 repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
2192 if (mainmode == MODE_REPOLIST)
2195 for (i = 0; i < nrepoinfos; i++)
2197 struct repoinfo *cinfo = repoinfos + i;
2198 if (!cinfo->enabled)
2200 printf("%d: %-20s %s\n", j++, cinfo->alias, cinfo->name);
2205 read_repos(pool, repoinfos, nrepoinfos);
2207 if (mainmode == MODE_SEARCH)
2213 map_init(&m, pool->nsolvables);
2214 dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2215 dataiterator_set_keyname(&di, SOLVABLE_NAME);
2216 dataiterator_set_search(&di, 0, 0);
2217 while (dataiterator_step(&di))
2218 MAPSET(&m, di.solvid);
2219 dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2220 dataiterator_set_search(&di, 0, 0);
2221 while (dataiterator_step(&di))
2222 MAPSET(&m, di.solvid);
2223 dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2224 dataiterator_set_search(&di, 0, 0);
2225 while (dataiterator_step(&di))
2226 MAPSET(&m, di.solvid);
2227 dataiterator_free(&di);
2229 for (p = 1; p < pool->nsolvables; p++)
2231 Solvable *s = pool_id2solvable(pool, p);
2234 printf(" - %s: %s\n", solvable2str(pool, s), solvable_lookup_str(s, SOLVABLE_SUMMARY));
2241 if (mainmode == MODE_LIST || mainmode == MODE_INSTALL)
2243 for (i = 1; i < argc; i++)
2246 l = strlen(argv[i]);
2247 if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2249 if (access(argv[i], R_OK))
2254 if (!commandlinepkgs)
2255 commandlinepkgs = sat_calloc(argc, sizeof(Id));
2256 if (!commandlinerepo)
2257 commandlinerepo = repo_create(pool, "@commandline");
2258 repo_add_rpms(commandlinerepo, (const char **)argv + i, 1, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2259 commandlinepkgs[i] = commandlinerepo->end - 1;
2261 if (commandlinerepo)
2262 repo_internalize(commandlinerepo);
2265 // FOR_REPOS(i, repo)
2266 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2267 addedfileprovides = 0;
2268 pool_addfileprovides_ids(pool, 0, &addedfileprovides);
2269 if (addedfileprovides && *addedfileprovides)
2270 rewrite_repos(pool, addedfileprovides);
2271 sat_free(addedfileprovides);
2272 pool_createwhatprovides(pool);
2275 for (i = 1; i < argc; i++)
2280 if (commandlinepkgs && commandlinepkgs[i])
2282 queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
2286 mkselect(pool, mode, argv[i], &job2);
2287 for (j = 0; j < job2.count; j++)
2288 queue_push(&job, job2.elements[j]);
2292 if (!job.count && mainmode != MODE_UPDATE && mainmode != MODE_DISTUPGRADE && mainmode != MODE_VERIFY && mainmode != MODE_PATCH)
2294 printf("no package matched\n");
2301 if (mainmode == MODE_LIST || mainmode == MODE_INFO)
2303 /* list mode, no solver needed */
2304 for (i = 0; i < job.count; i += 2)
2306 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
2308 Solvable *s = pool_id2solvable(pool, p);
2309 if (mainmode == MODE_INFO)
2311 printf("Name: %s\n", solvable2str(pool, s));
2312 printf("Repo: %s\n", s->repo->name);
2313 printf("Summary: %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
2314 printf("Url: %s\n", solvable_lookup_str(s, SOLVABLE_URL));
2315 printf("License: %s\n", solvable_lookup_str(s, SOLVABLE_LICENSE));
2316 printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
2321 const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de");
2322 printf(" - %s [%s]\n", solvable2str(pool, s), s->repo->name);
2324 printf(" %s\n", sum);
2330 free_repoinfos(repoinfos, nrepoinfos);
2331 sat_free(commandlinepkgs);
2335 if (mainmode == MODE_PATCH)
2341 map_init(&installedmap, pool->nsolvables);
2342 if (pool->installed)
2343 FOR_REPO_SOLVABLES(pool->installed, p, s)
2344 MAPSET(&installedmap, p);
2346 /* install all patches */
2347 for (p = 1; p < pool->nsolvables; p++)
2353 s = pool->solvables + p;
2354 if (strncmp(id2str(pool, s->name), "patch:", 6) != 0)
2356 FOR_PROVIDES(p2, pp, s->name)
2358 Solvable *s2 = pool->solvables + p2;
2359 if (s2->name != s->name)
2361 r = evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2362 if (r < 0 || (r == 0 && p > p2))
2367 type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2368 if (type && !strcmp(type, "optional"))
2370 r = solvable_trivial_installable_map(s, &installedmap, 0);
2373 if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2380 queue_push2(&job, SOLVER_SOLVABLE, p);
2382 map_free(&installedmap);
2386 for (i = 0; i < job.count; i += 2)
2388 if (mode == SOLVER_UPDATE)
2390 /* make update of not installed packages an install */
2391 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
2392 if (pool->installed && pool->solvables[p].repo == pool->installed)
2396 job.elements[i] |= SOLVER_INSTALL;
2400 job.elements[i] |= mode;
2403 // multiversion test
2404 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae", 1));
2405 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-base", 1));
2406 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, str2id(pool, "kernel-pae-extra", 1));
2408 #ifdef SOFTLOCKS_PATH
2409 addsoftlocks(pool, &job);
2415 Id problem, solution;
2418 solv = solver_create(pool);
2419 solv->ignorealreadyrecommended = 1;
2420 solv->updatesystem = allpkgs && (mainmode == MODE_UPDATE || mainmode == MODE_DISTUPGRADE);
2421 solv->dosplitprovides = solv->updatesystem;
2422 solv->fixsystem = allpkgs && (mainmode == MODE_VERIFY);
2423 if (mainmode == MODE_DISTUPGRADE)
2425 solv->distupgrade = 1;
2426 solv->allowdowngrade = 1;
2427 solv->allowarchchange = 1;
2428 solv->allowvendorchange = 1;
2430 if (mainmode == MODE_ERASE)
2431 solv->allowuninstall = 1; /* don't nag */
2433 // queue_push2(&job, SOLVER_DISTUPGRADE, 3);
2434 solver_solve(solv, &job);
2435 if (!solv->problems.count)
2437 pcnt = solver_problem_count(solv);
2438 printf("Found %d problems:\n", pcnt);
2439 for (problem = 1; problem <= pcnt; problem++)
2442 printf("Problem %d:\n", problem);
2443 solver_printprobleminfo(solv, problem);
2445 scnt = solver_solution_count(solv, problem);
2446 for (solution = 1; solution <= scnt; solution++)
2448 printf("Solution %d:\n", solution);
2449 solver_printsolution(solv, problem, solution);
2454 printf("Please choose a solution: ");
2457 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2462 while (*ip == ' ' || *ip == '\t')
2464 if (*ip >= '0' && *ip <= '9')
2467 if (take >= 1 && take <= scnt)
2483 solver_take_solution(solv, problem, take, &job);
2489 trans = &solv->trans;
2490 if (!trans->steps.count)
2492 printf("Nothing to do.\n");
2496 printf("Transaction summary:\n\n");
2497 solver_printtransaction(solv);
2506 duc[1].path = "/usr/share/man";
2507 duc[2].path = "/sbin";
2508 duc[3].path = "/etc";
2509 transaction_calc_duchanges(trans, duc, 4);
2510 for (i = 0; i < 4; i++)
2511 printf("duchanges %s: %d K %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
2514 printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
2517 if (!yesno("OK to continue (y/n)? "))
2523 queue_init(&checkq);
2524 newpkgs = transaction_installedresult(trans, &checkq);
2529 int downloadsize = 0;
2530 for (i = 0; i < newpkgs; i++)
2534 p = checkq.elements[i];
2535 s = pool_id2solvable(pool, p);
2536 downloadsize += solvable_lookup_num(s, SOLVABLE_DOWNLOADSIZE, 0);
2538 printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
2539 newpkgsfps = sat_calloc(newpkgs, sizeof(*newpkgsfps));
2540 for (i = 0; i < newpkgs; i++)
2542 unsigned int medianr;
2545 struct repoinfo *cinfo;
2546 const unsigned char *chksum;
2550 p = checkq.elements[i];
2551 s = pool_id2solvable(pool, p);
2552 if (s->repo == commandlinerepo)
2554 loc = solvable_get_location(s, &medianr);
2555 if (!(newpkgsfps[i] = fopen(loc, "r")))
2563 cinfo = s->repo->appdata;
2566 printf("%s: no repository information\n", s->repo->name);
2569 loc = solvable_get_location(s, &medianr);
2573 if (pool->installed && pool->installed->nsolvables)
2575 /* try a delta first */
2576 char *matchname = strdup(id2str(pool, s->name));
2577 dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
2578 dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
2579 while (dataiterator_step(&di))
2583 dataiterator_setpos_parent(&di);
2584 if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
2585 pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
2587 baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
2588 FOR_PROVIDES(op, pp, s->name)
2590 Solvable *os = pool->solvables + op;
2591 if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
2594 if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
2596 /* base is installed, run sequence check */
2597 const char *seqname;
2606 seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
2607 seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
2608 seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
2609 seq = pool_tmpjoin(pool, seqname, "-", seqevr);
2610 seq = pool_tmpjoin(pool, seq, "-", seqnum);
2612 sprintf(cmd, "/usr/bin/applydeltarpm -a %s -c -s ", id2str(pool, s->arch));
2614 sprintf(cmd, "/usr/bin/applydeltarpm -c -s ");
2616 if (system(pool_tmpjoin(pool, cmd, seq, 0)) != 0)
2617 continue; /* didn't match */
2618 /* looks good, download delta */
2620 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
2622 continue; /* no way! */
2623 dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
2624 dloc = pool_tmpjoin(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
2625 dloc = pool_tmpjoin(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
2626 dloc = pool_tmpjoin(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
2627 if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
2629 /* got it, now reconstruct */
2630 newfd = opentmpfile();
2632 sprintf(cmd, "applydeltarpm -a %s /dev/fd/%d /dev/fd/%d", id2str(pool, s->arch), fileno(fp), newfd);
2634 sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
2636 fcntl(fileno(fp), F_SETFD, 0);
2643 lseek(newfd, 0, SEEK_SET);
2645 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2646 if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
2652 newpkgsfps[i] = fdopen(newfd, "r");
2657 dataiterator_free(&di);
2658 sat_free(matchname);
2665 continue; /* delta worked! */
2667 if (cinfo->type == TYPE_SUSETAGS)
2669 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
2670 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
2673 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
2674 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
2676 printf("\n%s: %s not found in repository\n", s->repo->name, loc);
2689 printf("Searching for file conflicts\n");
2690 queue_init(&conflicts);
2691 fcstate.rpmdbstate = 0;
2692 fcstate.newpkgscnt = newpkgs;
2693 fcstate.checkq = &checkq;
2694 fcstate.newpkgsfps = newpkgsfps;
2695 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fileconflict_cb, &fcstate);
2696 if (conflicts.count)
2699 for (i = 0; i < conflicts.count; i += 5)
2700 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]));
2702 if (yesno("Re-run solver (y/n/q)? "))
2704 for (i = 0; i < newpkgs; i++)
2706 fclose(newpkgsfps[i]);
2707 newpkgsfps = sat_free(newpkgsfps);
2709 pool_add_fileconflicts_deps(pool, &conflicts);
2710 pool_createwhatprovides(pool); /* Hmm... */
2714 queue_free(&conflicts);
2717 printf("Committing transaction:\n\n");
2718 transaction_order(trans, 0);
2719 for (i = 0; i < trans->steps.count; i++)
2721 const char *evr, *evrp, *nvra;
2726 p = trans->steps.elements[i];
2727 s = pool_id2solvable(pool, p);
2728 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
2731 case SOLVER_TRANSACTION_ERASE:
2732 printf("erase %s\n", solvid2str(pool, p));
2733 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
2735 /* strip epoch from evr */
2736 evr = evrp = id2str(pool, s->evr);
2737 while (*evrp >= '0' && *evrp <= '9')
2739 if (evrp > evr && evrp[0] == ':' && evrp[1])
2741 nvra = pool_tmpjoin(pool, id2str(pool, s->name), "-", evr);
2742 nvra = pool_tmpjoin(pool, nvra, ".", id2str(pool, s->arch));
2743 runrpm("-e", nvra, -1); /* to bad that --querybynumber doesn't work */
2745 case SOLVER_TRANSACTION_INSTALL:
2746 case SOLVER_TRANSACTION_MULTIINSTALL:
2747 printf("install %s\n", solvid2str(pool, p));
2748 for (j = 0; j < newpkgs; j++)
2749 if (checkq.elements[j] == p)
2751 fp = j < newpkgs ? newpkgsfps[j] : 0;
2755 lseek(fileno(fp), 0, SEEK_SET);
2756 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
2765 for (i = 0; i < newpkgs; i++)
2767 fclose(newpkgsfps[i]);
2768 sat_free(newpkgsfps);
2769 queue_free(&checkq);
2773 free_repoinfos(repoinfos, nrepoinfos);
2774 sat_free(commandlinepkgs);