Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Fix backward compatibility for pkg-message.
Vsevolod Stakhov committed 10 years ago
commit cf3421ce8c3c9faf55a2ed20d0040622641db771
parent 78928ad
3 files changed +53 -11
modified libpkg/pkg.c
@@ -1718,21 +1718,30 @@ pkg_open_root_fd(struct pkg *pkg)
int
pkg_message_from_ucl(struct pkg *pkg, const ucl_object_t *obj)
{
-
	struct pkg_message *msg;
+
	struct pkg_message *msg = NULL;
	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));
+
		if (pkg->message == NULL) {
+
			msg = calloc(1, sizeof(*msg));
+

+
			if (msg == NULL) {
+
				pkg_emit_errno("malloc", "struct pkg_message");
+
				return (EPKG_FATAL);
+
			}
+
			msg->str = strdup(ucl_object_tostring(obj));
+
			msg->legacy = true;
+
		}
+
		else {
+
			/* Do no rewrite message with legacy message */
+
			return (EPKG_OK);
+
		}
+

	}
-
	else if (ucl_object_type(obj) == UCL_OBJECT) {
+
	else {
		/* New format of pkg message */
+

		elt = ucl_object_find_key(obj, "message");

		if (elt == NULL || ucl_object_type(elt) != UCL_STRING) {
@@ -1741,6 +1750,29 @@ pkg_message_from_ucl(struct pkg *pkg, const ucl_object_t *obj)
			return (EPKG_FATAL);
		}

+
		if (pkg->message != NULL) {
+
			if (pkg->message->legacy) {
+
				/* We can re-use legacy message */
+
				msg = pkg->message;
+
				msg->legacy = false;
+
				free(msg->str);
+
				msg->str = NULL;
+
			}
+
			else {
+
				pkg_emit_error("trying to rewrite message in a package");
+

+
				return (EPKG_FATAL);
+
			}
+
		}
+
		else {
+
			msg = calloc(1, sizeof(*msg));
+

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

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

modified libpkg/pkg_manifest.c
@@ -60,6 +60,9 @@
#define PKG_PROVIDES		-18
#define PKG_REQUIRES		-19

+
#define PKG_MESSAGE_LEGACY	1
+
#define PKG_MESSAGE_NEW 2
+

static int pkg_string(struct pkg *, const ucl_object_t *, uint32_t);
static int pkg_obj(struct pkg *, const ucl_object_t *, uint32_t);
static int pkg_array(struct pkg *, const ucl_object_t *, uint32_t);
@@ -138,7 +141,10 @@ static struct pkg_manifest_key {
	{ "maintainer",          offsetof(struct pkg, maintainer),
			TYPE_SHIFT(UCL_STRING), pkg_string},

-
	{ "message",             PKG_MESSAGE,
+
	{ "message_new",             PKG_MESSAGE_NEW,
+
			TYPE_SHIFT(UCL_STRING)|TYPE_SHIFT(UCL_OBJECT), pkg_message},
+

+
	{ "message",             PKG_MESSAGE_LEGACY,
			TYPE_SHIFT(UCL_STRING)|TYPE_SHIFT(UCL_OBJECT), pkg_message},

	{ "name",                offsetof(struct pkg, name),
@@ -1223,7 +1229,10 @@ pkg_emit_object(struct pkg *pkg, short flags)
	if (pkg->message != NULL) {
		ucl_object_insert_key(top,
			pkg_message_to_ucl(pkg),
-
		    "message", 7, false);
+
			"message_new", sizeof("message_new") - 1, false);
+
		ucl_object_insert_key(top,
+
			ucl_object_fromstring(pkg->message->str),
+
			"message", sizeof("message") - 1, false);
	}

	return (top);
modified libpkg/private/pkg.h
@@ -278,6 +278,7 @@ struct pkg_message {
	char		*str;
	char		*minimum_version;
	char		*maximum_version;
+
	bool		legacy;
};

enum pkg_conflict_type {