Radish alpha
H
HardenedBSD Package Manager
Radicle
Git (anonymous pull)
Log in to clone via SSH
implement str_replace, fill the exec and files in compat conversion
Baptiste Daroussin committed 15 years ago
commit fec1d4f71ac613ba19d1b3c6aed67ca8c0864e29
parent b2a0ea7dada3ea86bfdd3eaf0d0f9a431597bb64
3 files changed +91 -42
modified libpkg/pkg_compat.c
@@ -18,7 +18,6 @@ str_lowercase(char *str)
	}
}

-

static int
pkg_compat_plist_cmd(char *s, char **arg)
{
@@ -94,33 +93,15 @@ pkg_compat_plist_cmd(char *s, char **arg)
}

static void
-
pkg_compat_add_plist(cJSON *p, enum plist_t type, const char *arg)
-
{
-
	char *tmp;
-

-
	switch (type) {
-
		case PLIST_NAME:
-
			tmp = strrchr(arg, '-');
-
			tmp[0] = '\0';
-
			tmp++;
-
			cJSON_AddStringToObject(p, "name", arg);
-
			cJSON_AddStringToObject(p, "version", tmp);
-
			break;
-

-
		case PLIST_ORIGIN:
-
			cJSON_AddStringToObject(p, "origin", arg);
-
			break;
-

-
		default:
-
			break;
-
	}
-
}
-

-
static void
pkg_compat_read_plist(cJSON *pkg, char *plist_str)
{
	int cmd;
-
	char *buf, *next, *cp;
+
	char *buf, *next, *cp = NULL;
+
	char *tmp;
+

+
	char *prefix = NULL;
+
	char path_file[MAXPATHLEN];
+
	cJSON *object;

	buf = plist_str;
	while ((next = strchr(buf, '\n')) != NULL) {
@@ -131,25 +112,63 @@ pkg_compat_read_plist(cJSON *pkg, char *plist_str)

		if (buf[0] != '@') {
			cmd = PLIST_FILE;
-
			goto bottom;
+
		} else {
+
			cmd = pkg_compat_plist_cmd(buf + 1, &cp);
+
			if (cmd == -1) {
+
				warnx("%s: unknown command '%s'",
+
						__func__, buf);
+
			} else if (*cp == '\0') {
+
				cp = NULL;
+
				if (cmd == PLIST_PKGDEP) {
+
					warnx("corrupted record (pkgdep line without argument), ignoring");
+
					cmd = -1;
+
				}
+
			}
		}

-
		cmd = pkg_compat_plist_cmd(buf + 1, &cp);
-
		if (cmd == -1) {
-
			warnx("%s: unknown command '%s'",
-
					__func__, buf);
-
			goto bottom;
-
		}
-
		if (*cp == '\0') {
-
			cp = NULL;
-
			if (cmd == PLIST_PKGDEP) {
-
				warnx("corrupted record (pkgdep line without argument), ignoring");
-
				cmd = -1;
-
			}
-
			goto bottom;
+
		switch(cmd) {
+
			case PLIST_NAME:
+
				tmp = strrchr(cp, '-');
+
				tmp[0] = '\0';
+
				tmp++;
+
				cJSON_AddStringToObject(pkg, "name", cp);
+
				cJSON_AddStringToObject(pkg, "version", tmp);
+
				break;
+

+
			case PLIST_ORIGIN:
+
				cJSON_AddStringToObject(pkg, "origin", cp);
+
				break;
+

+
			case PLIST_CWD:
+
				prefix = cp;
+
				break;
+

+
			case PLIST_FILE:
+
				snprintf(path_file, MAXPATHLEN, "%s/%s", prefix, buf);
+
				break;
+

+
			case PLIST_MD5:
+
				object = cJSON_CreateObject();
+
				cJSON_AddStringToObject(object, "path", path_file);
+
				cJSON_AddStringToObject(object, "md5", cp);
+
				cJSON_AddItemToArray(cJSON_GetObjectItem(pkg, "files"), object);
+
				break;
+

+
			case PLIST_CMD:
+
				tmp = str_replace(cp, "\%D", prefix);
+
				cJSON_AddItemToArray(cJSON_GetObjectItem(pkg, "exec"), cJSON_CreateString(tmp));
+
				free(tmp);
+
				break;
+

+
			case PLIST_UNEXEC:
+
				tmp = str_replace(cp, "\%D", prefix);
+
				cJSON_AddItemToArray(cJSON_GetObjectItem(pkg, "unexec"), cJSON_CreateString(tmp));
+
				free(tmp);
+
				break;
+

+
			default:
+
				break;
		}
-
bottom:
-
		pkg_compat_add_plist(pkg, cmd, cp);
		buf = next;
		buf++;
	}
modified libpkg/util.c
@@ -1,3 +1,4 @@
+
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
@@ -7,6 +8,7 @@
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
+
#include <string.h>

#include "util.h"

@@ -47,5 +49,32 @@ file_to_buffer(const char *path, char **buffer)
	/* NULL terminate the buffer so it can be used by stdio.h functions */
	(*buffer)[st.st_size] = '\0';

-
	return st.st_size;
+
	return (st.st_size);
+
}
+

+
/* TODO make a cleaner version */
+
char *
+
str_replace(char *string, const char *find, char *replace)
+
{
+
	char *prev, *tmp;
+
	char *new_string;
+
	size_t newlen;
+

+
	new_string = malloc(1);
+
	new_string[0] = '\0';
+
	prev = string;
+
	tmp = string;
+
	while ((tmp = strstr(tmp, find)) != NULL) {
+
		tmp[0] = '\0';
+
		tmp += strlen(find);
+
		newlen = strlen(new_string) + strlen(prev) + strlen(replace) + 1;
+
		new_string = realloc(new_string, newlen);
+
		strlcat(new_string, prev, newlen);
+
		strlcat(new_string, replace, newlen);
+
		prev = tmp;
+
	}
+
	newlen = strlen(new_string) + strlen(prev) + 1;
+
	strlcat(new_string, prev, newlen);
+

+
	return new_string;
}
modified libpkg/util.h
@@ -2,4 +2,5 @@
#define _PKG_UTIL_H

off_t file_to_buffer(const char *path, char **buffer);
+
char *str_replace(char *string, const char *find, char *replace);
#endif