webmcp
changeset 344:985ffb3ef69f
Added new function extos.stat(...)
author | jbe |
---|---|
date | Thu Mar 26 02:11:59 2015 +0100 (2015-03-26) |
parents | 34bf5f7abe0d |
children | 546ec50e7170 |
files | libraries/extos/extos.autodoc.lua libraries/extos/extos.c |
line diff
1.1 --- a/libraries/extos/extos.autodoc.lua Thu Mar 26 01:56:10 2015 +0100 1.2 +++ b/libraries/extos/extos.autodoc.lua Thu Mar 26 02:11:59 2015 +0100 1.3 @@ -20,6 +20,43 @@ 1.4 1.5 1.6 --[[-- 1.7 +filestat_table, -- table with information on the file 1.8 +errmsg = -- error message if file information could not be read 1.9 +extos.stat( 1.10 + filename -- path to a file on the file system 1.11 +) 1.12 + 1.13 +Return information on a file. The returned table contains the following fields: 1.14 + 1.15 +- "dev" (numeric ID of the device containing the file) 1.16 +- "ino" (file's inode number) 1.17 +- "nlink" (number of hard links to the file) 1.18 +- "atime" (time when file data was last accessed) 1.19 +- "mtime" (time when file data was last modified) 1.20 +- "ctime" (time when file status was last changed) 1.21 +- "size" (file size in bytes) 1.22 +- "blksize" (optimal I/O block size for the file) 1.23 +- "blocks" (actual number of blocks allocated for the file in 512-byte units) 1.24 +- "uid" (user ID of the file's owner) 1.25 +- "gid" (group ID of the file) 1.26 +- "mode" (bitfield including the access permissions) 1.27 +- "isblk" (true if block special file) 1.28 +- "ischr" (true if character special file) 1.29 +- "isdir" (true if directory) 1.30 +- "isfifo" (true if pope of FIFO special file) 1.31 +- "islnk" (true if symbolic link) 1.32 +- "isreg" (true if regular file) 1.33 +- "issock" (true if socket) 1.34 + 1.35 +On error, nil and an error message are returned. 1.36 + 1.37 +--]]-- 1.38 +-- implemented in extos.c as 1.39 +-- static int extos_stat(lua_State *L) 1.40 +--//-- 1.41 + 1.42 + 1.43 +--[[-- 1.44 directory_entries = -- table of directory entries 1.45 extos.listdir{ 1.46 path = path -- path name
2.1 --- a/libraries/extos/extos.c Thu Mar 26 01:56:10 2015 +0100 2.2 +++ b/libraries/extos/extos.c Thu Mar 26 02:11:59 2015 +0100 2.3 @@ -4,6 +4,7 @@ 2.4 #include <time.h> 2.5 #include <unistd.h> 2.6 #include <sys/types.h> 2.7 +#include <sys/stat.h> 2.8 #include <sys/wait.h> 2.9 #include <signal.h> 2.10 #include <errno.h> 2.11 @@ -295,6 +296,59 @@ 2.12 return 1; 2.13 } 2.14 2.15 +static int extos_stat(lua_State *L) { 2.16 + const char *filename; 2.17 + struct stat sb; 2.18 + filename = luaL_checkstring(L, 1); 2.19 + if (stat(filename, &sb)) { 2.20 + char errmsg[EXTOS_MAX_ERRLEN+1]; 2.21 + strerror_r(errno, errmsg, EXTOS_MAX_ERRLEN+1); 2.22 + lua_pushnil(L); 2.23 + lua_pushfstring(L, "Could not get file stats for \"%s\": %s", filename, errmsg); 2.24 + return 2; 2.25 + } 2.26 + lua_createtable(L, 0, 19); 2.27 + lua_pushinteger(L, sb.st_dev); 2.28 + lua_setfield(L, -2, "dev"); 2.29 + lua_pushinteger(L, sb.st_ino); 2.30 + lua_setfield(L, -2, "ino"); 2.31 + lua_pushinteger(L, sb.st_nlink); 2.32 + lua_setfield(L, -2, "nlink"); 2.33 + lua_pushinteger(L, sb.st_atime); 2.34 + lua_setfield(L, -2, "atime"); 2.35 + lua_pushinteger(L, sb.st_mtime); 2.36 + lua_setfield(L, -2, "mtime"); 2.37 + lua_pushinteger(L, sb.st_ctime); 2.38 + lua_setfield(L, -2, "ctime"); 2.39 + lua_pushinteger(L, sb.st_size); 2.40 + lua_setfield(L, -2, "size"); 2.41 + lua_pushinteger(L, sb.st_blksize); 2.42 + lua_setfield(L, -2, "blksize"); 2.43 + lua_pushinteger(L, sb.st_blocks); 2.44 + lua_setfield(L, -2, "blocks"); 2.45 + lua_pushinteger(L, sb.st_uid); 2.46 + lua_setfield(L, -2, "uid"); 2.47 + lua_pushinteger(L, sb.st_gid); 2.48 + lua_setfield(L, -2, "gid"); 2.49 + lua_pushinteger(L, sb.st_mode); 2.50 + lua_setfield(L, -2, "mode"); 2.51 + lua_pushboolean(L, S_ISBLK(sb.st_mode)); 2.52 + lua_setfield(L, -2, "isblk"); 2.53 + lua_pushboolean(L, S_ISCHR(sb.st_mode)); 2.54 + lua_setfield(L, -2, "ischr"); 2.55 + lua_pushboolean(L, S_ISDIR(sb.st_mode)); 2.56 + lua_setfield(L, -2, "isdir"); 2.57 + lua_pushboolean(L, S_ISFIFO(sb.st_mode)); 2.58 + lua_setfield(L, -2, "isfifo"); 2.59 + lua_pushboolean(L, S_ISLNK(sb.st_mode)); 2.60 + lua_setfield(L, -2, "islnk"); 2.61 + lua_pushboolean(L, S_ISREG(sb.st_mode)); 2.62 + lua_setfield(L, -2, "isreg"); 2.63 + lua_pushboolean(L, S_ISSOCK(sb.st_mode)); 2.64 + lua_setfield(L, -2, "issock"); 2.65 + return 1; 2.66 +} 2.67 + 2.68 static int extos_crypt(lua_State *L) { 2.69 const char *key; 2.70 const char *salt; 2.71 @@ -331,6 +385,7 @@ 2.72 static const struct luaL_Reg extos_module_functions[] = { 2.73 {"pfilter", extos_pfilter}, 2.74 {"listdir", extos_listdir}, 2.75 + {"stat", extos_stat}, 2.76 {"crypt", extos_crypt}, 2.77 {"hires_time", extos_hires_time}, 2.78 {"monotonic_hires_time", extos_monotonic_hires_time},