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))
145 rpm_installedrpmdbids(0, "Providename", "redhat-release", &q);
148 void *handle, *state = 0;
150 handle = rpm_byrpmdbid(q.elements[0], 0, &state);
151 releaseevr = rpm_query(handle, SOLVABLE_EVR);
152 rpm_byrpmdbid(0, 0, &state);
153 if ((p = strchr(releaseevr, '-')) != 0)
158 releaseevr = strdup("?");
161 p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
162 p2 = p + (p2 - line);
164 p = p2 + strlen(releaseevr);
167 if (!strncmp(p2, "$basearch", 9))
177 basearch = strdup(un.machine);
178 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
182 p = pool_tmpjoin(pool, line, basearch, p2 + 9);
183 p2 = p + (p2 - line);
185 p = p2 + strlen(basearch);
194 #define TYPE_UNKNOWN 0
195 #define TYPE_SUSETAGS 1
197 #define TYPE_PLAINDIR 3
198 #define TYPE_DEBIAN 4
202 read_repoinfos_sort(const void *ap, const void *bp)
204 const struct repoinfo *a = ap;
205 const struct repoinfo *b = bp;
206 return strcmp(a->alias, b->alias);
210 #if defined(SUSE) || defined(FEDORA)
213 read_repoinfos(Pool *pool, int *nrepoinfosp)
215 const char *reposdir = REPOINFO_PATH;
217 char buf2[4096], *kp, *vp, *kpe;
222 struct repoinfo *repoinfos = 0, *cinfo;
225 rdlen = strlen(reposdir);
226 dir = opendir(reposdir);
232 while ((ent = readdir(dir)) != 0)
234 if (ent->d_name[0] == '.')
236 l = strlen(ent->d_name);
237 if (l < 6 || rdlen + 2 + l >= sizeof(buf) || strcmp(ent->d_name + l - 5, ".repo") != 0)
239 snprintf(buf, sizeof(buf), "%s/%s", reposdir, ent->d_name);
240 if ((fp = fopen(buf, "r")) == 0)
246 while(fgets(buf2, sizeof(buf2), fp))
251 while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
254 while (*kp == ' ' || *kp == '\t')
256 if (!*kp || *kp == '#')
260 kp = yum_substitute(pool, kp);
264 vp = strrchr(kp, ']');
268 repoinfos = solv_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
269 cinfo = repoinfos + nrepoinfos++;
270 memset(cinfo, 0, sizeof(*cinfo));
271 cinfo->alias = strdup(kp + 1);
272 cinfo->type = TYPE_RPMMD;
273 cinfo->autorefresh = 1;
274 cinfo->priority = 99;
276 cinfo->repo_gpgcheck = 1;
278 cinfo->metadata_expire = METADATA_EXPIRE;
283 vp = strchr(kp, '=');
286 for (kpe = vp - 1; kpe >= kp; kpe--)
287 if (*kpe != ' ' && *kpe != '\t')
292 while (*vp == ' ' || *vp == '\t')
295 if (!strcmp(kp, "name"))
296 cinfo->name = strdup(vp);
297 else if (!strcmp(kp, "enabled"))
298 cinfo->enabled = *vp == '0' ? 0 : 1;
299 else if (!strcmp(kp, "autorefresh"))
300 cinfo->autorefresh = *vp == '0' ? 0 : 1;
301 else if (!strcmp(kp, "gpgcheck"))
302 cinfo->pkgs_gpgcheck = *vp == '0' ? 0 : 1;
303 else if (!strcmp(kp, "repo_gpgcheck"))
304 cinfo->repo_gpgcheck = *vp == '0' ? 0 : 1;
305 else if (!strcmp(kp, "baseurl"))
306 cinfo->baseurl = strdup(vp);
307 else if (!strcmp(kp, "mirrorlist"))
309 if (strstr(vp, "metalink"))
310 cinfo->metalink = strdup(vp);
312 cinfo->mirrorlist = strdup(vp);
314 else if (!strcmp(kp, "path"))
316 if (vp && strcmp(vp, "/") != 0)
317 cinfo->path = strdup(vp);
319 else if (!strcmp(kp, "type"))
321 if (!strcmp(vp, "yast2"))
322 cinfo->type = TYPE_SUSETAGS;
323 else if (!strcmp(vp, "rpm-md"))
324 cinfo->type = TYPE_RPMMD;
325 else if (!strcmp(vp, "plaindir"))
326 cinfo->type = TYPE_PLAINDIR;
328 cinfo->type = TYPE_UNKNOWN;
330 else if (!strcmp(kp, "priority"))
331 cinfo->priority = atoi(vp);
332 else if (!strcmp(kp, "keeppackages"))
333 cinfo->keeppackages = *vp == '0' ? 0 : 1;
339 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
340 *nrepoinfosp = nrepoinfos;
349 read_repoinfos(Pool *pool, int *nrepoinfosp)
355 char *kp, *url, *distro;
356 struct repoinfo *repoinfos = 0, *cinfo;
361 fp = fopen("/etc/apt/sources.list", "r");
368 dir = opendir("/etc/apt/sources.list.d");
372 if ((ent = readdir(dir)) == 0)
377 if (ent->d_name[0] == '.')
379 l = strlen(ent->d_name);
380 if (l < 5 || strcmp(ent->d_name + l - 5, ".list") != 0)
382 snprintf(buf, sizeof(buf), "%s/%s", "/etc/apt/sources.list.d", ent->d_name);
383 if (!(fp = fopen(buf, "r")))
386 while(fgets(buf2, sizeof(buf2), fp))
391 while (l && (buf2[l - 1] == '\n' || buf2[l - 1] == ' ' || buf2[l - 1] == '\t'))
394 while (*kp == ' ' || *kp == '\t')
396 if (!*kp || *kp == '#')
398 if (strncmp(kp, "deb", 3) != 0)
401 if (*kp != ' ' && *kp != '\t')
403 while (*kp == ' ' || *kp == '\t')
408 while (*kp && *kp != ' ' && *kp != '\t')
412 while (*kp == ' ' || *kp == '\t')
417 while (*kp && *kp != ' ' && *kp != '\t')
421 while (*kp == ' ' || *kp == '\t')
425 repoinfos = solv_extend(repoinfos, nrepoinfos, 1, sizeof(*repoinfos), 15);
426 cinfo = repoinfos + nrepoinfos++;
427 memset(cinfo, 0, sizeof(*cinfo));
428 cinfo->baseurl = strdup(url);
429 cinfo->alias = solv_dupjoin(url, "/", distro);
430 cinfo->name = strdup(distro);
431 cinfo->type = TYPE_DEBIAN;
433 cinfo->autorefresh = 1;
434 cinfo->repo_gpgcheck = 1;
435 cinfo->metadata_expire = METADATA_EXPIRE;
439 while (*kp == ' ' || *kp == '\t')
444 while (*kp && *kp != ' ' && *kp != '\t')
448 cinfo->components = solv_extend(cinfo->components, cinfo->ncomponents, 1, sizeof(*cinfo->components), 15);
449 cinfo->components[cinfo->ncomponents++] = strdup(compo);
455 qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), read_repoinfos_sort);
456 *nrepoinfosp = nrepoinfos;
464 read_repoinfos(Pool *pool, int *nrepoinfosp)
473 free_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
476 for (i = 0; i < nrepoinfos; i++)
478 struct repoinfo *cinfo = repoinfos + i;
479 solv_free(cinfo->name);
480 solv_free(cinfo->alias);
481 solv_free(cinfo->path);
482 solv_free(cinfo->metalink);
483 solv_free(cinfo->mirrorlist);
484 solv_free(cinfo->baseurl);
485 for (j = 0; j < cinfo->ncomponents; j++)
486 solv_free(cinfo->components[j]);
487 solv_free(cinfo->components);
489 solv_free(repoinfos);
498 strcpy(tmpl, "/var/tmp/solvXXXXXX");
510 verify_checksum(int fd, const char *file, const unsigned char *chksum, Id chksumtype)
513 const unsigned char *sum;
517 h = solv_chksum_create(chksumtype);
520 printf("%s: unknown checksum type\n", file);
523 while ((l = read(fd, buf, sizeof(buf))) > 0)
524 solv_chksum_add(h, buf, l);
525 lseek(fd, 0, SEEK_SET);
527 sum = solv_chksum_get(h, &l);
528 if (memcmp(sum, chksum, l))
530 printf("%s: checksum mismatch\n", file);
531 solv_chksum_free(h, 0);
534 solv_chksum_free(h, 0);
539 findfastest(char **urls, int nurls)
546 struct addrinfo hints, *result;;
548 fds = solv_calloc(nurls, sizeof(*fds));
549 socks = solv_calloc(nurls, sizeof(*socks));
550 for (i = 0; i < nurls; i++)
553 p = strchr(urls[i], '/');
566 if ((p2 = strchr(p, '@')) != 0)
569 if (!strncmp("https:", urls[i], 6))
571 else if (!strncmp("ftp:", urls[i], 4))
573 if ((p2 = strrchr(p, ':')) != 0)
582 sprintf(portstr, "%d", port);
583 memset(&hints, 0, sizeof(struct addrinfo));
584 hints.ai_family = AF_UNSPEC;
585 hints.ai_socktype = SOCK_STREAM;
586 hints.ai_flags = AI_NUMERICSERV;
588 if (!getaddrinfo(p, portstr, &hints, &result))
590 socks[i] = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
593 fcntl(socks[i], F_SETFL, O_NONBLOCK);
594 if (connect(socks[i], result->ai_addr, result->ai_addrlen) == -1)
596 if (errno != EINPROGRESS)
603 freeaddrinfo(result);
610 for (i = j = 0; i < nurls; i++)
614 fds[j].fd = socks[i];
615 fds[j].events = POLLOUT;
623 if (poll(fds, j, 10000) <= 0)
625 i = -1; /* something is wrong */
628 for (i = 0; i < j; i++)
629 if ((fds[i].revents & POLLOUT) != 0)
632 socklen_t soel = sizeof(int);
633 if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, &soe, &soel) == -1 || soe != 0)
635 /* connect failed, kill socket */
636 for (j = 0; j < nurls; j++)
637 if (socks[j] == fds[i].fd)
650 i = -1; /* something is wrong, no bit was set */
653 /* now i contains the fastest fd index */
656 for (j = 0; j < nurls; j++)
657 if (socks[j] == fds[i].fd)
661 char *url0 = urls[0];
666 for (i = j = 0; i < nurls; i++)
674 findmetalinkurl(FILE *fp, unsigned char *chksump, Id *chksumtypep)
676 char buf[4096], *bp, *ep;
683 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
685 while (*bp == ' ' || *bp == '\t')
687 if (chksumtypep && !*chksumtypep && !strncmp(bp, "<hash type=\"sha256\">", 20))
690 if (solv_hex2bin((const char **)&bp, chksump, 32) == 32)
691 *chksumtypep = REPOKEY_TYPE_SHA256;
694 if (strncmp(bp, "<url", 4))
696 bp = strchr(bp, '>');
700 ep = strstr(bp, "repodata/repomd.xml</url>");
704 if (strncmp(bp, "http", 4))
706 urls = solv_extend(urls, nurls, 1, sizeof(*urls), 15);
707 urls[nurls++] = strdup(bp);
712 findfastest(urls, nurls > 5 ? 5 : nurls);
715 for (i = 0; i < nurls; i++)
718 ep = strchr(bp, '/');
719 if ((ep = strchr(ep + 2, '/')) != 0)
722 printf("[using mirror %s]\n", bp);
731 findmirrorlisturl(FILE *fp)
733 char buf[4096], *bp, *ep;
738 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
740 while (*bp == ' ' || *bp == '\t')
742 if (!*bp || *bp == '#')
745 while (l > 0 && (bp[l - 1] == ' ' || bp[l - 1] == '\t' || bp[l - 1] == '\n'))
747 urls = solv_extend(urls, nurls, 1, sizeof(*urls), 15);
748 urls[nurls++] = strdup(bp);
753 findfastest(urls, nurls > 5 ? 5 : nurls);
756 for (i = 0; i < nurls; i++)
759 ep = strchr(bp, '/');
760 if ((ep = strchr(ep + 2, '/')) != 0)
763 printf("[using mirror %s]\n", bp);
772 iscompressed(const char *name)
774 int l = strlen(name);
775 return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
779 curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype, int *badchecksump)
785 const char *baseurl = cinfo->baseurl;
789 if (!cinfo->metalink && !cinfo->mirrorlist)
791 if (file != cinfo->metalink && file != cinfo->mirrorlist)
793 FILE *fp = curlfopen(cinfo, cinfo->metalink ? cinfo->metalink : cinfo->mirrorlist, 0, 0, 0, 0);
794 unsigned char mlchksum[32];
799 cinfo->baseurl = findmetalinkurl(fp, mlchksum, &mlchksumtype);
801 cinfo->baseurl = findmirrorlisturl(fp);
806 if (strchr(cinfo->baseurl, '$'))
808 char *b = yum_substitute(cinfo->repo->pool, cinfo->baseurl);
809 free(cinfo->baseurl);
810 cinfo->baseurl = strdup(b);
813 if (!chksumtype && mlchksumtype && !strcmp(file, "repodata/repomd.xml"))
815 chksumtype = mlchksumtype;
818 return curlfopen(cinfo, file, uncompress, chksum, chksumtype, badchecksump);
820 snprintf(url, sizeof(url), "%s", file);
825 if (l && baseurl[l - 1] == '/')
826 snprintf(url, sizeof(url), "%s%s", baseurl, file);
828 snprintf(url, sizeof(url), "%s/%s", baseurl, file);
831 // printf("url: %s\n", url);
832 if ((pid = fork()) == (pid_t)-1)
844 execlp("curl", "curl", "-f", "-s", "-L", url, (char *)0);
849 while (waitpid(pid, &status, 0) != pid)
851 if (lseek(fd, 0, SEEK_END) == 0 && (!status || !chksumtype))
857 lseek(fd, 0, SEEK_SET);
860 printf("%s: download error %d\n", file, status >> 8 ? status >> 8 : status);
866 if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
874 return solv_xfopen_fd(".gz", fd, "r");
875 fcntl(fd, F_SETFD, FD_CLOEXEC);
876 return fdopen(fd, "r");
882 cleanupgpg(char *gpgdir)
885 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg", gpgdir);
887 snprintf(cmd, sizeof(cmd), "%s/pubring.gpg~", gpgdir);
889 snprintf(cmd, sizeof(cmd), "%s/secring.gpg", gpgdir);
891 snprintf(cmd, sizeof(cmd), "%s/trustdb.gpg", gpgdir);
893 snprintf(cmd, sizeof(cmd), "%s/keys", gpgdir);
899 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
908 off_t posfp, possigfp;
911 gpgdir = mkdtemp(pool_tmpjoin(sigpool, "/var/tmp/solvgpg.XXXXXX", 0, 0));
914 keysfile = pool_tmpjoin(sigpool, gpgdir, "/keys", 0);
915 if (!(kfp = fopen(keysfile, "w")) )
921 for (p = 1, s = sigpool->solvables + p; p < sigpool->nsolvables; p++, s++)
925 pubkey = solvable_lookup_str(s, SOLVABLE_DESCRIPTION);
926 if (!pubkey || !*pubkey)
928 if (fwrite(pubkey, strlen(pubkey), 1, kfp) != 1)
930 if (fputc('\n', kfp) == EOF) /* Just in case... */
934 if (fclose(kfp) || !nkeys)
939 snprintf(cmd, sizeof(cmd), "gpg2 -q --homedir %s --import %s", gpgdir, keysfile);
942 fprintf(stderr, "key import error\n");
947 posfp = lseek(fileno(fp), 0, SEEK_CUR);
948 lseek(fileno(fp), 0, SEEK_SET);
949 possigfp = lseek(fileno(sigfp), 0, SEEK_CUR);
950 lseek(fileno(sigfp), 0, SEEK_SET);
951 snprintf(cmd, sizeof(cmd), "gpg -q --homedir %s --verify /dev/fd/%d /dev/fd/%d >/dev/null 2>&1", gpgdir, fileno(sigfp), fileno(fp));
952 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
953 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
955 lseek(fileno(sigfp), possigfp, SEEK_SET);
956 lseek(fileno(fp), posfp, SEEK_SET);
957 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
958 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
960 return r == 0 ? 1 : 0;
966 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
971 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));
972 fcntl(fileno(fp), F_SETFD, 0); /* clear CLOEXEC */
973 fcntl(fileno(sigfp), F_SETFD, 0); /* clear CLOEXEC */
975 fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
976 fcntl(fileno(sigfp), F_SETFD, FD_CLOEXEC);
977 return r == 0 ? 1 : 0;
982 #define CHKSUM_IDENT "1.1"
985 calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
988 void *h = solv_chksum_create(chktype);
991 solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
992 while ((l = fread(buf, 1, sizeof(buf), fp)) > 0)
993 solv_chksum_add(h, buf, l);
995 solv_chksum_free(h, out);
999 calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
1001 void *h = solv_chksum_create(chktype);
1002 solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
1003 solv_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
1004 solv_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
1005 solv_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
1006 solv_chksum_add(h, &stb->st_mtime, sizeof(stb->st_mtime));
1007 solv_chksum_free(h, out);
1019 pool_setarch(pool, un.machine);
1022 char *calccachepath(Repo *repo, const char *repoext)
1024 char *q, *p = pool_tmpjoin(repo->pool, SOLVCACHE_PATH, "/", repo->name);
1027 p = pool_tmpappend(repo->pool, p, "_", repoext);
1028 p = pool_tmpappend(repo->pool, p, ".solvx", 0);
1031 p = pool_tmpappend(repo->pool, p, ".solv", 0);
1032 q = p + strlen(SOLVCACHE_PATH) + 1;
1042 usecachedrepo(Repo *repo, const char *repoext, unsigned char *cookie, int mark)
1045 unsigned char mycookie[32];
1046 unsigned char myextcookie[32];
1047 struct repoinfo *cinfo;
1050 cinfo = repo->appdata;
1051 if (!(fp = fopen(calccachepath(repo, repoext), "r")))
1053 if (fseek(fp, -sizeof(mycookie), SEEK_END) || fread(mycookie, sizeof(mycookie), 1, fp) != 1)
1058 if (cookie && memcmp(cookie, mycookie, sizeof(mycookie)))
1063 if (cinfo && !repoext)
1065 if (fseek(fp, -sizeof(mycookie) * 2, SEEK_END) || fread(myextcookie, sizeof(myextcookie), 1, fp) != 1)
1076 flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
1077 if (strcmp(repoext, "DL") != 0)
1078 flags |= REPO_LOCALPOOL; /* no local pool for DL so that we can compare IDs */
1081 if (repo_add_solv(repo, fp, flags))
1086 if (cinfo && !repoext)
1088 memcpy(cinfo->cookie, mycookie, sizeof(mycookie));
1089 memcpy(cinfo->extcookie, myextcookie, sizeof(myextcookie));
1092 futimes(fileno(fp), 0); /* try to set modification time */
1098 writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *cookie)
1103 struct repoinfo *cinfo;
1106 cinfo = repo->appdata;
1107 mkdir(SOLVCACHE_PATH, 0755);
1108 /* use dupjoin instead of tmpjoin because tmpl must survive repo_write */
1109 tmpl = solv_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
1117 if (!(fp = fdopen(fd, "w")))
1126 for (i = repo->start; i < repo->end; i++)
1127 if (repo->pool->solvables[i].repo != repo)
1133 repo_write(repo, fp);
1135 repodata_write(info, fp);
1138 int oldnrepodata = repo->nrepodata;
1139 repo->nrepodata = oldnrepodata > 2 ? 2 : oldnrepodata; /* XXX: do this right */
1140 repo_write(repo, fp);
1141 repo->nrepodata = oldnrepodata;
1145 if (!repoext && cinfo)
1147 if (!cinfo->extcookie[0])
1149 /* create the ext cookie and append it */
1150 /* we just need some unique ID */
1152 if (!fstat(fileno(fp), &stb))
1156 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
1157 for (i = 0; i < 32; i++)
1158 cinfo->extcookie[i] ^= cookie[i];
1160 if (cinfo->extcookie[0] == 0)
1161 cinfo->extcookie[0] = 1;
1163 if (fwrite(cinfo->extcookie, 32, 1, fp) != 1)
1171 /* append our cookie describing the metadata state */
1172 if (fwrite(cookie, 32, 1, fp) != 1)
1187 /* switch to just saved repo to activate paging and save memory */
1188 FILE *fp = fopen(tmpl, "r");
1194 repo_empty(repo, 1);
1195 if (repo_add_solv(repo, fp, SOLV_ADD_NO_STUBS))
1197 /* oops, no way to recover from here */
1198 fprintf(stderr, "internal error\n");
1204 /* make sure repodata contains complete repo */
1205 /* (this is how repodata_write saves it) */
1206 repodata_extend_block(info, repo->start, repo->end - repo->start);
1207 info->state = REPODATA_LOADING;
1208 /* no need for LOCALPOOL as pool already contains ids */
1209 repo_add_solv(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1210 info->state = REPODATA_AVAILABLE; /* in case the load failed */
1215 if (!rename(tmpl, calccachepath(repo, repoext)))
1224 Pool *sigpool = pool_create();
1225 #if defined(ENABLE_RPMDB_PUBKEY)
1226 Repo *repo = repo_create(sigpool, "rpmdbkeys");
1227 repo_add_rpmdb_pubkeys(repo, 0, 0);
1234 /* repomd helpers */
1236 static inline const char *
1237 repomd_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1239 Pool *pool = repo->pool;
1241 const char *filename;
1246 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_REPOMD_TYPE, what, SEARCH_STRING);
1247 dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
1248 if (dataiterator_step(&di))
1250 dataiterator_setpos_parent(&di);
1251 filename = pool_lookup_str(pool, SOLVID_POS, REPOSITORY_REPOMD_LOCATION);
1252 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, REPOSITORY_REPOMD_CHECKSUM, chksumtypep);
1254 dataiterator_free(&di);
1255 if (filename && !*chksumtypep)
1257 printf("no %s file checksum!\n", what);
1264 repomd_add_ext(Repo *repo, Repodata *data, const char *what)
1266 Id chksumtype, handle;
1267 const unsigned char *chksum;
1268 const char *filename;
1270 filename = repomd_find(repo, what, &chksum, &chksumtype);
1273 if (!strcmp(what, "prestodelta"))
1275 handle = repodata_new_handle(data);
1276 repodata_set_poolstr(data, handle, REPOSITORY_REPOMD_TYPE, what);
1277 repodata_set_str(data, handle, REPOSITORY_REPOMD_LOCATION, filename);
1278 repodata_set_bin_checksum(data, handle, REPOSITORY_REPOMD_CHECKSUM, chksumtype, chksum);
1279 if (!strcmp(what, "deltainfo"))
1281 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
1282 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
1284 if (!strcmp(what, "filelists"))
1286 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1287 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1289 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1294 repomd_load_ext(Repo *repo, Repodata *data)
1296 const char *filename, *repomdtype;
1299 struct repoinfo *cinfo;
1300 const unsigned char *filechksum;
1304 cinfo = repo->appdata;
1305 repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
1308 if (!strcmp(repomdtype, "filelists"))
1310 else if (!strcmp(repomdtype, "deltainfo"))
1315 printf("[%s:%s", repo->name, ext);
1317 if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1319 printf(" cached]\n");fflush(stdout);
1322 printf(" fetching]\n"); fflush(stdout);
1323 filename = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_LOCATION);
1325 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, REPOSITORY_REPOMD_CHECKSUM, &filechksumtype);
1326 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1328 if (!strcmp(ext, "FL"))
1329 r = repo_add_rpmmd(repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
1330 else if (!strcmp(ext, "DL"))
1331 r = repo_add_deltainfoxml(repo, fp, REPO_USE_LOADING);
1335 printf("%s\n", pool_errstr(repo->pool));
1338 writecachedrepo(repo, data, ext, cinfo->extcookie);
1345 #ifdef ENABLE_SUSEREPO
1346 /* susetags helpers */
1348 static inline const char *
1349 susetags_find(Repo *repo, const char *what, const unsigned char **chksump, Id *chksumtypep)
1351 Pool *pool = repo->pool;
1353 const char *filename;
1358 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, what, SEARCH_STRING);
1359 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1360 if (dataiterator_step(&di))
1362 dataiterator_setpos_parent(&di);
1363 *chksump = pool_lookup_bin_checksum(pool, SOLVID_POS, SUSETAGS_FILE_CHECKSUM, chksumtypep);
1366 dataiterator_free(&di);
1367 if (filename && !*chksumtypep)
1369 printf("no %s file checksum!\n", what);
1375 static Id susetags_langtags[] = {
1376 SOLVABLE_SUMMARY, REPOKEY_TYPE_STR,
1377 SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
1378 SOLVABLE_EULA, REPOKEY_TYPE_STR,
1379 SOLVABLE_MESSAGEINS, REPOKEY_TYPE_STR,
1380 SOLVABLE_MESSAGEDEL, REPOKEY_TYPE_STR,
1381 SOLVABLE_CATEGORY, REPOKEY_TYPE_ID,
1386 susetags_add_ext(Repo *repo, Repodata *data)
1388 Pool *pool = repo->pool;
1391 Id handle, filechksumtype;
1392 const unsigned char *filechksum;
1395 dataiterator_init(&di, pool, repo, SOLVID_META, SUSETAGS_FILE_NAME, 0, 0);
1396 dataiterator_prepend_keyname(&di, SUSETAGS_FILE);
1397 while (dataiterator_step(&di))
1399 if (strncmp(di.kv.str, "packages.", 9) != 0)
1401 if (!strcmp(di.kv.str + 9, "gz"))
1403 if (!di.kv.str[9] || !di.kv.str[10] || (di.kv.str[11] && di.kv.str[11] != '.'))
1405 ext[0] = di.kv.str[9];
1406 ext[1] = di.kv.str[10];
1408 if (!strcmp(ext, "en"))
1410 if (!susetags_find(repo, di.kv.str, &filechksum, &filechksumtype))
1412 handle = repodata_new_handle(data);
1413 repodata_set_str(data, handle, SUSETAGS_FILE_NAME, di.kv.str);
1415 repodata_set_bin_checksum(data, handle, SUSETAGS_FILE_CHECKSUM, filechksumtype, filechksum);
1416 if (!strcmp(ext, "DU"))
1418 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
1419 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
1421 else if (!strcmp(ext, "FL"))
1423 repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
1424 repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
1428 for (i = 0; susetags_langtags[i]; i += 2)
1430 repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, susetags_langtags[i], ext, 1));
1431 repodata_add_idarray(data, handle, REPOSITORY_KEYS, susetags_langtags[i + 1]);
1434 repodata_add_flexarray(data, SOLVID_META, REPOSITORY_EXTERNAL, handle);
1436 dataiterator_free(&di);
1440 susetags_load_ext(Repo *repo, Repodata *data)
1442 const char *filename, *descrdir;
1446 struct repoinfo *cinfo;
1447 const unsigned char *filechksum;
1450 cinfo = repo->appdata;
1451 filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
1455 ext[0] = filename[9];
1456 ext[1] = filename[10];
1459 printf("[%s:%s", repo->name, ext);
1461 if (usecachedrepo(repo, ext, cinfo->extcookie, 0))
1463 printf(" cached]\n"); fflush(stdout);
1467 printf(" fetching]\n"); fflush(stdout);
1469 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1470 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1472 descrdir = "suse/setup/descr";
1474 filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
1475 if ((fp = curlfopen(cinfo, pool_tmpjoin(repo->pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
1477 if (repo_add_susetags(repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES))
1480 printf("%s\n", pool_errstr(repo->pool));
1484 writecachedrepo(repo, data, ext, cinfo->extcookie);
1494 load_stub(Pool *pool, Repodata *data, void *dp)
1496 struct repoinfo *cinfo = data->repo->appdata;
1497 #ifdef ENABLE_SUSEREPO
1498 if (cinfo->type == TYPE_SUSETAGS)
1499 return susetags_load_ext(data->repo, data);
1502 if (cinfo->type == TYPE_RPMMD)
1503 return repomd_load_ext(data->repo, data);
1508 static unsigned char installedcookie[32];
1510 #ifdef ENABLE_DEBIAN
1513 debian_find_component(struct repoinfo *cinfo, FILE *fp, char *comp, const unsigned char **chksump, Id *chksumtypep)
1517 unsigned char *chksum;
1522 static char *basearch;
1534 basearch = strdup(un.machine);
1535 if (basearch[0] == 'i' && basearch[1] && !strcmp(basearch + 2, "86"))
1538 binarydir = solv_dupjoin("binary-", basearch, "/");
1539 lbinarydir = strlen(binarydir);
1540 compl = strlen(comp);
1544 chksum = solv_malloc(32);
1546 while(fgets(buf, sizeof(buf), fp))
1551 while (l && (buf[l - 1] == '\n' || buf[l - 1] == ' ' || buf[l - 1] == '\t'))
1553 if (!strncasecmp(buf, "MD5Sum:", 7))
1555 curchksumtype = REPOKEY_TYPE_MD5;
1558 if (!strncasecmp(buf, "SHA1:", 5))
1560 curchksumtype = REPOKEY_TYPE_SHA1;
1563 if (!strncasecmp(buf, "SHA256:", 7))
1565 curchksumtype = REPOKEY_TYPE_SHA256;
1577 while (*bp && *bp != ' ' && *bp != '\t')
1582 while (*bp == ' ' || *bp == '\t')
1584 while (*bp && *bp != ' ' && *bp != '\t')
1588 while (*bp == ' ' || *bp == '\t')
1591 if (strncmp(fn, comp, compl) != 0 || fn[compl] != '/')
1594 if (strncmp(bp, binarydir, lbinarydir))
1597 if (!strcmp(bp, "Packages") || !strcmp(bp, "Packages.gz"))
1599 unsigned char curchksum[32];
1601 if (filename && !strcmp(bp, "Packages"))
1603 curl = solv_chksum_len(curchksumtype);
1604 if (!curl || (chksumtype && solv_chksum_len(chksumtype) > curl))
1606 if (solv_hex2bin((const char **)&ch, curchksum, sizeof(curchksum)) != curl)
1608 solv_free(filename);
1609 filename = strdup(fn);
1610 chksumtype = curchksumtype;
1611 memcpy(chksum, curchksum, curl);
1617 fn = solv_dupjoin("/", filename, 0);
1618 solv_free(filename);
1619 filename = solv_dupjoin("dists/", cinfo->name, fn);
1623 chksum = solv_free(chksum);
1625 *chksumtypep = chksumtype;
1631 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
1634 struct repoinfo *cinfo;
1638 const char *filename;
1639 const unsigned char *filechksum;
1641 #ifdef ENABLE_SUSEREPO
1642 const char *descrdir;
1650 #if defined(ENABLE_DEBIAN)
1655 repo = repo_create(pool, "@System");
1656 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1657 printf("rpm database:");
1658 if (stat("/var/lib/rpm/Packages", &stb))
1659 memset(&stb, 0, sizeof(&stb));
1661 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1662 printf("dpgk database:");
1663 if (stat("/var/lib/dpkg/status", &stb))
1664 memset(&stb, 0, sizeof(&stb));
1667 printf("no installed database:");
1668 memset(&stb, 0, sizeof(&stb));
1670 calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, installedcookie);
1671 if (usecachedrepo(repo, 0, installedcookie, 0))
1672 printf(" cached\n");
1675 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1679 printf(" reading\n");
1681 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
1682 # if defined(ENABLE_SUSEREPO) && defined(PRODUCTS_PATH)
1683 if (repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE))
1685 fprintf(stderr, "product reading failed: %s\n", pool_errstr(pool));
1689 if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
1691 Repo *ref = repo_create(pool, "@System.old");
1692 if (!repo_add_solv(ref, ofp, 0))
1694 if (repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA))
1696 fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1706 if (repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA))
1708 fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1713 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
1714 if (repo_add_debdb(repo, 0, REPO_REUSE_REPODATA))
1716 fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
1720 writecachedrepo(repo, 0, 0, installedcookie);
1722 pool_set_installed(pool, repo);
1724 for (i = 0; i < nrepoinfos; i++)
1726 cinfo = repoinfos + i;
1727 if (!cinfo->enabled)
1730 repo = repo_create(pool, cinfo->alias);
1732 repo->appdata = cinfo;
1733 repo->priority = 99 - cinfo->priority;
1735 dorefresh = cinfo->autorefresh;
1736 if (dorefresh && cinfo->metadata_expire && stat(calccachepath(repo, 0), &stb) == 0)
1738 if (cinfo->metadata_expire == -1 || time(0) - stb.st_mtime < cinfo->metadata_expire)
1741 if (!dorefresh && usecachedrepo(repo, 0, 0, 0))
1743 printf("repo '%s':", cinfo->alias);
1744 printf(" cached\n");
1748 switch (cinfo->type)
1752 printf("rpmmd repo '%s':", cinfo->alias);
1754 if ((fp = curlfopen(cinfo, "repodata/repomd.xml", 0, 0, 0, 0)) == 0)
1756 printf(" no repomd.xml file, skipped\n");
1761 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1762 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1764 printf(" cached\n");
1768 if (cinfo->repo_gpgcheck)
1770 sigfp = curlfopen(cinfo, "repodata/repomd.xml.asc", 0, 0, 0, 0);
1773 printf(" unsigned, skipped\n");
1778 sigpool = read_sigs();
1779 if (!checksig(sigpool, fp, sigfp))
1781 printf(" checksig failed, skipped\n");
1788 if (repo_add_repomdxml(repo, fp, 0))
1790 printf("repomd.xml: %s\n", pool_errstr(pool));
1792 break; /* hopeless */
1795 printf(" fetching\n");
1796 filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
1797 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1799 if (repo_add_rpmmd(repo, fp, 0, 0))
1801 printf("primary: %s\n", pool_errstr(pool));
1807 break; /* hopeless */
1809 filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
1810 if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1812 if (repo_add_updateinfoxml(repo, fp, 0))
1814 printf("updateinfo: %s\n", pool_errstr(pool));
1820 data = repo_add_repodata(repo, 0);
1821 if (!repomd_add_ext(repo, data, "deltainfo"))
1822 repomd_add_ext(repo, data, "prestodelta");
1823 repomd_add_ext(repo, data, "filelists");
1824 repodata_internalize(data);
1826 writecachedrepo(repo, 0, 0, cinfo->cookie);
1827 repodata_create_stubs(repo_last_repodata(repo));
1831 #ifdef ENABLE_SUSEREPO
1833 printf("susetags repo '%s':", cinfo->alias);
1837 if ((fp = curlfopen(cinfo, "content", 0, 0, 0, 0)) == 0)
1839 printf(" no content file, skipped\n");
1844 calc_checksum_fp(fp, REPOKEY_TYPE_SHA256, cinfo->cookie);
1845 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1847 printf(" cached\n");
1851 if (cinfo->repo_gpgcheck)
1853 sigfp = curlfopen(cinfo, "content.asc", 0, 0, 0, 0);
1856 printf(" unsigned, skipped\n");
1863 sigpool = read_sigs();
1864 if (!checksig(sigpool, fp, sigfp))
1866 printf(" checksig failed, skipped\n");
1874 if (repo_add_content(repo, fp, 0))
1876 printf("content: %s\n", pool_errstr(pool));
1878 break; /* hopeless */
1881 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
1882 descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
1884 descrdir = "suse/setup/descr";
1885 filename = susetags_find(repo, "packages.gz", &filechksum, &filechksumtype);
1887 filename = susetags_find(repo, "packages", &filechksum, &filechksumtype);
1890 printf(" no packages file entry, skipped\n");
1893 printf(" fetching\n");
1894 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
1895 break; /* hopeless */
1896 if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|SUSETAGS_RECORD_SHARES))
1898 printf("packages: %s\n", pool_errstr(pool));
1900 break; /* hopeless */
1903 /* add default language */
1904 filename = susetags_find(repo, "packages.en.gz", &filechksum, &filechksumtype);
1906 filename = susetags_find(repo, "packages.en", &filechksum, &filechksumtype);
1909 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1911 if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|REPO_REUSE_REPODATA|REPO_EXTEND_SOLVABLES))
1913 printf("packages.en: %s\n", pool_errstr(pool));
1919 filename = susetags_find(repo, "patterns", &filechksum, &filechksumtype);
1922 if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1925 while (fgets(pbuf, sizeof(pbuf), fp))
1927 int l = strlen(pbuf);
1929 if (l && pbuf[l - 1] == '\n')
1931 if (!*pbuf || *pbuf == '.' || strchr(pbuf, '/') != 0)
1933 filename = susetags_find(repo, pbuf, &filechksum, &filechksumtype);
1934 if (filename && (fp2 = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
1936 if (repo_add_susetags(repo, fp2, defvendor, 0, REPO_NO_INTERNALIZE))
1938 printf("%s: %s\n", pbuf, pool_errstr(pool));
1947 repo_internalize(repo);
1948 data = repo_add_repodata(repo, 0);
1949 susetags_add_ext(repo, data);
1950 repodata_internalize(data);
1952 writecachedrepo(repo, 0, 0, cinfo->cookie);
1953 repodata_create_stubs(repo_last_repodata(repo));
1957 #if defined(ENABLE_DEBIAN)
1959 printf("debian repo '%s':", cinfo->alias);
1961 filename = solv_dupjoin("dists/", cinfo->name, "/Release");
1962 if ((fpr = curlfopen(cinfo, filename, 0, 0, 0, 0)) == 0)
1964 printf(" no Release file, skipped\n");
1967 free((char *)filename);
1970 solv_free((char *)filename);
1971 if (cinfo->repo_gpgcheck)
1973 filename = solv_dupjoin("dists/", cinfo->name, "/Release.gpg");
1974 sigfp = curlfopen(cinfo, filename, 0, 0, 0, 0);
1975 solv_free((char *)filename);
1978 printf(" unsigned, skipped\n");
1983 sigpool = read_sigs();
1984 if (!checksig(sigpool, fpr, sigfp))
1986 printf(" checksig failed, skipped\n");
1993 calc_checksum_fp(fpr, REPOKEY_TYPE_SHA256, cinfo->cookie);
1994 if (usecachedrepo(repo, 0, cinfo->cookie, 1))
1996 printf(" cached\n");
2000 printf(" fetching\n");
2001 for (j = 0; j < cinfo->ncomponents; j++)
2003 if (!(filename = debian_find_component(cinfo, fpr, cinfo->components[j], &filechksum, &filechksumtype)))
2005 printf("[component %s not found]\n", cinfo->components[j]);
2008 if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
2010 if (repo_add_debpackages(repo, fp, 0))
2012 printf("component %s: %s\n", cinfo->components[j], pool_errstr(pool));
2017 solv_free((char *)filechksum);
2018 solv_free((char *)filename);
2022 writecachedrepo(repo, 0, 0, cinfo->cookie);
2027 printf("unsupported repo '%s': skipped\n", cinfo->alias);
2039 str2archid(Pool *pool, char *arch)
2044 id = pool_str2id(pool, arch, 0);
2045 if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
2047 if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
2053 #define DEPGLOB_NAME 1
2054 #define DEPGLOB_DEP 2
2055 #define DEPGLOB_NAMEDEP 3
2058 depglob(Pool *pool, char *name, Queue *job, int what)
2061 Id id = pool_str2id(pool, name, 0);
2066 FOR_PROVIDES(p, pp, id)
2068 Solvable *s = pool->solvables + p;
2070 if (s->name == id && (what & DEPGLOB_NAME) != 0)
2072 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
2078 if (what == DEPGLOB_NAMEDEP)
2079 printf("[using capability match for '%s']\n", name);
2080 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
2085 if (strpbrk(name, "[*?") == 0)
2088 if ((what & DEPGLOB_NAME) != 0)
2090 /* looks like a name glob. hard work. */
2091 for (p = 1; p < pool->nsolvables; p++)
2093 Solvable *s = pool->solvables + p;
2094 if (!s->repo || !pool_installable(pool, s))
2097 if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
2099 for (i = 0; i < job->count; i += 2)
2100 if (job->elements[i] == SOLVER_SOLVABLE_NAME && job->elements[i + 1] == id)
2102 if (i == job->count)
2103 queue_push2(job, SOLVER_SOLVABLE_NAME, id);
2110 if ((what & DEPGLOB_DEP))
2112 /* looks like a dep glob. really hard work. */
2113 for (id = 1; id < pool->ss.nstrings; id++)
2115 if (!pool->whatprovides[id])
2117 if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
2119 if (!match && what == DEPGLOB_NAMEDEP)
2120 printf("[using capability match for '%s']\n", name);
2121 for (i = 0; i < job->count; i += 2)
2122 if (job->elements[i] == SOLVER_SOLVABLE_PROVIDES && job->elements[i + 1] == id)
2124 if (i == job->count)
2125 queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
2136 limitrelation(Pool *pool, Queue *job, int flags, Id evr)
2140 for (i = j = 0; i < job->count; i += 2)
2142 Id select = job->elements[i] & SOLVER_SELECTMASK;
2143 if (select != SOLVER_SOLVABLE_NAME && select != SOLVER_SOLVABLE_PROVIDES)
2145 fprintf(stderr, "limitrelation only works on name/provides jobs\n");
2148 job->elements[i + 1] = pool_rel2id(pool, job->elements[i + 1], evr, flags, 1);
2149 if (flags == REL_ARCH)
2150 job->elements[i] |= SOLVER_SETARCH;
2151 if (flags == REL_EQ && select == SOLVER_SOLVABLE_NAME && job->elements[i])
2154 const char *evrstr = pool_id2str(pool, evr);
2155 if (!strchr(evrstr, '-'))
2156 job->elements[i] |= SOLVER_SETEV;
2159 job->elements[i] |= SOLVER_SETEVR;
2161 /* make sure we still have matches */
2162 FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
2166 job->elements[j] = job->elements[i];
2167 job->elements[j + 1] = job->elements[i + 1];
2171 queue_truncate(job, j);
2176 limitrelation_arch(Pool *pool, Queue *job, int flags, char *evr)
2180 if ((r = strrchr(evr, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
2183 limitrelation(pool, job, REL_ARCH, archid);
2184 limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
2188 limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
2189 return job->count / 2;
2193 limitrepo(Pool *pool, Id repofilter, Queue *job)
2201 for (i = j = 0; i < job->count; i += 2)
2204 FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
2206 s = pool_id2solvable(pool, p);
2207 if (s->repo && s->repo->repoid == repofilter)
2212 /* here we assume that repo == vendor, so we also set SOLVER_SETVENDOR */
2213 Id flags = (job->elements[i] & SOLVER_SETMASK) | SOLVER_SETVENDOR | SOLVER_SETREPO;
2214 if ((job->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_NAME)
2215 flags |= SOLVER_SETNAME;
2218 job->elements[j] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET | flags;
2219 job->elements[j + 1] = mq.elements[0];
2223 job->elements[j] = SOLVER_SOLVABLE_ONE_OF | flags;
2224 job->elements[j + 1] = pool_queuetowhatprovides(pool, &mq);
2229 queue_truncate(job, j);
2235 mkselect(Pool *pool, int mode, char *name, Queue *job)
2243 int type = strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
2246 dataiterator_init(&di, pool, mode == SOLVER_ERASE ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
2247 while (dataiterator_step(&di))
2249 Solvable *s = pool->solvables + di.solvid;
2250 if (!s->repo || !pool_installable(pool, s))
2252 queue_push(&q, di.solvid);
2253 dataiterator_skip_solvable(&di);
2255 dataiterator_free(&di);
2258 printf("[using file list match for '%s']\n", name);
2260 queue_push2(job, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
2262 queue_push2(job, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
2264 return job->count / 2;
2267 if ((r = strpbrk(name, "<=>")) != 0)
2269 /* relation case, support:
2274 int nend = r - name;
2287 while (*r && *r == ' ' && *r == '\t')
2289 while (nend && (name[nend - 1] == ' ' || name[nend -1 ] == '\t'))
2293 fprintf(stderr, "bad relation\n");
2296 oldnend = name[nend];
2298 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2300 name[nend] = oldnend;
2301 limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
2302 return job->count / 2;
2304 if ((r2 = strrchr(name, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
2307 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2309 name[nend] = oldnend;
2311 limitrelation(pool, job, REL_ARCH, archid);
2312 limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
2313 return job->count / 2;
2317 name[nend] = oldnend;
2321 /* no relation case, support:
2325 * nameglob-version.arch
2326 * nameglob-version-release
2327 * nameglob-version-release.arch
2329 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2330 return job->count / 2;
2331 if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
2334 if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
2337 limitrelation(pool, job, REL_ARCH, archid);
2338 return job->count / 2;
2342 if ((r = strrchr(name, '-')) != 0)
2345 if (depglob(pool, name, job, DEPGLOB_NAME))
2347 /* have just the version */
2348 limitrelation_arch(pool, job, REL_EQ, r + 1);
2350 return job->count / 2;
2352 if ((r2 = strrchr(name, '-')) != 0)
2357 if (depglob(pool, name, job, DEPGLOB_NAME))
2359 /* have version-release */
2360 limitrelation_arch(pool, job, REL_EQ, r + 1);
2362 return job->count / 2;
2373 yesno(const char *str)
2375 char inbuf[128], *ip;
2382 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
2387 while (*ip == ' ' || *ip == '\t')
2394 if (*ip == 'y' || *ip == 'n')
2395 return *ip == 'y' ? 1 : 0;
2399 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2409 fileconflict_cb(Pool *pool, Id p, void *cbdata)
2411 struct fcstate *fcstate = cbdata;
2419 rpm_byrpmdbid(0, 0, &fcstate->rpmdbstate);
2422 s = pool_id2solvable(pool, p);
2423 if (pool->installed && s->repo == pool->installed)
2425 if (!s->repo->rpmdbid)
2427 rpmdbid = s->repo->rpmdbid[p - s->repo->start];
2430 return rpm_byrpmdbid(rpmdbid, 0, &fcstate->rpmdbstate);
2432 for (i = 0; i < fcstate->newpkgscnt; i++)
2433 if (fcstate->checkq->elements[i] == p)
2435 if (i == fcstate->newpkgscnt)
2437 fp = fcstate->newpkgsfps[i];
2441 return rpm_byfp(fp, pool_solvable2str(pool, s), &fcstate->rpmdbstate);
2446 runrpm(const char *arg, const char *name, int dupfd3)
2451 if ((pid = fork()) == (pid_t)-1)
2458 if (dupfd3 != -1 && dupfd3 != 3)
2464 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
2465 if (strcmp(arg, "-e") == 0)
2466 execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
2468 execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
2472 while (waitpid(pid, &status, 0) != pid)
2476 printf("rpm failed\n");
2483 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2486 rundpkg(const char *arg, const char *name, int dupfd3)
2491 if ((pid = fork()) == (pid_t)-1)
2498 if (dupfd3 != -1 && dupfd3 != 3)
2504 fcntl(3, F_SETFD, 0); /* clear CLOEXEC */
2505 if (strcmp(arg, "--install") == 0)
2506 execlp("dpkg", "dpkg", "--install", "--force", "all", name, (char *)0);
2508 execlp("dpkg", "dpkg", "--remove", "--force", "all", name, (char *)0);
2512 while (waitpid(pid, &status, 0) != pid)
2516 printf("dpkg failed\n");
2524 nscallback(Pool *pool, void *data, Id name, Id evr)
2526 if (name == NAMESPACE_PRODUCTBUDDY)
2528 /* SUSE specific hack: each product has an associated rpm */
2529 Solvable *s = pool->solvables + evr;
2533 cap = pool_str2id(pool, pool_tmpjoin(pool, "product(", pool_id2str(pool, s->name) + 8, ")"), 0);
2536 cap = pool_rel2id(pool, cap, s->evr, REL_EQ, 0);
2539 FOR_PROVIDES(p, pp, cap)
2541 Solvable *ps = pool->solvables + p;
2542 if (ps->repo == s->repo && ps->arch == s->arch)
2543 if (!bestp || pool_evrcmp(pool, pool->solvables[bestp].evr, ps->evr, EVRCMP_COMPARE) < 0)
2551 #ifdef SOFTLOCKS_PATH
2554 addsoftlocks(Pool *pool, Queue *job)
2558 char *bp, *ep, buf[4096];
2560 if ((fp = fopen(SOFTLOCKS_PATH, "r")) == 0)
2562 while((bp = fgets(buf, sizeof(buf), fp)) != 0)
2564 while (*bp == ' ' || *bp == '\t')
2566 if (!*bp || *bp == '#')
2568 for (ep = bp; *ep; ep++)
2569 if (*ep == ' ' || *ep == '\t' || *ep == '\n')
2572 type = SOLVER_SOLVABLE_NAME;
2573 if (!strncmp(bp, "provides:", 9) && bp[9])
2575 type = SOLVER_SOLVABLE_PROVIDES;
2578 id = pool_str2id(pool, bp, 1);
2579 if (pool->installed)
2581 FOR_JOB_SELECT(p, pp, type, id)
2582 if (pool->solvables[p].repo == pool->installed)
2585 continue; /* ignore, as it is already installed */
2587 queue_push2(job, SOLVER_LOCK|SOLVER_WEAK|type, id);
2596 rewrite_repos(Pool *pool, Queue *addedfileprovides, Queue *addedfileprovides_inst)
2601 Queue fileprovidesq;
2603 struct repoinfo *cinfo;
2605 map_init(&providedids, pool->ss.nstrings);
2606 queue_init(&fileprovidesq);
2607 for (i = 0; i < addedfileprovides->count; i++)
2608 MAPSET(&providedids, addedfileprovides->elements[i]);
2611 /* make sure all repodatas but the first are extensions */
2612 if (repo->nrepodata < 2)
2614 data = repo_id2repodata(repo, 1);
2615 if (data->loadcallback)
2617 for (j = 2; j < repo->nrepodata; j++)
2619 Repodata *edata = repo_id2repodata(repo, j);
2620 if (!edata->loadcallback)
2623 if (j < repo->nrepodata)
2624 continue; /* found a non-externsion repodata, can't rewrite */
2625 if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
2627 if (repo == pool->installed && addedfileprovides_inst)
2629 for (j = 0; j < addedfileprovides->count; j++)
2630 MAPCLR(&providedids, addedfileprovides->elements[j]);
2631 for (j = 0; j < addedfileprovides_inst->count; j++)
2632 MAPSET(&providedids, addedfileprovides_inst->elements[j]);
2635 for (j = 0; j < fileprovidesq.count; j++)
2636 if (MAPTST(&providedids, fileprovidesq.elements[j]))
2638 if (repo == pool->installed && addedfileprovides_inst)
2640 for (j = 0; j < addedfileprovides_inst->count; j++)
2641 MAPCLR(&providedids, addedfileprovides_inst->elements[j]);
2642 for (j = 0; j < addedfileprovides->count; j++)
2643 MAPSET(&providedids, addedfileprovides->elements[j]);
2644 if (n == addedfileprovides_inst->count)
2645 continue; /* nothing new added */
2647 else if (n == addedfileprovides->count)
2648 continue; /* nothing new added */
2650 repodata_set_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, repo == pool->installed && addedfileprovides_inst ? addedfileprovides_inst : addedfileprovides);
2651 repodata_internalize(data);
2652 cinfo = repo->appdata;
2653 writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
2655 queue_free(&fileprovidesq);
2656 map_free(&providedids);
2660 select_patches(Pool *pool, Queue *job)
2664 Map installedmap, noobsmap;
2667 map_init(&noobsmap, 0);
2668 map_init(&installedmap, pool->nsolvables);
2669 solver_calculate_noobsmap(pool, job, &noobsmap);
2670 if (pool->installed)
2671 FOR_REPO_SOLVABLES(pool->installed, p, s)
2672 MAPSET(&installedmap, p);
2674 /* install all patches */
2675 for (p = 1; p < pool->nsolvables; p++)
2681 s = pool->solvables + p;
2682 if (strncmp(pool_id2str(pool, s->name), "patch:", 6) != 0)
2684 FOR_PROVIDES(p2, pp, s->name)
2686 Solvable *s2 = pool->solvables + p2;
2687 if (s2->name != s->name)
2689 r = pool_evrcmp(pool, s->evr, s2->evr, EVRCMP_COMPARE);
2690 if (r < 0 || (r == 0 && p > p2))
2695 type = solvable_lookup_str(s, SOLVABLE_PATCHCATEGORY);
2696 if (type && !strcmp(type, "optional"))
2698 r = solvable_trivial_installable_map(s, &installedmap, 0, &noobsmap);
2701 if (solvable_lookup_bool(s, UPDATE_RESTART) && r == 0)
2708 queue_push2(job, SOLVER_SOLVABLE, p);
2710 map_free(&installedmap);
2711 map_free(&noobsmap);
2715 #define MODE_INSTALL 1
2716 #define MODE_ERASE 2
2717 #define MODE_UPDATE 3
2718 #define MODE_DISTUPGRADE 4
2719 #define MODE_VERIFY 5
2720 #define MODE_PATCH 6
2722 #define MODE_REPOLIST 8
2723 #define MODE_SEARCH 9
2728 fprintf(stderr, "Usage: solv COMMAND <select>\n");
2729 fprintf(stderr, "\n");
2730 fprintf(stderr, " dist-upgrade: replace installed packages with\n");
2731 fprintf(stderr, " versions from the repositories\n");
2732 fprintf(stderr, " erase: erase installed packages\n");
2733 fprintf(stderr, " info: display package information\n");
2734 fprintf(stderr, " install: install packages\n");
2735 fprintf(stderr, " list: list packages\n");
2736 fprintf(stderr, " repos: list enabled repositories\n");
2737 fprintf(stderr, " search: search name/summary/description\n");
2738 fprintf(stderr, " update: update installed packages\n");
2739 fprintf(stderr, " verify: check dependencies of installed packages\n");
2740 fprintf(stderr, "\n");
2745 main(int argc, char **argv)
2748 Repo *commandlinerepo = 0;
2749 Id *commandlinepkgs = 0;
2751 struct repoinfo *repoinfos;
2753 int mainmode = 0, mode = 0;
2758 char inbuf[128], *ip;
2761 Queue addedfileprovides;
2762 Queue addedfileprovides_inst;
2770 if (!strcmp(argv[0], "install") || !strcmp(argv[0], "in"))
2772 mainmode = MODE_INSTALL;
2773 mode = SOLVER_INSTALL;
2775 else if (!strcmp(argv[0], "patch"))
2777 mainmode = MODE_PATCH;
2778 mode = SOLVER_INSTALL;
2780 else if (!strcmp(argv[0], "erase") || !strcmp(argv[0], "rm"))
2782 mainmode = MODE_ERASE;
2783 mode = SOLVER_ERASE;
2785 else if (!strcmp(argv[0], "list"))
2787 mainmode = MODE_LIST;
2790 else if (!strcmp(argv[0], "info"))
2792 mainmode = MODE_INFO;
2795 else if (!strcmp(argv[0], "search"))
2797 mainmode = MODE_SEARCH;
2800 else if (!strcmp(argv[0], "verify"))
2802 mainmode = MODE_VERIFY;
2803 mode = SOLVER_VERIFY;
2805 else if (!strcmp(argv[0], "update") || !strcmp(argv[0], "up"))
2807 mainmode = MODE_UPDATE;
2808 mode = SOLVER_UPDATE;
2810 else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
2812 mainmode = MODE_DISTUPGRADE;
2813 mode = SOLVER_UPDATE;
2815 else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
2817 mainmode = MODE_REPOLIST;
2823 if (argc > 1 && !strcmp(argv[1], "--clean"))
2830 pool = pool_create();
2834 const char *langs[] = {"de_DE", "de", "en"};
2835 pool_set_languages(pool, langs, sizeof(langs)/sizeof(*langs));
2839 pool_setloadcallback(pool, load_stub, 0);
2840 pool->nscallback = nscallback;
2841 // pool_setdebuglevel(pool, 2);
2843 repoinfos = read_repoinfos(pool, &nrepoinfos);
2845 if (mainmode == MODE_REPOLIST)
2848 for (i = 0; i < nrepoinfos; i++)
2850 struct repoinfo *cinfo = repoinfos + i;
2851 if (!cinfo->enabled)
2853 printf("%d: %-20s %s (prio %d)\n", j++, cinfo->alias, cinfo->name, cinfo->priority);
2858 read_repos(pool, repoinfos, nrepoinfos);
2860 if (argc > 2 && !strcmp(argv[1], "-r"))
2862 const char *rname = argv[2], *rp;
2863 for (rp = rname; *rp; rp++)
2864 if (*rp <= '0' || *rp >= '9')
2868 /* repo specified by number */
2869 int rnum = atoi(rname);
2870 for (i = 0; i < nrepoinfos; i++)
2872 struct repoinfo *cinfo = repoinfos + i;
2873 if (!cinfo->enabled)
2876 repofilter = cinfo->repo->repoid;
2881 /* repo specified by alias */
2885 if (!strcasecmp(rname, repo->name))
2886 repofilter = repo->repoid;
2891 fprintf(stderr, "%s: no such repo\n", rname);
2897 if (mainmode == MODE_SEARCH)
2903 map_init(&m, pool->nsolvables);
2904 dataiterator_init(&di, pool, 0, 0, 0, argv[1], SEARCH_SUBSTRING|SEARCH_NOCASE);
2905 dataiterator_set_keyname(&di, SOLVABLE_NAME);
2906 dataiterator_set_search(&di, 0, 0);
2907 while (dataiterator_step(&di))
2908 MAPSET(&m, di.solvid);
2909 dataiterator_set_keyname(&di, SOLVABLE_SUMMARY);
2910 dataiterator_set_search(&di, 0, 0);
2911 while (dataiterator_step(&di))
2912 MAPSET(&m, di.solvid);
2913 dataiterator_set_keyname(&di, SOLVABLE_DESCRIPTION);
2914 dataiterator_set_search(&di, 0, 0);
2915 while (dataiterator_step(&di))
2916 MAPSET(&m, di.solvid);
2917 dataiterator_free(&di);
2919 for (p = 1; p < pool->nsolvables; p++)
2921 Solvable *s = pool_id2solvable(pool, p);
2924 printf(" - %s: %s\n", pool_solvable2str(pool, s), solvable_lookup_str(s, SOLVABLE_SUMMARY));
2931 if (mainmode == MODE_LIST || mainmode == MODE_INSTALL)
2933 for (i = 1; i < argc; i++)
2936 l = strlen(argv[i]);
2937 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2938 if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
2941 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2942 if (l <= 4 || strcmp(argv[i] + l - 4, ".deb"))
2945 if (access(argv[i], R_OK))
2950 if (!commandlinepkgs)
2951 commandlinepkgs = solv_calloc(argc, sizeof(Id));
2952 if (!commandlinerepo)
2953 commandlinerepo = repo_create(pool, "@commandline");
2955 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
2956 p = repo_add_rpm(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2958 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
2959 p = repo_add_deb(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
2963 fprintf(stderr, "could not add '%s'\n", argv[i]);
2966 commandlinepkgs[i] = p;
2968 if (commandlinerepo)
2969 repo_internalize(commandlinerepo);
2972 // FOR_REPOS(i, repo)
2973 // printf("%s: %d solvables\n", repo->name, repo->nsolvables);
2974 queue_init(&addedfileprovides);
2975 queue_init(&addedfileprovides_inst);
2976 pool_addfileprovides_queue(pool, &addedfileprovides, &addedfileprovides_inst);
2977 if (addedfileprovides.count || addedfileprovides_inst.count)
2978 rewrite_repos(pool, &addedfileprovides, &addedfileprovides_inst);
2979 queue_free(&addedfileprovides);
2980 queue_free(&addedfileprovides_inst);
2981 pool_createwhatprovides(pool);
2984 for (i = 1; i < argc; i++)
2989 if (commandlinepkgs && commandlinepkgs[i])
2991 queue_push2(&job, SOLVER_SOLVABLE, commandlinepkgs[i]);
2995 if (!mkselect(pool, mode, argv[i], &job2))
2997 fprintf(stderr, "nothing matches '%s'\n", argv[i]);
3000 if (repofilter && !limitrepo(pool, repofilter, &job2))
3002 fprintf(stderr, "nothing in repo matches '%s'\n", argv[i]);
3005 for (j = 0; j < job2.count; j++)
3006 queue_push(&job, job2.elements[j]);
3010 if (!job.count && mainmode != MODE_UPDATE && mainmode != MODE_DISTUPGRADE && mainmode != MODE_VERIFY && mainmode != MODE_PATCH)
3012 printf("no package matched\n");
3019 if (mainmode == MODE_LIST || mainmode == MODE_INFO)
3021 /* list mode, no solver needed */
3022 for (i = 0; i < job.count; i += 2)
3024 Id how = job.elements[i] & SOLVER_SELECTMASK;
3025 FOR_JOB_SELECT(p, pp, how, job.elements[i + 1])
3027 Solvable *s = pool_id2solvable(pool, p);
3028 if (mainmode == MODE_INFO)
3031 printf("Name: %s\n", pool_solvable2str(pool, s));
3032 printf("Repo: %s\n", s->repo->name);
3033 printf("Summary: %s\n", solvable_lookup_str(s, SOLVABLE_SUMMARY));
3034 str = solvable_lookup_str(s, SOLVABLE_URL);
3036 printf("Url: %s\n", str);
3037 str = solvable_lookup_str(s, SOLVABLE_LICENSE);
3039 printf("License: %s\n", str);
3040 printf("Description:\n%s\n", solvable_lookup_str(s, SOLVABLE_DESCRIPTION));
3046 const char *sum = solvable_lookup_str_lang(s, SOLVABLE_SUMMARY, "de", 1);
3048 const char *sum = solvable_lookup_str_poollang(s, SOLVABLE_SUMMARY);
3050 printf(" - %s [%s]\n", pool_solvable2str(pool, s), s->repo->name);
3052 printf(" %s\n", sum);
3058 free_repoinfos(repoinfos, nrepoinfos);
3059 solv_free(commandlinepkgs);
3061 yum_substitute(pool, 0);
3066 if (mainmode == MODE_PATCH)
3067 select_patches(pool, &job);
3070 for (i = 0; i < job.count; i += 2)
3072 if (mode == SOLVER_UPDATE)
3074 /* make update of not installed packages an install */
3075 FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
3076 if (pool->installed && pool->solvables[p].repo == pool->installed)
3080 job.elements[i] |= SOLVER_INSTALL;
3082 job.elements[i] |= SOLVER_CLEANDEPS;
3086 job.elements[i] |= mode;
3088 job.elements[i] |= SOLVER_CLEANDEPS;
3091 if (mainmode == MODE_DISTUPGRADE && allpkgs)
3094 queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_REPO, repofilter);
3096 queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_ALL, 0);
3098 if (mainmode == MODE_UPDATE && allpkgs)
3101 queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_REPO, repofilter);
3103 queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_ALL, 0);
3105 if (mainmode == MODE_VERIFY && allpkgs)
3108 queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_REPO, repofilter);
3110 queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_ALL, 0);
3113 // multiversion test
3114 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae", 1));
3115 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-base", 1));
3116 // queue_push2(&job, SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME, pool_str2id(pool, "kernel-pae-extra", 1));
3118 #ifdef SOFTLOCKS_PATH
3119 addsoftlocks(pool, &job);
3122 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3127 Id problem, solution;
3130 solv = solver_create(pool);
3131 solver_set_flag(solv, SOLVER_FLAG_SPLITPROVIDES, 1);
3132 if (mainmode == MODE_ERASE)
3133 solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1); /* don't nag */
3135 if (!solver_solve(solv, &job))
3137 pcnt = solver_problem_count(solv);
3138 printf("Found %d problems:\n", pcnt);
3139 for (problem = 1; problem <= pcnt; problem++)
3142 printf("Problem %d:\n", problem);
3143 solver_printprobleminfo(solv, problem);
3145 scnt = solver_solution_count(solv, problem);
3146 for (solution = 1; solution <= scnt; solution++)
3148 printf("Solution %d:\n", solution);
3149 solver_printsolution(solv, problem, solution);
3154 printf("Please choose a solution: ");
3157 if (!(ip = fgets(inbuf, sizeof(inbuf), stdin)))
3162 while (*ip == ' ' || *ip == '\t')
3164 if (*ip >= '0' && *ip <= '9')
3167 if (take >= 1 && take <= scnt)
3183 solver_take_solution(solv, problem, take, &job);
3189 trans = solver_create_transaction(solv);
3190 if (!trans->steps.count)
3192 printf("Nothing to do.\n");
3193 transaction_free(trans);
3197 free_repoinfos(repoinfos, nrepoinfos);
3198 solv_free(commandlinepkgs);
3200 yum_substitute(pool, 0);
3205 printf("Transaction summary:\n\n");
3206 transaction_print(trans);
3215 duc[1].path = "/usr/share/man";
3216 duc[2].path = "/sbin";
3217 duc[3].path = "/etc";
3218 transaction_calc_duchanges(trans, duc, 4);
3219 for (i = 0; i < 4; i++)
3220 printf("duchanges %s: %d K %d inodes\n", duc[i].path, duc[i].kbytes, duc[i].files);
3223 printf("install size change: %d K\n", transaction_calc_installsizechange(trans));
3226 if (!yesno("OK to continue (y/n)? "))
3229 transaction_free(trans);
3233 free_repoinfos(repoinfos, nrepoinfos);
3234 solv_free(commandlinepkgs);
3236 yum_substitute(pool, 0);
3241 queue_init(&checkq);
3242 newpkgs = transaction_installedresult(trans, &checkq);
3247 int downloadsize = 0;
3248 for (i = 0; i < newpkgs; i++)
3252 p = checkq.elements[i];
3253 s = pool_id2solvable(pool, p);
3254 downloadsize += solvable_lookup_sizek(s, SOLVABLE_DOWNLOADSIZE, 0);
3256 printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
3257 newpkgsfps = solv_calloc(newpkgs, sizeof(*newpkgsfps));
3258 for (i = 0; i < newpkgs; i++)
3260 unsigned int medianr;
3263 struct repoinfo *cinfo;
3264 const unsigned char *chksum;
3268 p = checkq.elements[i];
3269 s = pool_id2solvable(pool, p);
3270 if (s->repo == commandlinerepo)
3272 loc = solvable_get_location(s, &medianr);
3273 if (!(newpkgsfps[i] = fopen(loc, "r")))
3281 cinfo = s->repo->appdata;
3284 printf("%s: no repository information\n", s->repo->name);
3287 loc = solvable_get_location(s, &medianr);
3291 if (pool->installed && pool->installed->nsolvables)
3293 /* try a delta first */
3294 char *matchname = strdup(pool_id2str(pool, s->name));
3295 dataiterator_init(&di, pool, s->repo, SOLVID_META, DELTA_PACKAGE_NAME, matchname, SEARCH_STRING);
3296 dataiterator_prepend_keyname(&di, REPOSITORY_DELTAINFO);
3297 while (dataiterator_step(&di))
3301 dataiterator_setpos_parent(&di);
3302 if (pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_EVR) != s->evr ||
3303 pool_lookup_id(pool, SOLVID_POS, DELTA_PACKAGE_ARCH) != s->arch)
3305 baseevr = pool_lookup_id(pool, SOLVID_POS, DELTA_BASE_EVR);
3306 FOR_PROVIDES(op, pp, s->name)
3308 Solvable *os = pool->solvables + op;
3309 if (os->repo == pool->installed && os->name == s->name && os->arch == s->arch && os->evr == baseevr)
3312 if (op && access("/usr/bin/applydeltarpm", X_OK) == 0)
3314 /* base is installed, run sequence check */
3315 const char *seqname;
3320 const char *archstr;
3325 archstr = pool_id2str(pool, s->arch);
3326 if (strlen(archstr) > 10 || strchr(archstr, '\'') != 0)
3329 seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
3330 seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
3331 seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
3332 seq = pool_tmpjoin(pool, seqname, "-", seqevr);
3333 seq = pool_tmpappend(pool, seq, "-", seqnum);
3334 if (strchr(seq, '\'') != 0)
3337 sprintf(cmd, "/usr/bin/applydeltarpm -a '%s' -c -s '", archstr);
3339 sprintf(cmd, "/usr/bin/applydeltarpm -c -s '");
3341 if (system(pool_tmpjoin(pool, cmd, seq, "'")) != 0)
3342 continue; /* didn't match */
3343 /* looks good, download delta */
3345 chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
3347 continue; /* no way! */
3348 dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
3349 dloc = pool_tmpappend(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
3350 dloc = pool_tmpappend(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
3351 dloc = pool_tmpappend(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
3352 if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
3354 /* got it, now reconstruct */
3355 newfd = opentmpfile();
3357 sprintf(cmd, "applydeltarpm -a '%s' /dev/fd/%d /dev/fd/%d", archstr, fileno(fp), newfd);
3359 sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
3361 fcntl(fileno(fp), F_SETFD, 0);
3368 lseek(newfd, 0, SEEK_SET);
3370 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3371 if (chksumtype && !verify_checksum(newfd, loc, chksum, chksumtype))
3377 newpkgsfps[i] = fdopen(newfd, "r");
3382 dataiterator_free(&di);
3383 solv_free(matchname);
3390 continue; /* delta worked! */
3392 if (cinfo->type == TYPE_SUSETAGS)
3394 const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
3395 loc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", loc);
3398 chksum = solvable_lookup_bin_checksum(s, SOLVABLE_CHECKSUM, &chksumtype);
3399 if ((newpkgsfps[i] = curlfopen(cinfo, loc, 0, chksum, chksumtype, 0)) == 0)
3401 printf("\n%s: %s not found in repository\n", s->repo->name, loc);
3410 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3414 struct fcstate fcstate;
3416 printf("Searching for file conflicts\n");
3417 queue_init(&conflicts);
3418 fcstate.rpmdbstate = 0;
3419 fcstate.newpkgscnt = newpkgs;
3420 fcstate.checkq = &checkq;
3421 fcstate.newpkgsfps = newpkgsfps;
3422 pool_findfileconflicts(pool, &checkq, newpkgs, &conflicts, &fileconflict_cb, &fcstate);
3423 if (conflicts.count)
3426 for (i = 0; i < conflicts.count; i += 5)
3427 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]));
3429 if (yesno("Re-run solver (y/n/q)? "))
3431 for (i = 0; i < newpkgs; i++)
3433 fclose(newpkgsfps[i]);
3434 newpkgsfps = solv_free(newpkgsfps);
3436 pool_add_fileconflicts_deps(pool, &conflicts);
3437 pool_createwhatprovides(pool); /* Hmm... */
3441 queue_free(&conflicts);
3445 printf("Committing transaction:\n\n");
3446 transaction_order(trans, 0);
3447 for (i = 0; i < trans->steps.count; i++)
3449 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3450 const char *evr, *evrp, *nvra;
3456 p = trans->steps.elements[i];
3457 s = pool_id2solvable(pool, p);
3458 Id type = transaction_type(trans, p, SOLVER_TRANSACTION_RPM_ONLY);
3461 case SOLVER_TRANSACTION_ERASE:
3462 printf("erase %s\n", pool_solvid2str(pool, p));
3463 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3464 if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
3466 /* strip epoch from evr */
3467 evr = evrp = pool_id2str(pool, s->evr);
3468 while (*evrp >= '0' && *evrp <= '9')
3470 if (evrp > evr && evrp[0] == ':' && evrp[1])
3472 nvra = pool_tmpjoin(pool, pool_id2str(pool, s->name), "-", evr);
3473 nvra = pool_tmpappend(pool, nvra, ".", pool_id2str(pool, s->arch));
3474 runrpm("-e", nvra, -1); /* too bad that --querybynumber doesn't work */
3476 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3477 rundpkg("--remove", pool_id2str(pool, s->name), 0);
3480 case SOLVER_TRANSACTION_INSTALL:
3481 case SOLVER_TRANSACTION_MULTIINSTALL:
3482 printf("install %s\n", pool_solvid2str(pool, p));
3483 for (j = 0; j < newpkgs; j++)
3484 if (checkq.elements[j] == p)
3486 fp = j < newpkgs ? newpkgsfps[j] : 0;
3490 lseek(fileno(fp), 0, SEEK_SET);
3491 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
3492 runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
3494 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
3495 rundpkg("--install", "/dev/fd/3", fileno(fp));
3505 for (i = 0; i < newpkgs; i++)
3507 fclose(newpkgsfps[i]);
3508 solv_free(newpkgsfps);
3509 queue_free(&checkq);
3510 transaction_free(trans);
3514 free_repoinfos(repoinfos, nrepoinfos);
3515 solv_free(commandlinepkgs);
3517 yum_substitute(pool, 0);