Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
More rework for new pkg-message.
Vsevolod Stakhov committed 10 years ago
commit 0bcc13cf2414188fa502e09ab4cf18226064800f
parent 73772ce
7 files changed +133 -61
modified libpkg/pkg.c
@@ -1686,3 +1686,107 @@ pkg_open_root_fd(struct pkg *pkg)

	return (EPKG_FATAL);
}
+

+
int
+
pkg_message_from_ucl(struct pkg *pkg, const ucl_object_t *obj)
+
{
+
	struct pkg_message *msg;
+
	const ucl_object_t *elt;
+

+
	msg = calloc(1, sizeof(*msg));
+

+
	if (msg == NULL) {
+
		pkg_emit_errno("malloc", "struct pkg_message");
+
		return (EPKG_FATAL);
+
	}
+

+
	if (ucl_object_type(obj) == UCL_STRING) {
+
		msg->str = strdup(ucl_object_tostring(obj));
+
	}
+
	else if (ucl_object_type(obj) == UCL_OBJECT) {
+
		/* New format of pkg message */
+
		elt = ucl_object_find_key(obj, "message");
+

+
		if (elt == NULL || ucl_object_type(elt) != UCL_STRING) {
+
			pkg_emit_error("package message lacks 'message' key that is required");
+

+
			return (EPKG_FATAL);
+
		}
+

+
		msg->str = strdup(ucl_object_tostring(elt));
+
		elt = ucl_object_find_key(obj, "minimum_version");
+

+
		if (elt != NULL && ucl_object_type(elt) == UCL_STRING) {
+
			msg->minimum_version = strdup(ucl_object_tostring(elt));
+
		}
+
		elt = ucl_object_find_key(obj, "maximum_version");
+

+
		if (elt != NULL && ucl_object_type(elt) == UCL_STRING) {
+
			msg->maximum_version = strdup(ucl_object_tostring(elt));
+
		}
+
	}
+

+
	pkg->message = msg;
+

+
	return (EPKG_OK);
+
}
+

+
int
+
pkg_message_from_str(struct pkg *pkg, const char *str, size_t len)
+
{
+
	struct ucl_parser *parser;
+
	struct ucl_object *obj;
+
	int ret = EPKG_FATAL;
+

+
	assert(str != NULL);
+

+
	if (len == 0) {
+
		len = strlen(str);
+
	}
+

+
	parser = ucl_parser_new(0);
+

+
	if (ucl_parser_add_chunk(parser, (const unsigned char*)str, len)) {
+
		obj = ucl_parser_get_object(parser);
+
		ucl_parser_free(parser);
+

+
		ret = pkg_message_from_ucl(pkg, obj);
+
		ucl_object_unref(obj);
+

+
		return (ret);
+
	}
+

+
	ucl_parser_free (parser);
+

+
	return (ret);
+
}
+

