# HG changeset patch # User jbe # Date 1427332319 -3600 # Node ID 985ffb3ef69f550ba883ee6aea317b8c7a6b9074 # Parent 34bf5f7abe0d03b6059848bdafa447b6c062c970 Added new function extos.stat(...) diff -r 34bf5f7abe0d -r 985ffb3ef69f libraries/extos/extos.autodoc.lua --- a/libraries/extos/extos.autodoc.lua Thu Mar 26 01:56:10 2015 +0100 +++ b/libraries/extos/extos.autodoc.lua Thu Mar 26 02:11:59 2015 +0100 @@ -20,6 +20,43 @@ --[[-- +filestat_table, -- table with information on the file +errmsg = -- error message if file information could not be read +extos.stat( + filename -- path to a file on the file system +) + +Return information on a file. The returned table contains the following fields: + +- "dev" (numeric ID of the device containing the file) +- "ino" (file's inode number) +- "nlink" (number of hard links to the file) +- "atime" (time when file data was last accessed) +- "mtime" (time when file data was last modified) +- "ctime" (time when file status was last changed) +- "size" (file size in bytes) +- "blksize" (optimal I/O block size for the file) +- "blocks" (actual number of blocks allocated for the file in 512-byte units) +- "uid" (user ID of the file's owner) +- "gid" (group ID of the file) +- "mode" (bitfield including the access permissions) +- "isblk" (true if block special file) +- "ischr" (true if character special file) +- "isdir" (true if directory) +- "isfifo" (true if pope of FIFO special file) +- "islnk" (true if symbolic link) +- "isreg" (true if regular file) +- "issock" (true if socket) + +On error, nil and an error message are returned. + +--]]-- +-- implemented in extos.c as +-- static int extos_stat(lua_State *L) +--//-- + + +--[[-- directory_entries = -- table of directory entries extos.listdir{ path = path -- path name diff -r 34bf5f7abe0d -r 985ffb3ef69f libraries/extos/extos.c --- a/libraries/extos/extos.c Thu Mar 26 01:56:10 2015 +0100 +++ b/libraries/extos/extos.c Thu Mar 26 02:11:59 2015 +0100 @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -295,6 +296,59 @@ return 1; } +static int extos_stat(lua_State *L) { + const char *filename; + struct stat sb; + filename = luaL_checkstring(L, 1); + if (stat(filename, &sb)) { + char errmsg[EXTOS_MAX_ERRLEN+1]; + strerror_r(errno, errmsg, EXTOS_MAX_ERRLEN+1); + lua_pushnil(L); + lua_pushfstring(L, "Could not get file stats for \"%s\": %s", filename, errmsg); + return 2; + } + lua_createtable(L, 0, 19); + lua_pushinteger(L, sb.st_dev); + lua_setfield(L, -2, "dev"); + lua_pushinteger(L, sb.st_ino); + lua_setfield(L, -2, "ino"); + lua_pushinteger(L, sb.st_nlink); + lua_setfield(L, -2, "nlink"); + lua_pushinteger(L, sb.st_atime); + lua_setfield(L, -2, "atime"); + lua_pushinteger(L, sb.st_mtime); + lua_setfield(L, -2, "mtime"); + lua_pushinteger(L, sb.st_ctime); + lua_setfield(L, -2, "ctime"); + lua_pushinteger(L, sb.st_size); + lua_setfield(L, -2, "size"); + lua_pushinteger(L, sb.st_blksize); + lua_setfield(L, -2, "blksize"); + lua_pushinteger(L, sb.st_blocks); + lua_setfield(L, -2, "blocks"); + lua_pushinteger(L, sb.st_uid); + lua_setfield(L, -2, "uid"); + lua_pushinteger(L, sb.st_gid); + lua_setfield(L, -2, "gid"); + lua_pushinteger(L, sb.st_mode); + lua_setfield(L, -2, "mode"); + lua_pushboolean(L, S_ISBLK(sb.st_mode)); + lua_setfield(L, -2, "isblk"); + lua_pushboolean(L, S_ISCHR(sb.st_mode)); + lua_setfield(L, -2, "ischr"); + lua_pushboolean(L, S_ISDIR(sb.st_mode)); + lua_setfield(L, -2, "isdir"); + lua_pushboolean(L, S_ISFIFO(sb.st_mode)); + lua_setfield(L, -2, "isfifo"); + lua_pushboolean(L, S_ISLNK(sb.st_mode)); + lua_setfield(L, -2, "islnk"); + lua_pushboolean(L, S_ISREG(sb.st_mode)); + lua_setfield(L, -2, "isreg"); + lua_pushboolean(L, S_ISSOCK(sb.st_mode)); + lua_setfield(L, -2, "issock"); + return 1; +} + static int extos_crypt(lua_State *L) { const char *key; const char *salt; @@ -331,6 +385,7 @@ static const struct luaL_Reg extos_module_functions[] = { {"pfilter", extos_pfilter}, {"listdir", extos_listdir}, + {"stat", extos_stat}, {"crypt", extos_crypt}, {"hires_time", extos_hires_time}, {"monotonic_hires_time", extos_monotonic_hires_time},