Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Merge actions_script and validation for keywords
Baptiste Daroussin committed 5 years ago
commit b3b2224db1e15096a2fe685291187aed6d7ea97d
parent 7b4c01e
2 files changed +30 -72
modified libpkg/pkg_ports.c
@@ -826,7 +826,7 @@ parse_actions(const ucl_object_t *o, struct plist *p,
	const char *actname;
	ucl_object_iter_t it = NULL;
	int i, j = 0;
-
	int rc, r = EPKG_OK;
+
	int r, rc = EPKG_OK;

	while ((cur = ucl_iterate_object(o, &it, true))) {
		actname = ucl_object_tostring(cur);
@@ -935,7 +935,6 @@ apply_keyword_file(ucl_object_t *obj, struct plist *p, char *line, struct file_a
	struct file_attr *freeattr = NULL;
	int spaces, argc = 0;
	int ret = EPKG_FATAL;
-
	int pstat;

	if ((o = ucl_object_find_key(obj,  "arguments")) && ucl_object_toboolean(o)) {
		spaces = pkg_utils_count_spaces(line);
@@ -946,52 +945,6 @@ apply_keyword_file(ucl_object_t *obj, struct plist *p, char *line, struct file_a
		}
	}

-
	if ((o = ucl_object_find_key(obj, "validation"))) {
-
		pid_t pid = fork();
-
		if (pid == 0) {
-
			lua_State *L = luaL_newstate();
-
			luaL_openlibs(L);
-
			lua_pushlightuserdata(L, p->pkg);
-
			lua_setglobal(L, "package");
-
			lua_pushstring(L, line);
-
			lua_setglobal(L, "line");
-
			lua_args_table(L, args, argc);
-
			lua_override_ios(L);
-
#ifdef HAVE_CAPSICUM
-
			if (cap_enter() < 0 && errno != ENOSYS) {
-
				err(1, "cap_enter failed");
-
			}
-
#endif
-
			pkg_debug(3, "Scripts: executing lua\n--- BEGIN ---"
-
			    "\n%s\nScripts: --- END ---", ucl_object_tostring(o));
-
			if (luaL_dostring(L, ucl_object_tostring(o))) {
-
				pkg_emit_error("Failed to execute lua script: "
-
				    "%s", lua_tostring(L, -1));
-
				lua_close(L);
-
				exit(1);
-
			}
-

-
			lua_close(L);
-
			exit(0);
-
		} else if (pid < 0) {
-
			pkg_emit_errno("Cannot fork", "lua validation script");
-
			ret = EPKG_FATAL;
-
			goto keywords_cleanup;
-
		}
-
		while (waitpid(pid, &pstat, 0) == -1) {
-
			if (errno != EINTR) {
-
				pkg_emit_error("waitpid() failed: %s",
-
				    strerror(errno));
-
			}
-
		}
-
		if (WEXITSTATUS(pstat) != 0) {
-
			pkg_emit_error("lua validation script failed for '%s'",
-
			    line);
-
			ret = EPKG_FATAL;
-
			goto keywords_cleanup;
-
		}
-
	}
-

	if ((o = ucl_object_find_key(obj,  "attributes")))
		parse_attributes(o, attr != NULL ? &attr : &freeattr);

@@ -1040,7 +993,7 @@ apply_keyword_file(ucl_object_t *obj, struct plist *p, char *line, struct file_a
	if ((o = ucl_object_find_key(obj,  "actions")))
		ret = parse_actions(o, p, line, attr, argc, args);

-
	if (ret == EPKG_OK && (o = ucl_object_find_key(obj, "actions_script"))) {
+
	if (ret == EPKG_OK && (o = ucl_object_find_key(obj, "prepackaging"))) {
		lua_State *L = luaL_newstate();
		static const luaL_Reg plist_lib[] = {
			{ "config", lua_config },
@@ -1141,6 +1094,9 @@ external_keyword(struct plist *plist, char *keyword, char *line, struct file_att
		}
	}
	ret = apply_keyword_file(o, plist, line, attr);
+
	if (ret != EPKG_OK) {
+
		pkg_emit_error("Fail to apply keyword '%s'", keyword);
+
	}

	return (ret);
}
modified tests/frontend/create.sh
@@ -281,57 +281,57 @@ create_from_plist_keyword_validation_body() {

cat << EOF > test.ucl
actions: []
-
validation: <<EOS
+
prepackaging: <<EOS
io.stderr:write("meh\n")
-
os.exit(1)
+
return 1
EOS
EOF
	atf_check \
		-o empty \
-
		-e inline:"meh\n${PROGNAME}: lua validation script failed for 'file1'\n" \
+
		-e inline:"meh\n${PROGNAME}: Fail to apply keyword 'test'\n" \
		-s exit:1 \
		pkg -o PLIST_KEYWORDS_DIR=. create -o ${TMPDIR} -m . -p test.plist -r .

cat << EOF > test.ucl
actions: []
-
validation: <<EOS
+
prepackaging: <<EOS
print(line)
io.stderr:write("meh\n")
-
os.exit(1)
+
return 1
EOS
EOF
	atf_check \
		-o inline:"file1\n" \
-
		-e inline:"meh\n${PROGNAME}: lua validation script failed for 'file1'\n" \
+
		-e inline:"meh\n${PROGNAME}: Fail to apply keyword 'test'\n" \
		-s exit:1 \
		pkg -o PLIST_KEYWORDS_DIR=. create -o ${TMPDIR} -m . -p test.plist -r .

cat << EOF > test.ucl
actions: []
-
validation: <<EOS
+
prepackaging: <<EOS
print(#arg)
io.stderr:write("meh\n")
-
os.exit(1)
+
return 1
EOS
EOF
	atf_check \
		-o inline:"0\n" \
-
		-e inline:"meh\n${PROGNAME}: lua validation script failed for 'file1'\n" \
+
		-e inline:"meh\n${PROGNAME}: Fail to apply keyword 'test'\n" \
		-s exit:1 \
		pkg -o PLIST_KEYWORDS_DIR=. create -o ${TMPDIR} -m . -p test.plist -r .

cat << EOF > test.ucl
actions: []
arguments: true
-
validation: <<EOS
+
prepackaging: <<EOS
print(#arg)
io.stderr:write("meh\n")
-
os.exit(1)
+
return 1
EOS
EOF
	atf_check \
		-o inline:"1\n" \
-
		-e inline:"meh\n${PROGNAME}: lua validation script failed for 'file1'\n" \
+
		-e inline:"meh\n${PROGNAME}: Fail to apply keyword 'test'\n" \
		-s exit:1 \
		pkg -o PLIST_KEYWORDS_DIR=. create -o ${TMPDIR} -m . -p test.plist -r .

@@ -341,25 +341,26 @@ EOF
cat << EOF > test.ucl
actions: []
arguments: true
-
validation: <<EOS
+
prepackaging: <<EOS
if #arg == 1 then
-
   os.exit(0)
+
  return 0
end
if #arg == 2 then
  if arg[1] == arg[2] then
    io.stderr:write("The first and the second argument are identical\n")
-
    os.exit(1)
+
    return 1
  end
-
  os.exit(0)
+
  return 1
end
io.stderr:write("Invalid number of arguments '".. #arg .. "' expecting 1 or 2\n")
-
os.exit(1)
+
return 1
EOS
EOF
-
output="The first and the second argument are identical
-
${PROGNAME}: lua validation script failed for 'A A'
+
output="${PROGNAME}: Fail to apply keyword 'test'
+
The first and the second argument are identical
+
${PROGNAME}: Fail to apply keyword 'test'
Invalid number of arguments '3' expecting 1 or 2
-
${PROGNAME}: lua validation script failed for 'A B C'
+
${PROGNAME}: Fail to apply keyword 'test'
"
	atf_check \
		-e inline:"${output}" \
@@ -385,7 +386,7 @@ EOF

	atf_check \
		-o empty \
-
		-e inline:"${PROGNAME}: Requesting argument %2 while only 1 arguments are available\n" \
+
		-e inline:"${PROGNAME}: Requesting argument %2 while only 1 arguments are available\n${PROGNAME}: Fail to apply keyword 'testkeyword'\n" \
		-s exit:1 \
		pkg -o PLIST_KEYWORDS_DIR=. create -o ${TMPDIR} -m . -p test.plist -r .

@@ -400,7 +401,7 @@ EOF

	atf_check \
		-o empty \
-
		-e inline:"${PROGNAME}: Invalid argument: expecting a number got (%1)\n" \
+
		-e inline:"${PROGNAME}: Invalid argument: expecting a number got (%1)\n${PROGNAME}: Fail to apply keyword 'testkeyword'\n" \
		-s exit:1 \
		pkg -o PLIST_KEYWORDS_DIR=. create -o ${TMPDIR} -m . -p test.plist -r .

@@ -668,7 +669,7 @@ create_from_plist_keyword_lua_actions_body()

cat << EOF > test.ucl
arguments: true
-
actions_script: <<EOS
+
prepackaging: <<EOS
ok = true
for i = 1, #arg do
	if not pkg.file(arg[i]) then
@@ -687,6 +688,7 @@ touch D

output="${PROGNAME}: Unable to access file ./A:No such file or directory
${PROGNAME}: Unable to access file ./B:No such file or directory
+
${PROGNAME}: Fail to apply keyword 'test'
"

	atf_check \