Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add a simple stat() function
Baptiste Daroussin committed 5 years ago
commit 2a0b8761ff33cd4ea5ec414018f43d5965a439d1
parent 93e3998
2 files changed +32 -0
modified docs/pkg-lua-script.5
@@ -118,6 +118,13 @@ if an error occured
Copy a file preserving its attributes. return
.Va -1
if an error occured
+
.It Ft st Fn pkg.stat "file"
+
return an object table
+
.Ft st
+
with the following fields:
+
.Va size ,
+
.Va uid ,
+
.Va gid
.El
.Sh SEE ALSO
.Xr pkg_printf 3 ,
modified libpkg/lua_scripts.c
@@ -325,6 +325,30 @@ lua_os_rename(lua_State *L)
}

static int
+
lua_stat(lua_State *L)
+
{
+
	const char *path = RELATIVE_PATH(luaL_checkstring(L, 1));
+
	lua_getglobal(L, "package");
+
	struct pkg *pkg = lua_touserdata(L, -1);
+
	struct stat s;
+

+
	if (fstatat(pkg->rootfd, path, &s, AT_SYMLINK_NOFOLLOW) == -1) {
+
		return lua_pushnil(L), 1;
+
	}
+

+
	lua_createtable(L, 0, 3);
+
	lua_pushinteger(L, s.st_size);
+
	lua_setfield(L, -2, "size");
+
	lua_pushinteger(L, s.st_uid);
+
	lua_setfield(L, -2, "uid");
+
	lua_pushinteger(L, s.st_gid);
+
	lua_setfield(L, -2, "gid");
+
	lua_pushinteger(L, s.st_nlink);
+

+
	return (1);
+
}
+

+
static int
lua_os_execute(lua_State *L)
{
	return (luaL_error(L, "os.execute not available"));
@@ -389,6 +413,7 @@ pkg_lua_script_run(struct pkg * const pkg, pkg_lua_script type, bool upgrade)
				{ "prefixed_path", lua_prefix_path },
				{ "filecmp", lua_pkg_filecmp },
				{ "copy", lua_pkg_copy },
+
				{ "stat", lua_stat },
				{ NULL, NULL },
			};
			close(cur_pipe[0]);