Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
lua: add metalog_copy
Baptiste Daroussin committed 6 months ago
commit a2f455f7da1c3b58cf03dad2bbe9fc92a89a71ff
parent 72aedda
5 files changed +59 -6
modified libpkg/lua.c
@@ -1,5 +1,5 @@
/*-
-
 * Copyright (c) 2019-2022 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2019-2025 Baptiste Daroussin <bapt@FreeBSD.org>
 * Copyright (c) 2023 Serenity Cyber Security, LLC
 *                    Author: Gleb Popov <arrowd@FreeBSD.org>
 * All rights reserved.
@@ -569,3 +569,25 @@ lua_readdir(lua_State *L)
	}
	return 1;
}
+

+
int
+
lua_metalog_copy(lua_State *L)
+
{
+
	int n = lua_gettop(L);
+
	luaL_argcheck(L, n == 2, n > 2 ? 3 : n,
+
	    "pkg.metalog_copy takes exactly two arguments");
+
	const char *src = luaL_checkstring(L, 1);
+
	const char *dst = luaL_checkstring(L, 2);
+
	lua_getglobal(L, "package");
+
	struct pkg *p = lua_touserdata(L, -1);
+
	struct pkg_file *f = pkg_get_file(p, src);
+
	if (f == NULL) {
+
		lua_pushnil(L);
+
		lua_pushstring(L, "Unknown source file");
+
		return (2);
+
	}
+
	/* TODO: what about symlinks ? */
+
	metalog_add(PKG_METALOG_FILE, RELATIVE_PATH(dst),
+
	    f->uname, f->gname, f->perm & ~S_IFREG, f->fflags, f->symlink_target);
+
	return (1);
+
}
modified libpkg/lua_scripts.c
@@ -69,6 +69,7 @@ pkg_lua_script_run(struct pkg * const pkg, pkg_lua_script type, bool upgrade)
				{ "readdir", lua_readdir },
				{ "exec", lua_exec },
				{ "symlink", lua_pkg_symlink },
+
				{ "metalog_copy", lua_metalog_copy },
				{ NULL, NULL },
			};
			close(cur_pipe[0]);
modified libpkg/pkg_attributes.c
@@ -63,7 +63,6 @@ pkg_dep_is_locked(struct pkg_dep const * const d)
void
pkg_file_free(struct pkg_file *file)
{
-

	free(file->sum);
	free(file);
}
modified libpkg/private/lua.h
@@ -1,7 +1,7 @@
/*-
-
 * Copyright (c) 2019-2022 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * Copyright (c) 2019-2025 Baptiste Daroussin <bapt@FreeBSD.org>
 * All rights reserved.
-
 * 
+
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
@@ -11,7 +11,7 @@
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
-
 * 
+
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
@@ -33,6 +33,7 @@ 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_metalog_copy(lua_State *L);
int lua_exec(lua_State *L);
void lua_override_ios(lua_State *L, bool);
int lua_stat(lua_State *L);
modified tests/frontend/lua.sh
@@ -19,7 +19,8 @@ tests_init \
	script_sample_not_exists_two_files \
	script_sample_exists \
	script_stat \
-
	script_arguments
+
	script_arguments \
+
	script_metalog_add

script_arguments_body() {
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1" "/"
@@ -750,3 +751,32 @@ EOF
		-s exit:0 \
		pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.pkg
}
+

+
script_metalog_add_body()
+
{
+
	atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1" "/"
+
	touch plop
+
	echo "@(root,wheel,440) /plop" > test.plist
+
	cat << EOF >> test.ucl
+
lua_scripts: {
+
	post-install: [ <<EOS
+
	assert(pkg.metalog_copy("/plop", "/meh"))
+
EOS
+
, ]
+
}
+
EOF
+

+
OUTPUT="./plop type=file uname=root gname=wheel mode=440
+
./meh type=file uname=root gname=wheel mode=440
+
"
+
	atf_check -o ignore pkg create -M test.ucl -p test.plist -r .
+
	mkdir ${TMPDIR}/target
+
	atf_check -s exit:0 \
+
		pkg \
+
			-o REPOS_DIR=/dev/null \
+
			-o METALOG=${TMPDIR}/METALOG \
+
			-r ${TMPDIR}/target \
+
			install \
+
			-qfy ${TMPDIR}/test-1.pkg
+
	atf_check -o inline:"${OUTPUT}" cat METALOG
+
}