Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add pkg_open_fd to open from a file descriptor, this helps with capsicum
Baptiste Daroussin committed 12 years ago
commit 88636c1774bf3a31a206a39f887e52a14b130f43
parent 983fde4
4 files changed +39 -13
modified libpkg/pkg.c
@@ -1207,7 +1207,25 @@ pkg_open(struct pkg **pkg_p, const char *path, struct pkg_manifest_key *keys, in
	struct archive_entry *ae;
	int ret;

-
	ret = pkg_open2(pkg_p, &a, &ae, path, keys, flags);
+
	ret = pkg_open2(pkg_p, &a, &ae, path, keys, flags, -1);
+

+
	if (ret != EPKG_OK && ret != EPKG_END)
+
		return (EPKG_FATAL);
+

+
	archive_read_close(a);
+
	archive_read_free(a);
+

+
	return (EPKG_OK);
+
}
+

+
int
+
pkg_open_fd(struct pkg **pkg_p, int fd, struct pkg_manifest_key *keys, int flags)
+
{
+
	struct archive *a;
+
	struct archive_entry *ae;
+
	int ret;
+

+
	ret = pkg_open2(pkg_p, &a, &ae, NULL, keys, flags, fd);

	if (ret != EPKG_OK && ret != EPKG_END)
		return (EPKG_FATAL);
@@ -1220,7 +1238,7 @@ pkg_open(struct pkg **pkg_p, const char *path, struct pkg_manifest_key *keys, in

int
pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae,
-
		const char *path, struct pkg_manifest_key *keys, int flags)
+
		const char *path, struct pkg_manifest_key *keys, int flags, int fd)
{
	struct pkg	 *pkg;
	pkg_error_t	  retcode = EPKG_OK;
@@ -1242,8 +1260,6 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae,
		{ NULL, 0 }
	};

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

	*a = archive_read_new();
	archive_read_support_filter_all(*a);
	archive_read_support_format_tar(*a);
@@ -1254,14 +1270,23 @@ pkg_open2(struct pkg **pkg_p, struct archive **a, struct archive_entry **ae,
	 * read an on-disk file called "-", just say "./-" or some
	 * other leading path. */

-
	read_from_stdin = (strncmp(path, "-", 2) == 0);
+
	if (fd == -1) {
+
		read_from_stdin = (strncmp(path, "-", 2) == 0);

-
	if (archive_read_open_filename(*a,
-
	    read_from_stdin ? NULL : path, 4096) != ARCHIVE_OK) {
-
		pkg_emit_error("archive_read_open_filename(%s): %s", path,
-
		    archive_error_string(*a));
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
+
		if (archive_read_open_filename(*a,
+
		    read_from_stdin ? NULL : path, 4096) != ARCHIVE_OK) {
+
			pkg_emit_error("archive_read_open_filename(%s): %s", path,
+
			    archive_error_string(*a));
+
			retcode = EPKG_FATAL;
+
			goto cleanup;
+
		}
+
	} else {
+
		if (archive_read_open_fd(*a, fd, 4096) != ARCHIVE_OK) {
+
			pkg_emit_error("archive_read_open_fd: %s",
+
			    archive_error_string(*a));
+
			retcode = EPKG_FATAL;
+
			goto cleanup;
+
		}
	}

	if (*pkg_p == NULL) {
modified libpkg/pkg.h.in
@@ -558,6 +558,7 @@ int pkg_is_valid(const struct pkg * restrict);
 * @param flags open flags
 */
int pkg_open(struct pkg **p, const char *path, struct pkg_manifest_key *keys, int flags);
+
int pkg_open_fd(struct pkg **p, int fd, struct pkg_manifest_key *keys, int flags);

/**
 * @return the type of the package.
modified libpkg/pkg_add.c
@@ -178,7 +178,7 @@ pkg_add(struct pkgdb *db, const char *path, unsigned flags, struct pkg_manifest_
	 * current archive_entry to the first non-meta file.
	 * If there is no non-meta files, EPKG_END is returned.
	 */
-
	ret = pkg_open2(&pkg, &a, &ae, path, keys, 0);
+
	ret = pkg_open2(&pkg, &a, &ae, path, keys, 0, -1);
	if (ret == EPKG_END)
		extract = false;
	else if (ret != EPKG_OK) {
modified libpkg/private/pkg.h
@@ -375,7 +375,7 @@ int pkg_add_user_group(struct pkg *pkg);
int pkg_delete_user_group(struct pkgdb *db, struct pkg *pkg);

int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae,
-
	      const char *path, struct pkg_manifest_key *keys, int flags);
+
	      const char *path, struct pkg_manifest_key *keys, int flags, int fd);

void pkg_list_free(struct pkg *, pkg_list);