Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add util.{c,h}, few cleanup
Baptiste Daroussin committed 15 years ago
commit 0e3bef7a238e9b8c3cb696b37967d70ff6bd7a69
parent 882a1fc
5 files changed +46 -31
modified libpkg/Makefile
@@ -6,7 +6,7 @@ WARNS= 6
SHLIBDIR?=	/usr/lib
SHLIB_MAJOR=	0

-
SRCS=	pkgdb.c pkgdb_cache.c pkg_compat.c
+
SRCS=	pkgdb.c pkgdb_cache.c pkg_compat.c util.c

CFLAGS+=  -std=c99
CFLAGS+=  -I${.CURDIR} -I${.CURDIR}/../external/tinycdb -I${.CURDIR}/../external/cjson/
modified libpkg/pkg_compat.c
@@ -6,6 +6,7 @@
#include <sys/stat.h>
#include <sys/utsname.h>

+
#include "util.h"
#include "pkg_compat.h"

static void
@@ -196,34 +197,24 @@ pkg_compat_convert_installed(const char *pkg_dbdir, char *pkgname, char *manifes
{
	cJSON *rootpkg;
	char *cjson_output;
-
	char *content_buffer;
-
	char filepath[MAXPATHLEN];
	FILE *fs;
-
	struct stat st;
+
	char *buffer;
+
	char filepath[MAXPATHLEN];

	strlcpy(filepath, pkg_dbdir, MAXPATHLEN);
	strlcat(filepath, "/", MAXPATHLEN);
	strlcat(filepath, pkgname, MAXPATHLEN);
	strlcat(filepath, "/+CONTENTS", MAXPATHLEN);

-
	if (stat(filepath, &st) == -1 ) {
-
		warn("No content for %s, should be corrupted, skipping",
-
				pkgname);
-
		return (0);
-
	}
-

	rootpkg = cJSON_CreateObject();
-
	if ((fs = fopen(filepath, "r")) == NULL) {
-
		warn("Unable to read %s file, skipping", filepath);
+

+
	if ((buffer = file_to_buffer(filepath)) == NULL) {
+
		warn("Unable to read +CONTENTS for %s", pkgname);
		return (0);
	}

-
	content_buffer = malloc(st.st_size + 1);
-
	fread(content_buffer, st.st_size, 1, fs);
-
	fclose(fs);
-

-
	rootpkg = pkg_compat_converter(content_buffer);
-
	free(content_buffer);
+
	rootpkg = pkg_compat_converter(buffer);
+
	free(buffer);

	if (rootpkg == 0) {
		warnx("%s: Manifest corrupted, skipping", pkgname);
modified libpkg/pkgdb_cache.c
@@ -12,6 +12,7 @@
#include <cdb.h>
#include <cJSON.h>

+
#include "util.h"
#include "pkg_compat.h"
#include "pkgdb_cache.h"

@@ -19,33 +20,24 @@ static cJSON *
pkgdb_cache_load_port(const char *pkg_dbdir, char *pkgname)
{
	cJSON *manifest;
-
	FILE *fs;
	char manifestpath[MAXPATHLEN];
	char *buffer;
-
	struct stat st;

	strlcpy(manifestpath, pkg_dbdir, MAXPATHLEN);
	strlcat(manifestpath, "/", MAXPATHLEN);
	strlcat(manifestpath, pkgname, MAXPATHLEN);
	strlcat(manifestpath, "/+MANIFEST", MAXPATHLEN);

-
	if (stat(manifestpath, &st) == -1) {
-
		warnx("No manifest for %s trying old format", pkgname);
+
	if ((buffer = file_to_buffer(manifestpath)) == NULL) {
+
		warn("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,
				manifestpath);

		return (manifest);
	}

-
	if ((fs = fopen(manifestpath, "r")) == NULL) {
-
		warn("Unable to read %s file, skipping", manifestpath);
-
		return (0);
-
	}
-

-
	buffer = malloc(st.st_size + 1);
-
	fread(buffer, st.st_size, 1, fs);
-
	fclose(fs);
-

	if ((manifest = cJSON_Parse(buffer)) == 0)
		warnx("%s: Manifest corrputed, skipping", pkgname);

added libpkg/util.c
@@ -0,0 +1,27 @@
+
#include <stdio.h>
+
#include <stdlib.h>
+
#include <sys/stat.h>
+

+
#include "util.h"
+

+
char *
+
file_to_buffer(const char *path)
+
{
+
	FILE *fs;
+
	struct stat st;
+
	char *buffer = NULL;
+

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

+
	if ((fs = fopen(path, "r")) == NULL)
+
		return (NULL);
+

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

+
	fread(buffer, st.st_size, 1, fs);
+
	fclose(fs);
+

+
	return buffer;
+
}
added libpkg/util.h
@@ -0,0 +1,5 @@
+
#ifndef _PKG_UTIL_H
+
#define _PKG_UTIL_H
+

+
char *file_to_buffer(const char *path);
+
#endif