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 (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(fn, O_RDONLY, 0)) < 0)
347 pool_debug(pool, SOLV_ERROR, "repo_add_arch_pkg: %s: %s\n", fn, strerror(errno));
352 pool_debug(pool, SOLV_ERROR, "repo_add_arch_pkg: %s: fstat failed\n", fn);
356 if (!(fp = solv_xfopen_fd(fn, fd, "r")))
358 pool_debug(pool, SOLV_ERROR, "repo_add_arch_pkg: %s: fdopen failed\n", 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 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
449 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
450 repodata_set_location(data, s - pool->solvables, 0, 0, fn);
451 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
454 unsigned char pkgid[16];
455 solv_chksum_free(pkgidhandle, pkgid);
456 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
461 solv_chksum_free(pkgidhandle, 0);
462 if (!(flags & REPO_NO_INTERNALIZE))
463 repodata_internalize(data);
464 return s ? s - pool->solvables : 0;
467 static char *getsentrynl(struct tarhead *th, char *s, int size)
470 if (!getsentry(th, s, size))
478 if (l && s[l - 1] == '\n')
483 while (getsentry(th, s, size))
486 if (!l || s[l - 1] == '\n')
494 joinhash_init(Repo *repo, Hashmask *hmp)
496 Hashmask hm = mkmask(repo->nsolvables);
497 Hashtable ht = solv_calloc(hm + 1, sizeof(*ht));
502 FOR_REPO_SOLVABLES(repo, i, s)
504 hh = HASHCHAIN_START;
507 h = HASHCHAIN_NEXT(h, hh, hm);
515 joinhash_lookup(Repo *repo, Hashtable ht, Hashmask hm, const char *fn)
521 if ((p = strrchr(fn, '/')) != 0)
523 /* here we assume that the dirname is name-evr */
526 for (p = fn + strlen(fn) - 1; p > fn; p--)
528 while (p > fn && *p != '-')
532 name = pool_strn2id(repo->pool, fn, p - fn, 0);
535 evr = pool_str2id(repo->pool, p + 1, 0);
538 /* found valid name/evr combination, check hash */
539 hh = HASHCHAIN_START;
543 Solvable *s = repo->pool->solvables + ht[h];
544 if (s->name == name && s->evr == evr)
546 h = HASHCHAIN_NEXT(h, hh, hm);
553 adddata(Repodata *data, Solvable *s, struct tarhead *th)
555 Repo *repo = data->repo;
556 Pool *pool = repo->pool;
561 while (getsentry(th, line, sizeof(line)))
564 if (l == 0 || line[l - 1] != '\n')
567 if (l <= 2 || line[0] != '%' || line[l - 1] != '%')
569 if (!strcmp(line, "%FILENAME%"))
571 if (getsentrynl(th, line, sizeof(line)))
572 repodata_set_location(data, s - pool->solvables, 0, 0, line);
574 else if (!strcmp(line, "%NAME%"))
576 if (getsentrynl(th, line, sizeof(line)))
577 s->name = pool_str2id(pool, line, 1);
579 else if (!strcmp(line, "%VERSION%"))
581 if (getsentrynl(th, line, sizeof(line)))
582 s->evr = pool_str2id(pool, line, 1);
584 else if (!strcmp(line, "%DESC%"))
586 if (getsentrynl(th, line, sizeof(line)))
588 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line);
589 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, line);
592 else if (!strcmp(line, "%GROUPS%"))
594 if (getsentrynl(th, line, sizeof(line)))
595 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_GROUP, line);
597 else if (!strcmp(line, "%CSIZE%"))
599 if (getsentrynl(th, line, sizeof(line)))
600 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, strtoull(line, 0, 10));
602 else if (!strcmp(line, "%ISIZE%"))
604 if (getsentrynl(th, line, sizeof(line)))
605 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(line, 0, 10));
607 else if (!strcmp(line, "%MD5SUM%"))
609 if (getsentrynl(th, line, sizeof(line)) && !havesha256)
610 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, REPOKEY_TYPE_MD5, line);
612 else if (!strcmp(line, "%SHA256SUM%"))
614 if (getsentrynl(th, line, sizeof(line)))
616 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, REPOKEY_TYPE_SHA256, line);
620 else if (!strcmp(line, "%URL%"))
622 if (getsentrynl(th, line, sizeof(line)))
623 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, line);
625 else if (!strcmp(line, "%LICENSE%"))
627 if (getsentrynl(th, line, sizeof(line)))
628 repodata_set_str(data, s - pool->solvables, SOLVABLE_LICENSE, line);
630 else if (!strcmp(line, "%ARCH%"))
632 if (getsentrynl(th, line, sizeof(line)))
633 s->arch = pool_str2id(pool, line, 1);
635 else if (!strcmp(line, "%BUILDDATE%"))
637 if (getsentrynl(th, line, sizeof(line)))
638 repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, strtoull(line, 0, 10));
640 else if (!strcmp(line, "%PACKAGER%"))
642 if (getsentrynl(th, line, sizeof(line)))
643 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_PACKAGER, line);
645 else if (!strcmp(line, "%REPLACES%"))
647 while (getsentrynl(th, line, sizeof(line)) && *line)
648 s->obsoletes = adddep(repo, s->obsoletes, line);
650 else if (!strcmp(line, "%DEPENDS%"))
652 while (getsentrynl(th, line, sizeof(line)) && *line)
653 s->requires = adddep(repo, s->requires, line);
655 else if (!strcmp(line, "%CONFLICTS%"))
657 while (getsentrynl(th, line, sizeof(line)) && *line)
658 s->conflicts = adddep(repo, s->conflicts, line);
660 else if (!strcmp(line, "%PROVIDES%"))
662 while (getsentrynl(th, line, sizeof(line)) && *line)
663 s->provides = adddep(repo, s->provides, line);
665 else if (!strcmp(line, "%OPTDEPENDS%"))
667 while (getsentrynl(th, line, sizeof(line)) && *line)
669 char *p = strchr(line, ':');
672 s->suggests = adddep(repo, s->suggests, line);
675 else if (!strcmp(line, "%FILES%"))
677 while (getsentrynl(th, line, sizeof(line)) && *line)
682 if (l > 1 && line[l - 1] == '/')
683 line[--l] = 0; /* remove trailing slashes */
684 if ((p = strrchr(line , '/')) != 0)
687 if (line[0] != '/') /* anchor */
690 memmove(line + 1, line, p - 1 - line);
693 id = repodata_str2dir(data, line, 1);
697 id = repodata_str2dir(data, line, 1);
705 id = repodata_str2dir(data, "/", 1);
706 repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, id, p);
710 getsentrynl(th, line, sizeof(line));
715 finishsolvable(Repo *repo, Solvable *s)
717 Pool *pool = repo->pool;
722 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
729 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
733 repo_add_arch_repo(Repo *repo, FILE *fp, int flags)
735 Pool *pool = repo->pool;
741 Hashtable joinhash = 0;
742 Hashmask joinhashmask = 0;
744 data = repo_add_repodata(repo, flags);
746 if (flags & REPO_EXTEND_SOLVABLES)
747 joinhash = joinhash_init(repo, &joinhashmask);
749 inittarhead(&th, fp);
750 while (gettarhead(&th) > 0)
758 bn = strrchr(th.path, '/');
759 if (!bn || (strcmp(bn + 1, "desc") != 0 && strcmp(bn + 1, "depends") != 0 && strcmp(bn + 1, "files") != 0))
764 if ((flags & REPO_EXTEND_SOLVABLES) != 0 && (!strcmp(bn + 1, "desc") || !strcmp(bn + 1, "depends")))
767 continue; /* skip those when we're extending */
769 if (!lastdn || (bn - th.path) != lastdnlen || strncmp(lastdn, th.path, lastdnlen) != 0)
771 finishsolvable(repo, s);
773 lastdn = solv_strdup(th.path);
774 lastdnlen = bn - th.path;
775 lastdn[lastdnlen] = 0;
776 if (flags & REPO_EXTEND_SOLVABLES)
778 s = joinhash_lookup(repo, joinhash, joinhashmask, lastdn);
786 s = pool_id2solvable(pool, repo_add_solvable(repo));
788 adddata(data, s, &th);
790 finishsolvable(repo, s);
793 if (!(flags & REPO_NO_INTERNALIZE))
794 repodata_internalize(data);
799 repo_add_arch_local(Repo *repo, const char *dir, int flags)
801 Pool *pool = repo->pool;
805 char *entrydir, *file;
809 data = repo_add_repodata(repo, flags);
814 while ((de = readdir(dp)) != 0)
816 if (!de->d_name[0] || de->d_name[0] == '.')
818 entrydir = solv_dupjoin(dir, "/", de->d_name);
819 file = pool_tmpjoin(repo->pool, entrydir, "/desc", 0);
821 if ((fp = fopen(file, "r")) != 0)
824 inittarhead(&th, fp);
825 s = pool_id2solvable(pool, repo_add_solvable(repo));
826 adddata(data, s, &th);
829 file = pool_tmpjoin(repo->pool, entrydir, "/files", 0);
830 if ((fp = fopen(file, "r")) != 0)
832 inittarhead(&th, fp);
833 adddata(data, s, &th);
842 if (!(flags & REPO_NO_INTERNALIZE))
843 repodata_internalize(data);