Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add more flexibility to keyword arguments
Baptiste Daroussin committed 11 years ago
commit 91871d92ec6b43b60a9eb13074ffc55168108ed9
parent 6e0310c
3 files changed +113 -46
modified libpkg/pkg_ports.c
@@ -982,6 +982,71 @@ external_keyword(struct plist *plist, char *keyword, char *line, struct file_att
	return (ret);
}

+
static struct file_attr *
+
parse_keyword_args(char *args, char *keyword)
+
{
+
	struct file_attr *attr;
+
	char *owner, *group, *permstr, *fflags;
+
	void *set = NULL;
+
	u_long fset = 0;
+

+
	owner = group = permstr = fflags = NULL;
+

+
	/* remove last ')' */
+
	args[strlen(args) -1] = '\0';
+

+
	do {
+
		args[0] = '\0';
+
		args++;
+
		if (*args == '\0')
+
			break;
+
		if (owner == NULL) {
+
			owner = args;
+
		} else if (group == NULL) {
+
			group = args;
+
		} else if (permstr == NULL) {
+
			permstr = args;
+
		} else if (fflags == NULL) {
+
			fflags = args;
+
			break;
+
		} else {
+
			pkg_emit_error("Malformed keyword '%s', expecting "
+
			    "keyword or keyword(owner,group,mode,fflags...)",
+
			    keyword);
+
			return (NULL);
+
		}
+
	} while ((args = strchr(args, ',')) != NULL);
+

+
	if (fflags != NULL && *fflags != '\0') {
+
		if (strtofflags(&fflags, &fset, NULL) != 0) {
+
			pkg_emit_error("Malformed keyword '%s', wrong fflags",
+
			    keyword);
+
			return (NULL);
+
		}
+
	}
+

+
	if (permstr != NULL && *permstr != '\0') {
+
		if ((set = parse_mode(permstr)) == NULL) {
+
			pkg_emit_error("Malformed keyword '%s', wrong mode "
+
			    "section", keyword);
+
			return (NULL);
+
		}
+
	}
+

+
	attr = calloc(1, sizeof(struct file_attr));
+
	if (owner != NULL && *owner != '\0')
+
		attr->owner = strdup(owner);
+
	if (group != NULL && *group != '\0')
+
		attr->group = strdup(group);
+
	if (set != NULL) {
+
		attr->mode = getmode(set, 0);
+
		free(set);
+
	}
+
	attr->fflags = fset;
+

+
	return (attr);
+
}
+

