Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Clean up and refactoring
Baptiste Daroussin committed 15 years ago
commit 2d3916236e2b64d1be29fd53a1f08040519b0049
parent 1f0cdaf
3 files changed +97 -72
modified libpkg/pkg_compat.c
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
+
#include <sys/stat.h>
#include <sys/utsname.h>

#include "pkg_compat.h"
@@ -189,3 +190,51 @@ pkg_compat_converter(char *plist_str)

	return (rootpkg);
}
+

+
cJSON *
+
pkg_compat_convert_installed(const char *pkg_dbdir, char *pkgname, char *manifestpath)
+
{
+
	cJSON *rootpkg;
+
	char *cjson_output;
+
	char *content_buffer;
+
	char filepath[MAXPATHLEN];
+
	FILE *fs;
+
	struct stat st;
+

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

+
	if (rootpkg == 0) {
+
		warnx("%s: Manifest corrupted, skipping", pkgname);
+
		return (0);
+
	}
+

+
	/* write the new manifest */
+
	cjson_output = cJSON_Print(rootpkg);
+
	fs = fopen(manifestpath, "w+");
+
	fprintf(fs, "%s", cjson_output);
+
	fclose(fs);
+

+
	return (rootpkg);
+
}
modified libpkg/pkg_compat.h
@@ -31,5 +31,6 @@ struct oldpackage {
};

cJSON *pkg_compat_converter(char *);
+
cJSON *pkg_compat_convert_installed(const char *, char *, char *);

#endif
modified libpkg/pkgdb_cache.c
@@ -15,7 +15,44 @@
#include "pkg_compat.h"
#include "pkgdb_cache.h"

-
static void pkgdb_cache_rebuild(const char *, const char *);
+
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);
+
		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);
+

+
	free(buffer);
+

+
	return (manifest);
+
}

static void
pkgdb_cache_rebuild(const char *pkg_dbdir, const char *cache_path)
@@ -24,18 +61,11 @@ pkgdb_cache_rebuild(const char *pkg_dbdir, const char *cache_path)
	char key[BUFSIZ];
	char *value;
	char tmppath[MAXPATHLEN];
-
	char contentpath[MAXPATHLEN];
-
	char manifestpath[MAXPATHLEN];
	struct cdb_make cdb_make;
	DIR *dir;
	struct dirent *portsdir;
-
	struct stat st;
-
	FILE *content, *manifest;
-
	char *content_buffer = NULL;
-
	cJSON *manifest_json;
+
	cJSON *manifest;
	int nb_packages = 0;
-
	char *manifest_out;
-


	strlcpy(tmppath, pkg_dbdir, MAXPATHLEN);
	strlcat(tmppath, "/pkgdb.cache", MAXPATHLEN);
@@ -55,76 +85,21 @@ pkgdb_cache_rebuild(const char *pkg_dbdir, const char *cache_path)
				if (portsdir->d_type != DT_DIR)
					continue;

-
				strlcpy(manifestpath, pkg_dbdir, MAXPATHLEN);
-
				strlcat(manifestpath, "/", MAXPATHLEN);
-
				strlcat(manifestpath, portsdir->d_name, MAXPATHLEN);
-
				strlcat(manifestpath, "/+MANIFEST", MAXPATHLEN);
-

-
				if (stat(manifestpath, &st) == -1) {
-
					warnx("No manifest for %s trying old format", portsdir->d_name);
-
					strlcpy(contentpath, pkg_dbdir, MAXPATHLEN);
-
					strlcat(contentpath, "/", MAXPATHLEN);
-
					strlcat(contentpath, portsdir->d_name, MAXPATHLEN);
-
					strlcat(contentpath, "/+CONTENTS", MAXPATHLEN);
-
					if (stat(contentpath, &st) == -1 ) {
-
						warn("No content for %s, should be corrupted, skipping", portsdir->d_name);
-
						continue;
-
					}
-

-
					if ((content = fopen(contentpath, "r")) == NULL) {
-
						warn("Unable to read %s file, skipping", contentpath);
-
						continue;
-
					}
-
					
-
					content_buffer = malloc(st.st_size + 1);
-
					fread(content_buffer, st.st_size, 1, content);
-
					fclose(content);
-

-
					manifest_json = pkg_compat_converter(content_buffer);
-
					
-
					if (manifest_json == 0) {
-
						warnx("%s: Manifest corrupted, skipping", portsdir->d_name);
-
						continue;
-
					}
-

-
					/* writing the manifest to end
-
					 * conversion */
-
					manifest_out = cJSON_Print(manifest_json);
-
					manifest = fopen(manifestpath, "w+");
-
					fprintf(manifest, "%s", manifest_out);
-
					fclose(manifest);
-

-
				} else {
-

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

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

-
					manifest_json = cJSON_Parse(content_buffer);
-

-
					if (manifest_json == 0) {
-
						warnx("%s: Manifest corrputed, skipping", portsdir->d_name);
-
						continue;
-
					}
-
				}
+
				manifest = pkgdb_cache_load_port(pkg_dbdir,
+
						portsdir->d_name);
+

+
				if (manifest == 0)
+
					continue;

				nb_packages++;
				snprintf(key, BUFSIZ, "%d_name",nb_packages);
-
				value = cJSON_GetObjectItem(manifest_json, "name")->valuestring;
+
				value = cJSON_GetObjectItem(manifest, "name")->valuestring;
				cdb_make_add(&cdb_make, key, strlen(key), value, strlen(value));
				snprintf(key, BUFSIZ, "%d_version", nb_packages);
-
				value = cJSON_GetObjectItem(manifest_json, "version")->valuestring;
+
				value = cJSON_GetObjectItem(manifest, "version")->valuestring;
				cdb_make_add(&cdb_make, key, strlen(key), value, strlen(value));

-
				cJSON_Delete(manifest_json);
-

-
				if (content_buffer != NULL)
-
					free(content_buffer);
+
				cJSON_Delete(manifest);
			}
		}
	}