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)
200 data = pp = solv_malloc(l + 512);
201 for (; l > 0; l -= 512, pp += 512)
202 if (readblock(th->fp, (unsigned char *)pp))
207 type = 3; /* extension */
208 if (th->blk[156] == 'L')
220 for (l = 0; l < length && pp[l] >= '0' && pp[l] <= '9'; l++)
221 ll = ll * 10 + (pp[l] - '0');
222 if (l == length || pp[l] != ' ' || ll < 1 || ll > length || pp[ll - 1] != '\n')
231 if (!strncmp(pp, "path=", 5))
234 th->path = solv_strdup(pp + 5);
244 type = 3; /* extension */
247 if ((type == 1 || type == 2) && !th->path)
250 memcpy(path, th->blk, 156);
252 if (!memcmp(th->blk + 257, "ustar\0\060\060", 8) && !th->path && th->blk[345])
254 /* POSIX ustar with prefix */
256 memcpy(prefix, th->blk + 345, 155);
259 if (l && prefix[l - 1] == '/')
261 th->path = solv_dupjoin(prefix, "/", path);
264 th->path = solv_dupjoin(path, 0, 0);
266 if (type == 1 || type == 2)
268 l = strlen(th->path);
269 if (l && th->path[l - 1] == '/')
280 r = readblock(th->fp, th->blk);
292 adddep(Repo *repo, Offset olddeps, char *line)
294 Pool *pool = repo->pool;
298 while (*line == ' ' && *line == '\t')
301 while (*p && *p != ' ' && *p != '\t' && *p != '<' && *p != '=' && *p != '>')
303 id = pool_strn2id(pool, line, p - line, 1);
304 while (*p == ' ' && *p == '\t')
306 if (*p == '<' || *p == '=' || *p == '>')
320 while (*p == ' ' && *p == '\t')
323 while (*p && *p != ' ' && *p != '\t')
325 id = pool_rel2id(pool, id, pool_strn2id(pool, line, p - line, 1), flags, 1);
327 return repo_addid_dep(repo, olddeps, id, 0);
331 repo_add_arch_pkg(Repo *repo, const char *fn, int flags)
333 Pool *pool = repo->pool;
342 void *pkgidhandle = 0;
344 data = repo_add_repodata(repo, flags);
345 if ((fd = open(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, fn) : fn, O_RDONLY, 0)) < 0)
347 pool_error(pool, -1, "%s: %s", fn, strerror(errno));
352 pool_error(pool, -1, "%s: fstat: %s", fn, strerror(errno));
356 if (!(fp = solv_xfopen_fd(fn, fd, "r")))
358 pool_error(pool, -1, "%s: fdopen failed", fn);
363 inittarhead(&th, fp);
364 while (gettarhead(&th) > 0)
366 if (th.type != 1 || strcmp(th.path, ".PKGINFO") != 0)
372 s = pool_id2solvable(pool, repo_add_solvable(repo));
373 if (flags & ARCH_ADD_WITH_PKGID)
374 pkgidhandle = solv_chksum_create(REPOKEY_TYPE_MD5);
375 while (getsentry(&th, line, sizeof(line)))
381 solv_chksum_add(pkgidhandle, line, l);
382 if (line[l - 1] != '\n')
393 if (l == 0 || line[0] == '#')
395 if (!strncmp(line, "pkgname = ", 10))
396 s->name = pool_str2id(pool, line + 10, 1);
397 else if (!strncmp(line, "pkgver = ", 9))
398 s->evr = pool_str2id(pool, line + 9, 1);
399 else if (!strncmp(line, "pkgdesc = ", 10))
401 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line + 10);
402 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, line + 10);
404 else if (!strncmp(line, "url = ", 6))
405 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, line + 6);
406 else if (!strncmp(line, "builddate = ", 12))
407 repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, strtoull(line + 12, 0, 10));
408 else if (!strncmp(line, "packager = ", 11))
409 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_PACKAGER, line + 11);
410 else if (!strncmp(line, "size = ", 7))
411 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(line + 7, 0, 10));
412 else if (!strncmp(line, "arch = ", 7))
413 s->arch = pool_str2id(pool, line + 7, 1);
414 else if (!strncmp(line, "license = ", 10))
415 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_LICENSE, line + 10);
416 else if (!strncmp(line, "replaces = ", 11))
417 s->obsoletes = adddep(repo, s->obsoletes, line + 11);
418 else if (!strncmp(line, "group = ", 8))
419 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_GROUP, line + 8);
420 else if (!strncmp(line, "depend = ", 9))
421 s->requires = adddep(repo, s->requires, line + 9);
422 else if (!strncmp(line, "optdepend = ", 12))
424 char *p = strchr(line, ':');
427 s->suggests = adddep(repo, s->suggests, line + 12);
429 else if (!strncmp(line, "conflict = ", 11))
430 s->conflicts = adddep(repo, s->conflicts, line + 11);
431 else if (!strncmp(line, "provides = ", 11))
432 s->provides = adddep(repo, s->provides, line + 11);
440 pool_error(pool, -1, "%s: not an arch package", fn);
442 solv_chksum_free(pkgidhandle, 0);
447 pool_error(pool, -1, "%s: package has no name", fn);
448 repo_free_solvable(repo, s - pool->solvables, 1);
457 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
458 if (!(flags & REPO_NO_LOCATION))
459 repodata_set_location(data, s - pool->solvables, 0, 0, fn);
460 if (S_ISREG(stb.st_mode))
461 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
464 unsigned char pkgid[16];
465 solv_chksum_free(pkgidhandle, pkgid);
466 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
471 solv_chksum_free(pkgidhandle, 0);
472 if (!(flags & REPO_NO_INTERNALIZE))
473 repodata_internalize(data);
474 return s ? s - pool->solvables : 0;
477 static char *getsentrynl(struct tarhead *th, char *s, int size)
480 if (!getsentry(th, s, size))
488 if (l && s[l - 1] == '\n')
493 while (getsentry(th, s, size))
496 if (!l || s[l - 1] == '\n')
504 joinhash_init(Repo *repo, Hashmask *hmp)
506 Hashmask hm = mkmask(repo->nsolvables);
507 Hashtable ht = solv_calloc(hm + 1, sizeof(*ht));
512 FOR_REPO_SOLVABLES(repo, i, s)
514 hh = HASHCHAIN_START;
517 h = HASHCHAIN_NEXT(h, hh, hm);
525 joinhash_lookup(Repo *repo, Hashtable ht, Hashmask hm, const char *fn)
531 if ((p = strrchr(fn, '/')) != 0)
533 /* here we assume that the dirname is name-evr */
536 for (p = fn + strlen(fn) - 1; p > fn; p--)
538 while (p > fn && *p != '-')
542 name = pool_strn2id(repo->pool, fn, p - fn, 0);
545 evr = pool_str2id(repo->pool, p + 1, 0);
548 /* found valid name/evr combination, check hash */
549 hh = HASHCHAIN_START;
553 Solvable *s = repo->pool->solvables + ht[h];
554 if (s->name == name && s->evr == evr)
556 h = HASHCHAIN_NEXT(h, hh, hm);
563 adddata(Repodata *data, Solvable *s, struct tarhead *th)
565 Repo *repo = data->repo;
566 Pool *pool = repo->pool;
571 while (getsentry(th, line, sizeof(line)))
574 if (l == 0 || line[l - 1] != '\n')
577 if (l <= 2 || line[0] != '%' || line[l - 1] != '%')
579 if (!strcmp(line, "%FILENAME%"))
581 if (getsentrynl(th, line, sizeof(line)))
582 repodata_set_location(data, s - pool->solvables, 0, 0, line);
584 else if (!strcmp(line, "%NAME%"))
586 if (getsentrynl(th, line, sizeof(line)))
587 s->name = pool_str2id(pool, line, 1);
589 else if (!strcmp(line, "%VERSION%"))
591 if (getsentrynl(th, line, sizeof(line)))
592 s->evr = pool_str2id(pool, line, 1);
594 else if (!strcmp(line, "%DESC%"))
596 if (getsentrynl(th, line, sizeof(line)))
598 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line);
599 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, line);
602 else if (!strcmp(line, "%GROUPS%"))
604 if (getsentrynl(th, line, sizeof(line)))
605 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_GROUP, line);
607 else if (!strcmp(line, "%CSIZE%"))
609 if (getsentrynl(th, line, sizeof(line)))
610 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, strtoull(line, 0, 10));
612 else if (!strcmp(line, "%ISIZE%"))
614 if (getsentrynl(th, line, sizeof(line)))
615 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(line, 0, 10));
617 else if (!strcmp(line, "%MD5SUM%"))
619 if (getsentrynl(th, line, sizeof(line)) && !havesha256)
620 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, REPOKEY_TYPE_MD5, line);
622 else if (!strcmp(line, "%SHA256SUM%"))
624 if (getsentrynl(th, line, sizeof(line)))
626 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, REPOKEY_TYPE_SHA256, line);
630 else if (!strcmp(line, "%URL%"))
632 if (getsentrynl(th, line, sizeof(line)))
633 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, line);
635 else if (!strcmp(line, "%LICENSE%"))
637 if (getsentrynl(th, line, sizeof(line)))
638 repodata_set_str(data, s - pool->solvables, SOLVABLE_LICENSE, line);
640 else if (!strcmp(line, "%ARCH%"))
642 if (getsentrynl(th, line, sizeof(line)))
643 s->arch = pool_str2id(pool, line, 1);
645 else if (!strcmp(line, "%BUILDDATE%"))
647 if (getsentrynl(th, line, sizeof(line)))
648 repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, strtoull(line, 0, 10));
650 else if (!strcmp(line, "%PACKAGER%"))
652 if (getsentrynl(th, line, sizeof(line)))
653 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_PACKAGER, line);
655 else if (!strcmp(line, "%REPLACES%"))
657 while (getsentrynl(th, line, sizeof(line)) && *line)
658 s->obsoletes = adddep(repo, s->obsoletes, line);
660 else if (!strcmp(line, "%DEPENDS%"))
662 while (getsentrynl(th, line, sizeof(line)) && *line)
663 s->requires = adddep(repo, s->requires, line);
665 else if (!strcmp(line, "%CONFLICTS%"))
667 while (getsentrynl(th, line, sizeof(line)) && *line)
668 s->conflicts = adddep(repo, s->conflicts, line);
670 else if (!strcmp(line, "%PROVIDES%"))
672 while (getsentrynl(th, line, sizeof(line)) && *line)
673 s->provides = adddep(repo, s->provides, line);
675 else if (!strcmp(line, "%OPTDEPENDS%"))
677 while (getsentrynl(th, line, sizeof(line)) && *line)
679 char *p = strchr(line, ':');
682 s->suggests = adddep(repo, s->suggests, line);
685 else if (!strcmp(line, "%FILES%"))
687 while (getsentrynl(th, line, sizeof(line)) && *line)
692 if (l > 1 && line[l - 1] == '/')
693 line[--l] = 0; /* remove trailing slashes */
694 if ((p = strrchr(line , '/')) != 0)
697 if (line[0] != '/') /* anchor */
700 memmove(line + 1, line, p - 1 - line);
703 id = repodata_str2dir(data, line, 1);
707 id = repodata_str2dir(data, line, 1);
715 id = repodata_str2dir(data, "/", 1);
716 repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, id, p);
720 getsentrynl(th, line, sizeof(line));
725 finishsolvable(Repo *repo, Solvable *s)
727 Pool *pool = repo->pool;
732 repo_free_solvable(repo, s - pool->solvables, 1);
739 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
743 repo_add_arch_repo(Repo *repo, FILE *fp, int flags)
745 Pool *pool = repo->pool;
751 Hashtable joinhash = 0;
752 Hashmask joinhashmask = 0;
754 data = repo_add_repodata(repo, flags);
756 if (flags & REPO_EXTEND_SOLVABLES)
757 joinhash = joinhash_init(repo, &joinhashmask);
759 inittarhead(&th, fp);
760 while (gettarhead(&th) > 0)
768 bn = strrchr(th.path, '/');
769 if (!bn || (strcmp(bn + 1, "desc") != 0 && strcmp(bn + 1, "depends") != 0 && strcmp(bn + 1, "files") != 0))
774 if ((flags & REPO_EXTEND_SOLVABLES) != 0 && (!strcmp(bn + 1, "desc") || !strcmp(bn + 1, "depends")))
777 continue; /* skip those when we're extending */
779 if (!lastdn || (bn - th.path) != lastdnlen || strncmp(lastdn, th.path, lastdnlen) != 0)
781 finishsolvable(repo, s);
783 lastdn = solv_strdup(th.path);
784 lastdnlen = bn - th.path;
785 lastdn[lastdnlen] = 0;
786 if (flags & REPO_EXTEND_SOLVABLES)
788 s = joinhash_lookup(repo, joinhash, joinhashmask, lastdn);
796 s = pool_id2solvable(pool, repo_add_solvable(repo));
798 adddata(data, s, &th);
800 finishsolvable(repo, s);
803 if (!(flags & REPO_NO_INTERNALIZE))
804 repodata_internalize(data);
809 repo_add_arch_local(Repo *repo, const char *dir, int flags)
811 Pool *pool = repo->pool;
815 char *entrydir, *file;
819 data = repo_add_repodata(repo, flags);
821 if (flags & REPO_USE_ROOTDIR)
822 dir = pool_prepend_rootdir(pool, dir);
826 while ((de = readdir(dp)) != 0)
828 if (!de->d_name[0] || de->d_name[0] == '.')
830 entrydir = solv_dupjoin(dir, "/", de->d_name);
831 file = pool_tmpjoin(repo->pool, entrydir, "/desc", 0);
833 if ((fp = fopen(file, "r")) != 0)
836 inittarhead(&th, fp);
837 s = pool_id2solvable(pool, repo_add_solvable(repo));
838 adddata(data, s, &th);
841 file = pool_tmpjoin(repo->pool, entrydir, "/files", 0);
842 if ((fp = fopen(file, "r")) != 0)
844 inittarhead(&th, fp);
845 adddata(data, s, &th);
854 if (!(flags & REPO_NO_INTERNALIZE))
855 repodata_internalize(data);
856 if (flags & REPO_USE_ROOTDIR)
857 solv_free((char *)dir);