Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Be able to reuse the manifest buffer when opening multiple packages
Baptiste Daroussin committed 14 years ago
commit 9f14b88579cddbdcbb50076a26647b01a7faa07b
parent 649a60b
7 files changed +34 -18
modified libpkg/pkg.c
@@ -883,13 +883,13 @@ pkg_list_free(struct pkg *pkg, pkg_list list) {
}

int
-
pkg_open(struct pkg **pkg_p, const char *path)
+
pkg_open(struct pkg **pkg_p, const char *path, struct sbuf *mbuf)
{
	struct archive *a;
	struct archive_entry *ae;
	int ret;

-
	ret = pkg_open2(pkg_p, &a, &ae, path);
+
	ret = pkg_open2(pkg_p, &a, &ae, path, mbuf);

	if (ret != EPKG_OK && ret != EPKG_END)
		return (EPKG_FATAL);
@@ -900,15 +900,15 @@ pkg_open(struct pkg **pkg_p, const char *path)
}

int
-
pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae, const char *path)
+
pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae, const char *path, struct sbuf *mbuf)
{
	struct pkg *pkg;
	pkg_error_t retcode = EPKG_OK;
	int ret;
	int64_t size;
-
	char *manifest = NULL;
+
	struct sbuf *manifest = mbuf;
	const char *fpath;
-
	char buf[2048];
+
	char buf[BUFSIZ];
	struct sbuf **sbuf;
	int i;

@@ -922,6 +922,11 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae, con

	assert(path != NULL && path[0] != '\0');

+
	if (manifest == NULL)
+
		manifest = sbuf_new_auto();
+
	else
+
		sbuf_clear(manifest);
+

	*a = archive_read_new();
	archive_read_support_compression_all(*a);
	archive_read_support_format_tar(*a);
@@ -953,9 +958,14 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae, con
				pkg_emit_error("%s is not a valid package: empty +MANIFEST found", path);
				goto cleanup;
			}
-
			manifest = calloc(1, size + 1);
-
			archive_read_data(*a, manifest, size);
-
			ret = pkg_parse_manifest(pkg, manifest);
+

+
			while ((size = archive_read_data(*a, buf, sizeof(buf))) > 0) {
+
				sbuf_bcat(manifest, buf, size);
+
			}
+

+
			sbuf_finish(manifest);
+

+
			ret = pkg_parse_manifest(pkg, sbuf_data(manifest));
			if (ret != EPKG_OK) {
				retcode = EPKG_FATAL;
				goto cleanup;
@@ -986,14 +996,16 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae, con
	if (ret == ARCHIVE_EOF)
		retcode = EPKG_END;

-
	if (manifest == NULL) {
+
	if (sbuf_len(manifest) == 0) {
		retcode = EPKG_FATAL;
		pkg_emit_error("%s is not a valid package: no +MANIFEST found", path);
	}

	cleanup:
-
	if (manifest != NULL)
-
		free(manifest);
+
	if (mbuf == NULL)
+
		sbuf_delete(manifest);
+
	else
+
		sbuf_clear(manifest);

	if (retcode != EPKG_OK && retcode != EPKG_END) {
		if (*a != NULL)
modified libpkg/pkg.h
@@ -3,6 +3,7 @@

#include <stdarg.h>
#include <stdbool.h>
+
#include <sys/sbuf.h>
#include <sys/types.h>
#include <openssl/pem.h>

@@ -219,7 +220,7 @@ int pkg_isvalid(struct pkg *);
 * NULL pointer, the function allocate a new pkg using pkg_new().
 * @param path The path to the local package archive.
 */
-
int pkg_open(struct pkg **p, const char *path);
+
int pkg_open(struct pkg **p, const char *path, struct sbuf *mbuf);

/**
 * @return the type of the package.
modified libpkg/pkg_private.h
@@ -7,6 +7,7 @@
#include <sys/types.h>

#include <archive.h>
+
#include <sqlite3.h>
#include <openssl/sha.h>
#include <stdbool.h>

@@ -127,7 +128,7 @@ struct pkg_group {
	STAILQ_ENTRY(pkg_group) next;
};

-
int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae, const char *path);
+
int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae, const char *path, struct sbuf *mbuf);

void pkg_list_free(struct pkg *, pkg_list);

modified libpkg/pkg_repo.c
@@ -169,6 +169,7 @@ pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *
	struct pkg_category *category = NULL;
	struct pkg_license *license = NULL;
	struct pkg_option *option = NULL;
+
	struct sbuf *manifest = sbuf_new_auto();
	char *ext = NULL;

	sqlite3 *sqlite = NULL;
@@ -358,11 +359,10 @@ pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *
		while (pkg_path[0] == '/' )
			pkg_path++;

-
		if (pkg_open(&pkg, ent->fts_accpath) != EPKG_OK) {
+
		if (pkg_open(&pkg, ent->fts_accpath, manifest) != EPKG_OK) {
			retcode = EPKG_WARN;
			continue;
		}
-

		if (progress != NULL)
			progress(pkg, data);

@@ -502,6 +502,8 @@ pkg_create_repo(char *path, void (progress)(struct pkg *pkg, void *data), void *
	if (errmsg != NULL)
		sqlite3_free(errmsg);

+
	sbuf_delete(manifest);
+

	sqlite3_shutdown();

	return (retcode);
modified pkg/clean.c
@@ -54,7 +54,7 @@ exec_clean(int argc, char **argv)
		if (repopath[0] == '/')
			repopath++;

-
		if (pkg_open(&pkg, ent->fts_path) != EPKG_OK) {
+
		if (pkg_open(&pkg, ent->fts_path, NULL) != EPKG_OK) {
			warnx("skipping %s", ent->fts_path);
			continue;
		}
modified pkg/info.c
@@ -126,7 +126,7 @@ exec_info(int argc, char **argv)
	}

	if (file != NULL) {
-
		if (pkg_open(&pkg, file) != EPKG_OK) {
+
		if (pkg_open(&pkg, file, NULL) != EPKG_OK) {
			return (1);
		}
		print_info(pkg, opt);
modified pkg/query.c
@@ -448,7 +448,7 @@ exec_query(int argc, char **argv)
		return (EX_USAGE);

	if (pkgname != NULL) {
-
		if (pkg_open(&pkg, pkgname) != EPKG_OK) {
+
		if (pkg_open(&pkg, pkgname, NULL) != EPKG_OK) {
			return (1);
		}