Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Implement file_to_buffer_at
Baptiste Daroussin committed 11 years ago
commit 47f0d8ed7712355ddbf474d1a77ce35851458cc4
parent d800b7c
2 files changed +50 -1
modified libpkg/private/utils.h
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2011-2014 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
 * All rights reserved.
 * 
@@ -89,6 +89,7 @@ ssize_t sbuf_size(struct sbuf *);

int mkdirs(const char *path);
int file_to_buffer(const char *, char **, off_t *);
+
int file_to_bufferat(int, const char *, char **, off_t *);
int format_exec_cmd(char **, const char *, const char *, const char *, char *);
int is_dir(const char *);
int is_conf_file(const char *path, char *newpath, size_t len);
modified libpkg/utils.c
@@ -146,6 +146,54 @@ mkdirs(const char *_path)

	return (EPKG_OK);
}
+
int
+
file_to_bufferat(int dfd, 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 = openat(dfd, path, O_RDONLY)) == -1) {
+
		pkg_emit_errno("open", path);
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	if (fstatat(dfd, path, &st, 0) == -1) {
+
		pkg_emit_errno("fstatat", path);
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	if ((*buffer = malloc(st.st_size + 1)) == NULL) {
+
		pkg_emit_errno("malloc", "");
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	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);
+
}

int
file_to_buffer(const char *path, char **buffer, off_t *sz)