Skip to content

Commit

Permalink
Factorize some code, so simplify it.
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed Sep 2, 2019
1 parent 2b68c72 commit 13f6366
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
24 changes: 12 additions & 12 deletions libpkg/pkg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,16 @@ pkg_open_fd(struct pkg **pkg_p, int fd, struct pkg_manifest_key *keys, int flags
return (EPKG_OK);
}

static int
pkg_parse_archive(struct pkg *pkg, struct pkg_manifest_key *keys,
struct archive *a, size_t len)
{
char buffer[len];

archive_read_data(a, buffer, len);
return (pkg_parse_manifest(pkg, buffer, len, keys));
}

int
pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae,
const char *path, struct pkg_manifest_key *keys, int flags, int fd)
Expand Down Expand Up @@ -1407,14 +1417,9 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae,
if (!manifest &&
(flags & PKG_OPEN_MANIFEST_COMPACT) &&
strcmp(fpath, "+COMPACT_MANIFEST") == 0) {
char *buffer;
manifest = true;

size_t len = archive_entry_size(*ae);
buffer = xmalloc(len);
archive_read_data(*a, buffer, archive_entry_size(*ae));
ret = pkg_parse_manifest(pkg, buffer, len, keys);
free(buffer);
ret = pkg_parse_archive(pkg, keys, *a, archive_entry_size(*ae));
if (ret != EPKG_OK) {
retcode = EPKG_FATAL;
goto cleanup;
Expand All @@ -1424,13 +1429,8 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae,
}
if (!manifest && strcmp(fpath, "+MANIFEST") == 0) {
manifest = true;
char *buffer;

size_t len = archive_entry_size(*ae);
buffer = xmalloc(len);
archive_read_data(*a, buffer, archive_entry_size(*ae));
ret = pkg_parse_manifest(pkg, buffer, len, keys);
free(buffer);
ret = pkg_parse_archive(pkg, keys, *a, archive_entry_size(*ae));
if (ret != EPKG_OK) {
if ((flags & PKG_OPEN_TRY) == 0)
pkg_emit_error("%s is not a valid package: "
Expand Down
37 changes: 21 additions & 16 deletions libpkg/pkg_repo.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,31 @@ struct pkg_extract_cbdata {
bool need_sig;
};

static int
pkg_repo_write_sig_from_archive(struct archive *a, int fd, size_t siglen)
{
char sig[siglen];

if (archive_read_data(a, sig, siglen) == -1) {
pkg_emit_errno("pkg_repo_meta_extract_signature",
"archive_read_data failed");
return (EPKG_FATAL);
}
if (write(fd, sig, siglen) == -1) {
pkg_emit_errno("pkg_repo_meta_extract_signature",
"write failed");
return (EPKG_FATAL);
}
return (EPKG_OK);
}

static int
pkg_repo_meta_extract_signature_pubkey(int fd, void *ud)
{
struct archive *a = NULL;
struct archive_entry *ae = NULL;
struct pkg_extract_cbdata *cb = ud;
int siglen;
void *sig;
int rc = EPKG_FATAL;

pkg_debug(1, "PkgRepo: extracting signature of repo in a sandbox");
Expand All @@ -247,21 +264,9 @@ pkg_repo_meta_extract_signature_pubkey(int fd, void *ud)
while (archive_read_next_header(a, &ae) == ARCHIVE_OK) {
if (cb->need_sig && strcmp(archive_entry_pathname(ae), "signature") == 0) {
siglen = archive_entry_size(ae);
sig = xmalloc(siglen);
if (archive_read_data(a, sig, siglen) == -1) {
pkg_emit_errno("pkg_repo_meta_extract_signature",
"archive_read_data failed");
free(sig);
return (EPKG_FATAL);
}
if (write(fd, sig, siglen) == -1) {
pkg_emit_errno("pkg_repo_meta_extract_signature",
"write failed");
free(sig);
return (EPKG_FATAL);
}
free(sig);
rc = EPKG_OK;
rc = pkg_repo_write_sig_from_archive(a, fd, siglen);
if (rc != EPKG_OK)
break;
}
else if (strcmp(archive_entry_pathname(ae), cb->fname) == 0) {
if (archive_read_data_into_fd(a, cb->tfd) != 0) {
Expand Down

0 comments on commit 13f6366

Please sign in to comment.