Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Remove copypasta on various ...fileat() functions
John Hood committed 4 years ago
commit 88f45cb99f1e04c46339c6911f81184da331f52b
parent b616cda
3 files changed +20 -161
modified libpkg/pkg_checksum.c
@@ -734,19 +734,7 @@ pkg_checksum_fileat(int rootfd, const char *path, pkg_checksum_type_t type)
unsigned char *
pkg_checksum_file(const char *path, pkg_checksum_type_t type)
{
-
	int fd;
-
	unsigned char *ret;
-

-
	if ((fd = open(path, O_RDONLY)) == -1) {
-
		pkg_emit_errno("open", path);
-
		return (NULL);
-
	}
-

-
	ret = pkg_checksum_fd(fd, type);
-

-
	close(fd);
-

-
	return (ret);
+
	return pkg_checksum_fileat(AT_FDCWD, path, type);
}

unsigned char *
@@ -792,16 +780,7 @@ pkg_checksum_symlink_readlink(const char *linkbuf, int linklen,
unsigned char *
pkg_checksum_symlink(const char *path, pkg_checksum_type_t type)
{
-
	char linkbuf[MAXPATHLEN];
-
	int linklen;
-

-
	if ((linklen = readlink(path, linkbuf, sizeof(linkbuf) - 1)) == -1) {
-
		pkg_emit_errno("pkg_checksum_symlink", "readlink failed");
-
		return (NULL);
-
	}
-
	linkbuf[linklen] = '\0';
-

-
	return (pkg_checksum_symlink_readlink(linkbuf, linklen, type));
+
	return pkg_checksum_symlinkat(AT_FDCWD, path, type);
}

unsigned char *
@@ -822,65 +801,7 @@ pkg_checksum_symlinkat(int fd, const char *path, pkg_checksum_type_t type)
int
pkg_checksum_validate_file(const char *path, const char *sum)
{
-
	struct stat st;
-
	char *newsum;
-
	pkg_checksum_type_t type;
-

-
	type = pkg_checksum_file_get_type(sum, strlen(sum));
-
	if (type == PKG_HASH_TYPE_UNKNOWN) {
-
		type = PKG_HASH_TYPE_SHA256_HEX;
-
	} else {
-
		sum = strchr(sum, PKG_CKSUM_SEPARATOR);
-
		if (sum != NULL)
-
			sum++;
-
	}
-

-
	if (lstat(path, &st) == -1) {
-
		return (errno);
-
	}
-

-
	if (S_ISLNK(st.st_mode))
-
		newsum = pkg_checksum_symlink(path, type);
-
	else
-
		newsum = pkg_checksum_file(path, type);
-

-
	if (newsum == NULL)
-
		return (-1);
-

-
	if (strcmp(sum, newsum) != 0) {
-
		free(newsum);
-
		return (-1);
-
	}
-

-
	free(newsum);
-

-
	return (0);
-
}
-

-
char *
-
pkg_checksum_generate_file(const char *path, pkg_checksum_type_t type)
-
{
-
	struct stat st;
-
	unsigned char *sum;
-
	char *cksum;
-

-
	if (lstat(path, &st) == -1) {
-
		pkg_emit_errno("pkg_checksum_generate_file", "lstat");
-
		return (NULL);
-
	}
-

-
	if (S_ISLNK(st.st_mode))
-
		sum = pkg_checksum_symlink(path, type);
-
	else
-
		sum = pkg_checksum_file(path, type);
-

-
	if (sum == NULL)
-
		return (NULL);
-

-
	xasprintf(&cksum, "%d%c%s", type, PKG_CKSUM_SEPARATOR, sum);
-
	free(sum);
-

-
	return (cksum);
+
	return pkg_checksum_validate_fileat(AT_FDCWD, path, sum);
}

int
@@ -922,6 +843,12 @@ pkg_checksum_validate_fileat(int rootfd, const char *path, const char *sum)
}

char *
+
pkg_checksum_generate_file(const char *path, pkg_checksum_type_t type)
+
{
+
	return pkg_checksum_generate_fileat(AT_FDCWD, path, type);
+
}
+

