fs: init function "status"

fs.status returns a table containing information concerning the status
of a file. Currently, the table includes the following information:

  string  path       -- the given path to the file; the first argument
  integer mode       -- bitmask of file modes; see chmod(2)
  integer uid        -- owner of the file in UID form
  integer gid        -- owner group of the file in GID form
  integer accessdate -- date the file was last accessed
  integer modifydate -- date the file was last modified
  integer chdate     -- date the file's status last changed

Dates are given in the form of an integer number representing the
number of seconds since Jan 1, 1970 (unixtime).

For more information consult the definition of sys/stat.h
according to POSIX:
  https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_stat.h.html
This commit is contained in:
Jeremy Baxter 2024-03-20 16:00:46 +13:00
parent cfca6cf880
commit d8519cc39f

35
lfs.c
View file

@ -523,6 +523,40 @@ fs_rmdir(lua_State *L)
return lfail(L); return lfail(L);
} }
static int
fs_status(lua_State *L)
{
struct stat s;
const char *path; /* parameter 1 (string) */
path = luaL_checkstring(L, 1);
if (lstat(path, &s) == -1)
return lfail(L);
lua_createtable(L, 0, 7);
lua_pushvalue(L, 1);
lua_setfield(L, -2, "path");
lua_pushinteger(L, s.st_mode);
lua_setfield(L, -2, "mode");
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_atim.tv_sec);
lua_setfield(L, -2, "accessdate");
lua_pushinteger(L, s.st_mtim.tv_sec);
lua_setfield(L, -2, "modifydate");
lua_pushinteger(L, s.st_ctim.tv_sec);
lua_setfield(L, -2, "chdate");
return 1;
}
/*** /***
* Returns or sets the current working directory. * Returns or sets the current working directory.
* *
@ -583,6 +617,7 @@ static const luaL_Reg fslib[] = {
{"move", fs_move}, {"move", fs_move},
{"remove", fs_remove}, {"remove", fs_remove},
{"rmdir", fs_rmdir}, {"rmdir", fs_rmdir},
{"status", fs_status},
{"workdir", fs_workdir}, {"workdir", fs_workdir},
{NULL, NULL} {NULL, NULL}
}; };