2 * Copyright (c) 2012, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
21 #include "solv_xfopen.h"
22 #include "repo_arch.h"
24 static long long parsenum(unsigned char *p, int cnt)
32 x = *p & 0x40 ? (-1 << 8 | *p) : (*p ^ 0x80);
37 while (cnt > 0 && (*p == ' ' || *p == '\t'))
41 for (; cnt > 0 && *p >= '0' && *p < '8'; cnt--, p++)
42 x = (x << 3) | (*p - '0');
46 static int readblock(FILE *fp, unsigned char *blk)
51 r = fread(blk + l, 1, 512 - l, fp);
61 unsigned char blk[512];
71 static char *getsentry(struct tarhead *th, char *s, int size)
74 if (th->eof || size <= 1)
76 size--; /* terminating 0 */
80 for (i = th->off; i < th->end; i++)
84 if (!size || th->blk[i] == '\n')
94 if (readblock(th->fp, th->blk))
100 th->end = th->length > 512 ? 512 : th->length;
101 th->length -= th->end;
105 static void skipentry(struct tarhead *th)
107 for (; th->length > 0; th->length -= 512)
109 if (readblock(th->fp, th->blk))
117 th->off = th->end = 0;
120 static void inittarhead(struct tarhead *th, FILE *fp)
122 memset(th, 0, sizeof(*th));
126 static void freetarhead(struct tarhead *th)
131 static int gettarhead(struct tarhead *th)
136 th->path = solv_free(th->path);
146 int r = readblock(th->fp, th->blk);
161 length = parsenum(th->blk + 124, 12);
165 switch (th->blk[156])
171 /* hard link, special length magic... */
178 case '2': case '3': case '4': case '6':
181 case 'X': case 'x': case 'L':
184 if (length < 1 || length >= 1024 * 1024)
187 data = pp = solv_malloc(l + 512);
188 for (; l > 0; l -= 512, pp += 512)
189 if (readblock(th->fp, (unsigned char *)pp))
194 type = 3; /* extension */
195 if (th->blk[156] == 'L')
207 for (l = 0; l < length && pp[l] >= '0' && pp[l] <= '9'; l++)
208 ll = ll * 10 + (pp[l] - '0');
209 if (l == length || pp[l] != ' ' || ll < 1 || ll > length || pp[ll - 1] != '\n')
218 if (!strncmp(pp, "path=", 5))
221 th->path = solv_strdup(pp + 5);
231 type = 3; /* extension */
234 if ((type == 1 || type == 2) && !th->path)
237 memcpy(path, th->blk, 156);
239 if (!memcmp(th->blk + 257, "ustar\0\060\060", 8) && !th->path && th->blk[345])
241 /* POSIX ustar with prefix */
243 memcpy(prefix, th->blk + 345, 155);
246 if (prefix[l - 1] == '/')
248 th->path = solv_dupjoin(prefix, "/", path);
251 th->path = solv_dupjoin(path, 0, 0);
253 if (type == 1 || type == 2)
255 l = strlen(th->path);
256 if (l && th->path[l - 1] == '/')
267 r = readblock(th->fp, th->blk);
279 adddep(Repo *repo, Offset olddeps, char *line)
281 Pool *pool = repo->pool;
285 while (*line == ' ' && *line == '\t')
288 while (*p && *p != ' ' && *p != '\t' && *p != '<' && *p != '=' && *p != '>')
290 id = pool_strn2id(pool, line, p - line, 1);
291 while (*p == ' ' && *p == '\t')
293 if (*p == '<' || *p == '=' || *p == '>')
307 while (*p == ' ' && *p == '\t')
310 while (*p && *p != ' ' && *p != '\t')
312 id = pool_rel2id(pool, id, pool_strn2id(pool, line, p - line, 1), flags, 1);
314 return repo_addid_dep(repo, olddeps, id, 0);
318 repo_add_arch_pkg(Repo *repo, const char *fn, int flags)
320 Pool *pool = repo->pool;
329 void *pkgidhandle = 0;
331 data = repo_add_repodata(repo, flags);
332 if ((fd = open(fn, O_RDONLY, 0)) < 0)
334 pool_debug(pool, SOLV_ERROR, "repo_add_arch_pkg: %s: %s\n", fn, strerror(errno));
339 pool_debug(pool, SOLV_ERROR, "repo_add_arch_pkg: %s: fstat failed\n", fn);
343 if (!(fp = solv_xfopen_fd(fn, fd, "r")))
345 pool_debug(pool, SOLV_ERROR, "repo_add_arch_pkg: %s: fdopen failed\n", fn);
350 inittarhead(&th, fp);
351 while (gettarhead(&th) > 0)
353 if (th.type != 1 || strcmp(th.path, ".PKGINFO") != 0)
359 s = pool_id2solvable(pool, repo_add_solvable(repo));
360 if (flags & ARCH_ADD_WITH_PKGID)
361 pkgidhandle = solv_chksum_create(REPOKEY_TYPE_MD5);
362 while (getsentry(&th, line, sizeof(line)))
368 solv_chksum_add(pkgidhandle, line, l);
369 if (line[l - 1] != '\n')
380 if (l == 0 || line[0] == '#')
382 if (!strncmp(line, "pkgname = ", 10))
383 s->name = pool_str2id(pool, line + 10, 1);
384 else if (!strncmp(line, "pkgver = ", 9))
385 s->evr = pool_str2id(pool, line + 9, 1);
386 else if (!strncmp(line, "pkgdesc = ", 10))
388 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line + 10);
389 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, line + 10);
391 else if (!strncmp(line, "url = ", 6))
392 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, line + 6);
393 else if (!strncmp(line, "builddate = ", 12))
394 repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, strtoull(line + 12, 0, 10));
395 else if (!strncmp(line, "packager = ", 11))
396 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_PACKAGER, line + 11);
397 else if (!strncmp(line, "size = ", 7))
398 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(line + 7, 0, 10));
399 else if (!strncmp(line, "arch = ", 7))
400 s->arch = pool_str2id(pool, line + 7, 1);
401 else if (!strncmp(line, "license = ", 10))
402 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_LICENSE, line + 10);
403 else if (!strncmp(line, "replaces = ", 11))
404 s->obsoletes = adddep(repo, s->obsoletes, line + 11);
405 else if (!strncmp(line, "group = ", 8))
406 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_GROUP, line + 8);
407 else if (!strncmp(line, "depend = ", 9))
408 s->requires = adddep(repo, s->requires, line + 9);
409 else if (!strncmp(line, "optdepend = ", 12))
411 char *p = strchr(line, ':');
414 s->suggests = adddep(repo, s->suggests, line + 12);
416 else if (!strncmp(line, "conflict = ", 11))
417 s->conflicts = adddep(repo, s->conflicts, line + 11);
418 else if (!strncmp(line, "provides = ", 11))
419 s->provides = adddep(repo, s->provides, line + 11);
427 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
433 s->arch = ARCH_NOARCH;
436 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
437 repodata_set_location(data, s - pool->solvables, 0, 0, fn);
438 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
441 unsigned char pkgid[16];
442 solv_chksum_free(pkgidhandle, pkgid);
443 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
448 solv_chksum_free(pkgidhandle, 0);
449 if (!(flags & REPO_NO_INTERNALIZE))
450 repodata_internalize(data);
451 return s ? s - pool->solvables : 0;
454 static char *getsentrynl(struct tarhead *th, char *s, int size)
457 if (!getsentry(th, s, size))
465 if (l && s[l - 1] == '\n')
470 while (getsentry(th, s, size))
473 if (!l || s[l - 1] == '\n')
481 joinhash_init(Repo *repo, Hashmask *hmp)
483 Hashmask hm = mkmask(repo->nsolvables);
484 Hashtable ht = solv_calloc(hm + 1, sizeof(*ht));
489 FOR_REPO_SOLVABLES(repo, i, s)
491 hh = HASHCHAIN_START;
494 h = HASHCHAIN_NEXT(h, hh, hm);
502 joinhash_lookup(Repo *repo, Hashtable ht, Hashmask hm, const char *fn)
508 if ((p = strrchr(fn, '/')) != 0)
510 /* here we assume that the dirname is name-evr */
513 for (p = fn + strlen(fn) - 1; p > fn; p--)
515 while (p > fn && *p != '-')
519 name = pool_strn2id(repo->pool, fn, p - fn, 0);
522 evr = pool_str2id(repo->pool, p + 1, 0);
525 /* found valid name/evr combination, check hash */
526 hh = HASHCHAIN_START;
530 Solvable *s = repo->pool->solvables + ht[h];
531 if (s->name == name && s->evr == evr)
533 h = HASHCHAIN_NEXT(h, hh, hm);
540 repo_add_arch_repo(Repo *repo, FILE *fp, int flags)
542 Pool *pool = repo->pool;
544 data = repo_add_repodata(repo, flags);
547 int l, lastdnlen = 0;
551 Hashtable joinhash = 0;
552 Hashmask joinhashmask = 0;
554 if (flags & REPO_EXTEND_SOLVABLES)
555 joinhash = joinhash_init(repo, &joinhashmask);
557 inittarhead(&th, fp);
558 while (gettarhead(&th) > 0)
566 bn = strrchr(th.path, '/');
567 if (!bn || (strcmp(bn + 1, "desc") != 0 && strcmp(bn + 1, "depends") != 0 && strcmp(bn + 1, "files") != 0))
572 if ((flags & REPO_EXTEND_SOLVABLES) != 0 && (!strcmp(bn + 1, "desc") || !strcmp(bn + 1, "depends")))
575 continue; /* skip those when we're extending */
577 if (!lastdn || (bn - th.path) != lastdnlen || strncmp(lastdn, th.path, lastdnlen) != 0)
583 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
589 s->arch = ARCH_NOARCH;
592 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
596 lastdn = solv_strdup(th.path);
597 lastdnlen = bn - th.path;
598 lastdn[lastdnlen] = 0;
599 if (flags & REPO_EXTEND_SOLVABLES)
601 s = joinhash_lookup(repo, joinhash, joinhashmask, lastdn);
609 s = pool_id2solvable(pool, repo_add_solvable(repo));
612 while (getsentry(&th, line, sizeof(line)))
615 if (l == 0 || line[l - 1] != '\n')
618 if (l <= 2 || line[0] != '%' || line[l - 1] != '%')
620 if (!strcmp(line, "%FILENAME%"))
622 if (getsentrynl(&th, line, sizeof(line)))
623 repodata_set_location(data, s - pool->solvables, 0, 0, line);
625 else if (!strcmp(line, "%NAME%"))
627 if (getsentrynl(&th, line, sizeof(line)))
628 s->name = pool_str2id(pool, line, 1);
630 else if (!strcmp(line, "%VERSION%"))
632 if (getsentrynl(&th, line, sizeof(line)))
633 s->evr = pool_str2id(pool, line, 1);
635 else if (!strcmp(line, "%DESC%"))
637 if (getsentrynl(&th, line, sizeof(line)))
639 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line);
640 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, line);
643 else if (!strcmp(line, "%GROUPS%"))
645 if (getsentrynl(&th, line, sizeof(line)))
646 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_GROUP, line);
648 else if (!strcmp(line, "%CSIZE%"))
650 if (getsentrynl(&th, line, sizeof(line)))
651 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, strtoull(line, 0, 10));
653 else if (!strcmp(line, "%ISIZE%"))
655 if (getsentrynl(&th, line, sizeof(line)))
656 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(line, 0, 10));
658 else if (!strcmp(line, "%MD5SUM%"))
660 if (getsentrynl(&th, line, sizeof(line)) && !havesha256)
661 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, REPOKEY_TYPE_MD5, line);
663 else if (!strcmp(line, "%SHA256SUM%"))
665 if (getsentrynl(&th, line, sizeof(line)))
667 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, REPOKEY_TYPE_SHA256, line);
671 else if (!strcmp(line, "%URL%"))
673 if (getsentrynl(&th, line, sizeof(line)))
674 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, line);
676 else if (!strcmp(line, "%LICENSE%"))
678 if (getsentrynl(&th, line, sizeof(line)))
679 repodata_set_str(data, s - pool->solvables, SOLVABLE_LICENSE, line);
681 else if (!strcmp(line, "%ARCH%"))
683 if (getsentrynl(&th, line, sizeof(line)))
684 s->arch = pool_str2id(pool, line, 1);
686 else if (!strcmp(line, "%BUILDDATE%"))
688 if (getsentrynl(&th, line, sizeof(line)))
689 repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, strtoull(line, 0, 10));
691 else if (!strcmp(line, "%PACKAGER%"))
693 if (getsentrynl(&th, line, sizeof(line)))
694 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_PACKAGER, line);
696 else if (!strcmp(line, "%REPLACES%"))
698 while (getsentrynl(&th, line, sizeof(line)) && *line)
699 s->obsoletes = adddep(repo, s->obsoletes, line);
701 else if (!strcmp(line, "%DEPENDS%"))
703 while (getsentrynl(&th, line, sizeof(line)) && *line)
704 s->requires = adddep(repo, s->requires, line);
706 else if (!strcmp(line, "%CONFLICTS%"))
708 while (getsentrynl(&th, line, sizeof(line)) && *line)
709 s->conflicts = adddep(repo, s->conflicts, line);
711 else if (!strcmp(line, "%PROVIDES%"))
713 while (getsentrynl(&th, line, sizeof(line)) && *line)
714 s->provides = adddep(repo, s->provides, line);
716 else if (!strcmp(line, "%OPTDEPENDS%"))
718 while (getsentrynl(&th, line, sizeof(line)) && *line)
720 char *p = strchr(line, ':');
723 s->suggests = adddep(repo, s->suggests, line);
726 else if (!strcmp(line, "%FILES%"))
728 while (getsentrynl(&th, line, sizeof(line)) && *line)
733 if (l > 1 && line[l - 1] == '/')
734 line[--l] = 0; /* remove trailing slashes */
735 if ((p = strrchr(line , '/')) != 0)
738 if (line[0] != '/') /* anchor */
741 memmove(line + 1, line, p - 1 - line);
744 id = repodata_str2dir(data, line, 1);
748 id = repodata_str2dir(data, line, 1);
756 id = repodata_str2dir(data, "/", 1);
757 repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, id, p);
761 getsentrynl(&th, line, sizeof(line));
768 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
774 s->arch = ARCH_NOARCH;
777 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
782 if (!(flags & REPO_NO_INTERNALIZE))
783 repodata_internalize(data);