Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Improve file_to_buffer()
Julien Laffaye committed 15 years ago
commit 8959bb134f1d30592f915181f1e175ee64f85d57
parent 607d673
4 files changed +47 -22
modified libpkg/pkg_compat.c
@@ -195,6 +195,7 @@ pkg_compat_convert_installed(const char *pkg_dbdir, char *pkgname, char *manifes
	char *cjson_output;
	FILE *fs;
	char *buffer;
+
	off_t buffer_len;
	char filepath[MAXPATHLEN];
	char *tmp;

@@ -205,7 +206,7 @@ pkg_compat_convert_installed(const char *pkg_dbdir, char *pkgname, char *manifes

	rootpkg = cJSON_CreateObject();

-
	if ((buffer = file_to_buffer(filepath)) == NULL) {
+
	if ((file_to_buffer(filepath, &buffer)) == -1) {
		warn("Unable to read +CONTENTS for %s", pkgname);
		return (0);
	}
@@ -223,11 +224,11 @@ pkg_compat_convert_installed(const char *pkg_dbdir, char *pkgname, char *manifes
	tmp[0] = '\0';
	strlcat(filepath, "+COMMENT", MAXPATHLEN);

-
	if ((buffer = file_to_buffer(filepath)) == NULL) {
+
	if ((buffer_len = file_to_buffer(filepath, &buffer)) == -1) {
		warn("Unable to read +COMMENT for %s", pkgname);
	} else {
-
		if (buffer[strlen(buffer) - 1 ] == '\n')
-
			buffer[strlen(buffer) -1 ] = '\0';
+
		if (buffer[buffer_len - 1 ] == '\n')
+
			buffer[buffer_len -1 ] = '\0';

		cJSON_AddStringToObject(rootpkg, "comment", buffer);
		free(buffer);
modified libpkg/pkgdb_cache.c
@@ -28,8 +28,8 @@ pkgdb_cache_load_port(const char *pkg_dbdir, char *pkgname)
	strlcat(manifestpath, pkgname, MAXPATHLEN);
	strlcat(manifestpath, "/+MANIFEST", MAXPATHLEN);

-
	if ((buffer = file_to_buffer(manifestpath)) == NULL) {
-
		warn("An error occured while trying to read "
+
	if ((file_to_buffer(manifestpath, &buffer)) == -1) {
+
		warnx("An error occured while trying to read "
				"+MANIFEST for %s, falling back to old "
				"+CONTENTS format", pkgname);
		manifest = pkg_compat_convert_installed( pkg_dbdir, pkgname,
modified libpkg/util.c
@@ -1,27 +1,51 @@
-
#include <stdio.h>
-
#include <stdlib.h>
#include <sys/stat.h>
+
#include <sys/types.h>
+
#include <sys/uio.h>
+

+
#include <assert.h>
+
#include <err.h>
+
#include <fcntl.h>
+
#include <stdlib.h>
+
#include <unistd.h>

#include "util.h"

-
char *
-
file_to_buffer(const char *path)
+
off_t
+
file_to_buffer(const char *path, char **buffer)
{
-
	FILE *fs;
+
	int fd;
	struct stat st;
-
	char *buffer = NULL;

-
	if (stat(path, &st) == -1)
-
		return (NULL);
+
	assert(path != NULL);
+
	assert(buffer != NULL);
+

+
	if ((fd = open(path, O_RDONLY)) == -1) {
+
		warn("open(%s)", path);
+
		return (-1);
+
	}
+

+
	if (fstat(fd, &st) == -1) {
+
		warn("fstat(%d)", fd);
+
		close(fd);
+
		return (-1);
+
	}
+

+
	if ((*buffer = malloc(st.st_size + 1)) == NULL) {
+
		warn("malloc(%llu)", (unsigned long long)st.st_size + 1);
+
		close(fd);
+
		return (-1);
+
	}

-
	if ((fs = fopen(path, "r")) == NULL)
-
		return (NULL);
+
	if (read(fd, *buffer, st.st_size) == -1) {
+
		warn("read()");
+
		close(fd);
+
		return (-1);
+
	}

-
	if ((buffer = malloc(st.st_size + 1)) == NULL)
-
		return (NULL);
+
	close(fd);

-
	fread(buffer, st.st_size, 1, fs);
-
	fclose(fs);
+
	/* NULL terminate the buffer so it can be used by stdio.h functions */
+
	(*buffer)[st.st_size] = '\0';

-
	return buffer;
+
	return st.st_size;
}
modified libpkg/util.h
@@ -1,5 +1,5 @@
#ifndef _PKG_UTIL_H
#define _PKG_UTIL_H

-
char *file_to_buffer(const char *path);
+
off_t file_to_buffer(const char *path, char **buffer);
#endif