summaryrefslogtreecommitdiff
path: root/lib/libalpm/be_package.c
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-11-13 23:45:55 -0600
committerDan McGee <dan@archlinux.org>2011-11-16 14:51:06 -0600
commitdee8144ce5ca96ceca643d74fd6152ecf812f8af (patch)
tree9206036726557a195046ccb5bcf9eb796c6d47ae /lib/libalpm/be_package.c
parentf1ec3b9b10d1657412d6592180c56887c3a6ae20 (diff)
downloadpacman-dee8144ce5ca96ceca643d74fd6152ecf812f8af.tar.xz
Add helper method for creating and opening archive object
This moves the common setup code of about 5 different callers into one method. Error messages will now be common and shared in all places; several paths did not have any messages at all before. In addition, we now pick an ideal block size for the archive read based off the larger value of our default buffer size or the st.st_blksize field. For a filesystem such as NFS, this is often much larger than the default 8192- values such as 32768 and 131072 are common. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'lib/libalpm/be_package.c')
-rw-r--r--lib/libalpm/be_package.c70
1 files changed, 35 insertions, 35 deletions
diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index 90a19722..4f530e0b 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -40,6 +40,11 @@
#include "package.h"
#include "deps.h" /* _alpm_splitdep */
+struct package_changelog {
+ struct archive *archive;
+ int fd;
+};
+
/**
* Open a package changelog for reading. Similar to fopen in functionality,
* except that the returned 'file stream' is from an archive.
@@ -50,31 +55,38 @@ static void *_package_changelog_open(alpm_pkg_t *pkg)
{
ASSERT(pkg != NULL, return NULL);
- struct archive *archive = NULL;
+ struct package_changelog *changelog;
+ struct archive *archive;
struct archive_entry *entry;
const char *pkgfile = pkg->origin_data.file;
+ struct stat buf;
+ int fd;
- if((archive = archive_read_new()) == NULL) {
- RET_ERR(pkg->handle, ALPM_ERR_LIBARCHIVE, NULL);
- }
-
- archive_read_support_compression_all(archive);
- archive_read_support_format_all(archive);
-
- if(archive_read_open_filename(archive, pkgfile,
- ALPM_BUFFER_SIZE) != ARCHIVE_OK) {
- RET_ERR(pkg->handle, ALPM_ERR_PKG_OPEN, NULL);
+ fd = _alpm_open_archive(pkg->handle, pkgfile, &buf,
+ &archive, ALPM_ERR_PKG_OPEN);
+ if(fd < 0) {
+ return NULL;
}
while(archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
const char *entry_name = archive_entry_pathname(entry);
if(strcmp(entry_name, ".CHANGELOG") == 0) {
- return archive;
+ changelog = malloc(sizeof(struct package_changelog));
+ if(!changelog) {
+ pkg->handle->pm_errno = ALPM_ERR_MEMORY;
+ archive_read_finish(archive);
+ CLOSE(fd);
+ return NULL;
+ }
+ changelog->archive = archive;
+ changelog->fd = fd;
+ return changelog;
}
}
/* we didn't find a changelog */
archive_read_finish(archive);
+ CLOSE(fd);
errno = ENOENT;
return NULL;
@@ -92,7 +104,8 @@ static void *_package_changelog_open(alpm_pkg_t *pkg)
static size_t _package_changelog_read(void *ptr, size_t size,
const alpm_pkg_t UNUSED *pkg, void *fp)
{
- ssize_t sret = archive_read_data((struct archive *)fp, ptr, size);
+ struct package_changelog *changelog = fp;
+ ssize_t sret = archive_read_data(changelog->archive, ptr, size);
/* Report error (negative values) */
if(sret < 0) {
RET_ERR(pkg->handle, ALPM_ERR_LIBARCHIVE, 0);
@@ -110,7 +123,12 @@ static size_t _package_changelog_read(void *ptr, size_t size,
*/
static int _package_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
{
- return archive_read_finish((struct archive *)fp);
+ int ret;
+ struct package_changelog *changelog = fp;
+ ret = archive_read_finish(changelog->archive);
+ CLOSE(changelog->fd);
+ free(changelog);
+ return ret;
}
/** Package file operations struct accessor. We implement this as a method
@@ -370,24 +388,10 @@ alpm_pkg_t *_alpm_pkg_load_internal(alpm_handle_t *handle,
RET_ERR(handle, ALPM_ERR_WRONG_ARGS, NULL);
}
- /* try to create an archive object to read in the package */
- if((archive = archive_read_new()) == NULL) {
- RET_ERR(handle, ALPM_ERR_LIBARCHIVE, NULL);
- }
-
- archive_read_support_compression_all(archive);
- archive_read_support_format_all(archive);
-
- OPEN(fd, pkgfile, O_RDONLY);
- if(fd < 0 || archive_read_open_fd(archive, fd,
- ALPM_BUFFER_SIZE) != ARCHIVE_OK) {
- const char *err = fd < 0 ? strerror(errno) : archive_error_string(archive);
- _alpm_log(handle, ALPM_LOG_ERROR,
- _("could not open file %s: %s\n"), pkgfile, err);
- if(fd < 0 && errno == ENOENT) {
+ fd = _alpm_open_archive(handle, pkgfile, &st, &archive, ALPM_ERR_PKG_OPEN);
+ if(fd < 0) {
+ if(errno == ENOENT) {
handle->pm_errno = ALPM_ERR_PKG_NOT_FOUND;
- } else {
- handle->pm_errno = ALPM_ERR_PKG_OPEN;
}
goto error;
}
@@ -397,10 +401,6 @@ alpm_pkg_t *_alpm_pkg_load_internal(alpm_handle_t *handle,
handle->pm_errno = ALPM_ERR_MEMORY;
goto error;
}
- if(fstat(fd, &st) != 0) {
- handle->pm_errno = ALPM_ERR_PKG_OPEN;
- goto error;
- }
STRDUP(newpkg->filename, pkgfile,
handle->pm_errno = ALPM_ERR_MEMORY; goto error);
newpkg->size = st.st_size;