[pacman-dev] [PATCH 3/8] Add internal functions for reading mtree file from local db

Allan McRae allan at archlinux.org
Sat May 5 05:14:01 EDT 2012


Signed-off-by: Allan McRae <allan at archlinux.org>
---
 lib/libalpm/be_local.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/libalpm/package.c  |   21 +++++++++++++++++
 lib/libalpm/package.h  |    8 +++++++
 3 files changed, 88 insertions(+)

diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c
index 30e59d0..402637a 100644
--- a/lib/libalpm/be_local.c
+++ b/lib/libalpm/be_local.c
@@ -28,6 +28,10 @@
 #include <dirent.h>
 #include <limits.h> /* PATH_MAX */
 
+/* libarchive */
+#include <archive.h>
+#include <archive_entry.h>
+
 /* libalpm */
 #include "db.h"
 #include "alpm_list.h"
@@ -210,6 +214,57 @@ static int _cache_changelog_close(const alpm_pkg_t UNUSED *pkg, void *fp)
 	return fclose((FILE *)fp);
 }
 
+static struct archive *_cache_mtree_open(alpm_pkg_t *pkg)
+{
+	int r;
+	struct archive *mtree;
+
+	pkg->handle->pm_errno = 0;
+
+	alpm_db_t *db = alpm_pkg_get_db(pkg);
+	char *mtfile = _alpm_local_db_pkgpath(db, pkg, "mtree");
+
+	if(access(mtfile, F_OK) != 0) {
+		/* there is no mtree file for this package */
+		goto error;
+	}
+
+	if((mtree = archive_read_new()) == NULL) {
+		pkg->handle->pm_errno = ALPM_ERR_LIBARCHIVE;
+		goto error;
+	}
+
+	archive_read_support_filter_gzip(mtree);
+	archive_read_support_format_mtree(mtree);
+
+	if((r = archive_read_open_file(mtree, mtfile, ALPM_BUFFER_SIZE))) {
+		_alpm_log(pkg->handle, ALPM_LOG_ERROR, _("error while reading file %s: %s\n"),
+					mtfile, archive_error_string(mtree));
+		pkg->handle->pm_errno = ALPM_ERR_LIBARCHIVE;
+		archive_read_finish(mtree);
+		goto error;
+	}
+
+	free(mtfile);
+	return mtree;
+
+error:
+	free(mtfile);
+	return NULL;
+}
+
+static int _cache_mtree_next(const alpm_pkg_t UNUSED *pkg,
+		struct archive *mtree, struct archive_entry **entry)
+{
+	return archive_read_next_header(mtree, entry);
+}
+
+static int _cache_mtree_close(const alpm_pkg_t UNUSED *pkg,
+		struct archive *mtree)
+{
+	return archive_read_finish(mtree);
+}
+
 static int _cache_force_load(alpm_pkg_t *pkg)
 {
 	return local_db_read(pkg, INFRQ_ALL);
@@ -245,6 +300,10 @@ static struct pkg_operations local_pkg_ops = {
 	.changelog_read  = _cache_changelog_read,
 	.changelog_close = _cache_changelog_close,
 
+	.mtree_open      = _cache_mtree_open,
+	.mtree_next      = _cache_mtree_next,
+	.mtree_close     = _cache_mtree_close,
+
 	.force_load      = _cache_force_load,
 };
 
diff --git a/lib/libalpm/package.c b/lib/libalpm/package.c
index 46d1473..3906b0f 100644
--- a/lib/libalpm/package.c
+++ b/lib/libalpm/package.c
@@ -121,6 +121,23 @@ static int _pkg_changelog_close(const alpm_pkg_t UNUSED *pkg,
 	return EOF;
 }
 
+static struct archive *_pkg_mtree_open(alpm_pkg_t UNUSED *pkg)
+{
+	return NULL;
+}
+
+static int _pkg_mtree_next(const alpm_pkg_t UNUSED *pkg,
+		struct archive UNUSED *archive, struct archive_entry UNUSED **entry)
+{
+	return -1;
+}
+
+static int _pkg_mtree_close(const alpm_pkg_t UNUSED *pkg,
+		struct archive UNUSED *archive)
+{
+	return -1;
+}
+
 static int _pkg_force_load(alpm_pkg_t UNUSED *pkg) { return 0; }
 
 /** The standard package operations struct. Get fields directly from the
@@ -152,6 +169,10 @@ struct pkg_operations default_pkg_ops = {
 	.changelog_read  = _pkg_changelog_read,
 	.changelog_close = _pkg_changelog_close,
 
+	.mtree_open      = _pkg_mtree_open,
+	.mtree_next      = _pkg_mtree_next,
+	.mtree_close     = _pkg_mtree_close,
+
 	.force_load      = _pkg_force_load,
 };
 
diff --git a/lib/libalpm/package.h b/lib/libalpm/package.h
index c473549..542a6d9 100644
--- a/lib/libalpm/package.h
+++ b/lib/libalpm/package.h
@@ -26,6 +26,10 @@
 
 #include <sys/types.h> /* off_t */
 
+/* libarchive */
+#include <archive.h>
+#include <archive_entry.h>
+
 #include "alpm.h"
 #include "backup.h"
 #include "db.h"
@@ -64,6 +68,10 @@ struct pkg_operations {
 	size_t (*changelog_read) (void *, size_t, const alpm_pkg_t *, void *);
 	int (*changelog_close) (const alpm_pkg_t *, void *);
 
+	struct archive *(*mtree_open) (alpm_pkg_t *);
+	int (*mtree_next) (const alpm_pkg_t *, struct archive *, struct archive_entry **);
+	int (*mtree_close) (const alpm_pkg_t *, struct archive *);
+
 	int (*force_load) (alpm_pkg_t *);
 };
 
-- 
1.7.10.1



More information about the pacman-dev mailing list