2 * Copyright (c) 2012, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
22 #include "solv_xfopen.h"
23 #include "repo_arch.h"
25 static long long parsenum(unsigned char *p, int cnt)
33 x = *p & 0x40 ? (-1 << 8 | *p) : (*p ^ 0x80);
38 while (cnt > 0 && (*p == ' ' || *p == '\t'))
42 for (; cnt > 0 && *p >= '0' && *p < '8'; cnt--, p++)
43 x = (x << 3) | (*p - '0');
47 static int readblock(FILE *fp, unsigned char *blk)
52 r = fread(blk + l, 1, 512 - l, fp);
62 unsigned char blk[512];
72 static char *getsentry(struct tarhead *th, char *s, int size)
75 if (th->eof || size <= 1)
77 size--; /* terminating 0 */
81 for (i = th->off; i < th->end; i++)
85 if (!size || th->blk[i] == '\n')
96 th->end = fread(th->blk, 1, 512, th->fp);
107 if (readblock(th->fp, th->blk))
113 th->end = th->length > 512 ? 512 : th->length;
114 th->length -= th->end;
118 static void skipentry(struct tarhead *th)
120 for (; th->length > 0; th->length -= 512)
122 if (readblock(th->fp, th->blk))
130 th->off = th->end = 0;
133 static void inittarhead(struct tarhead *th, FILE *fp)
135 memset(th, 0, sizeof(*th));
139 static void freetarhead(struct tarhead *th)
144 static int gettarhead(struct tarhead *th)
149 th->path = solv_free(th->path);
159 int r = readblock(th->fp, th->blk);
174 length = parsenum(th->blk + 124, 12);
178 switch (th->blk[156])
184 /* hard link, special length magic... */
191 case '2': case '3': case '4': case '6':
194 case 'X': case 'x': case 'L':
197 if (length < 1 || length >= 1024 * 1024)
199 data = pp = solv_malloc(length + 512);
200 for (l = length; l > 0; l -= 512, pp += 512)
201 if (readblock(th->fp, (unsigned char *)pp))
207 type = 3; /* extension */
208 if (th->blk[156] == 'L')
219 for (l = 0; l < length && pp[l] >= '0' && pp[l] <= '9'; l++)
220 ll = ll * 10 + (pp[l] - '0');
221 if (l == length || pp[l] != ' ' || ll < 1 || ll > length || pp[ll - 1] != '\n')
230 if (!strncmp(pp, "path=", 5))
233 th->path = solv_strdup(pp + 5);
243 type = 3; /* extension */
246 if ((type == 1 || type == 2) && !th->path)
249 memcpy(path, th->blk, 156);
251 if (!memcmp(th->blk + 257, "ustar\0\060\060", 8) && !th->path && th->blk[345])
253 /* POSIX ustar with prefix */
255 memcpy(prefix, th->blk + 345, 155);
258 if (l && prefix[l - 1] == '/')
260 th->path = solv_dupjoin(prefix, "/", path);
263 th->path = solv_dupjoin(path, 0, 0);
265 if (type == 1 || type == 2)
267 l = strlen(th->path);
268 if (l && th->path[l - 1] == '/')
279 r = readblock(th->fp, th->blk);
291 adddep(Repo *repo, Offset olddeps, char *line)
293 Pool *pool = repo->pool;
297 while (*line == ' ' || *line == '\t')
300 while (*p && *p != ' ' && *p != '\t' && *p != '<' && *p != '=' && *p != '>')
302 id = pool_strn2id(pool, line, p - line, 1);
303 while (*p == ' ' || *p == '\t')
305 if (*p == '<' || *p == '=' || *p == '>')
319 while (*p == ' ' || *p == '\t')
322 while (*p && *p != ' ' && *p != '\t')
324 id = pool_rel2id(pool, id, pool_strn2id(pool, line, p - line, 1), flags, 1);
326 return repo_addid_dep(repo, olddeps, id, 0);
330 repo_add_arch_pkg(Repo *repo, const char *fn, int flags)
332 Pool *pool = repo->pool;
341 Chksum *pkgidchk = 0;
343 data = repo_add_repodata(repo, flags);
344 if ((fd = open(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, fn) : fn, O_RDONLY, 0)) < 0)
346 pool_error(pool, -1, "%s: %s", fn, strerror(errno));
351 pool_error(pool, -1, "%s: fstat: %s", fn, strerror(errno));
355 if (!(fp = solv_xfopen_fd(fn, fd, "r")))
357 pool_error(pool, -1, "%s: fdopen failed", fn);
362 inittarhead(&th, fp);
363 while (gettarhead(&th) > 0)
365 if (th.type != 1 || strcmp(th.path, ".PKGINFO") != 0)
371 s = pool_id2solvable(pool, repo_add_solvable(repo));
372 if (flags & ARCH_ADD_WITH_PKGID)
373 pkgidchk = solv_chksum_create(REPOKEY_TYPE_MD5);
374 while (getsentry(&th, line, sizeof(line)))
380 solv_chksum_add(pkgidchk, line, l);
381 if (line[l - 1] != '\n')
392 if (l == 0 || line[0] == '#')
394 if (!strncmp(line, "pkgname = ", 10))
395 s->name = pool_str2id(pool, line + 10, 1);
396 else if (!strncmp(line, "pkgver = ", 9))
397 s->evr = pool_str2id(pool, line + 9, 1);
398 else if (!strncmp(line, "pkgdesc = ", 10))
400 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line + 10);
401 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, line + 10);
403 else if (!strncmp(line, "url = ", 6))
404 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, line + 6);
405 else if (!strncmp(line, "builddate = ", 12))
406 repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, strtoull(line + 12, 0, 10));
407 else if (!strncmp(line, "packager = ", 11))
408 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_PACKAGER, line + 11);
409 else if (!strncmp(line, "size = ", 7))
410 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(line + 7, 0, 10));
411 else if (!strncmp(line, "arch = ", 7))
412 s->arch = pool_str2id(pool, line + 7, 1);
413 else if (!strncmp(line, "license = ", 10))
414 repodata_add_poolstr_array(data, s - pool->solvables, SOLVABLE_LICENSE, line + 10);
415 else if (!strncmp(line, "replaces = ", 11))
416 s->obsoletes = adddep(repo, s->obsoletes, line + 11);
417 else if (!strncmp(line, "group = ", 8))
418 repodata_add_poolstr_array(data, s - pool->solvables, SOLVABLE_GROUP, line + 8);
419 else if (!strncmp(line, "depend = ", 9))
420 s->requires = adddep(repo, s->requires, line + 9);
421 else if (!strncmp(line, "optdepend = ", 12))
423 char *p = strchr(line, ':');
426 s->suggests = adddep(repo, s->suggests, line + 12);
428 else if (!strncmp(line, "conflict = ", 11))
429 s->conflicts = adddep(repo, s->conflicts, line + 11);
430 else if (!strncmp(line, "provides = ", 11))
431 s->provides = adddep(repo, s->provides, line + 11);
439 pool_error(pool, -1, "%s: not an arch package", fn);
441 solv_chksum_free(pkgidchk, 0);
446 pool_error(pool, -1, "%s: package has no name", fn);
447 s = solvable_free(s, 1);
455 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
456 if (!(flags & REPO_NO_LOCATION))
457 repodata_set_location(data, s - pool->solvables, 0, 0, fn);
458 if (S_ISREG(stb.st_mode))
459 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
462 unsigned char pkgid[16];
463 solv_chksum_free(pkgidchk, pkgid);
464 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
469 solv_chksum_free(pkgidchk, 0);
470 if (!(flags & REPO_NO_INTERNALIZE))
471 repodata_internalize(data);
472 return s ? s - pool->solvables : 0;
475 static char *getsentrynl(struct tarhead *th, char *s, int size)
478 if (!getsentry(th, s, size))
486 if (l && s[l - 1] == '\n')
491 while (getsentry(th, s, size))
494 if (!l || s[l - 1] == '\n')
502 joinhash_init(Repo *repo, Hashval *hmp)
504 Hashval hm = mkmask(repo->nsolvables);
505 Hashtable ht = solv_calloc(hm + 1, sizeof(*ht));
510 FOR_REPO_SOLVABLES(repo, i, s)
512 hh = HASHCHAIN_START;
515 h = HASHCHAIN_NEXT(h, hh, hm);
523 joinhash_lookup(Repo *repo, Hashtable ht, Hashval hm, const char *fn)
529 if ((p = strrchr(fn, '/')) != 0)
531 /* here we assume that the dirname is name-evr */
534 for (p = fn + strlen(fn) - 1; p > fn; p--)
536 while (p > fn && *p != '-')
540 name = pool_strn2id(repo->pool, fn, p - fn, 0);
543 evr = pool_str2id(repo->pool, p + 1, 0);
546 /* found valid name/evr combination, check hash */
547 hh = HASHCHAIN_START;
551 Solvable *s = repo->pool->solvables + ht[h];
552 if (s->name == name && s->evr == evr)
554 h = HASHCHAIN_NEXT(h, hh, hm);
561 adddata(Repodata *data, Solvable *s, struct tarhead *th)
563 Repo *repo = data->repo;
564 Pool *pool = repo->pool;
569 while (getsentry(th, line, sizeof(line)))
572 if (l == 0 || line[l - 1] != '\n')
575 if (l <= 2 || line[0] != '%' || line[l - 1] != '%')
577 if (!strcmp(line, "%FILENAME%"))
579 if (getsentrynl(th, line, sizeof(line)))
580 repodata_set_location(data, s - pool->solvables, 0, 0, line);
582 else if (!strcmp(line, "%NAME%"))
584 if (getsentrynl(th, line, sizeof(line)))
585 s->name = pool_str2id(pool, line, 1);
587 else if (!strcmp(line, "%VERSION%"))
589 if (getsentrynl(th, line, sizeof(line)))
590 s->evr = pool_str2id(pool, line, 1);
592 else if (!strcmp(line, "%DESC%"))
594 if (getsentrynl(th, line, sizeof(line)))
596 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line);
597 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, line);
600 else if (!strcmp(line, "%GROUPS%"))
602 if (getsentrynl(th, line, sizeof(line)))
603 repodata_add_poolstr_array(data, s - pool->solvables, SOLVABLE_GROUP, line);
605 else if (!strcmp(line, "%CSIZE%"))
607 if (getsentrynl(th, line, sizeof(line)))
608 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, strtoull(line, 0, 10));
610 else if (!strcmp(line, "%ISIZE%"))
612 if (getsentrynl(th, line, sizeof(line)))
613 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(line, 0, 10));
615 else if (!strcmp(line, "%MD5SUM%"))
617 if (getsentrynl(th, line, sizeof(line)) && !havesha256)
618 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, REPOKEY_TYPE_MD5, line);
620 else if (!strcmp(line, "%SHA256SUM%"))
622 if (getsentrynl(th, line, sizeof(line)))
624 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, REPOKEY_TYPE_SHA256, line);
628 else if (!strcmp(line, "%URL%"))
630 if (getsentrynl(th, line, sizeof(line)))
631 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, line);
633 else if (!strcmp(line, "%LICENSE%"))
635 if (getsentrynl(th, line, sizeof(line)))
636 repodata_add_poolstr_array(data, s - pool->solvables, SOLVABLE_LICENSE, line);
638 else if (!strcmp(line, "%ARCH%"))
640 if (getsentrynl(th, line, sizeof(line)))
641 s->arch = pool_str2id(pool, line, 1);
643 else if (!strcmp(line, "%BUILDDATE%"))
645 if (getsentrynl(th, line, sizeof(line)))
646 repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, strtoull(line, 0, 10));
648 else if (!strcmp(line, "%PACKAGER%"))
650 if (getsentrynl(th, line, sizeof(line)))
651 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_PACKAGER, line);
653 else if (!strcmp(line, "%REPLACES%"))
655 while (getsentrynl(th, line, sizeof(line)) && *line)
656 s->obsoletes = adddep(repo, s->obsoletes, line);
658 else if (!strcmp(line, "%DEPENDS%"))
660 while (getsentrynl(th, line, sizeof(line)) && *line)
661 s->requires = adddep(repo, s->requires, line);
663 else if (!strcmp(line, "%CONFLICTS%"))
665 while (getsentrynl(th, line, sizeof(line)) && *line)
666 s->conflicts = adddep(repo, s->conflicts, line);
668 else if (!strcmp(line, "%PROVIDES%"))
670 while (getsentrynl(th, line, sizeof(line)) && *line)
671 s->provides = adddep(repo, s->provides, line);
673 else if (!strcmp(line, "%OPTDEPENDS%"))
675 while (getsentrynl(th, line, sizeof(line)) && *line)
677 char *p = strchr(line, ':');
680 s->suggests = adddep(repo, s->suggests, line);
683 else if (!strcmp(line, "%FILES%"))
685 while (getsentrynl(th, line, sizeof(line)) && *line)
690 if (l > 1 && line[l - 1] == '/')
691 line[--l] = 0; /* remove trailing slashes */
692 if ((p = strrchr(line , '/')) != 0)
695 if (line[0] != '/') /* anchor */
698 memmove(line + 1, line, p - 1 - line);
701 id = repodata_str2dir(data, line, 1);
705 id = repodata_str2dir(data, line, 1);
713 id = repodata_str2dir(data, "/", 1);
714 repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, id, p);
718 getsentrynl(th, line, sizeof(line));
723 finishsolvable(Repo *repo, Solvable *s)
725 Pool *pool = repo->pool;
737 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
741 repo_add_arch_repo(Repo *repo, FILE *fp, int flags)
743 Pool *pool = repo->pool;
749 Hashtable joinhash = 0;
750 Hashval joinhashmask = 0;
752 data = repo_add_repodata(repo, flags);
754 if (flags & REPO_EXTEND_SOLVABLES)
755 joinhash = joinhash_init(repo, &joinhashmask);
757 inittarhead(&th, fp);
758 while (gettarhead(&th) > 0)
766 bn = strrchr(th.path, '/');
767 if (!bn || (strcmp(bn + 1, "desc") != 0 && strcmp(bn + 1, "depends") != 0 && strcmp(bn + 1, "files") != 0))
772 if ((flags & REPO_EXTEND_SOLVABLES) != 0 && (!strcmp(bn + 1, "desc") || !strcmp(bn + 1, "depends")))
775 continue; /* skip those when we're extending */
777 if (!lastdn || (bn - th.path) != lastdnlen || strncmp(lastdn, th.path, lastdnlen) != 0)
779 finishsolvable(repo, s);
781 lastdn = solv_strdup(th.path);
782 lastdnlen = bn - th.path;
783 lastdn[lastdnlen] = 0;
784 if (flags & REPO_EXTEND_SOLVABLES)
786 s = joinhash_lookup(repo, joinhash, joinhashmask, lastdn);
794 s = pool_id2solvable(pool, repo_add_solvable(repo));
796 adddata(data, s, &th);
798 finishsolvable(repo, s);
801 if (!(flags & REPO_NO_INTERNALIZE))
802 repodata_internalize(data);
807 repo_add_arch_local(Repo *repo, const char *dir, int flags)
809 Pool *pool = repo->pool;
813 char *entrydir, *file;
817 data = repo_add_repodata(repo, flags);
819 if (flags & REPO_USE_ROOTDIR)
820 dir = pool_prepend_rootdir(pool, dir);
824 while ((de = readdir(dp)) != 0)
826 if (!de->d_name[0] || de->d_name[0] == '.')
828 entrydir = solv_dupjoin(dir, "/", de->d_name);
829 file = pool_tmpjoin(repo->pool, entrydir, "/desc", 0);
831 if ((fp = fopen(file, "r")) != 0)
834 inittarhead(&th, fp);
835 s = pool_id2solvable(pool, repo_add_solvable(repo));
836 adddata(data, s, &th);
839 file = pool_tmpjoin(repo->pool, entrydir, "/files", 0);
840 if ((fp = fopen(file, "r")) != 0)
842 inittarhead(&th, fp);
843 adddata(data, s, &th);
852 if (!(flags & REPO_NO_INTERNALIZE))
853 repodata_internalize(data);
854 if (flags & REPO_USE_ROOTDIR)
855 solv_free((char *)dir);