2 * Copyright (c) 2009, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
8 /* solv, a little software installer demoing the sat solver library */
11 * - understands globs for package names / dependencies
12 * - understands .arch suffix
13 * - installation of commandline packages
14 * - repository data caching
15 * - on demand loading of secondary repository data
16 * - gpg and checksum verification
19 * - fastestmirror implementation
21 * things available in the library but missing from solv:
22 * - vendor policy loading
23 * - soft locks file handling
24 * - multi version handling
37 #include <sys/utsname.h>
38 #include <sys/types.h>
45 #include <sys/socket.h>
57 #include "solverdebug.h"
59 #include "repo_solv.h"
61 #include "repo_write.h"
63 #include "repo_rpmdb.h"
64 #include "pool_fileconflicts.h"
70 #include "repo_rpmmd.h"
71 #include "repo_repomdxml.h"
72 #include "repo_updateinfoxml.h"
73 #include "repo_deltainfoxml.h"
75 #ifdef ENABLE_SUSEREPO
76 #include "repo_products.h"
77 #include "repo_susetags.h"
78 #include "repo_content.h"
80 #include "solv_xfopen.h"
83 # define REPOINFO_PATH "/etc/yum.repos.d"
86 # define REPOINFO_PATH "/etc/zypp/repos.d"
87 # define PRODUCTS_PATH "/etc/products.d"
88 # define SOFTLOCKS_PATH "/var/lib/zypp/SoftLocks"
91 #define SOLVCACHE_PATH "/var/cache/solv"
93 #define METADATA_EXPIRE (60 * 15)
115 unsigned char cookie[32];
116 unsigned char extcookie[32];
121 yum_substitute(Pool *pool, char *line)
124 static char *releaseevr;
125 static char *basearch;
129 solv_free(releaseevr);
136 while ((p2 = strchr(p, '$')) != 0)
138 if (!strncmp(p2, "$releasever", 11))
143 const char *rootdir = pool_get_rootdir(pool);
146 rpm_installedrpmdbids(rootdir, "Providename", "redhat-release", &q);
149 void *handle, *state = 0;
151 handle = rpm_byrpmdbid(q.elements[0], rootdir, &state);
152 releaseevr = rpm_query(handle, SOLVABLE_EVR);
153 rpm_byrpmdbid(0, 0, &state);
154 if ((p = strchr(releaseevr, '-')) != 0)
160 fprintf(stderr, "no installed package provides 'redhat-release', cannot determine $releasever\n");
165 p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
166 p2 = p + (p2 - line);
168 p = p2 + strlen(releaseevr);
171 if (!strncmp(p2, "$basearch", 9))
181 basearch = strdup(un.machine);
182 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
186 p = pool_tmpjoin(pool, line, basearch, p2 + 9);
187 p2 = p + (p2 - line);
189 p = p2 + strlen(basearch);
198 #define TYPE_UNKNOWN 0
199 #define TYPE_SUSETAGS 1
201 #define TYPE_PLAINDIR 3
202 #define TYPE_DEBIAN 4
206 read_repoinfos_sort(const void *ap, const void *bp)
208 const struct repoinfo *a = ap;
209 const struct repoinfo *b = bp;
210 return strcmp(a->alias, b->alias);
214 #if defined(SUSE) || defined(FEDORA)
217 read_repoinfos(Pool *pool, int *nrepoinfosp)
219 const char *reposdir = REPOINFO_PATH;
221 char buf2[4096], *kp, *vp, *kpe;
226 struct repoinfo *repoinfos = 0, *cinfo;
229 rdlen = strlen(reposdir);
230 dir = opendir(reposdir);
236 while ((ent = readdir(dir)) != 0)
238 if (ent->d_name[0] == '.')
240 l = strlen(ent->d_name);
241 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
243 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
244 if ((fp = fopen(buf, "r")) == 0)
250 while(fgets(buf2, sizeof(buf2), fp))
255 while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
258 while (*kp == ' ' || *kp == '\t')
260 if (!*kp || *kp == '#')
264 kp = yum_substitute(pool, kp);
268 vp = strrchr(kp, ']');
272 repoinfos = solv_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
273 cinfo = repoinfos + nrepoinfos++;
274 memset(cinfo, 0, sizeof(*cinfo));
275 cinfo->alias = strdup(kp + 1);
276 cinfo->type = TYPE_RPMMD;
277 cinfo->autorefresh = 1;
278 cinfo->priority = 99;
280 cinfo->repo_gpgcheck = 1;
282 cinfo->metadata_expire = METADATA_EXPIRE;
287 vp = strchr(kp, '=');
290 for (kpe = vp - 1; kpe >= kp; kpe--)
291 if (*kpe != ' ' && *kpe != '\t')
296 while (*vp == ' ' || *vp == '\t')
299 if (!strcmp(kp, "name"))
300 cinfo->name = strdup(vp);
301 else if (!strcmp(kp, "enabled"))
302 cinfo->enabled = *vp == '0' ? 0 : 1;
303 else if (!strcmp(kp, "autorefresh"))
304 cinfo->autorefresh = *vp == '0' ? 0 : 1;
305 else if (!strcmp(kp, "gpgcheck"))
306 cinfo->pkgs_gpgcheck = *vp == '0' ? 0 : 1;
307 else if (!strcmp(kp, "repo_gpgcheck"))
308 cinfo->repo_gpgcheck = *vp == '0' ? 0 : 1;
309 else if (!strcmp(kp, "baseurl"))
310 cinfo->baseurl = strdup(vp);
311 else if (!strcmp(kp, "mirrorlist"))
313 if (strstr(vp, "metalink"))
314 cinfo->metalink = strdup(vp);
316 cinfo->mirrorlist = strdup(vp);
318 else if (!strcmp(kp, "path"))
320 if (vp && strcmp(vp, "/") != 0)
321 cinfo->path = strdup(vp);
323 else if (!strcmp(kp, "type"))
325 if (!strcmp(vp, "yast2"))
326 cinfo->type = TYPE_SUSETAGS;
327 else if (!strcmp(vp, "rpm-md"))
328 cinfo->type = TYPE_RPMMD;
329 else if (!strcmp(vp, "plaindir"))
330 cinfo->type = TYPE_PLAINDIR;
332 cinfo->type = TYPE_UNKNOWN;
334 else if (!strcmp(kp, "priority"))
335 cinfo->priority = atoi(vp);
336 else if (!strcmp(kp, "keeppackages"))
337 cinfo->keeppackages = *vp == '0' ? 0 : 1;
343 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
344 *nrepoinfosp = nrepoinfos;
353 read_repoinfos(Pool *pool, int *nrepoinfosp)
359 char *kp, *url, *distro;
360 struct repoinfo *repoinfos = 0, *cinfo;
365 fp = fopen("/etc/apt/sources.list", "r");
372 dir = opendir("/etc/apt/sources.list.d");
376 if ((ent = readdir(dir)) == 0)
381 if (ent->d_name[0] == '.')
383 l = strlen(ent->d_name);
384 if (l < 5 || strcmp(ent->d_name + l - 5, ".list") != 0)
386 snprintf(buf, sizeof(buf), "%s/%s", "/etc/apt/sources.list.d", ent->d_name);
387 if (!(fp = fopen(buf, "r")))
390 while(fgets(buf2, sizeof(buf2), fp))
395 while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
398 while (*kp == ' ' || *kp == '\t')
400 if (!*kp || *kp == '#')
402 if (strncmp(kp, "deb", 3) != 0)
405 if (*kp != ' ' && *kp != '\t')
407 while (*kp == ' ' || *kp == '\t')
412 while (*kp && *kp != ' ' && *kp != '\t')
416 while (*kp == ' ' || *kp == '\t')
421 while (*kp && *kp != ' ' && *kp != '\t')
425 while (*kp == ' ' || *kp == '\t')
429 repoinfos = solv_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
430 cinfo = repoinfos + nrepoinfos++;
431 memset(cinfo, 0, sizeof(*cinfo));
432 cinfo->baseurl = strdup(url);
433 cinfo->alias = solv_dupjoin(url, "/", distro);
434 cinfo->name = strdup(distro);
435 cinfo->type = TYPE_DEBIAN;
437 cinfo->autorefresh = 1;
438 cinfo->repo_gpgcheck = 1;
439 cinfo->metadata_expire = METADATA_EXPIRE;
443 while (*kp == ' ' || *kp == '\t')
448 while (*kp && *kp != ' ' && *kp != '\t')
452 cinfo->components = solv_extend(cinfo->components, cinfo->ncomponents, 1, sizeof(*cinfo->components), 15);
453 cinfo->components[cinfo->ncomponents++] = strdup(compo);
459 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
460 *nrepoinfosp = nrepoinfos;
468 read_repoinfos(Pool *pool, int *nrepoinfosp)
477 free_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
480 for (i = 0; i < nrepoinfos; i++)
482 struct repoinfo *cinfo = repoinfos + i;
483 solv_free(cinfo->name);
484 solv_free(cinfo->alias);
485 solv_free(cinfo->path);
486 solv_free(cinfo->metalink);
487 solv_free(cinfo->mirrorlist);
488 solv_free(cinfo->baseurl);
489 for (j = 0; j < cinfo->ncomponents; j++)
490 solv_free(cinfo->components[j]);
491 solv_free(cinfo->components);
493 solv_free(repoinfos);
502 strcpy(tmpl, "/var/tmp/solvXXXXXX");
514 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
517 const unsigned char *sum;
521 h = solv_chksum_create(chksumtype);
524 printf("%s: unknown checksum type\n", file);
527 while ((l = read(fd, buf, sizeof(buf))) > 0)
528 solv_chksum_add(h, buf, l);
529 lseek(fd, 0, SEEK_SET);
531 sum = solv_chksum_get(h, &l);
532 if (memcmp(sum, chksum, l))
534 printf("%s: checksum mismatch\n", file);
535 solv_chksum_free(h, 0);
538 solv_chksum_free(h, 0);
543 findfastest(char **urls, int nurls)
550 struct addrinfo hints, *result;;
552 fds = solv_calloc(nurls, sizeof(*fds));
553 socks = solv_calloc(nurls, sizeof(*socks));
554 for (i = 0; i < nurls; i++)
557 p = strchr(urls[i], '/');
570 if ((p2 = strchr(p, '@')) != 0)
573 if (!strncmp("https:", urls[i], 6))
575 else if (!strncmp("ftp:", urls[i], 4))
577 if ((p2 = strrchr(p, ':')) != 0)
586 sprintf(portstr, "%d", port);
587 memset(&hints, 0, sizeof(struct addrinfo));
588 hints.ai_family = AF_UNSPEC;
589 hints.ai_socktype = SOCK_STREAM;
590 hints.ai_flags = AI_NUMERICSERV;
592 if (!getaddrinfo(p, portstr, &hints, &result))
594 socks[i] = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
597 fcntl(socks[i], F_SETFL, O_NONBLOCK);
598 if (connect(socks[i], result->ai_addr, result->ai_addrlen) == -1)
600 if (errno != EINPROGRESS)
607 freeaddrinfo(result);
614 for (i = j = 0; i < nurls; i++)
618 fds[j].fd = socks[i];
619 fds[j].events = POLLOUT;
627 if (poll(fds, j, 10000) <= 0)
629 i = -1; /* something is wrong */
632 for (i = 0; i < j; i++)
633 if ((fds[i].revents & POLLOUT) != 0)
636 socklen_t soel = sizeof(int);
637 if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, &soe, &soel) == -1 || soe != 0)
639 /* connect failed, kill socket */
640 for (j = 0; j < nurls; j++)
641 if (socks[j] == fds[i].fd)
654 i = -1; /* something is wrong, no bit was set */
657 /* now i contains the fastest fd index */
660 for (j = 0; j < nurls; j++)
661 if (socks[j] == fds[i].fd)
665 char *url0 = urls[0];
670 for (i = j = 0; i < nurls; i++)
678 findmetalinkurl(FILE *fp, unsigned char *chksump, Id *chksumtypep)
680 char buf[4096], *bp, *ep;
687 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
689 while (*bp == ' ' || *bp == '\t')
691 if (chksumtypep && !*chksumtypep && !strncmp(bp, "<hash type=\"sha256\">", 20))
694 if (solv_hex2bin((const char **)&bp, chksump, 32) == 32)
695 *chksumtypep = REPOKEY_TYPE_SHA256;
698 if (strncmp(bp, "<url", 4))
700 bp = strchr(bp, '>');
704 ep = strstr(bp, "repodata/repomd.xml</url>");
708 if (strncmp(bp, "http", 4))
710 urls = solv_extend(urls, nurls, 1, sizeof(*urls), 15);
711 urls[nurls++] = strdup(bp);
716 findfastest(urls, nurls > 5 ? 5 : nurls);
719 for (i = 0; i < nurls; i++)
722 ep = strchr(bp, '/');
723 if ((ep = strchr(ep + 2, '/')) != 0)
726 printf("[using mirror %s]\n", bp);
735 findmirrorlisturl(FILE *fp)
737 char buf[4096], *bp, *ep;
742 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
744 while (*bp == ' ' || *bp == '\t')
746 if (!*bp || *bp == '#')
749 while (l > 0 && (bp[l - 1] == ' ' || bp[l - 1] == '\t' || bp[l - 1] == '\n'))
751 urls = solv_extend(urls, nurls, 1, sizeof(*urls), 15);
752 urls[nurls++] = strdup(bp);
757 findfastest(urls, nurls > 5 ? 5 : nurls);
760 for (i = 0; i < nurls; i++)
763 ep = strchr(bp, '/');
764 if ((ep = strchr(ep + 2, '/')) != 0)
767 printf("[using mirror %s]\n", bp);
776 iscompressed(const char *name)
778 int l = strlen(name);
779 return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
783 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype, int *badchecksump)
789 const char *baseurl = cinfo->baseurl;
793 if (!cinfo->metalink && !cinfo->mirrorlist)
795 if (file != cinfo->metalink && file != cinfo->mirrorlist)
797 FILE *fp = curlfopen(cinfo, cinfo->metalink ? cinfo->metalink : cinfo->mirrorlist, 0, 0, 0, 0);
798 unsigned char mlchksum[32];
803 cinfo->baseurl = findmetalinkurl(fp, mlchksum, &mlchksumtype);
805 cinfo->baseurl = findmirrorlisturl(fp);
810 if (strchr(cinfo->baseurl, '$'))
812 char *b = yum_substitute(cinfo->repo->pool, cinfo->baseurl);
813 free(cinfo->baseurl);
814 cinfo->baseurl = strdup(b);
817 if (!chksumtype && mlchksumtype && !strcmp(file, "repodata/repomd.xml"))
819 chksumtype = mlchksumtype;
822 return curlfopen(cinfo, file, uncompress, chksum, chksumtype, badchecksump);
824 snprintf(url, sizeof(url), "%s", file);
829 if (l && baseurl[l - 1] == '/')
830 snprintf(url, sizeof(url), "%s%s", baseurl, file);
832 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
835 // printf("url: %s\n", url);
836 if ((pid = fork()) == (pid_t)-1)
848 execlp("curl", "curl", "-f", "-s", "-L", url, (char *)0);
853 while (waitpid(pid, &status, 0) != pid)
855 if (lseek(fd, 0, SEEK_END) == 0 && (!status || !chksumtype))
861 lseek(fd, 0, SEEK_SET);
864 printf("%s: download error %d\n", file, status >> 8 ? status >> 8 : status);
870 if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
878 return solv_xfopen_fd(".gz", fd, "r");
879 fcntl(fd, F_SETFD, FD_CLOEXEC);
880 return fdopen(fd, "r");
886 cleanupgpg(char *gpgdir)
889 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
891 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
893 snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
895 snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
897 snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
903 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
912 off_t posfp, possigfp;
915 gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
918 keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
919 if (!(kfp = fopen(keysfile, "w")) )
925 for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
929 pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
930 if (!pubkey || !*pubkey)
932 if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
934 if (fputc('\n', kfp) == EOF) /* Just in case... */
938 if (fclose(kfp) || !nkeys)
943 snprintf(cmd, sizeof(cmd), "gpg2 -q --homedir %s --import %s", gpgdir, keysfile);
946 fprintf(stderr, "key import error\n");
951 posfp = lseek(fileno(fp), 0, SEEK_CUR);
952 lseek(fileno(fp), 0, SEEK_SET);
953 possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
954 lseek(fileno(sigfp), 0, SEEK_SET);
955 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
956 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
957 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
959 lseek(fileno(sigfp), possigfp, SEEK_SET);
960 lseek(fileno(fp), posfp, SEEK_SET);
961 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
962 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
964 return r == 0 ? 1 : 0;
970 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
975 snprintf(cmd, sizeof(cmd), "gpgv -q --keyring /etc/apt/trusted.gpg /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", fileno(sigfp), fileno(fp));
976 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
977 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
979 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
980 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
981 return r == 0 ? 1 : 0;
989 Pool *sigpool = pool_create();
990 #if defined(ENABLE_RPMDB_PUBKEY)
991 Repo *repo = repo_create(sigpool, "rpmdbkeys");
992 repo_add_rpmdb_pubkeys(repo, 0);
998 downloadchecksig(struct repoinfo *cinfo, FILE *fp, const char *sigurl, Pool **sigpool)
1001 sigfp = curlfopen(cinfo, sigurl, 0, 0, 0, 0);
1004 printf(" unsigned, skipped\n");
1008 *sigpool = read_sigs();
1009 if (!checksig(*sigpool, fp, sigfp))
1011 printf(" checksig failed, skipped\n");
1019 #define CHKSUM_IDENT "1.1"
1022 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
1025 void *h = solv_chksum_create(chktype);
1028 solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
1029 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
1030 solv_chksum_add(h, buf, l);
1032 solv_chksum_free(h, out);
1036 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
1038 void *h = solv_chksum_create(chktype);
1039 solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
1040 solv_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
1041 solv_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
1042 solv_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
1043 solv_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
1044 solv_chksum_free(h, out);
1056 pool_setarch(pool, un.machine);
1059 char *calccachepath(Repo *repo, const char *repoext)
1061 char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
1064 p = pool_tmpappend(repo->pool, p, "_", repoext);
1065 p = pool_tmpappend(repo->pool, p, ".solvx", 0);
1068 p = pool_tmpappend(repo->pool, p, ".solv", 0);
1069 q = p + strlen(SOLVCACHE_PATH) + 1;
1079 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie, int mark)
1082 unsigned char mycookie[32];
1083 unsigned char myextcookie[32];
1084 struct repoinfo *cinfo;
1087 cinfo = repo->appdata;
1088 if (!(fp = fopen(calccachepath(repo, repoext), "r")))
1090 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
1095 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
1100 if (cinfo && !repoext)
1102 if (fseek(fp, -sizeof(mycookie) * 2, SEEK_END) || fread(myextcookie, sizeof(myextcookie), 1, fp) != 1)
1113 flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
1114 if (strcmp(repoext, "DL") != 0)
1115 flags |= REPO_LOCALPOOL; /* no local pool for DL so that we can compare IDs */
1118 if (repo_add_solv(repo, fp, flags))
1123 if (cinfo && !repoext)
1125 memcpy(cinfo->cookie, mycookie, sizeof(mycookie));
1126 memcpy(cinfo->extcookie, myextcookie, sizeof(myextcookie));
1129 futimes(fileno(fp), 0); /* try to set modification time */
1135 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
1140 struct repoinfo *cinfo;
1143 cinfo = repo->appdata;
1144 mkdir(SOLVCACHE_PATH, 0755);
1145 /* use dupjoin instead of tmpjoin because tmpl must survive repo_write */
1146 tmpl = solv_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
1154 if (!(fp = fdopen(fd, "w")))
1163 for (i = repo->start; i < repo->end; i++)
1164 if (repo->pool->solvables[i].repo != repo)
1170 repo_write(repo, fp);
1172 repodata_write(info, fp);
1175 int oldnrepodata = repo->nrepodata;
1176 repo->nrepodata = oldnrepodata > 2 ? 2 : oldnrepodata; /* XXX: do this right */
1177 repo_write(repo, fp);
1178 repo->nrepodata = oldnrepodata;
1182 if (!repoext && cinfo)
1184 if (!cinfo->extcookie[0])
1186 /* create the ext cookie and append it */
1187 /* we just need some unique ID */
1189 if (!fstat(fileno(fp), &stb))
1193 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
1194 for (i = 0; i < 32; i++)
1195 cinfo->extcookie[i] ^= cookie[i];
1197 if (cinfo->extcookie[0] == 0)
1198 cinfo->extcookie[0] = 1;
1200 if (fwrite(cinfo->extcookie, 32, 1, fp) != 1)
1208 /* append our cookie describing the metadata state */
1209 if (fwrite(cookie, 32, 1, fp) != 1)
1224 /* switch to just saved repo to activate paging and save memory */
1225 FILE *fp = fopen(tmpl, "r");
1231 repo_empty(repo, 1);
1232 if (repo_add_solv(repo, fp, SOLV_ADD_NO_STUBS))
1234 /* oops, no way to recover from here */
1235 fprintf(stderr, "internal error\n");
1241 /* make sure repodata contains complete repo */
1242 /* (this is how repodata_write saves it) */
1243 repodata_extend_block(info, repo->start, repo->end - repo->start);
1244 info->state = REPODATA_LOADING;
1245 /* no need for LOCALPOOL as pool already contains ids */
1246 repo_add_solv(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1247 info->state = REPODATA_AVAILABLE; /* in case the load failed */
1252 if (!rename(tmpl, calccachepath(repo, repoext)))
1259 /* repomd helpers */
1261 static inline const char *
1262 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1264 Pool *pool = repo->pool;
1266 const char *filename;
1271 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1272 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1273 if (dataiterator_step(&di))
1275 dataiterator_setpos_parent(&di);
1276 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1277 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
1279 dataiterator_free(&di);
1280 if (filename && !*chksumtypep)
1282 printf("no %s file checksum!\n", what);
1289 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
1291 Id chksumtype, handle;
1292 const unsigned char *chksum;
1293 const char *filename;
1295 filename = repomd_find(repo, what, &chksum, &chksumtype);
1298 if (!strcmp(what, "prestodelta"))
1300 handle = repodata_new_handle(data);
1301 repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
1302 repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
1303 repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
1304 if (!strcmp(what, "deltainfo"))
1306 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
1307 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
1309 if (!strcmp(what, "filelists"))
1311 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1312 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1314 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1319 repomd_load_ext(Repo *repo, Repodata *data)
1321 const char *filename, *repomdtype;
1324 struct repoinfo *cinfo;
1325 const unsigned char *filechksum;
1329 cinfo = repo->appdata;
1330 repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
1333 if (!strcmp(repomdtype, "filelists"))
1335 else if (!strcmp(repomdtype, "deltainfo"))
1340 printf("[%s:%s", repo->name, ext);
1342 if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1344 printf(" cached]\n");fflush(stdout);
1347 printf(" fetching]\n"); fflush(stdout);
1348 filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
1350 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, REPOSITORY_REPOMD_CHECKSUM, &filechksumtype);
1351 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1353 if (!strcmp(ext, "FL"))
1354 r = repo_add_rpmmd(repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1355 else if (!strcmp(ext, "DL"))
1356 r = repo_add_deltainfoxml(repo, fp, REPO_USE_LOADING);
1360 printf("%s\n", pool_errstr(repo->pool));
1363 writecachedrepo(repo, data, ext, cinfo->extcookie);
1370 #ifdef ENABLE_SUSEREPO
1371 /* susetags helpers */
1373 static inline const char *
1374 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1376 Pool *pool = repo->pool;
1378 const char *filename;
1383 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
1384 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1385 if (dataiterator_step(&di))
1387 dataiterator_setpos_parent(&di);
1388 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
1391 dataiterator_free(&di);
1392 if (filename && !*chksumtypep)
1394 printf("no %s file checksum!\n", what);
1400 static Id susetags_langtags[] = {
1401 SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
1402 SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
1403 SOLVABLE_EULA, REPOKEY_TYPE_STR,
1404 SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
1405 SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
1406 SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
1411 susetags_add_ext(Repo *repo, Repodata *data)
1413 Pool *pool = repo->pool;
1416 Id handle, filechksumtype;
1417 const unsigned char *filechksum;
1420 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
1421 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1422 while (dataiterator_step(&di))
1424 if (strncmp(di.kv.str, "packages.", 9) != 0)
1426 if (!strcmp(di.kv.str + 9, "gz"))
1428 if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
1430 ext[0] = di.kv.str[9];
1431 ext[1] = di.kv.str[10];
1433 if (!strcmp(ext, "en"))
1435 if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
1437 handle = repodata_new_handle(data);
1438 repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
1440 repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
1441 if (!strcmp(ext, "DU"))
1443 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
1444 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
1446 else if (!strcmp(ext, "FL"))
1448 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1449 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1453 for (i = 0; susetags_langtags[i]; i += 2)
1455 repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
1456 repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
1459 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1461 dataiterator_free(&di);
1465 susetags_load_ext(Repo *repo, Repodata *data)
1467 const char *filename, *descrdir;
1471 struct repoinfo *cinfo;
1472 const unsigned char *filechksum;
1475 cinfo = repo->appdata;
1476 filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
1480 ext[0] = filename[9];
1481 ext[1] = filename[10];
1484 printf("[%s:%s", repo->name, ext);
1486 if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1488 printf(" cached]\n"); fflush(stdout);
1492 printf(" fetching]\n"); fflush(stdout);
1494 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1495 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1497 descrdir = "suse/setup/descr";
1499 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
1500 if ((fp = curlfopen(cinfo, pool_tmpjoin(repo->pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1502 if (repo_add_susetags(repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES))
1505 printf("%s\n", pool_errstr(repo->pool));
1509 writecachedrepo(repo, data, ext, cinfo->extcookie);
1519 load_stub(Pool *pool, Repodata *data, void *dp)
1521 struct repoinfo *cinfo = data->repo->appdata;
1522 switch (cinfo->type)
1524 #ifdef ENABLE_SUSEREPO
1526 return susetags_load_ext(data->repo, data);
1530 return repomd_load_ext(data->repo, data);
1537 static unsigned char installedcookie[32];
1539 #ifdef ENABLE_DEBIAN
1542 debian_find_component(struct repoinfo *cinfo, FILE *fp, char *comp, const unsigned char **chksump, Id *chksumtypep)
1546 unsigned char *chksum;
1551 static char *basearch;
1563 basearch = strdup(un.machine);
1564 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
1567 binarydir = solv_dupjoin("binary-", basearch, "/");
1568 lbinarydir = strlen(binarydir);
1569 compl = strlen(comp);
1573 chksum = solv_malloc(32);
1575 while(fgets(buf, sizeof(buf), fp))
1580 while (l && (buf[l - 1] == '\n' || buf[l - 1] == ' ' || buf[l - 1] == '\t'))
1582 if (!strncasecmp(buf, "MD5Sum:", 7))
1584 curchksumtype = REPOKEY_TYPE_MD5;
1587 if (!strncasecmp(buf, "SHA1:", 5))
1589 curchksumtype = REPOKEY_TYPE_SHA1;
1592 if (!strncasecmp(buf, "SHA256:", 7))
1594 curchksumtype = REPOKEY_TYPE_SHA256;
1606 while (*bp && *bp != ' ' && *bp != '\t')
1611 while (*bp == ' ' || *bp == '\t')
1613 while (*bp && *bp != ' ' && *bp != '\t')
1617 while (*bp == ' ' || *bp == '\t')
1620 if (strncmp(fn, comp, compl) != 0 || fn[compl] != '/')
1623 if (strncmp(bp, binarydir, lbinarydir))
1626 if (!strcmp(bp, "Packages") || !strcmp(bp, "Packages.gz"))
1628 unsigned char curchksum[32];
1630 if (filename && !strcmp(bp, "Packages"))
1632 curl = solv_chksum_len(curchksumtype);
1633 if (!curl || (chksumtype && solv_chksum_len(chksumtype) > curl))
1635 if (solv_hex2bin((const char **)&ch, curchksum, sizeof(curchksum)) != curl)
1637 solv_free(filename);
1638 filename = strdup(fn);
1639 chksumtype = curchksumtype;
1640 memcpy(chksum, curchksum, curl);
1646 fn = solv_dupjoin("/", filename, 0);
1647 solv_free(filename);
1648 filename = solv_dupjoin("dists/", cinfo->name, fn);
1652 chksum = solv_free(chksum);
1654 *chksumtypep = chksumtype;
1660 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
1663 struct repoinfo *cinfo;
1666 const char *filename;
1667 const unsigned char *filechksum;
1669 #ifdef ENABLE_SUSEREPO
1670 const char *descrdir;
1675 #if defined(ENABLE_SUSEREPO) || defined(ENABLE_RPMMD)
1680 #if defined(ENABLE_DEBIAN)
1685 repo = repo_create(pool, "@System");
1686 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1687 printf("rpm database:");
1688 if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/rpm/Packages"), &stb))
1689 memset(&stb, 0, sizeof(&stb));
1691 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1692 printf("dpgk database:");
1693 if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/dpkg/status"), &stb))
1694 memset(&stb, 0, sizeof(&stb));
1697 printf("no installed database:");
1698 memset(&stb, 0, sizeof(&stb));
1700 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, installedcookie);
1701 if (usecachedrepo(repo, 0, installedcookie, 0))
1702 printf(" cached\n");
1705 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1709 printf(" reading\n");
1711 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1712 # if defined(ENABLE_SUSEREPO) && defined(PRODUCTS_PATH)
1713 if (repo_add_products(repo, PRODUCTS_PATH, REPO_NO_INTERNALIZE | REPO_USE_ROOTDIR))
1715 fprintf(stderr, "product reading failed: %s\n", pool_errstr(pool));
1719 if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1721 ref = repo_create(pool, "@System.old");
1722 if (repo_add_solv(ref, ofp, 0))
1729 if (repo_add_rpmdb(repo, ref, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
1731 fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1737 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1738 if (repo_add_debdb(repo, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
1740 fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1744 writecachedrepo(repo, 0, 0, installedcookie);
1746 pool_set_installed(pool, repo);
1748 for (i = 0; i < nrepoinfos; i++)
1750 cinfo = repoinfos + i;
1751 if (!cinfo->enabled)
1754 repo = repo_create(pool, cinfo->alias);
1756 repo->appdata = cinfo;
1757 repo->priority = 99 - cinfo->priority;
1759 dorefresh = cinfo->autorefresh;
1760 if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0), &stb) == 0)
1762 if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1765 if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1767 printf("repo '%s':", cinfo->alias);
1768 printf(" cached\n");
1772 switch (cinfo->type)
1776 printf("rpmmd repo '%s':", cinfo->alias);
1778 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1780 printf(" no repomd.xml file, skipped\n");
1785 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1786 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1788 printf(" cached\n");
1792 if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "repodata/repomd.xml.asc", &sigpool))
1797 if (repo_add_repomdxml(repo, fp, 0))
1799 printf("repomd.xml: %s\n", pool_errstr(pool));
1801 break; /* hopeless */
1804 printf(" fetching\n");
1805 filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1806 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1808 if (repo_add_rpmmd(repo, fp, 0, 0))
1810 printf("primary: %s\n", pool_errstr(pool));
1816 break; /* hopeless */
1818 filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1819 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1821 if (repo_add_updateinfoxml(repo, fp, 0))
1823 printf("updateinfo: %s\n", pool_errstr(pool));
1829 data = repo_add_repodata(repo, 0);
1830 if (!repomd_add_ext(repo, data, "deltainfo"))
1831 repomd_add_ext(repo, data, "prestodelta");
1832 repomd_add_ext(repo, data, "filelists");
1833 repodata_internalize(data);
1835 writecachedrepo(repo, 0, 0, cinfo->cookie);
1836 repodata_create_stubs(repo_last_repodata(repo));
1840 #ifdef ENABLE_SUSEREPO
1842 printf("susetags repo '%s':", cinfo->alias);
1846 if ((fp = curlfopen(cinfo, "content", 0, 0, 0, 0)) == 0)
1848 printf(" no content file, skipped\n");
1853 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1854 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1856 printf(" cached\n");
1860 if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "content.asc", &sigpool))
1865 if (repo_add_content(repo, fp, 0))
1867 printf("content: %s\n", pool_errstr(pool));
1869 break; /* hopeless */
1872 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1873 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1875 descrdir = "suse/setup/descr";
1876 filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1878 filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1881 printf(" no packages file entry, skipped\n");
1884 printf(" fetching\n");
1885 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
1886 break; /* hopeless */
1887 if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|SUSETAGS_RECORD_SHARES))
1889 printf("packages: %s\n", pool_errstr(pool));
1891 break; /* hopeless */
1894 /* add default language */
1895 filename = susetags_find(repo, "packages.en.gz", &filechksum, &filechksumtype);
1897 filename = susetags_find(repo, "packages.en", &filechksum, &filechksumtype);
1900 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1902 if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|REPO_REUSE_REPODATA|REPO_EXTEND_SOLVABLES))
1904 printf("packages.en: %s\n", pool_errstr(pool));
1910 filename = susetags_find(repo, "patterns", &filechksum, &filechksumtype);
1913 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1916 while (fgets(pbuf, sizeof(pbuf), fp))
1918 int l = strlen(pbuf);
1920 if (l && pbuf[l - 1] == '\n')
1922 if (!*pbuf || *pbuf == '.' || strchr(pbuf, '/') != 0)
1924 filename = susetags_find(repo, pbuf, &filechksum, &filechksumtype);
1925 if (filename && (fp2 = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1927 if (repo_add_susetags(repo, fp2, defvendor, 0, REPO_NO_INTERNALIZE))
1929 printf("%s: %s\n", pbuf, pool_errstr(pool));
1938 repo_internalize(repo);
1939 data = repo_add_repodata(repo, 0);
1940 susetags_add_ext(repo, data);
1941 repodata_internalize(data);
1943 writecachedrepo(repo, 0, 0, cinfo->cookie);
1944 repodata_create_stubs(repo_last_repodata(repo));
1948 #if defined(ENABLE_DEBIAN)
1950 printf("debian repo '%s':", cinfo->alias);
1952 filename = solv_dupjoin("dists/", cinfo->name, "/Release");
1953 if ((fpr = curlfopen(cinfo, filename, 0, 0, 0, 0)) == 0)
1955 printf(" no Release file, skipped\n");
1958 free((char *)filename);
1961 solv_free((char *)filename);
1962 if (cinfo->repo_gpgcheck)
1964 filename = solv_dupjoin("dists/", cinfo->name, "/Release.gpg");
1965 if (!downloadchecksig(cinfo, fp, filename, &sigpool))
1968 solv_free((char *)filename);
1971 solv_free((char *)filename);
1973 calc_checksum_fp(fpr, REPOKEY_TYPE_SHA256, cinfo->cookie);
1974 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1976 printf(" cached\n");
1980 printf(" fetching\n");
1981 for (j = 0; j < cinfo->ncomponents; j++)
1983 if (!(filename = debian_find_component(cinfo, fpr, cinfo->components[j], &filechksum, &filechksumtype)))
1985 printf("[component %s not found]\n", cinfo->components[j]);
1988 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1990 if (repo_add_debpackages(repo, fp, 0))
1992 printf("component %s: %s\n", cinfo->components[j], pool_errstr(pool));
1997 solv_free((char *)filechksum);
1998 solv_free((char *)filename);
2002 writecachedrepo(repo, 0, 0, cinfo->cookie);
2007 printf("unsupported repo '%s': skipped\n", cinfo->alias);
2019 str2archid(Pool *pool, char *arch)
2024 id = pool_str2id(pool, arch, 0);
2025 if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
2027 if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
2033 #define DEPGLOB_NAME 1
2034 #define DEPGLOB_DEP 2
2035 #define DEPGLOB_NAMEDEP 3
2038 depglob(Pool *pool, char *name, Queue *job, int what)
2041 Id id = pool_str2id(pool, name, 0);
2046 FOR_PROVIDES(p, pp, id)
2048 Solvable *s = pool->solvables + p;
2050 if (s->name == id && (what & DEPGLOB_NAME) != 0)
2052 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
2058 if (what == DEPGLOB_NAMEDEP)
2059 printf("[using capability match for '%s']\n", name);
2060 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
2065 if (strpbrk(name, "[*?") == 0)
2068 if ((what & DEPGLOB_NAME) != 0)
2070 /* looks like a name glob. hard work. */
2071 for (p = 1; p < pool->nsolvables; p++)
2073 Solvable *s = pool->solvables + p;
2074 if (!s->repo || !pool_installable(pool, s))
2077 if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
2079 for (i = 0; i < job->count; i += 2)
2080 if (job->elements[i] == SOLVER_SOLVABLE_NAME && job->elements[i + 1] == id)
2082 if (i == job->count)
2083 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
2090 if ((what & DEPGLOB_DEP))
2092 /* looks like a dep glob. really hard work. */
2093 for (id = 1; id < pool->ss.nstrings; id++)
2095 if (!pool->whatprovides[id])
2097 if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
2099 if (!match && what == DEPGLOB_NAMEDEP)
2100 printf("[using capability match for '%s']\n", name);
2101 for (i = 0; i < job->count; i += 2)
2102 if (job->elements[i] == SOLVER_SOLVABLE_PROVIDES && job->elements[i + 1] == id)
2104 if (i == job->count)
2105 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
2116 limitrelation(Pool *pool, Queue *job, int flags, Id evr)
2120 for (i = j = 0; i < job->count; i += 2)
2122 Id select = job->elements[i] & SOLVER_SELECTMASK;
2123 if (select != SOLVER_SOLVABLE_NAME && select != SOLVER_SOLVABLE_PROVIDES)
2125 fprintf(stderr, "limitrelation only works on name/provides jobs\n");
2128 job->elements[i + 1] = pool_rel2id(pool, job->elements[i + 1], evr, flags, 1);
2129 if (flags == REL_ARCH)
2130 job->elements[i] |= SOLVER_SETARCH;
2131 if (flags == REL_EQ && select == SOLVER_SOLVABLE_NAME && job->elements[i])
2134 const char *evrstr = pool_id2str(pool, evr);
2135 if (!strchr(evrstr, '-'))
2136 job->elements[i] |= SOLVER_SETEV;
2139 job->elements[i] |= SOLVER_SETEVR;
2141 /* make sure we still have matches */
2142 FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
2146 job->elements[j] = job->elements[i];
2147 job->elements[j + 1] = job->elements[i + 1];
2151 queue_truncate(job, j);
2156 limitrelation_arch(Pool *pool, Queue *job, int flags, char *evr)
2160 if ((r = strrchr(evr, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
2163 limitrelation(pool, job, REL_ARCH, archid);
2164 limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
2168 limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
2169 return job->count / 2;
2173 limitrepo(Pool *pool, Id repofilter, Queue *job)
2181 for (i = j = 0; i < job->count; i += 2)
2184 FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
2186 s = pool_id2solvable(pool, p);
2187 if (s->repo && s->repo->repoid == repofilter)
2192 /* here we assume that repo == vendor, so we also set SOLVER_SETVENDOR */
2193 Id flags = (job->elements[i] & SOLVER_SETMASK) | SOLVER_SETVENDOR | SOLVER_SETREPO;
2194 if ((job->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_NAME)
2195 flags |= SOLVER_SETNAME;
2198 job->elements[j] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET | flags;
2199 job->elements[j + 1] = mq.elements[0];
2203 job->elements[j] = SOLVER_SOLVABLE_ONE_OF | flags;
2204 job->elements[j + 1] = pool_queuetowhatprovides(pool, &mq);
2209 queue_truncate(job, j);
2215 mkselect(Pool *pool, int mode, char *name, Queue *job)
2223 int type = strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
2226 dataiterator_init(&di, pool, mode == SOLVER_ERASE ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
2227 while (dataiterator_step(&di))
2229 Solvable *s = pool->solvables + di.solvid;
2230 if (!s->repo || !pool_installable(pool, s))
2232 queue_push(&q, di.solvid);
2233 dataiterator_skip_solvable(&di);
2235 dataiterator_free(&di);
2238 printf("[using file list match for '%s']\n", name);
2240 queue_push2(job, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
2242 queue_push2(job, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
2244 return job->count / 2;
2247 if ((r = strpbrk(name, "<=>")) != 0)
2249 /* relation case, support:
2254 int nend = r - name;
2267 while (*r && *r == ' ' && *r == '\t')
2269 while (nend && (name[nend - 1] == ' ' || name[nend -1 ] == '\t'))
2273 fprintf(stderr, "bad relation\n");
2276 oldnend = name[nend];
2278 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2280 name[nend] = oldnend;
2281 limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
2282 return job->count / 2;
2284 if ((r2 = strrchr(name, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
2287 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2289 name[nend] = oldnend;
2291 limitrelation(pool, job, REL_ARCH, archid);
2292 limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
2293 return job->count / 2;
2297 name[nend] = oldnend;
2301 /* no relation case, support:
2305 * nameglob-version.arch
2306 * nameglob-version-release
2307 * nameglob-version-release.arch
2309 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2310 return job->count / 2;
2311 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
2314 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2317 limitrelation(pool, job, REL_ARCH, archid);
2318 return job->count / 2;
2322 if ((r = strrchr(name, '-')) != 0)
2325 if (depglob(pool, name, job, DEPGLOB_NAME))
2327 /* have just the version */
2328 limitrelation_arch(pool, job, REL_EQ, r + 1);
2330 return job->count / 2;
2332 if ((r2 = strrchr(name, '-')) != 0)
2337 if (depglob(pool, name, job, DEPGLOB_NAME))
2339 /* have version-release */
2340 limitrelation_arch(pool, job, REL_EQ, r + 1);
2342 return job->count / 2;
2353 yesno(const char *str)
2355 char inbuf[128], *ip;
2362 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2367 while (*ip == ' ' || *ip == '\t')
2374 if (*ip == 'y' || *ip == 'n')
2375 return *ip == 'y' ? 1 : 0;
2379 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2389 fileconflict_cb(Pool *pool, Id p, void *cbdata)
2391 struct fcstate *fcstate = cbdata;
2399 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
2402 s = pool_id2solvable(pool, p);
2403 if (pool->installed && s->repo == pool->installed)
2405 if (!s->repo->rpmdbid)
2407 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
2410 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
2412 for (i = 0; i < fcstate->newpkgscnt; i++)
2413 if (fcstate->checkq->elements[i] == p)
2415 if (i == fcstate->newpkgscnt)
2417 fp = fcstate->newpkgsfps[i];
2421 return rpm_byfp(fp, pool_solvable2str(pool, s), &fcstate->rpmdbstate);
2426 runrpm(const char *arg, const char *name, int dupfd3, const char *rootdir)
2431 if ((pid = fork()) == (pid_t)-1)
2440 if (dupfd3 != -1 && dupfd3 != 3)
2446 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
2447 if (strcmp(arg, "-e") == 0)
2448 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2450 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2454 while (waitpid(pid, &status, 0) != pid)
2458 printf("rpm failed\n");
2465 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2468 rundpkg(const char *arg, const char *name, int dupfd3, const char *rootdir)
2473 if ((pid = fork()) == (pid_t)-1)
2482 if (dupfd3 != -1 && dupfd3 != 3)
2488 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
2489 if (strcmp(arg, "--install") == 0)
2490 execlp("dpkg", "dpkg", "--install", "--root", rootdir, "--force", "all", name, (char *)0);
2492 execlp("dpkg", "dpkg", "--remove", "--root", rootdir, "--force", "all", name, (char *)0);
2496 while (waitpid(pid, &status, 0) != pid)
2500 printf("dpkg failed\n");
2508 nscallback(Pool *pool, void *data, Id name, Id evr)
2510 if (name == NAMESPACE_PRODUCTBUDDY)
2512 /* SUSE specific hack: each product has an associated rpm */
2513 Solvable *s = pool->solvables + evr;
2517 cap = pool_str2id(pool, pool_tmpjoin(pool, "product(", pool_id2str(pool, s->name) + 8, ")"), 0);
2520 cap = pool_rel2id(pool, cap, s->evr, REL_EQ, 0);
2523 FOR_PROVIDES(p, pp, cap)
2525 Solvable *ps = pool->solvables + p;
2526 if (ps->repo == s->repo && ps->arch == s->arch)
2527 if (!bestp || pool_evrcmp(pool, pool->solvables[bestp].evr, ps->evr, EVRCMP_COMPARE) < 0)
2535 #ifdef SOFTLOCKS_PATH
2538 addsoftlocks(Pool *pool, Queue *job)
2542 char *bp, *ep, buf[4096];
2544 if ((fp = fopen(SOFTLOCKS_PATH, "r")) == 0)
2546 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
2548 while (*bp == ' ' || *bp == '\t')
2550 if (!*bp || *bp == '#')
2552 for (ep = bp; *ep; ep++)
2553 if (*ep == ' ' || *ep == '\t' || *ep == '\n')
2556 type = SOLVER_SOLVABLE_NAME;
2557 if (!strncmp(bp, "provides:", 9) && bp[9])
2559 type = SOLVER_SOLVABLE_PROVIDES;
2562 id = pool_str2id(pool, bp, 1);
2563 if (pool->installed)
2565 FOR_JOB_SELECT(p, pp, type, id)
2566 if (pool->solvables[p].repo == pool->installed)
2569 continue; /* ignore, as it is already installed */
2571 queue_push2(job, SOLVER_LOCK|SOLVER_WEAK|type, id);
2580 rewrite_repos(Pool *pool, Queue *addedfileprovides, Queue *addedfileprovides_inst)
2585 Queue fileprovidesq;
2587 struct repoinfo *cinfo;
2589 map_init(&providedids, pool->ss.nstrings);
2590 queue_init(&fileprovidesq);
2591 for (i = 0; i < addedfileprovides->count; i++)
2592 MAPSET(&providedids, addedfileprovides->elements[i]);
2595 /* make sure all repodatas but the first are extensions */
2596 if (repo->nrepodata < 2)
2598 data = repo_id2repodata(repo, 1);
2599 if (data->loadcallback)
2601 for (j = 2; j < repo->nrepodata; j++)
2603 Repodata *edata = repo_id2repodata(repo, j);
2604 if (!edata->loadcallback)
2607 if (j < repo->nrepodata)
2608 continue; /* found a non-externsion repodata, can't rewrite */
2609 if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
2611 if (repo == pool->installed && addedfileprovides_inst)
2613 for (j = 0; j < addedfileprovides->count; j++)
2614 MAPCLR(&providedids, addedfileprovides->elements[j]);
2615 for (j = 0; j < addedfileprovides_inst->count; j++)
2616 MAPSET(&providedids, addedfileprovides_inst->elements[j]);
2619 for (j = 0; j < fileprovidesq.count; j++)
2620 if (MAPTST(&providedids, fileprovidesq.elements[j]))
2622 if (repo == pool->installed && addedfileprovides_inst)
2624 for (j = 0; j < addedfileprovides_inst->count; j++)
2625 MAPCLR(&providedids, addedfileprovides_inst->elements[j]);
2626 for (j = 0; j < addedfileprovides->count; j++)
2627 MAPSET(&providedids, addedfileprovides->elements[j]);
2628 if (n == addedfileprovides_inst->count)
2629 continue; /* nothing new added */
2631 else if (n == addedfileprovides->count)
2632 continue; /* nothing new added */
2634 repodata_set_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, repo == pool->installed && addedfileprovides_inst ? addedfileprovides_inst : addedfileprovides);
2635 repodata_internalize(data);
2636 cinfo = repo->appdata;
2637 writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
2639 queue_free(&fileprovidesq);
2640 map_free(&providedids);
2644 select_patches(Pool *pool, Queue *job)
2648 Map installedmap, noobsmap;
2651 map_init(&noobsmap, 0);
2652 map_init(&installedmap, pool->nsolvables);
2653 solver_calculate_noobsmap(pool, job, &noobsmap);
2654 if (pool->installed)
2655 FOR_REPO_SOLVABLES(pool->installed, p, s)
2656 MAPSET(&installedmap, p);
2658 /* install all patches */
2659 for (p = 1; p < pool->nsolvables; p++)
2665 s = pool->solvables + p;
2666 if (strncmp(pool_id2str(pool, s->name), "patch:", 6) != 0)
2668 FOR_PROVIDES(p2, pp, s->name)
2670 Solvable *s2 = pool->solvables + p2;
2671 if (s2->name != s->name)
2673 r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2674 if (r < 0 || (r == 0 && p > p2))
2679 type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2680 if (type && !strcmp(type, "optional"))
2682 r = solvable_trivial_installable_map(s, &installedmap, 0, &noobsmap);
2685 if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2692 queue_push2(job, SOLVER_SOLVABLE, p);
2694 map_free(&installedmap);
2695 map_free(&noobsmap);
2699 #define MODE_INSTALL 1
2700 #define MODE_ERASE 2
2701 #define MODE_UPDATE 3
2702 #define MODE_DISTUPGRADE 4
2703 #define MODE_VERIFY 5
2704 #define MODE_PATCH 6
2706 #define MODE_REPOLIST 8
2707 #define MODE_SEARCH 9
2712 fprintf(stderr, "Usage: solv COMMAND <select>\n");
2713 fprintf(stderr, "\n");
2714 fprintf(stderr, " dist-upgrade: replace installed packages with\n");
2715 fprintf(stderr, " versions from the repositories\n");
2716 fprintf(stderr, " erase: erase installed packages\n");
2717 fprintf(stderr, " info: display package information\n");
2718 fprintf(stderr, " install: install packages\n");
2719 fprintf(stderr, " list: list packages\n");
2720 fprintf(stderr, " repos: list enabled repositories\n");
2721 fprintf(stderr, " search: search name/summary/description\n");
2722 fprintf(stderr, " update: update installed packages\n");
2723 fprintf(stderr, " verify: check dependencies of installed packages\n");
2724 fprintf(stderr, "\n");
2729 main(int argc, char **argv)
2732 Repo *commandlinerepo = 0;
2733 Id *commandlinepkgs = 0;
2735 struct repoinfo *repoinfos;
2737 int mainmode = 0, mode = 0;
2742 char inbuf[128], *ip;
2745 Queue addedfileprovides;
2746 Queue addedfileprovides_inst;
2755 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2757 mainmode = MODE_INSTALL;
2758 mode = SOLVER_INSTALL;
2760 else if (!strcmp(argv[0], "patch"))
2762 mainmode = MODE_PATCH;
2763 mode = SOLVER_INSTALL;
2765 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2767 mainmode = MODE_ERASE;
2768 mode = SOLVER_ERASE;
2770 else if (!strcmp(argv[0], "list"))
2772 mainmode = MODE_LIST;
2775 else if (!strcmp(argv[0], "info"))
2777 mainmode = MODE_INFO;
2780 else if (!strcmp(argv[0], "search"))
2782 mainmode = MODE_SEARCH;
2785 else if (!strcmp(argv[0], "verify"))
2787 mainmode = MODE_VERIFY;
2788 mode = SOLVER_VERIFY;
2790 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2792 mainmode = MODE_UPDATE;
2793 mode = SOLVER_UPDATE;
2795 else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2797 mainmode = MODE_DISTUPGRADE;
2798 mode = SOLVER_UPDATE;
2800 else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2802 mainmode = MODE_REPOLIST;
2810 if (argc > 2 && !strcmp(argv[1], "--root"))
2817 else if (argc > 1 && !strcmp(argv[1], "--clean"))
2827 pool = pool_create();
2828 pool_set_rootdir(pool, rootdir);
2832 const char *langs[] = {"de_DE", "de", "en"};
2833 pool_set_languages(pool, langs, sizeof(langs)/sizeof(*langs));
2837 pool_setloadcallback(pool, load_stub, 0);
2838 pool->nscallback = nscallback;
2839 // pool_setdebuglevel(pool, 2);
2841 repoinfos = read_repoinfos(pool, &nrepoinfos);
2843 if (mainmode == MODE_REPOLIST)
2846 for (i = 0; i < nrepoinfos; i++)
2848 struct repoinfo *cinfo = repoinfos + i;
2849 if (!cinfo->enabled)
2851 printf("%d: %-20s %s (prio %d)\n", j++, cinfo->alias, cinfo->name, cinfo->priority);
2856 read_repos(pool, repoinfos, nrepoinfos);
2858 if (argc > 2 && !strcmp(argv[1], "-r"))
2860 const char *rname = argv[2], *rp;
2861 for (rp = rname; *rp; rp++)
2862 if (*rp <= '0' || *rp >= '9')
2866 /* repo specified by number */
2867 int rnum = atoi(rname);
2868 for (i = 0; i < nrepoinfos; i++)
2870 struct repoinfo *cinfo = repoinfos + i;
2871 if (!cinfo->enabled)
2874 repofilter = cinfo->repo->repoid;
2879 /* repo specified by alias */
2883 if (!strcasecmp(rname, repo->name))
2884 repofilter = repo->repoid;
2889 fprintf(stderr, "%s: no such repo\n", rname);
2895 if (mainmode == MODE_SEARCH)
2901 map_init(&m, pool->nsolvables);
2902 dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2903 dataiterator_set_keyname(&di, SOLVABLE_NAME);
2904 dataiterator_set_search(&di, 0, 0);
2905 while (dataiterator_step(&di))
2906 MAPSET(&m, di.solvid);
2907 dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2908 dataiterator_set_search(&di, 0, 0);
2909 while (dataiterator_step(&di))
2910 MAPSET(&m, di.solvid);
2911 dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2912 dataiterator_set_search(&di, 0, 0);
2913 while (dataiterator_step(&di))
2914 MAPSET(&m, di.solvid);
2915 dataiterator_free(&di);
2917 for (p = 1; p < pool->nsolvables; p++)
2919 Solvable *s = pool_id2solvable(pool, p);
2922 printf(" - %s: %s\n", pool_solvable2str(pool, s), solvable_lookup_str(s, SOLVABLE_SUMMARY));
2929 if (mainmode == MODE_LIST || mainmode == MODE_INSTALL)
2931 for (i = 1; i < argc; i++)
2934 l = strlen(argv[i]);
2935 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2936 if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2939 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2940 if (l <= 4 || strcmp(argv[i] + l - 4, ".deb"))
2943 if (access(argv[i], R_OK))
2948 if (!commandlinepkgs)
2949 commandlinepkgs = solv_calloc(argc, sizeof(Id));
2950 if (!commandlinerepo)
2951 commandlinerepo = repo_create(pool, "@commandline");
2953 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2954 p = repo_add_rpm(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2956 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2957 p = repo_add_deb(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2961 fprintf(stderr, "could not add '%s'\n", argv[i]);
2964 commandlinepkgs[i] = p;
2966 if (commandlinerepo)
2967 repo_internalize(commandlinerepo);
2970 // FOR_REPOS(i, repo)
2971 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2972 queue_init(&addedfileprovides);
2973 queue_init(&addedfileprovides_inst);
2974 pool_addfileprovides_queue(pool, &addedfileprovides, &addedfileprovides_inst);
2975 if (addedfileprovides.count || addedfileprovides_inst.count)
2976 rewrite_repos(pool, &addedfileprovides, &addedfileprovides_inst);
2977 queue_free(&addedfileprovides);
2978 queue_free(&addedfileprovides_inst);
2979 pool_createwhatprovides(pool);
2982 for (i = 1; i < argc; i++)
2987 if (commandlinepkgs && commandlinepkgs[i])
2989 queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
2993 if (!mkselect(pool, mode, argv[i], &job2))
2995 fprintf(stderr, "nothing matches '%s'\n", argv[i]);
2998 if (repofilter && !limitrepo(pool, repofilter, &job2))
3000 fprintf(stderr, "nothing in repo matches '%s'\n", argv[i]);
3003 for (j = 0; j < job2.count; j++)
3004 queue_push(&job, job2.elements[j]);
3008 if (!job.count && mainmode != MODE_UPDATE && mainmode != MODE_DISTUPGRADE && mainmode != MODE_VERIFY && mainmode != MODE_PATCH)
3010 printf("no package matched\n");
3017 if (mainmode == MODE_LIST || mainmode == MODE_INFO)
3019 /* list mode, no solver needed */
3020 for (i = 0; i < job.count; i += 2)
3022 Id how = job.elements[i] & SOLVER_SELECTMASK;
3023 FOR_JOB_SELECT(p, pp, how, job.elements[i + 1])
3025 Solvable *s = pool_id2solvable(pool, p);
3026 if (mainmode == MODE_INFO)
3029 printf("Name: %s\n", pool_solvable2str(pool, s));
3030 printf("Repo: %s\n", s->repo->name);
3031 printf("Summary: %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
3032 str = solvable_lookup_str(s, SOLVABLE_URL);
3034 printf("Url: %s\n", str);
3035 str = solvable_lookup_str(s, SOLVABLE_LICENSE);
3037 printf("License: %s\n", str);
3038 printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
3044 const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de", 1);
3046 const char *sum = solvable_lookup_str_poollang(s, SOLVABLE_SUMMARY);
3048 printf(" - %s [%s]\n", pool_solvable2str(pool, s), s->repo->name);
3050 printf(" %s\n", sum);
3056 free_repoinfos(repoinfos, nrepoinfos);
3057 solv_free(commandlinepkgs);
3059 yum_substitute(pool, 0);
3064 if (mainmode == MODE_PATCH)
3065 select_patches(pool, &job);
3068 for (i = 0; i < job.count; i += 2)
3070 if (mode == SOLVER_UPDATE)
3072 /* make update of not installed packages an install */
3073 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
3074 if (pool->installed && pool->solvables[p].repo == pool->installed)
3078 job.elements[i] |= SOLVER_INSTALL;
3080 job.elements[i] |= SOLVER_CLEANDEPS;
3084 job.elements[i] |= mode;
3086 job.elements[i] |= SOLVER_CLEANDEPS;
3089 if (mainmode == MODE_DISTUPGRADE && allpkgs)
3092 queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_REPO, repofilter);
3094 queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_ALL, 0);
3096 if (mainmode == MODE_UPDATE && allpkgs)
3099 queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_REPO, repofilter);
3101 queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_ALL, 0);
3103 if (mainmode == MODE_VERIFY && allpkgs)
3106 queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_REPO, repofilter);
3108 queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_ALL, 0);
3111 // multiversion test
3112 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae", 1));
3113 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-base", 1));
3114 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-extra", 1));
3116 #ifdef SOFTLOCKS_PATH
3117 addsoftlocks(pool, &job);
3120 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3125 Id problem, solution;
3128 solv = solver_create(pool);
3129 solver_set_flag(solv, SOLVER_FLAG_SPLITPROVIDES, 1);
3130 if (mainmode == MODE_ERASE)
3131 solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1); /* don't nag */
3133 if (!solver_solve(solv, &job))
3135 pcnt = solver_problem_count(solv);
3136 printf("Found %d problems:\n", pcnt);
3137 for (problem = 1; problem <= pcnt; problem++)
3140 printf("Problem %d:\n", problem);
3141 solver_printprobleminfo(solv, problem);
3143 scnt = solver_solution_count(solv, problem);
3144 for (solution = 1; solution <= scnt; solution++)
3146 printf("Solution %d:\n", solution);
3147 solver_printsolution(solv, problem, solution);
3152 printf("Please choose a solution: ");
3155 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
3160 while (*ip == ' ' || *ip == '\t')
3162 if (*ip >= '0' && *ip <= '9')
3165 if (take >= 1 && take <= scnt)
3181 solver_take_solution(solv, problem, take, &job);
3187 trans = solver_create_transaction(solv);
3188 if (!trans->steps.count)
3190 printf("Nothing to do.\n");
3191 transaction_free(trans);
3195 free_repoinfos(repoinfos, nrepoinfos);
3196 solv_free(commandlinepkgs);
3198 yum_substitute(pool, 0);
3203 printf("Transaction summary:\n\n");
3204 transaction_print(trans);
3213 duc[1].path = "/usr/share/man";
3214 duc[2].path = "/sbin";
3215 duc[3].path = "/etc";
3216 transaction_calc_duchanges(trans, duc, 4);
3217 for (i = 0; i < 4; i++)
3218 printf("duchanges %s: %d K %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
3221 printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
3224 if (!yesno("OK to continue (y/n)? "))
3227 transaction_free(trans);
3231 free_repoinfos(repoinfos, nrepoinfos);
3232 solv_free(commandlinepkgs);
3234 yum_substitute(pool, 0);
3239 queue_init(&checkq);
3240 newpkgs = transaction_installedresult(trans, &checkq);
3245 int downloadsize = 0;
3246 for (i = 0; i < newpkgs; i++)
3250 p = checkq.elements[i];
3251 s = pool_id2solvable(pool, p);
3252 downloadsize += solvable_lookup_sizek(s, SOLVABLE_DOWNLOADSIZE, 0);
3254 printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
3255 newpkgsfps = solv_calloc(newpkgs, sizeof(*newpkgsfps));
3256 for (i = 0; i < newpkgs; i++)
3258 unsigned int medianr;
3261 struct repoinfo *cinfo;
3262 const unsigned char *chksum;
3266 p = checkq.elements[i];
3267 s = pool_id2solvable(pool, p);
3268 if (s->repo == commandlinerepo)
3270 loc = solvable_get_location(s, &medianr);
3271 if (!(newpkgsfps[i] = fopen(loc, "r")))
3279 cinfo = s->repo->appdata;
3282 printf("%s: no repository information\n", s->repo->name);
3285 loc = solvable_get_location(s, &medianr);
3289 if (pool->installed && pool->installed->nsolvables)
3291 /* try a delta first */
3292 char *matchname = strdup(pool_id2str(pool, s->name));
3293 dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
3294 dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
3295 while (dataiterator_step(&di))
3299 dataiterator_setpos_parent(&di);
3300 if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
3301 pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
3303 baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
3304 FOR_PROVIDES(op, pp, s->name)
3306 Solvable *os = pool->solvables + op;
3307 if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
3310 if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
3312 /* base is installed, run sequence check */
3313 const char *seqname;
3318 const char *archstr;
3323 archstr = pool_id2str(pool, s->arch);
3324 if (strlen(archstr) > 10 || strchr(archstr, '\'') != 0)
3327 seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
3328 seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
3329 seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
3330 seq = pool_tmpjoin(pool, seqname, "-", seqevr);
3331 seq = pool_tmpappend(pool, seq, "-", seqnum);
3332 if (strchr(seq, '\'') != 0)
3335 sprintf(cmd, "/usr/bin/applydeltarpm -a '%s' -c -s '", archstr);
3337 sprintf(cmd, "/usr/bin/applydeltarpm -c -s '");
3339 if (system(pool_tmpjoin(pool, cmd, seq, "'")) != 0)
3340 continue; /* didn't match */
3341 /* looks good, download delta */
3343 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
3345 continue; /* no way! */
3346 dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
3347 dloc = pool_tmpappend(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
3348 dloc = pool_tmpappend(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
3349 dloc = pool_tmpappend(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
3350 if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
3352 /* got it, now reconstruct */
3353 newfd = opentmpfile();
3355 sprintf(cmd, "applydeltarpm -a '%s' /dev/fd/%d /dev/fd/%d", archstr, fileno(fp), newfd);
3357 sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
3359 fcntl(fileno(fp), F_SETFD, 0);
3366 lseek(newfd, 0, SEEK_SET);
3368 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3369 if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
3375 newpkgsfps[i] = fdopen(newfd, "r");
3380 dataiterator_free(&di);
3381 solv_free(matchname);
3388 continue; /* delta worked! */
3390 if (cinfo->type == TYPE_SUSETAGS)
3392 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
3393 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
3396 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3397 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
3399 printf("\n%s: %s not found in repository\n", s->repo->name, loc);
3408 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3412 struct fcstate fcstate;
3414 printf("Searching for file conflicts\n");
3415 queue_init(&conflicts);
3416 fcstate.rpmdbstate = 0;
3417 fcstate.newpkgscnt = newpkgs;
3418 fcstate.checkq = &checkq;
3419 fcstate.newpkgsfps = newpkgsfps;
3420 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fileconflict_cb, &fcstate);
3421 if (conflicts.count)
3424 for (i = 0; i < conflicts.count; i += 5)
3425 printf("file %s of package %s conflicts with package %s\n", pool_id2str(pool, conflicts.elements[i]), pool_solvid2str(pool, conflicts.elements[i + 1]), pool_solvid2str(pool, conflicts.elements[i + 3]));
3427 if (yesno("Re-run solver (y/n/q)? "))
3429 for (i = 0; i < newpkgs; i++)
3431 fclose(newpkgsfps[i]);
3432 newpkgsfps = solv_free(newpkgsfps);
3434 pool_add_fileconflicts_deps(pool, &conflicts);
3435 pool_createwhatprovides(pool); /* Hmm... */
3439 queue_free(&conflicts);
3443 printf("Committing transaction:\n\n");
3444 transaction_order(trans, 0);
3445 for (i = 0; i < trans->steps.count; i++)
3447 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3448 const char *evr, *evrp, *nvra;
3454 p = trans->steps.elements[i];
3455 s = pool_id2solvable(pool, p);
3456 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
3459 case SOLVER_TRANSACTION_ERASE:
3460 printf("erase %s\n", pool_solvid2str(pool, p));
3461 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3462 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
3464 /* strip epoch from evr */
3465 evr = evrp = pool_id2str(pool, s->evr);
3466 while (*evrp >= '0' && *evrp <= '9')
3468 if (evrp > evr && evrp[0] == ':' && evrp[1])
3470 nvra = pool_tmpjoin(pool, pool_id2str(pool, s->name), "-", evr);
3471 nvra = pool_tmpappend(pool, nvra, ".", pool_id2str(pool, s->arch));
3472 runrpm("-e", nvra, -1, rootdir); /* too bad that --querybynumber doesn't work */
3474 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3475 rundpkg("--remove", pool_id2str(pool, s->name), 0, rootdir);
3478 case SOLVER_TRANSACTION_INSTALL:
3479 case SOLVER_TRANSACTION_MULTIINSTALL:
3480 printf("install %s\n", pool_solvid2str(pool, p));
3481 for (j = 0; j < newpkgs; j++)
3482 if (checkq.elements[j] == p)
3484 fp = j < newpkgs ? newpkgsfps[j] : 0;
3488 lseek(fileno(fp), 0, SEEK_SET);
3489 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3490 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp), rootdir);
3492 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3493 rundpkg("--install", "/dev/fd/3", fileno(fp), rootdir);
3503 for (i = 0; i < newpkgs; i++)
3505 fclose(newpkgsfps[i]);
3506 solv_free(newpkgsfps);
3507 queue_free(&checkq);
3508 transaction_free(trans);
3512 free_repoinfos(repoinfos, nrepoinfos);
3513 solv_free(commandlinepkgs);
3515 yum_substitute(pool, 0);