static int
parse_keywords(struct plist *plist, char *keyword, char *line)
{
@@ -990,10 +1055,6 @@ parse_keywords(struct plist *plist, char *keyword, char *line)
	struct file_attr *attr = NULL;
	char *tmp;
	int ret = EPKG_FATAL;
-
	char *owner = NULL;
-
	char *group = NULL;
-
	char *permstr = NULL;
-
	void *set = NULL;

	if ((tmp = strchr(keyword, '(')) != NULL &&
	    keyword[strlen(keyword) -1] != ')') {
@@ -1003,43 +1064,9 @@ parse_keywords(struct plist *plist, char *keyword, char *line)
	}

	if (tmp != NULL) {
-
		tmp[0] = '\0';
-
		tmp++;
-
		tmp[strlen(tmp) -1] = '\0';
-
		owner = tmp;
-
		if ((tmp = strchr(tmp, ',')) == NULL) {
-
			pkg_emit_error("Malformed keyword %s, expecting @keyword "
-
			    "or @keyword(owner,group,mode)", keyword);
-
			return (ret);
-
		}
-
		tmp[0] = '\0';
-
		tmp++;
-
		group = tmp;
-
		if ((tmp = strchr(tmp, ',')) == NULL) {
-
			pkg_emit_error("Malformed keyword %s, expecting @keyword "
-
			    "or @keyword(owner,group,mode)", keyword);
+
		attr = parse_keyword_args(tmp, keyword);
+
		if (attr == NULL)
			return (ret);
-
		}
-
		tmp[0] = '\0';
-
		tmp++;
-
		permstr = tmp;
-
		if (*permstr != '\0' && ((set = parse_mode(permstr)) == NULL)) {
-
			pkg_emit_error("Malformed keyword %s, wrong mode section",
-
			    keyword);
-
			return (ret);
-
		}
-

-
		/* remove the trailing ) */
-
		permstr[strlen(permstr) - 1] = '\0';
-
		attr = calloc(1, sizeof(struct file_attr));
-
		if (*owner != '\0')
-
			attr->owner = strdup(owner);
-
		if (*group != '\0')
-
			attr->group = strdup(group);
-
		if (set != NULL) {
-
			attr->mode = getmode(set, 0);
-
			free(set);
-
		}
	}

	/* if keyword is empty consider it as a file */
modified libpkg/private/pkg.h
@@ -442,6 +442,7 @@ struct file_attr {
	char *owner;
	char *group;
	mode_t mode;
+
	u_long fflags;
};

struct action {
modified tests/frontend/create.shin
@@ -158,19 +158,26 @@ create_from_plist_set_mode_body() {
		tar tvf test-1.txz
}

-
atf_test_case create_from_plist_bad_creds
-
create_from_plist_bad_creds_head() {
+
atf_test_case create_from_plist_mini
+
create_from_plist_mini_head() {
	atf_set "descr" "Testing credentials set from the plist"
}
-
create_from_plist_bad_creds_body() {
+
create_from_plist_mini_body() {

	preparetestcredentials "(plop,)"

	atf_check \
		-o empty \
-
		-e inline:"pkg: Malformed keyword , expecting @keyword or @keyword(owner,group,mode)\n" \
-
		-s exit:70 \
+
		-e empty \
+
		-s exit:0 \
		pkg create -o ${TMPDIR} -m . -p test.plist -r .
+

+
	basic_validation
+
	atf_check \
+
		-o match:"-rw-r--r-- .*plop +wheel.* /file1$" \
+
		-e empty \
+
		-s exit:0 \
+
		tar tvf test-1.txz
}

atf_test_case create_from_plist_dirrm
@@ -232,6 +239,36 @@ aline"
		pkg -o DEVELOPER_MODE=yes create -o ${TMPDIR} -m . -p test.plist -r .
}

+
atf_test_case create_from_plist_fflags
+
create_from_plist_fflags_head() {
+
	atf_set "descr" "Test fflags set from plist"
+
}
+

+
create_from_plist_fflags_body() {
+
	preparetestcredentials "(,,,schg)"
+

+
	atf_check \
+
		-o empty \
+
		-e empty \
+
		-s exit:0 \
+
		pkg create -o ${TMPDIR} -m . -p test.plist -r .
+
}
+

+
atf_test_case create_from_plist_bad_fflags
+
create_from_plist_bad_fflags_head() {
+
	atf_set "descr" "Test bad fflags set from plist"
+
}
+

+
create_from_plist_bad_fflags_body() {
+
	preparetestcredentials "(,,,schg,bad)"
+

+
	atf_check \
+
		-o empty \
+
		-e inline:"pkg: Malformed keyword '', wrong fflags\n" \
+
		-s exit:70 \
+
		pkg create -o ${TMPDIR} -m . -p test.plist -r .
+
}
+

atf_init_test_cases() {
        . $(atf_get_srcdir)/test_environment.sh

@@ -240,7 +277,9 @@ atf_init_test_cases() {
	atf_add_test_case create_from_plist_set_group
	atf_add_test_case create_from_plist_gather_mode
	atf_add_test_case create_from_plist_set_mode
-
	atf_add_test_case create_from_plist_bad_creds
+
	atf_add_test_case create_from_plist_mini
	atf_add_test_case create_from_plist_dirrm
	atf_add_test_case create_from_plist_ignore
+
	atf_add_test_case create_from_plist_fflags
+
	atf_add_test_case create_from_plist_bad_fflags
}