+
char *
pkg_checksum_generate_fileat(int rootfd, const char *path,
    pkg_checksum_type_t type)
{
modified libpkg/pkg_manifest.c
@@ -896,11 +896,18 @@ pkg_parse_manifest_fileat(int dfd, struct pkg *pkg, const char *file,
	if (!ucl_parser_add_string(p, data, sz)) {
		pkg_emit_error("manifest parsing error: %s", ucl_parser_get_error(p));
		ucl_parser_free(p);
+
		free(data);
+
		return (EPKG_FATAL);
+
	}
+

+
	if ((obj = ucl_parser_get_object(p)) == NULL) {
+
		ucl_parser_free(p);
+
		free(data);
		return (EPKG_FATAL);
	}

-
	obj = ucl_parser_get_object(p);
	rc = pkg_parse_manifest_ucl(pkg, obj, keys);
+
	ucl_object_unref(obj);
	ucl_parser_free(p);
	free(data);

@@ -910,43 +917,7 @@ pkg_parse_manifest_fileat(int dfd, struct pkg *pkg, const char *file,
int
pkg_parse_manifest_file(struct pkg *pkg, const char *file, struct pkg_manifest_key *keys)
{
-
	struct ucl_parser *p = NULL;
-
	ucl_object_t *obj = NULL;
-
	int rc, fd;
-

-
	assert(pkg != NULL);
-
	assert(file != NULL);
-

-
	pkg_debug(1, "Parsing manifest from '%s'", file);
-
	fd = open(file, O_RDONLY);
-

-
	if (fd == -1) {
-
		pkg_emit_error("Error loading manifest from %s: %s",
-
				    file, strerror(errno));
-
	}
-

-
	errno = 0;
-
	p = ucl_parser_new(UCL_PARSER_NO_FILEVARS);
-
	if (!ucl_parser_add_fd(p, fd)) {
-
		pkg_emit_error("Error parsing manifest: %s",
-
		    ucl_parser_get_error(p));
-
		ucl_parser_free(p);
-
		close(fd);
-
		return (EPKG_FATAL);
-
	}
-

-
	close(fd);
-

-
	if ((obj = ucl_parser_get_object(p)) == NULL) {
-
		ucl_parser_free(p);
-
		return (EPKG_FATAL);
-
	}
-

-
	ucl_parser_free(p);
-
	rc = pkg_parse_manifest_ucl(pkg, obj, keys);
-
	ucl_object_unref(obj);
-

-
	return (rc);
+
	return pkg_parse_manifest_fileat(AT_FDCWD, pkg, file, keys);
}

int
modified libpkg/utils.c
@@ -140,7 +140,7 @@ file_to_bufferat(int dfd, const char *path, char **buffer, off_t *sz)
		goto cleanup;
	}

-
	if (fstatat(dfd, path, &st, 0) == -1) {
+
	if (fstat(fd, &st) == -1) {
		pkg_emit_errno("fstatat", path);
		retcode = EPKG_FATAL;
		goto cleanup;
@@ -171,46 +171,7 @@ file_to_bufferat(int dfd, const char *path, char **buffer, off_t *sz)
int
file_to_buffer(const char *path, char **buffer, off_t *sz)
{
-
	int fd = -1;
-
	struct stat st;
-
	int retcode = EPKG_OK;
-

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

-
	if ((fd = open(path, O_RDONLY)) == -1) {
-
		pkg_emit_errno("open", path);
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	if (fstat(fd, &st) == -1) {
-
		pkg_emit_errno("fstat", path);
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	*buffer = xmalloc(st.st_size + 1);
-

-
	if (read(fd, *buffer, st.st_size) == -1) {
-
		pkg_emit_errno("read", path);
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	cleanup:
-
	if (fd >= 0)
-
		close(fd);
-

-
	if (retcode == EPKG_OK) {
-
		(*buffer)[st.st_size] = '\0';
-
		*sz = st.st_size;
-
	} else {
-
		*buffer = NULL;
-
		*sz = -1;
-
	}
-
	return (retcode);
+
	return file_to_bufferat(AT_FDCWD, path, buffer, sz);
}

int