+
char*
+
pkg_message_to_str(struct pkg *pkg)
+
{
+
	ucl_object_t *obj;
+
	char *ret = NULL;
+

+
	if (pkg->message == NULL) {
+
		return (NULL);
+
	}
+

+
	obj = ucl_object_typed_new (UCL_OBJECT);
+
	ucl_object_insert_key(obj, ucl_object_fromstring(pkg->message->str),
+
			"message", 0, false);
+

+
	if (pkg->message->maximum_version) {
+
		ucl_object_insert_key(obj,
+
				ucl_object_fromstring(pkg->message->maximum_version),
+
				"maximum_version", 0, false);
+
	}
+
	if (pkg->message->minimum_version) {
+
		ucl_object_insert_key(obj,
+
				ucl_object_fromstring(pkg->message->minimum_version),
+
				"minimum_version", 0, false);
+
	}
+

+
	ret = ucl_object_emit(obj, UCL_EMIT_JSON_COMPACT);
+

+
	return (ret);
+
}
modified libpkg/pkg_create.c
@@ -289,8 +289,7 @@ pkg_load_message_from_file(int fd, struct pkg *pkg, const char *path, bool is_uc
	char *buf = NULL;
	char *cp;
	off_t size = 0;
-
	int ret = EPKG_OK;
-
	struct ucl_parser *parser;
+
	int ret;
	struct ucl_object *obj;

	assert(pkg != NULL);
@@ -300,25 +299,14 @@ pkg_load_message_from_file(int fd, struct pkg *pkg, const char *path, bool is_uc
		pkg_debug(1, "Reading message: '%s'", path);

		if ((ret = file_to_bufferat(fd, path, &buf, &size)) != EPKG_OK) {
-
			return (false);
+
			return (ret);
		}

		if (is_ucl) {
-
			parser = ucl_parser_new(0);
-

-
			if (ucl_parser_add_chunk(parser, (const unsigned char*)buf, size)) {
-
				obj = ucl_parser_get_object(parser);
-
				ucl_parser_free(parser);
-
				free(buf);
-

-
				ret = pkg_message_from_ucl(pkg, obj);
-
				ucl_object_unref(obj);
-

-
				return (ret);
-
			}
-

-
			ucl_parser_free (parser);
+
			ret = pkg_message_from_str(pkg, buf, size);
			free(buf);
+

+
			return (ret);
		}
		else {
			obj = ucl_object_fromlstring(buf, size);
modified libpkg/pkg_manifest.c
@@ -552,45 +552,6 @@ pkg_obj(struct pkg *pkg, const ucl_object_t *obj, int attr)
	return (EPKG_OK);
}

-
int
-
pkg_message_from_ucl(struct pkg *pkg, const ucl_object_t *obj)
-
{
-
	struct pkg_message *msg;
-
	const ucl_object_t *elt;
-

-
	msg = calloc(1, sizeof(*msg));
-

-
	if (msg == NULL) {
-
		pkg_emit_errno("malloc", "struct pkg_message");
-
		return (EPKG_FATAL);
-
	}
-

-
	if (ucl_object_type(obj) == UCL_STRING) {
-
		msg->str = strdup(ucl_object_tostring(obj));
-
	}
-
	else if (ucl_object_type(obj) == UCL_OBJECT) {
-
		/* New format of pkg message */
-
		elt = ucl_object_find_key(obj, "message");
-

-
		if (elt == NULL || ucl_object_type(elt) != UCL_STRING) {
-
			pkg_emit_error("package message lacks 'message' key that is required");
-

-
			return (EPKG_FATAL);
-
		}
-

-
		msg->str = strdup(ucl_object_tostring(elt));
-
		elt = ucl_object_find_key(obj, "minimum_version");
-

-
		if (elt != NULL && ucl_object_type(elt) == UCL_STRING) {
-
			msg->minimum_version = strdup(ucl_object_tostring(elt));
-
		}
-
	}
-

-
	pkg->message = msg;
-

-
	return (EPKG_OK);
-
}
-

static int
pkg_message(struct pkg *pkg, const ucl_object_t *obj, int attr)
{
modified libpkg/pkg_printf.c
@@ -1239,7 +1239,7 @@ format_message(struct sbuf *sbuf, const void *data, struct percent_esc *p)
{
	const struct pkg	*pkg = data;

-
	return (string_val(sbuf, pkg->message, p));
+
	return (string_val(sbuf, pkg->message ? pkg->message->str : NULL, p));
}

/*
modified libpkg/pkgdb.c
@@ -1668,7 +1668,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
	struct pkg_conflict	*conflict = NULL;
	struct pkg_config_file	*cf = NULL;
	struct pkgdb_it		*it = NULL;
-
	char			*buf;
+
	char			*buf, *msg = NULL;

	sqlite3			*s;

@@ -1696,8 +1696,9 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)
	/*
	 * Insert package record
	 */
+
	msg = pkg_message_to_str(pkg);
	ret = run_prstmt(PKG, pkg->origin, pkg->name, pkg->version,
-
	    pkg->comment, pkg->desc, pkg->message, arch, pkg->maintainer,
+
	    pkg->comment, pkg->desc, msg, arch, pkg->maintainer,
	    pkg->www, pkg->prefix, pkg->flatsize, (int64_t)pkg->automatic,
	    (int64_t)pkg->licenselogic, NULL, pkg->digest, pkg->dep_formula);
	if (ret != SQLITE_DONE) {
@@ -1957,6 +1958,8 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int complete, int forced)

	cleanup:

+
	free(msg);
+

	return (retcode);
}

modified libpkg/pkgdb_iterator.c
@@ -777,7 +777,7 @@ static void
populate_pkg(sqlite3_stmt *stmt, struct pkg *pkg) {
	int		 icol = 0;
	const char	*colname;
-
	char		 legacyarch[BUFSIZ];
+
	char		 legacyarch[BUFSIZ], *msg;

	assert(stmt != NULL);

@@ -816,7 +816,20 @@ populate_pkg(sqlite3_stmt *stmt, struct pkg *pkg) {
				pkg->digest = strdup(sqlite3_column_text(stmt, icol));
				break;
			case PKG_MESSAGE:
-
				pkg->message = strdup(sqlite3_column_text(stmt, icol));
+
				msg = sqlite3_column_text(stmt, icol);
+
				if (msg) {
+
					/* A stupid logic to detect legacy pkg message */
+
					if (msg[0] == '{') {
+
						pkg_message_from_str(pkg, msg, 0);
+
					}
+
					else {
+
						pkg->message = calloc(1, sizeof(*pkg->message));
+
						pkg->message->str = strdup(msg);
+
					}
+
				}
+
				else {
+
					pkg->message = NULL;
+
				}
				break;
			case PKG_NAME:
				pkg->name = strdup(sqlite3_column_text(stmt, icol));
modified libpkg/private/pkg.h
@@ -273,6 +273,7 @@ struct pkg_dep {
struct pkg_message {
	char		*str;
	char		*minimum_version;
+
	char		*maximum_version;
};

enum pkg_conflict_type {
@@ -732,5 +733,7 @@ int pkg_addoption_description(struct pkg *pkg, const char *key, const char *desc
int pkg_arch_to_legacy(const char *arch, char *dest, size_t sz);
bool pkg_is_config_file(struct pkg *p, const char *path, const struct pkg_file **file, struct pkg_config_file **cfile);
int pkg_message_from_ucl(struct pkg *pkg, const ucl_object_t *obj);
+
int pkg_message_from_str(struct pkg *pkg, const char *str, size_t len);
+
char* pkg_message_to_str(struct pkg *pkg);

#endif