Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
lua: add a function to make symlinks
Baptiste Daroussin committed 3 years ago
commit 9bf1a55fa2decc8f2b8e7e37fd99bf366de924be
parent c754e31
6 files changed +66 -3
modified docs/pkg-lua-script.5
@@ -11,7 +11,7 @@
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
-
.Dd March 26, 2021
+
.Dd July 27, 2022
.Dt PKG-LUA-SCRIPT 5
.Os
.Sh NAME
@@ -125,6 +125,11 @@ with the following fields:
.Va size ,
.Va uid ,
.Va gid
+
.It Fn pkg.symlink "source" "destination"
+
Create a symbolic link
+
.Va destination
+
pointing at
+
.Va source
.It Fn pkg.exec arguments
Will execute the command
.Ar arguments
modified libpkg/lua.c
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2019-2021 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2019-2022 Baptiste Daroussin <bapt@FreeBSD.org>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
@@ -328,6 +328,21 @@ lua_pkg_filecmp(lua_State *L)
}

int
+
lua_pkg_symlink(lua_State *L)
+
{
+
	int n = lua_gettop(L);
+
	luaL_argcheck(L, n == 2, n > 2 ? 3 : n,
+
	    "pkg.symlink takes exactly two arguments");
+
	const char *from = luaL_checkstring(L, 1);
+
	const char *to = luaL_checkstring(L, 2);
+
	lua_getglobal(L, "rootfd");
+
	int rootfd = lua_tointeger(L, -1);
+
	if (symlinkat(from, rootfd, RELATIVE_PATH(to)) == -1)
+
		return (luaL_fileresult(L, 0, from));
+
	return (1);
+
}
+

+
int
lua_prefix_path(lua_State *L)
{
	int n = lua_gettop(L);
modified libpkg/lua_scripts.c
@@ -87,6 +87,7 @@ pkg_lua_script_run(struct pkg * const pkg, pkg_lua_script type, bool upgrade)
				{ "stat", lua_stat },
				{ "readdir", lua_readdir },
				{ "exec", lua_exec },
+
				{ "symlink", lua_pkg_symlink },
				{ NULL, NULL },
			};
			close(cur_pipe[0]);
modified libpkg/private/lua.h
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2019 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2019-2022 Baptiste Daroussin <bapt@FreeBSD.org>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
@@ -31,6 +31,7 @@ lua_CFunction stack_dump(lua_State *L);
int lua_print_msg(lua_State *L);
int lua_pkg_copy(lua_State *L);
int lua_pkg_filecmp(lua_State *L);
+
int lua_pkg_symlink(lua_State *L);
int lua_prefix_path(lua_State *L);
int lua_exec(lua_State *L);
void lua_override_ios(lua_State *L, bool);
modified libpkg/triggers.c
@@ -443,6 +443,7 @@ trigger_execute_lua(const char *script, bool sandbox, pkghash *args)
			{ "stat", lua_stat },
			{ "readdir", lua_readdir },
			{ "exec", lua_exec },
+
			{ "symlink", lua_pkg_symlink },
			{ NULL, NULL },
		};
		luaL_newlib(L, pkg_lib);
modified tests/lib/lua.c
@@ -23,6 +23,8 @@
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

+
#include <sys/stat.h>
+

#include <atf-c.h>
#include <err.h>
#include <unistd.h>
@@ -353,6 +355,7 @@ ATF_TC_BODY(override, tc)

ATF_TC_BODY(fileops, tc)
{
+
	char b[1024];
	int rootfd = open(getcwd(NULL, 0), O_DIRECTORY);
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);
@@ -362,6 +365,7 @@ ATF_TC_BODY(fileops, tc)
	static const luaL_Reg test_lib[] = {
		{ "copy", lua_pkg_copy },
		{ "cmp", lua_pkg_filecmp},
+
		{ "symlink", lua_pkg_symlink},
		{ NULL, NULL },
	};
	luaL_newlib(L, test_lib);
@@ -459,6 +463,42 @@ ATF_TC_BODY(fileops, tc)
		exit(lua_tonumber(L, -1));
	}
	atf_utils_wait(p, 0, "", "");
+

+
	p = atf_utils_fork();
+
	if (p == 0) {
+
		if (luaL_dostring(L, "test.symlink(\"a\", \"b\", \"meh\")\n")) {
+
			printf("%s\n", lua_tostring(L, -1));
+
		}
+
		exit(lua_tonumber(L, -1));
+
	}
+
	atf_utils_wait(p, 0, "[string \"test.symlink(\"a\", \"b\", \"meh\")...\"]:1: bad argument #3 to 'symlink' (pkg.symlink takes exactly two arguments)\n", "");
+

+
	p = atf_utils_fork();
+
	if (p == 0) {
+
		if (luaL_dostring(L, "test.symlink(\"a\")\n")) {
+
			printf("%s\n", lua_tostring(L, -1));
+
		}
+
		exit(lua_tonumber(L, -1));
+
	}
+
	atf_utils_wait(p, 0, "[string \"test.symlink(\"a\")...\"]:1: bad argument #1 to 'symlink' (pkg.symlink takes exactly two arguments)\n", "");
+

+
	p = atf_utils_fork();
+
	if (p == 0) {
+
		if (luaL_dostring(L, "test.symlink(\"a\", \"b\")\n")) {
+
			printf("%s\n", lua_tostring(L, -1));
+
		}
+
		exit(lua_tonumber(L, -1));
+
	}
+
	atf_utils_wait(p, 0, "", "");
+
	struct stat st;
+
	if (lstat("b", &st) != 0)
+
		atf_tc_fail("File 'b' not created");
+
	if (!S_ISLNK(st.st_mode))
+
		atf_tc_fail("File 'b' is not a symlink");
+
	memset(b, 0, sizeof(b));
+
	readlink("b", b, sizeof(b));
+
	ATF_REQUIRE_STREQ(b, "a");
+

}

ATF_TC_BODY(prefix_path, tc)