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 return solv_xfopen_iscompressed(name) != 0;
782 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 unsigned char mlchksum[32];
798 fp = curlfopen(cinfo, cinfo->metalink ? cinfo->metalink : cinfo->mirrorlist, 0, 0, 0, 0);
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))
877 fcntl(fd, F_SETFD, FD_CLOEXEC);
880 if (solv_xfopen_iscompressed(file) < 0)
882 printf("%s: unsupported compression\n", file);
888 fp = solv_xfopen_fd(file, fd, "r");
891 fp = fdopen(fd, "r");
900 cleanupgpg(char *gpgdir)
903 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
905 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
907 snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
909 snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
911 snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
917 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
926 off_t posfp, possigfp;
929 gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
932 keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
933 if (!(kfp = fopen(keysfile, "w")) )
939 for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
943 pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
944 if (!pubkey || !*pubkey)
946 if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
948 if (fputc('\n', kfp) == EOF) /* Just in case... */
952 if (fclose(kfp) || !nkeys)
957 snprintf(cmd, sizeof(cmd), "gpg2 -q --homedir %s --import %s", gpgdir, keysfile);
960 fprintf(stderr, "key import error\n");
965 posfp = lseek(fileno(fp), 0, SEEK_CUR);
966 lseek(fileno(fp), 0, SEEK_SET);
967 possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
968 lseek(fileno(sigfp), 0, SEEK_SET);
969 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
970 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
971 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
973 lseek(fileno(sigfp), possigfp, SEEK_SET);
974 lseek(fileno(fp), posfp, SEEK_SET);
975 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
976 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
978 return r == 0 ? 1 : 0;
984 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
989 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));
990 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
991 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
993 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
994 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
995 return r == 0 ? 1 : 0;
1003 Pool *sigpool = pool_create();
1004 #if defined(ENABLE_RPMDB_PUBKEY)
1005 Repo *repo = repo_create(sigpool, "rpmdbkeys");
1006 repo_add_rpmdb_pubkeys(repo, 0);
1012 downloadchecksig(struct repoinfo *cinfo, FILE *fp, const char *sigurl, Pool **sigpool)
1015 sigfp = curlfopen(cinfo, sigurl, 0, 0, 0, 0);
1018 printf(" unsigned, skipped\n");
1022 *sigpool = read_sigs();
1023 if (!checksig(*sigpool, fp, sigfp))
1025 printf(" checksig failed, skipped\n");
1033 #define CHKSUM_IDENT "1.1"
1036 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
1039 void *h = solv_chksum_create(chktype);
1042 solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
1043 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
1044 solv_chksum_add(h, buf, l);
1046 solv_chksum_free(h, out);
1050 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
1052 void *h = solv_chksum_create(chktype);
1053 solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
1054 solv_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
1055 solv_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
1056 solv_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
1057 solv_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
1058 solv_chksum_free(h, out);
1070 pool_setarch(pool, un.machine);
1073 char *calccachepath(Repo *repo, const char *repoext)
1075 char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
1078 p = pool_tmpappend(repo->pool, p, "_", repoext);
1079 p = pool_tmpappend(repo->pool, p, ".solvx", 0);
1082 p = pool_tmpappend(repo->pool, p, ".solv", 0);
1083 q = p + strlen(SOLVCACHE_PATH) + 1;
1093 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie, int mark)
1096 unsigned char mycookie[32];
1097 unsigned char myextcookie[32];
1098 struct repoinfo *cinfo;
1101 cinfo = repo->appdata;
1102 if (!(fp = fopen(calccachepath(repo, repoext), "r")))
1104 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
1109 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
1114 if (cinfo && !repoext)
1116 if (fseek(fp, -sizeof(mycookie) * 2, SEEK_END) || fread(myextcookie, sizeof(myextcookie), 1, fp) != 1)
1127 flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
1128 if (strcmp(repoext, "DL") != 0)
1129 flags |= REPO_LOCALPOOL; /* no local pool for DL so that we can compare IDs */
1132 if (repo_add_solv(repo, fp, flags))
1137 if (cinfo && !repoext)
1139 memcpy(cinfo->cookie, mycookie, sizeof(mycookie));
1140 memcpy(cinfo->extcookie, myextcookie, sizeof(myextcookie));
1143 futimes(fileno(fp), 0); /* try to set modification time */
1149 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
1154 struct repoinfo *cinfo;
1157 cinfo = repo->appdata;
1158 mkdir(SOLVCACHE_PATH, 0755);
1159 /* use dupjoin instead of tmpjoin because tmpl must survive repo_write */
1160 tmpl = solv_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
1168 if (!(fp = fdopen(fd, "w")))
1177 for (i = repo->start; i < repo->end; i++)
1178 if (repo->pool->solvables[i].repo != repo)
1184 repo_write(repo, fp);
1186 repodata_write(info, fp);
1189 int oldnrepodata = repo->nrepodata;
1190 repo->nrepodata = oldnrepodata > 2 ? 2 : oldnrepodata; /* XXX: do this right */
1191 repo_write(repo, fp);
1192 repo->nrepodata = oldnrepodata;
1196 if (!repoext && cinfo)
1198 if (!cinfo->extcookie[0])
1200 /* create the ext cookie and append it */
1201 /* we just need some unique ID */
1203 if (!fstat(fileno(fp), &stb))
1207 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
1208 for (i = 0; i < 32; i++)
1209 cinfo->extcookie[i] ^= cookie[i];
1211 if (cinfo->extcookie[0] == 0)
1212 cinfo->extcookie[0] = 1;
1214 if (fwrite(cinfo->extcookie, 32, 1, fp) != 1)
1222 /* append our cookie describing the metadata state */
1223 if (fwrite(cookie, 32, 1, fp) != 1)
1238 /* switch to just saved repo to activate paging and save memory */
1239 FILE *fp = fopen(tmpl, "r");
1245 repo_empty(repo, 1);
1246 if (repo_add_solv(repo, fp, SOLV_ADD_NO_STUBS))
1248 /* oops, no way to recover from here */
1249 fprintf(stderr, "internal error\n");
1255 /* make sure repodata contains complete repo */
1256 /* (this is how repodata_write saves it) */
1257 repodata_extend_block(info, repo->start, repo->end - repo->start);
1258 info->state = REPODATA_LOADING;
1259 /* no need for LOCALPOOL as pool already contains ids */
1260 repo_add_solv(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1261 info->state = REPODATA_AVAILABLE; /* in case the load failed */
1266 if (!rename(tmpl, calccachepath(repo, repoext)))
1273 /* repomd helpers */
1275 static inline const char *
1276 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1278 Pool *pool = repo->pool;
1280 const char *filename;
1285 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1286 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1287 if (dataiterator_step(&di))
1289 dataiterator_setpos_parent(&di);
1290 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1291 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
1293 dataiterator_free(&di);
1294 if (filename && !*chksumtypep)
1296 printf("no %s file checksum!\n", what);
1303 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
1305 Id chksumtype, handle;
1306 const unsigned char *chksum;
1307 const char *filename;
1309 filename = repomd_find(repo, what, &chksum, &chksumtype);
1312 if (!strcmp(what, "prestodelta"))
1314 handle = repodata_new_handle(data);
1315 repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
1316 repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
1317 repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
1318 if (!strcmp(what, "deltainfo"))
1320 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
1321 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
1323 if (!strcmp(what, "filelists"))
1325 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1326 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1328 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1333 repomd_load_ext(Repo *repo, Repodata *data)
1335 const char *filename, *repomdtype;
1338 struct repoinfo *cinfo;
1339 const unsigned char *filechksum;
1343 cinfo = repo->appdata;
1344 repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
1347 if (!strcmp(repomdtype, "filelists"))
1349 else if (!strcmp(repomdtype, "deltainfo"))
1354 printf("[%s:%s", repo->name, ext);
1356 if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1358 printf(" cached]\n");fflush(stdout);
1361 printf(" fetching]\n"); fflush(stdout);
1362 filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
1364 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, REPOSITORY_REPOMD_CHECKSUM, &filechksumtype);
1365 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1367 if (!strcmp(ext, "FL"))
1368 r = repo_add_rpmmd(repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1369 else if (!strcmp(ext, "DL"))
1370 r = repo_add_deltainfoxml(repo, fp, REPO_USE_LOADING);
1374 printf("%s\n", pool_errstr(repo->pool));
1377 writecachedrepo(repo, data, ext, cinfo->extcookie);
1384 #ifdef ENABLE_SUSEREPO
1385 /* susetags helpers */
1387 static inline const char *
1388 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1390 Pool *pool = repo->pool;
1392 const char *filename;
1397 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
1398 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1399 if (dataiterator_step(&di))
1401 dataiterator_setpos_parent(&di);
1402 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
1405 dataiterator_free(&di);
1406 if (filename && !*chksumtypep)
1408 printf("no %s file checksum!\n", what);
1414 static Id susetags_langtags[] = {
1415 SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
1416 SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
1417 SOLVABLE_EULA, REPOKEY_TYPE_STR,
1418 SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
1419 SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
1420 SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
1425 susetags_add_ext(Repo *repo, Repodata *data)
1427 Pool *pool = repo->pool;
1430 Id handle, filechksumtype;
1431 const unsigned char *filechksum;
1434 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
1435 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1436 while (dataiterator_step(&di))
1438 if (strncmp(di.kv.str, "packages.", 9) != 0)
1440 if (!strcmp(di.kv.str + 9, "gz"))
1442 if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
1444 ext[0] = di.kv.str[9];
1445 ext[1] = di.kv.str[10];
1447 if (!strcmp(ext, "en"))
1449 if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
1451 handle = repodata_new_handle(data);
1452 repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
1454 repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
1455 if (!strcmp(ext, "DU"))
1457 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
1458 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
1460 else if (!strcmp(ext, "FL"))
1462 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1463 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1467 for (i = 0; susetags_langtags[i]; i += 2)
1469 repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
1470 repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
1473 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1475 dataiterator_free(&di);
1479 susetags_load_ext(Repo *repo, Repodata *data)
1481 const char *filename, *descrdir;
1485 struct repoinfo *cinfo;
1486 const unsigned char *filechksum;
1489 cinfo = repo->appdata;
1490 filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
1494 ext[0] = filename[9];
1495 ext[1] = filename[10];
1498 printf("[%s:%s", repo->name, ext);
1500 if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1502 printf(" cached]\n"); fflush(stdout);
1506 printf(" fetching]\n"); fflush(stdout);
1508 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1509 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1511 descrdir = "suse/setup/descr";
1513 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
1514 if ((fp = curlfopen(cinfo, pool_tmpjoin(repo->pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1516 if (repo_add_susetags(repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES))
1519 printf("%s\n", pool_errstr(repo->pool));
1523 writecachedrepo(repo, data, ext, cinfo->extcookie);
1533 load_stub(Pool *pool, Repodata *data, void *dp)
1535 struct repoinfo *cinfo = data->repo->appdata;
1536 switch (cinfo->type)
1538 #ifdef ENABLE_SUSEREPO
1540 return susetags_load_ext(data->repo, data);
1544 return repomd_load_ext(data->repo, data);
1551 static unsigned char installedcookie[32];
1553 #ifdef ENABLE_DEBIAN
1556 debian_find_component(struct repoinfo *cinfo, FILE *fp, char *comp, const unsigned char **chksump, Id *chksumtypep)
1560 unsigned char *chksum;
1565 static char *basearch;
1577 basearch = strdup(un.machine);
1578 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
1581 binarydir = solv_dupjoin("binary-", basearch, "/");
1582 lbinarydir = strlen(binarydir);
1583 compl = strlen(comp);
1587 chksum = solv_malloc(32);
1589 while(fgets(buf, sizeof(buf), fp))
1594 while (l && (buf[l - 1] == '\n' || buf[l - 1] == ' ' || buf[l - 1] == '\t'))
1596 if (!strncasecmp(buf, "MD5Sum:", 7))
1598 curchksumtype = REPOKEY_TYPE_MD5;
1601 if (!strncasecmp(buf, "SHA1:", 5))
1603 curchksumtype = REPOKEY_TYPE_SHA1;
1606 if (!strncasecmp(buf, "SHA256:", 7))
1608 curchksumtype = REPOKEY_TYPE_SHA256;
1620 while (*bp && *bp != ' ' && *bp != '\t')
1625 while (*bp == ' ' || *bp == '\t')
1627 while (*bp && *bp != ' ' && *bp != '\t')
1631 while (*bp == ' ' || *bp == '\t')
1634 if (strncmp(fn, comp, compl) != 0 || fn[compl] != '/')
1637 if (strncmp(bp, binarydir, lbinarydir))
1640 if (!strcmp(bp, "Packages") || !strcmp(bp, "Packages.gz"))
1642 unsigned char curchksum[32];
1644 if (filename && !strcmp(bp, "Packages"))
1646 curl = solv_chksum_len(curchksumtype);
1647 if (!curl || (chksumtype && solv_chksum_len(chksumtype) > curl))
1649 if (solv_hex2bin((const char **)&ch, curchksum, sizeof(curchksum)) != curl)
1651 solv_free(filename);
1652 filename = strdup(fn);
1653 chksumtype = curchksumtype;
1654 memcpy(chksum, curchksum, curl);
1660 fn = solv_dupjoin("/", filename, 0);
1661 solv_free(filename);
1662 filename = solv_dupjoin("dists/", cinfo->name, fn);
1666 chksum = solv_free(chksum);
1668 *chksumtypep = chksumtype;
1674 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
1677 struct repoinfo *cinfo;
1680 const char *filename;
1681 const unsigned char *filechksum;
1683 #ifdef ENABLE_SUSEREPO
1684 const char *descrdir;
1689 #if defined(ENABLE_SUSEREPO) || defined(ENABLE_RPMMD)
1694 #if defined(ENABLE_DEBIAN)
1699 repo = repo_create(pool, "@System");
1700 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1701 printf("rpm database:");
1702 if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/rpm/Packages"), &stb))
1703 memset(&stb, 0, sizeof(&stb));
1705 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1706 printf("dpgk database:");
1707 if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/dpkg/status"), &stb))
1708 memset(&stb, 0, sizeof(&stb));
1711 printf("no installed database:");
1712 memset(&stb, 0, sizeof(&stb));
1714 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, installedcookie);
1715 if (usecachedrepo(repo, 0, installedcookie, 0))
1716 printf(" cached\n");
1719 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1723 printf(" reading\n");
1725 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1726 # if defined(ENABLE_SUSEREPO) && defined(PRODUCTS_PATH)
1727 if (repo_add_products(repo, PRODUCTS_PATH, REPO_NO_INTERNALIZE | REPO_USE_ROOTDIR))
1729 fprintf(stderr, "product reading failed: %s\n", pool_errstr(pool));
1733 if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1735 ref = repo_create(pool, "@System.old");
1736 if (repo_add_solv(ref, ofp, 0))
1743 if (repo_add_rpmdb(repo, ref, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
1745 fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1751 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1752 if (repo_add_debdb(repo, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
1754 fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1758 writecachedrepo(repo, 0, 0, installedcookie);
1760 pool_set_installed(pool, repo);
1762 for (i = 0; i < nrepoinfos; i++)
1764 cinfo = repoinfos + i;
1765 if (!cinfo->enabled)
1768 repo = repo_create(pool, cinfo->alias);
1770 repo->appdata = cinfo;
1771 repo->priority = 99 - cinfo->priority;
1773 dorefresh = cinfo->autorefresh;
1774 if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0), &stb) == 0)
1776 if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1779 if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1781 printf("repo '%s':", cinfo->alias);
1782 printf(" cached\n");
1786 switch (cinfo->type)
1790 printf("rpmmd repo '%s':", cinfo->alias);
1792 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1794 printf(" no repomd.xml file, skipped\n");
1799 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1800 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1802 printf(" cached\n");
1806 if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "repodata/repomd.xml.asc", &sigpool))
1811 if (repo_add_repomdxml(repo, fp, 0))
1813 printf("repomd.xml: %s\n", pool_errstr(pool));
1815 break; /* hopeless */
1818 printf(" fetching\n");
1819 filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1820 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1822 if (repo_add_rpmmd(repo, fp, 0, 0))
1824 printf("primary: %s\n", pool_errstr(pool));
1830 break; /* hopeless */
1832 filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1833 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1835 if (repo_add_updateinfoxml(repo, fp, 0))
1837 printf("updateinfo: %s\n", pool_errstr(pool));
1843 data = repo_add_repodata(repo, 0);
1844 if (!repomd_add_ext(repo, data, "deltainfo"))
1845 repomd_add_ext(repo, data, "prestodelta");
1846 repomd_add_ext(repo, data, "filelists");
1847 repodata_internalize(data);
1849 writecachedrepo(repo, 0, 0, cinfo->cookie);
1850 repodata_create_stubs(repo_last_repodata(repo));
1854 #ifdef ENABLE_SUSEREPO
1856 printf("susetags repo '%s':", cinfo->alias);
1860 if ((fp = curlfopen(cinfo, "content", 0, 0, 0, 0)) == 0)
1862 printf(" no content file, skipped\n");
1867 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1868 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1870 printf(" cached\n");
1874 if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "content.asc", &sigpool))
1879 if (repo_add_content(repo, fp, 0))
1881 printf("content: %s\n", pool_errstr(pool));
1883 break; /* hopeless */
1886 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1887 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1889 descrdir = "suse/setup/descr";
1890 filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1892 filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1895 printf(" no packages file entry, skipped\n");
1898 printf(" fetching\n");
1899 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
1900 break; /* hopeless */
1901 if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|SUSETAGS_RECORD_SHARES))
1903 printf("packages: %s\n", pool_errstr(pool));
1905 break; /* hopeless */
1908 /* add default language */
1909 filename = susetags_find(repo, "packages.en.gz", &filechksum, &filechksumtype);
1911 filename = susetags_find(repo, "packages.en", &filechksum, &filechksumtype);
1914 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1916 if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|REPO_REUSE_REPODATA|REPO_EXTEND_SOLVABLES))
1918 printf("packages.en: %s\n", pool_errstr(pool));
1924 filename = susetags_find(repo, "patterns", &filechksum, &filechksumtype);
1927 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1930 while (fgets(pbuf, sizeof(pbuf), fp))
1932 int l = strlen(pbuf);
1934 if (l && pbuf[l - 1] == '\n')
1936 if (!*pbuf || *pbuf == '.' || strchr(pbuf, '/') != 0)
1938 filename = susetags_find(repo, pbuf, &filechksum, &filechksumtype);
1939 if (filename && (fp2 = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1941 if (repo_add_susetags(repo, fp2, defvendor, 0, REPO_NO_INTERNALIZE))
1943 printf("%s: %s\n", pbuf, pool_errstr(pool));
1952 repo_internalize(repo);
1953 data = repo_add_repodata(repo, 0);
1954 susetags_add_ext(repo, data);
1955 repodata_internalize(data);
1957 writecachedrepo(repo, 0, 0, cinfo->cookie);
1958 repodata_create_stubs(repo_last_repodata(repo));
1962 #if defined(ENABLE_DEBIAN)
1964 printf("debian repo '%s':", cinfo->alias);
1966 filename = solv_dupjoin("dists/", cinfo->name, "/Release");
1967 if ((fpr = curlfopen(cinfo, filename, 0, 0, 0, 0)) == 0)
1969 printf(" no Release file, skipped\n");
1972 free((char *)filename);
1975 solv_free((char *)filename);
1976 if (cinfo->repo_gpgcheck)
1978 filename = solv_dupjoin("dists/", cinfo->name, "/Release.gpg");
1979 if (!downloadchecksig(cinfo, fp, filename, &sigpool))
1982 solv_free((char *)filename);
1985 solv_free((char *)filename);
1987 calc_checksum_fp(fpr, REPOKEY_TYPE_SHA256, cinfo->cookie);
1988 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1990 printf(" cached\n");
1994 printf(" fetching\n");
1995 for (j = 0; j < cinfo->ncomponents; j++)
1997 if (!(filename = debian_find_component(cinfo, fpr, cinfo->components[j], &filechksum, &filechksumtype)))
1999 printf("[component %s not found]\n", cinfo->components[j]);
2002 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
2004 if (repo_add_debpackages(repo, fp, 0))
2006 printf("component %s: %s\n", cinfo->components[j], pool_errstr(pool));
2011 solv_free((char *)filechksum);
2012 solv_free((char *)filename);
2016 writecachedrepo(repo, 0, 0, cinfo->cookie);
2021 printf("unsupported repo '%s': skipped\n", cinfo->alias);
2033 str2archid(Pool *pool, char *arch)
2038 id = pool_str2id(pool, arch, 0);
2039 if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
2041 if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
2047 #define DEPGLOB_NAME 1
2048 #define DEPGLOB_DEP 2
2049 #define DEPGLOB_NAMEDEP 3
2052 depglob(Pool *pool, char *name, Queue *job, int what)
2055 Id id = pool_str2id(pool, name, 0);
2060 FOR_PROVIDES(p, pp, id)
2062 Solvable *s = pool->solvables + p;
2064 if (s->name == id && (what & DEPGLOB_NAME) != 0)
2066 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
2072 if (what == DEPGLOB_NAMEDEP)
2073 printf("[using capability match for '%s']\n", name);
2074 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
2079 if (strpbrk(name, "[*?") == 0)
2082 if ((what & DEPGLOB_NAME) != 0)
2084 /* looks like a name glob. hard work. */
2085 for (p = 1; p < pool->nsolvables; p++)
2087 Solvable *s = pool->solvables + p;
2088 if (!s->repo || !pool_installable(pool, s))
2091 if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
2093 for (i = 0; i < job->count; i += 2)
2094 if (job->elements[i] == SOLVER_SOLVABLE_NAME && job->elements[i + 1] == id)
2096 if (i == job->count)
2097 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
2104 if ((what & DEPGLOB_DEP))
2106 /* looks like a dep glob. really hard work. */
2107 for (id = 1; id < pool->ss.nstrings; id++)
2109 if (!pool->whatprovides[id])
2111 if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
2113 if (!match && what == DEPGLOB_NAMEDEP)
2114 printf("[using capability match for '%s']\n", name);
2115 for (i = 0; i < job->count; i += 2)
2116 if (job->elements[i] == SOLVER_SOLVABLE_PROVIDES && job->elements[i + 1] == id)
2118 if (i == job->count)
2119 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
2130 limitrelation(Pool *pool, Queue *job, int flags, Id evr)
2134 for (i = j = 0; i < job->count; i += 2)
2136 Id select = job->elements[i] & SOLVER_SELECTMASK;
2137 if (select != SOLVER_SOLVABLE_NAME && select != SOLVER_SOLVABLE_PROVIDES)
2139 fprintf(stderr, "limitrelation only works on name/provides jobs\n");
2142 job->elements[i + 1] = pool_rel2id(pool, job->elements[i + 1], evr, flags, 1);
2143 if (flags == REL_ARCH)
2144 job->elements[i] |= SOLVER_SETARCH;
2145 if (flags == REL_EQ && select == SOLVER_SOLVABLE_NAME && job->elements[i])
2148 const char *evrstr = pool_id2str(pool, evr);
2149 if (!strchr(evrstr, '-'))
2150 job->elements[i] |= SOLVER_SETEV;
2153 job->elements[i] |= SOLVER_SETEVR;
2155 /* make sure we still have matches */
2156 FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
2160 job->elements[j] = job->elements[i];
2161 job->elements[j + 1] = job->elements[i + 1];
2165 queue_truncate(job, j);
2170 limitrelation_arch(Pool *pool, Queue *job, int flags, char *evr)
2174 if ((r = strrchr(evr, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
2177 limitrelation(pool, job, REL_ARCH, archid);
2178 limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
2182 limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
2183 return job->count / 2;
2187 limitrepo(Pool *pool, Id repofilter, Queue *job)
2195 for (i = j = 0; i < job->count; i += 2)
2198 FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
2200 s = pool_id2solvable(pool, p);
2201 if (s->repo && s->repo->repoid == repofilter)
2206 /* here we assume that repo == vendor, so we also set SOLVER_SETVENDOR */
2207 Id flags = (job->elements[i] & SOLVER_SETMASK) | SOLVER_SETVENDOR | SOLVER_SETREPO;
2208 if ((job->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_NAME)
2209 flags |= SOLVER_SETNAME;
2212 job->elements[j] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET | flags;
2213 job->elements[j + 1] = mq.elements[0];
2217 job->elements[j] = SOLVER_SOLVABLE_ONE_OF | flags;
2218 job->elements[j + 1] = pool_queuetowhatprovides(pool, &mq);
2223 queue_truncate(job, j);
2229 mkselect(Pool *pool, int mode, char *name, Queue *job)
2237 int type = strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
2240 dataiterator_init(&di, pool, mode == SOLVER_ERASE ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
2241 while (dataiterator_step(&di))
2243 Solvable *s = pool->solvables + di.solvid;
2244 if (!s->repo || !pool_installable(pool, s))
2246 queue_push(&q, di.solvid);
2247 dataiterator_skip_solvable(&di);
2249 dataiterator_free(&di);
2252 printf("[using file list match for '%s']\n", name);
2254 queue_push2(job, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
2256 queue_push2(job, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
2258 return job->count / 2;
2261 if ((r = strpbrk(name, "<=>")) != 0)
2263 /* relation case, support:
2268 int nend = r - name;
2281 while (*r && *r == ' ' && *r == '\t')
2283 while (nend && (name[nend - 1] == ' ' || name[nend -1 ] == '\t'))
2287 fprintf(stderr, "bad relation\n");
2290 oldnend = name[nend];
2292 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2294 name[nend] = oldnend;
2295 limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
2296 return job->count / 2;
2298 if ((r2 = strrchr(name, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
2301 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2303 name[nend] = oldnend;
2305 limitrelation(pool, job, REL_ARCH, archid);
2306 limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
2307 return job->count / 2;
2311 name[nend] = oldnend;
2315 /* no relation case, support:
2319 * nameglob-version.arch
2320 * nameglob-version-release
2321 * nameglob-version-release.arch
2323 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2324 return job->count / 2;
2325 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
2328 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2331 limitrelation(pool, job, REL_ARCH, archid);
2332 return job->count / 2;
2336 if ((r = strrchr(name, '-')) != 0)
2339 if (depglob(pool, name, job, DEPGLOB_NAME))
2341 /* have just the version */
2342 limitrelation_arch(pool, job, REL_EQ, r + 1);
2344 return job->count / 2;
2346 if ((r2 = strrchr(name, '-')) != 0)
2351 if (depglob(pool, name, job, DEPGLOB_NAME))
2353 /* have version-release */
2354 limitrelation_arch(pool, job, REL_EQ, r + 1);
2356 return job->count / 2;
2367 yesno(const char *str)
2369 char inbuf[128], *ip;
2376 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2381 while (*ip == ' ' || *ip == '\t')
2388 if (*ip == 'y' || *ip == 'n')
2389 return *ip == 'y' ? 1 : 0;
2393 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2403 fileconflict_cb(Pool *pool, Id p, void *cbdata)
2405 struct fcstate *fcstate = cbdata;
2413 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
2416 s = pool_id2solvable(pool, p);
2417 if (pool->installed && s->repo == pool->installed)
2419 if (!s->repo->rpmdbid)
2421 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
2424 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
2426 for (i = 0; i < fcstate->newpkgscnt; i++)
2427 if (fcstate->checkq->elements[i] == p)
2429 if (i == fcstate->newpkgscnt)
2431 fp = fcstate->newpkgsfps[i];
2435 return rpm_byfp(fp, pool_solvable2str(pool, s), &fcstate->rpmdbstate);
2440 runrpm(const char *arg, const char *name, int dupfd3, const char *rootdir)
2445 if ((pid = fork()) == (pid_t)-1)
2454 if (dupfd3 != -1 && dupfd3 != 3)
2460 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
2461 if (strcmp(arg, "-e") == 0)
2462 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2464 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
2468 while (waitpid(pid, &status, 0) != pid)
2472 printf("rpm failed\n");
2479 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2482 rundpkg(const char *arg, const char *name, int dupfd3, const char *rootdir)
2487 if ((pid = fork()) == (pid_t)-1)
2496 if (dupfd3 != -1 && dupfd3 != 3)
2502 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
2503 if (strcmp(arg, "--install") == 0)
2504 execlp("dpkg", "dpkg", "--install", "--root", rootdir, "--force", "all", name, (char *)0);
2506 execlp("dpkg", "dpkg", "--remove", "--root", rootdir, "--force", "all", name, (char *)0);
2510 while (waitpid(pid, &status, 0) != pid)
2514 printf("dpkg failed\n");
2522 nscallback(Pool *pool, void *data, Id name, Id evr)
2524 if (name == NAMESPACE_PRODUCTBUDDY)
2526 /* SUSE specific hack: each product has an associated rpm */
2527 Solvable *s = pool->solvables + evr;
2531 cap = pool_str2id(pool, pool_tmpjoin(pool, "product(", pool_id2str(pool, s->name) + 8, ")"), 0);
2534 cap = pool_rel2id(pool, cap, s->evr, REL_EQ, 0);
2537 FOR_PROVIDES(p, pp, cap)
2539 Solvable *ps = pool->solvables + p;
2540 if (ps->repo == s->repo && ps->arch == s->arch)
2541 if (!bestp || pool_evrcmp(pool, pool->solvables[bestp].evr, ps->evr, EVRCMP_COMPARE) < 0)
2549 #ifdef SOFTLOCKS_PATH
2552 addsoftlocks(Pool *pool, Queue *job)
2556 char *bp, *ep, buf[4096];
2558 if ((fp = fopen(SOFTLOCKS_PATH, "r")) == 0)
2560 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
2562 while (*bp == ' ' || *bp == '\t')
2564 if (!*bp || *bp == '#')
2566 for (ep = bp; *ep; ep++)
2567 if (*ep == ' ' || *ep == '\t' || *ep == '\n')
2570 type = SOLVER_SOLVABLE_NAME;
2571 if (!strncmp(bp, "provides:", 9) && bp[9])
2573 type = SOLVER_SOLVABLE_PROVIDES;
2576 id = pool_str2id(pool, bp, 1);
2577 if (pool->installed)
2579 FOR_JOB_SELECT(p, pp, type, id)
2580 if (pool->solvables[p].repo == pool->installed)
2583 continue; /* ignore, as it is already installed */
2585 queue_push2(job, SOLVER_LOCK|SOLVER_WEAK|type, id);
2594 rewrite_repos(Pool *pool, Queue *addedfileprovides, Queue *addedfileprovides_inst)
2599 Queue fileprovidesq;
2601 struct repoinfo *cinfo;
2603 map_init(&providedids, pool->ss.nstrings);
2604 queue_init(&fileprovidesq);
2605 for (i = 0; i < addedfileprovides->count; i++)
2606 MAPSET(&providedids, addedfileprovides->elements[i]);
2609 /* make sure all repodatas but the first are extensions */
2610 if (repo->nrepodata < 2)
2612 data = repo_id2repodata(repo, 1);
2613 if (data->loadcallback)
2615 for (j = 2; j < repo->nrepodata; j++)
2617 Repodata *edata = repo_id2repodata(repo, j);
2618 if (!edata->loadcallback)
2621 if (j < repo->nrepodata)
2622 continue; /* found a non-externsion repodata, can't rewrite */
2623 if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
2625 if (repo == pool->installed && addedfileprovides_inst)
2627 for (j = 0; j < addedfileprovides->count; j++)
2628 MAPCLR(&providedids, addedfileprovides->elements[j]);
2629 for (j = 0; j < addedfileprovides_inst->count; j++)
2630 MAPSET(&providedids, addedfileprovides_inst->elements[j]);
2633 for (j = 0; j < fileprovidesq.count; j++)
2634 if (MAPTST(&providedids, fileprovidesq.elements[j]))
2636 if (repo == pool->installed && addedfileprovides_inst)
2638 for (j = 0; j < addedfileprovides_inst->count; j++)
2639 MAPCLR(&providedids, addedfileprovides_inst->elements[j]);
2640 for (j = 0; j < addedfileprovides->count; j++)
2641 MAPSET(&providedids, addedfileprovides->elements[j]);
2642 if (n == addedfileprovides_inst->count)
2643 continue; /* nothing new added */
2645 else if (n == addedfileprovides->count)
2646 continue; /* nothing new added */
2648 repodata_set_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, repo == pool->installed && addedfileprovides_inst ? addedfileprovides_inst : addedfileprovides);
2649 repodata_internalize(data);
2650 cinfo = repo->appdata;
2651 writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
2653 queue_free(&fileprovidesq);
2654 map_free(&providedids);
2658 select_patches(Pool *pool, Queue *job)
2662 Map installedmap, noobsmap;
2665 map_init(&noobsmap, 0);
2666 map_init(&installedmap, pool->nsolvables);
2667 solver_calculate_noobsmap(pool, job, &noobsmap);
2668 if (pool->installed)
2669 FOR_REPO_SOLVABLES(pool->installed, p, s)
2670 MAPSET(&installedmap, p);
2672 /* install all patches */
2673 for (p = 1; p < pool->nsolvables; p++)
2679 s = pool->solvables + p;
2680 if (strncmp(pool_id2str(pool, s->name), "patch:", 6) != 0)
2682 FOR_PROVIDES(p2, pp, s->name)
2684 Solvable *s2 = pool->solvables + p2;
2685 if (s2->name != s->name)
2687 r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2688 if (r < 0 || (r == 0 && p > p2))
2693 type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2694 if (type && !strcmp(type, "optional"))
2696 r = solvable_trivial_installable_map(s, &installedmap, 0, &noobsmap);
2699 if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2706 queue_push2(job, SOLVER_SOLVABLE, p);
2708 map_free(&installedmap);
2709 map_free(&noobsmap);
2713 #define MODE_INSTALL 1
2714 #define MODE_ERASE 2
2715 #define MODE_UPDATE 3
2716 #define MODE_DISTUPGRADE 4
2717 #define MODE_VERIFY 5
2718 #define MODE_PATCH 6
2720 #define MODE_REPOLIST 8
2721 #define MODE_SEARCH 9
2726 fprintf(stderr, "Usage: solv COMMAND <select>\n");
2727 fprintf(stderr, "\n");
2728 fprintf(stderr, " dist-upgrade: replace installed packages with\n");
2729 fprintf(stderr, " versions from the repositories\n");
2730 fprintf(stderr, " erase: erase installed packages\n");
2731 fprintf(stderr, " info: display package information\n");
2732 fprintf(stderr, " install: install packages\n");
2733 fprintf(stderr, " list: list packages\n");
2734 fprintf(stderr, " repos: list enabled repositories\n");
2735 fprintf(stderr, " search: search name/summary/description\n");
2736 fprintf(stderr, " update: update installed packages\n");
2737 fprintf(stderr, " verify: check dependencies of installed packages\n");
2738 fprintf(stderr, "\n");
2743 main(int argc, char **argv)
2746 Repo *commandlinerepo = 0;
2747 Id *commandlinepkgs = 0;
2749 struct repoinfo *repoinfos;
2751 int mainmode = 0, mode = 0;
2756 char inbuf[128], *ip;
2759 Queue addedfileprovides;
2760 Queue addedfileprovides_inst;
2769 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2771 mainmode = MODE_INSTALL;
2772 mode = SOLVER_INSTALL;
2774 else if (!strcmp(argv[0], "patch"))
2776 mainmode = MODE_PATCH;
2777 mode = SOLVER_INSTALL;
2779 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2781 mainmode = MODE_ERASE;
2782 mode = SOLVER_ERASE;
2784 else if (!strcmp(argv[0], "list"))
2786 mainmode = MODE_LIST;
2789 else if (!strcmp(argv[0], "info"))
2791 mainmode = MODE_INFO;
2794 else if (!strcmp(argv[0], "search"))
2796 mainmode = MODE_SEARCH;
2799 else if (!strcmp(argv[0], "verify"))
2801 mainmode = MODE_VERIFY;
2802 mode = SOLVER_VERIFY;
2804 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2806 mainmode = MODE_UPDATE;
2807 mode = SOLVER_UPDATE;
2809 else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2811 mainmode = MODE_DISTUPGRADE;
2812 mode = SOLVER_UPDATE;
2814 else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2816 mainmode = MODE_REPOLIST;
2824 if (argc > 2 && !strcmp(argv[1], "--root"))
2831 else if (argc > 1 && !strcmp(argv[1], "--clean"))
2841 pool = pool_create();
2842 pool_set_rootdir(pool, rootdir);
2846 const char *langs[] = {"de_DE", "de", "en"};
2847 pool_set_languages(pool, langs, sizeof(langs)/sizeof(*langs));
2851 pool_setloadcallback(pool, load_stub, 0);
2852 pool->nscallback = nscallback;
2853 // pool_setdebuglevel(pool, 2);
2855 repoinfos = read_repoinfos(pool, &nrepoinfos);
2857 if (mainmode == MODE_REPOLIST)
2860 for (i = 0; i < nrepoinfos; i++)
2862 struct repoinfo *cinfo = repoinfos + i;
2863 if (!cinfo->enabled)
2865 printf("%d: %-20s %s (prio %d)\n", j++, cinfo->alias, cinfo->name, cinfo->priority);
2870 read_repos(pool, repoinfos, nrepoinfos);
2872 if (argc > 2 && !strcmp(argv[1], "-r"))
2874 const char *rname = argv[2], *rp;
2875 for (rp = rname; *rp; rp++)
2876 if (*rp <= '0' || *rp >= '9')
2880 /* repo specified by number */
2881 int rnum = atoi(rname);
2882 for (i = 0; i < nrepoinfos; i++)
2884 struct repoinfo *cinfo = repoinfos + i;
2885 if (!cinfo->enabled)
2888 repofilter = cinfo->repo->repoid;
2893 /* repo specified by alias */
2897 if (!strcasecmp(rname, repo->name))
2898 repofilter = repo->repoid;
2903 fprintf(stderr, "%s: no such repo\n", rname);
2909 if (mainmode == MODE_SEARCH)
2915 map_init(&m, pool->nsolvables);
2916 dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2917 dataiterator_set_keyname(&di, SOLVABLE_NAME);
2918 dataiterator_set_search(&di, 0, 0);
2919 while (dataiterator_step(&di))
2920 MAPSET(&m, di.solvid);
2921 dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2922 dataiterator_set_search(&di, 0, 0);
2923 while (dataiterator_step(&di))
2924 MAPSET(&m, di.solvid);
2925 dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2926 dataiterator_set_search(&di, 0, 0);
2927 while (dataiterator_step(&di))
2928 MAPSET(&m, di.solvid);
2929 dataiterator_free(&di);
2931 for (p = 1; p < pool->nsolvables; p++)
2933 Solvable *s = pool_id2solvable(pool, p);
2936 printf(" - %s: %s\n", pool_solvable2str(pool, s), solvable_lookup_str(s, SOLVABLE_SUMMARY));
2943 if (mainmode == MODE_LIST || mainmode == MODE_INSTALL)
2945 for (i = 1; i < argc; i++)
2948 l = strlen(argv[i]);
2949 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2950 if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2953 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2954 if (l <= 4 || strcmp(argv[i] + l - 4, ".deb"))
2957 if (access(argv[i], R_OK))
2962 if (!commandlinepkgs)
2963 commandlinepkgs = solv_calloc(argc, sizeof(Id));
2964 if (!commandlinerepo)
2965 commandlinerepo = repo_create(pool, "@commandline");
2967 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2968 p = repo_add_rpm(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2970 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2971 p = repo_add_deb(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2975 fprintf(stderr, "could not add '%s'\n", argv[i]);
2978 commandlinepkgs[i] = p;
2980 if (commandlinerepo)
2981 repo_internalize(commandlinerepo);
2984 // FOR_REPOS(i, repo)
2985 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2986 queue_init(&addedfileprovides);
2987 queue_init(&addedfileprovides_inst);
2988 pool_addfileprovides_queue(pool, &addedfileprovides, &addedfileprovides_inst);
2989 if (addedfileprovides.count || addedfileprovides_inst.count)
2990 rewrite_repos(pool, &addedfileprovides, &addedfileprovides_inst);
2991 queue_free(&addedfileprovides);
2992 queue_free(&addedfileprovides_inst);
2993 pool_createwhatprovides(pool);
2996 for (i = 1; i < argc; i++)
3001 if (commandlinepkgs && commandlinepkgs[i])
3003 queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
3007 if (!mkselect(pool, mode, argv[i], &job2))
3009 fprintf(stderr, "nothing matches '%s'\n", argv[i]);
3012 if (repofilter && !limitrepo(pool, repofilter, &job2))
3014 fprintf(stderr, "nothing in repo matches '%s'\n", argv[i]);
3017 for (j = 0; j < job2.count; j++)
3018 queue_push(&job, job2.elements[j]);
3022 if (!job.count && mainmode != MODE_UPDATE && mainmode != MODE_DISTUPGRADE && mainmode != MODE_VERIFY && mainmode != MODE_PATCH)
3024 printf("no package matched\n");
3031 if (mainmode == MODE_LIST || mainmode == MODE_INFO)
3033 /* list mode, no solver needed */
3034 for (i = 0; i < job.count; i += 2)
3036 Id how = job.elements[i] & SOLVER_SELECTMASK;
3037 FOR_JOB_SELECT(p, pp, how, job.elements[i + 1])
3039 Solvable *s = pool_id2solvable(pool, p);
3040 if (mainmode == MODE_INFO)
3043 printf("Name: %s\n", pool_solvable2str(pool, s));
3044 printf("Repo: %s\n", s->repo->name);
3045 printf("Summary: %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
3046 str = solvable_lookup_str(s, SOLVABLE_URL);
3048 printf("Url: %s\n", str);
3049 str = solvable_lookup_str(s, SOLVABLE_LICENSE);
3051 printf("License: %s\n", str);
3052 printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
3058 const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de", 1);
3060 const char *sum = solvable_lookup_str_poollang(s, SOLVABLE_SUMMARY);
3062 printf(" - %s [%s]\n", pool_solvable2str(pool, s), s->repo->name);
3064 printf(" %s\n", sum);
3070 free_repoinfos(repoinfos, nrepoinfos);
3071 solv_free(commandlinepkgs);
3073 yum_substitute(pool, 0);
3078 if (mainmode == MODE_PATCH)
3079 select_patches(pool, &job);
3082 for (i = 0; i < job.count; i += 2)
3084 if (mode == SOLVER_UPDATE)
3086 /* make update of not installed packages an install */
3087 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
3088 if (pool->installed && pool->solvables[p].repo == pool->installed)
3092 job.elements[i] |= SOLVER_INSTALL;
3094 job.elements[i] |= SOLVER_CLEANDEPS;
3098 job.elements[i] |= mode;
3100 job.elements[i] |= SOLVER_CLEANDEPS;
3103 if (mainmode == MODE_DISTUPGRADE && allpkgs)
3106 queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_REPO, repofilter);
3108 queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_ALL, 0);
3110 if (mainmode == MODE_UPDATE && allpkgs)
3113 queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_REPO, repofilter);
3115 queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_ALL, 0);
3117 if (mainmode == MODE_VERIFY && allpkgs)
3120 queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_REPO, repofilter);
3122 queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_ALL, 0);
3125 // multiversion test
3126 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae", 1));
3127 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-base", 1));
3128 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-extra", 1));
3130 #ifdef SOFTLOCKS_PATH
3131 addsoftlocks(pool, &job);
3134 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3139 Id problem, solution;
3142 solv = solver_create(pool);
3143 solver_set_flag(solv, SOLVER_FLAG_SPLITPROVIDES, 1);
3144 if (mainmode == MODE_ERASE)
3145 solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1); /* don't nag */
3147 if (!solver_solve(solv, &job))
3149 pcnt = solver_problem_count(solv);
3150 printf("Found %d problems:\n", pcnt);
3151 for (problem = 1; problem <= pcnt; problem++)
3154 printf("Problem %d:\n", problem);
3155 solver_printprobleminfo(solv, problem);
3157 scnt = solver_solution_count(solv, problem);
3158 for (solution = 1; solution <= scnt; solution++)
3160 printf("Solution %d:\n", solution);
3161 solver_printsolution(solv, problem, solution);
3166 printf("Please choose a solution: ");
3169 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
3174 while (*ip == ' ' || *ip == '\t')
3176 if (*ip >= '0' && *ip <= '9')
3179 if (take >= 1 && take <= scnt)
3195 solver_take_solution(solv, problem, take, &job);
3201 trans = solver_create_transaction(solv);
3202 if (!trans->steps.count)
3204 printf("Nothing to do.\n");
3205 transaction_free(trans);
3209 free_repoinfos(repoinfos, nrepoinfos);
3210 solv_free(commandlinepkgs);
3212 yum_substitute(pool, 0);
3217 printf("Transaction summary:\n\n");
3218 transaction_print(trans);
3227 duc[1].path = "/usr/share/man";
3228 duc[2].path = "/sbin";
3229 duc[3].path = "/etc";
3230 transaction_calc_duchanges(trans, duc, 4);
3231 for (i = 0; i < 4; i++)
3232 printf("duchanges %s: %d K %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
3235 printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
3238 if (!yesno("OK to continue (y/n)? "))
3241 transaction_free(trans);
3245 free_repoinfos(repoinfos, nrepoinfos);
3246 solv_free(commandlinepkgs);
3248 yum_substitute(pool, 0);
3253 queue_init(&checkq);
3254 newpkgs = transaction_installedresult(trans, &checkq);
3259 int downloadsize = 0;
3260 for (i = 0; i < newpkgs; i++)
3264 p = checkq.elements[i];
3265 s = pool_id2solvable(pool, p);
3266 downloadsize += solvable_lookup_sizek(s, SOLVABLE_DOWNLOADSIZE, 0);
3268 printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
3269 newpkgsfps = solv_calloc(newpkgs, sizeof(*newpkgsfps));
3270 for (i = 0; i < newpkgs; i++)
3272 unsigned int medianr;
3275 struct repoinfo *cinfo;
3276 const unsigned char *chksum;
3280 p = checkq.elements[i];
3281 s = pool_id2solvable(pool, p);
3282 if (s->repo == commandlinerepo)
3284 loc = solvable_get_location(s, &medianr);
3285 if (!(newpkgsfps[i] = fopen(loc, "r")))
3293 cinfo = s->repo->appdata;
3296 printf("%s: no repository information\n", s->repo->name);
3299 loc = solvable_get_location(s, &medianr);
3303 if (pool->installed && pool->installed->nsolvables)
3305 /* try a delta first */
3306 char *matchname = strdup(pool_id2str(pool, s->name));
3307 dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
3308 dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
3309 while (dataiterator_step(&di))
3313 dataiterator_setpos_parent(&di);
3314 if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
3315 pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
3317 baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
3318 FOR_PROVIDES(op, pp, s->name)
3320 Solvable *os = pool->solvables + op;
3321 if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
3324 if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
3326 /* base is installed, run sequence check */
3327 const char *seqname;
3332 const char *archstr;
3337 archstr = pool_id2str(pool, s->arch);
3338 if (strlen(archstr) > 10 || strchr(archstr, '\'') != 0)
3341 seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
3342 seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
3343 seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
3344 seq = pool_tmpjoin(pool, seqname, "-", seqevr);
3345 seq = pool_tmpappend(pool, seq, "-", seqnum);
3346 if (strchr(seq, '\'') != 0)
3349 sprintf(cmd, "/usr/bin/applydeltarpm -a '%s' -c -s '", archstr);
3351 sprintf(cmd, "/usr/bin/applydeltarpm -c -s '");
3353 if (system(pool_tmpjoin(pool, cmd, seq, "'")) != 0)
3354 continue; /* didn't match */
3355 /* looks good, download delta */
3357 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
3359 continue; /* no way! */
3360 dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
3361 dloc = pool_tmpappend(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
3362 dloc = pool_tmpappend(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
3363 dloc = pool_tmpappend(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
3364 if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
3366 /* got it, now reconstruct */
3367 newfd = opentmpfile();
3369 sprintf(cmd, "applydeltarpm -a '%s' /dev/fd/%d /dev/fd/%d", archstr, fileno(fp), newfd);
3371 sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
3373 fcntl(fileno(fp), F_SETFD, 0);
3380 lseek(newfd, 0, SEEK_SET);
3382 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3383 if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
3389 newpkgsfps[i] = fdopen(newfd, "r");
3394 dataiterator_free(&di);
3395 solv_free(matchname);
3402 continue; /* delta worked! */
3404 if (cinfo->type == TYPE_SUSETAGS)
3406 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
3407 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
3410 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3411 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
3413 printf("\n%s: %s not found in repository\n", s->repo->name, loc);
3422 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3426 struct fcstate fcstate;
3428 printf("Searching for file conflicts\n");
3429 queue_init(&conflicts);
3430 fcstate.rpmdbstate = 0;
3431 fcstate.newpkgscnt = newpkgs;
3432 fcstate.checkq = &checkq;
3433 fcstate.newpkgsfps = newpkgsfps;
3434 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fileconflict_cb, &fcstate);
3435 if (conflicts.count)
3438 for (i = 0; i < conflicts.count; i += 5)
3439 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]));
3441 if (yesno("Re-run solver (y/n/q)? "))
3443 for (i = 0; i < newpkgs; i++)
3445 fclose(newpkgsfps[i]);
3446 newpkgsfps = solv_free(newpkgsfps);
3448 pool_add_fileconflicts_deps(pool, &conflicts);
3449 pool_createwhatprovides(pool); /* Hmm... */
3453 queue_free(&conflicts);
3457 printf("Committing transaction:\n\n");
3458 transaction_order(trans, 0);
3459 for (i = 0; i < trans->steps.count; i++)
3461 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3462 const char *evr, *evrp, *nvra;
3468 p = trans->steps.elements[i];
3469 s = pool_id2solvable(pool, p);
3470 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
3473 case SOLVER_TRANSACTION_ERASE:
3474 printf("erase %s\n", pool_solvid2str(pool, p));
3475 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3476 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
3478 /* strip epoch from evr */
3479 evr = evrp = pool_id2str(pool, s->evr);
3480 while (*evrp >= '0' && *evrp <= '9')
3482 if (evrp > evr && evrp[0] == ':' && evrp[1])
3484 nvra = pool_tmpjoin(pool, pool_id2str(pool, s->name), "-", evr);
3485 nvra = pool_tmpappend(pool, nvra, ".", pool_id2str(pool, s->arch));
3486 runrpm("-e", nvra, -1, rootdir); /* too bad that --querybynumber doesn't work */
3488 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3489 rundpkg("--remove", pool_id2str(pool, s->name), 0, rootdir);
3492 case SOLVER_TRANSACTION_INSTALL:
3493 case SOLVER_TRANSACTION_MULTIINSTALL:
3494 printf("install %s\n", pool_solvid2str(pool, p));
3495 for (j = 0; j < newpkgs; j++)
3496 if (checkq.elements[j] == p)
3498 fp = j < newpkgs ? newpkgsfps[j] : 0;
3502 lseek(fileno(fp), 0, SEEK_SET);
3503 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3504 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp), rootdir);
3506 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3507 rundpkg("--install", "/dev/fd/3", fileno(fp), rootdir);
3517 for (i = 0; i < newpkgs; i++)
3519 fclose(newpkgsfps[i]);
3520 solv_free(newpkgsfps);
3521 queue_free(&checkq);
3522 transaction_free(trans);
3526 free_repoinfos(repoinfos, nrepoinfos);
3527 solv_free(commandlinepkgs);
3529 yum_substitute(pool, 0);