From 24ff0472dd7015b68c4b9f0bfdb8ee6bc2a8a768 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Thu, 29 Aug 2013 14:30:14 +0200 Subject: [PATCH] Use fs blksize from stat(2)ed file when reading archives. --- bin/xbps-create/main.c | 7 ++++++- bin/xbps-rindex/repoflush.c | 2 +- include/xbps_api_impl.h | 2 -- lib/package_unpack.c | 34 ++++++++++++++++++++++++---------- lib/plist_fetch.c | 7 +++---- lib/repo.c | 17 ++++++++++++++--- 6 files changed, 48 insertions(+), 21 deletions(-) diff --git a/bin/xbps-create/main.c b/bin/xbps-create/main.c index 557cae62..29169ee5 100644 --- a/bin/xbps-create/main.c +++ b/bin/xbps-create/main.c @@ -50,6 +50,11 @@ #define _PROGNAME "xbps-create" +/* libarchive 2.x compat */ +#if ARCHIVE_VERSION_NUMBER >= 3000000 +# define archive_write_finish(x) archive_write_free(x) +#endif + struct xentry { TAILQ_ENTRY(xentry) entries; char *file, *type, *target, *hash; @@ -781,7 +786,7 @@ main(int argc, char **argv) } archive_entry_linkresolver_free(resolver); /* close and free archive */ - archive_write_free(ar); + archive_write_finish(ar); /* * Archive was created successfully; flush data to storage, diff --git a/bin/xbps-rindex/repoflush.c b/bin/xbps-rindex/repoflush.c index 7d243611..6067f6ba 100644 --- a/bin/xbps-rindex/repoflush.c +++ b/bin/xbps-rindex/repoflush.c @@ -78,7 +78,7 @@ repodata_flush(struct xbps_handle *xhp, const char *repodir, } free(xml); - archive_write_free(ar); + archive_write_finish(ar); /* Write data to tempfile and rename */ fdatasync(repofd); diff --git a/include/xbps_api_impl.h b/include/xbps_api_impl.h index 3eccb87b..c62f5a2c 100644 --- a/include/xbps_api_impl.h +++ b/include/xbps_api_impl.h @@ -50,8 +50,6 @@ #include "queue.h" #include "fetch.h" -#define ARCHIVE_READ_BLOCKSIZE 10240 - #define EXTRACT_FLAGS ARCHIVE_EXTRACT_SECURE_NODOTDOT | \ ARCHIVE_EXTRACT_SECURE_SYMLINKS #define FEXTRACT_FLAGS ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM | \ diff --git a/lib/package_unpack.c b/lib/package_unpack.c index 34a20544..13b60f20 100644 --- a/lib/package_unpack.c +++ b/lib/package_unpack.c @@ -626,9 +626,10 @@ int HIDDEN xbps_unpack_binary_pkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod) { struct archive *ar = NULL; + struct stat st; const char *pkgver; - char *bpkg; - int pkg_fd, rv = 0; + char *bpkg = NULL; + int pkg_fd = -1, rv = 0; assert(xbps_object_type(pkg_repod) == XBPS_TYPE_DICTIONARY); @@ -658,17 +659,29 @@ xbps_unpack_binary_pkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod) pkg_fd = open(bpkg, O_RDONLY|O_CLOEXEC); if (pkg_fd == -1) { - rv = archive_errno(ar); + rv = errno; xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, rv, pkgver, "%s: [unpack] failed to open binary package `%s': %s", pkgver, bpkg, strerror(rv)); - free(bpkg); - archive_read_free(ar); - return rv; + goto out; + } + if (fstat(pkg_fd, &st) == -1) { + rv = errno; + xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, + rv, pkgver, + "%s: [unpack] failed to fstat binary package `%s': %s", + pkgver, bpkg, strerror(rv)); + goto out; + } + if (archive_read_open_fd(ar, pkg_fd, st.st_blksize) == ARCHIVE_FATAL) { + rv = archive_errno(ar); + xbps_set_cb_state(xhp, XBPS_STATE_UNPACK_FAIL, + rv, pkgver, + "%s: [unpack] failed to read binary package `%s': %s", + pkgver, bpkg, strerror(rv)); + goto out; } - archive_read_open_fd(ar, pkg_fd, ARCHIVE_READ_BLOCKSIZE); - /* * Extract archive files. */ @@ -690,9 +703,10 @@ xbps_unpack_binary_pkg(struct xbps_handle *xhp, xbps_dictionary_t pkg_repod) pkgver, strerror(rv)); } out: - close(pkg_fd); + if (pkg_fd != -1) + close(pkg_fd); if (ar) - archive_read_free(ar); + archive_read_finish(ar); if (bpkg) free(bpkg); diff --git a/lib/plist_fetch.c b/lib/plist_fetch.c index 7df8fa13..d87f5fa6 100644 --- a/lib/plist_fetch.c +++ b/lib/plist_fetch.c @@ -123,9 +123,8 @@ open_archive(const char *url) archive_read_support_compression_xz(a); archive_read_support_format_tar(a); - if (archive_read_open_filename(a, url, - ARCHIVE_READ_BLOCKSIZE)) { - archive_read_close(a); + if (archive_read_open_filename(a, url, 32768)) { + archive_read_finish(a); return NULL; } return a; @@ -185,7 +184,7 @@ xbps_get_pkg_plist_from_binpkg(const char *fname, const char *plistf) break; } - archive_read_free(a); + archive_read_finish(a); return plistd; } diff --git a/lib/repo.c b/lib/repo.c index babdec95..c031b782 100644 --- a/lib/repo.c +++ b/lib/repo.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "xbps_api_impl.h" @@ -50,6 +51,7 @@ struct xbps_repo * xbps_repo_open(struct xbps_handle *xhp, const char *url) { struct xbps_repo *repo; + struct stat st; const char *arch; char *repofile; @@ -85,13 +87,22 @@ xbps_repo_open(struct xbps_handle *xhp, const char *url) archive_read_support_compression_xz(repo->ar); archive_read_support_format_tar(repo->ar); - if (archive_read_open_filename(repo->ar, repofile, ARCHIVE_READ_BLOCKSIZE)) { + if (stat(repofile, &st) == -1) { + xbps_dbg_printf(xhp, "[repo] cannot stat repository file %s: %s\n", + repofile, strerror(errno)); + archive_read_finish(repo->ar); + free(repo); + repo = NULL; + goto out; + } + if (archive_read_open_filename(repo->ar, repofile, st.st_blksize) == ARCHIVE_FATAL) { xbps_dbg_printf(xhp, "[repo] cannot open repository file %s: %s\n", repofile, strerror(archive_errno(repo->ar))); - archive_read_free(repo->ar); + archive_read_finish(repo->ar); free(repo); repo = NULL; } +out: free(repofile); return repo; } @@ -144,7 +155,7 @@ xbps_repo_close(struct xbps_repo *repo) if (repo->ar == NULL) return; - archive_read_free(repo->ar); + archive_read_finish(repo->ar); if (xbps_object_type(repo->idx) == XBPS_TYPE_DICTIONARY) xbps_object_release(repo->idx); if (xbps_object_type(repo->idxfiles) == XBPS_TYPE_DICTIONARY)