webmcp

changeset 351:b1748c6c3c89

extos.stat(...) returns false (instead of nil) if file does not exist; Added extos.lstat(...) and extos.fstat(...)
author jbe
date Thu Mar 26 16:38:30 2015 +0100 (2015-03-26)
parents e00d11c12b68
children 2b5bdf9028fb
files libraries/extos/extos.autodoc.lua libraries/extos/extos.c
line diff
     1.1 --- a/libraries/extos/extos.autodoc.lua	Thu Mar 26 12:26:00 2015 +0100
     1.2 +++ b/libraries/extos/extos.autodoc.lua	Thu Mar 26 16:38:30 2015 +0100
     1.3 @@ -20,13 +20,30 @@
     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 +directory_entries,  -- table of directory entries
    1.10 +errmsg =            -- error message if directory could not be read
    1.11 +extos.listdir(
    1.12 +  path              -- path name
    1.13 +)
    1.14 +
    1.15 +This function returns a table containing strings representing each entry in a directory. On error nil and an error message are returned.
    1.16 +
    1.17 +--]]--
    1.18 +-- implemented in extos.c as
    1.19 +-- static int extos_listdir(lua_State *L)
    1.20 +--//--
    1.21 +
    1.22 +
    1.23 +--[[--
    1.24 +filestat_table,  -- table with information on the file, false if file does not exist, nil on error
    1.25 +errmsg =         -- error message if file information could not be read or file does not exist
    1.26  extos.stat(
    1.27    filename       -- path to a file on the file system
    1.28  )
    1.29  
    1.30 -Return information on a file. The returned table contains the following fields:
    1.31 +Return information on a file, following symbolic links if applicable. See also: extos.lstat(...) and extos.fstat(...).
    1.32 +
    1.33 +The returned table contains the following fields:
    1.34  
    1.35  - "dev" (numeric ID of the device containing the file)
    1.36  - "ino" (file's inode number)
    1.37 @@ -48,7 +65,9 @@
    1.38  - "isreg" (true if regular file)
    1.39  - "issock" (true if socket)
    1.40  
    1.41 -On error, nil and an error message are returned.
    1.42 +If the file does not exist, false and an error message are returned.
    1.43 +In case of any other error, nil and an error message are returned.
    1.44 +
    1.45  
    1.46  --]]--
    1.47  -- implemented in extos.c as
    1.48 @@ -57,17 +76,36 @@
    1.49  
    1.50  
    1.51  --[[--
    1.52 -directory_entries,  -- table of directory entries
    1.53 -errmsg =            -- error message if directory could not be read
    1.54 -extos.listdir(
    1.55 -  path              -- path name
    1.56 +filestat_table,  -- table with information on the file, false if file does not exist, nil on error
    1.57 +errmsg =         -- error message if file information could not be read or file does not exist
    1.58 +extos.lstat(
    1.59 +  filename       -- path to a file on the file system
    1.60  )
    1.61  
    1.62 -This function returns a table containing strings representing each entry in a directory. On error nil and an error message are returned.
    1.63 +Return information on a file. Symbolic links are not followed, which means that if the filename points to a symbolic link, information on that symbolic link will be returned. Otherwise this function behaves like extos.stat(filename).
    1.64 +
    1.65 +See extos.stat(...) for further information.
    1.66  
    1.67  --]]--
    1.68  -- implemented in extos.c as
    1.69 --- static int extos_listdir(lua_State *L)
    1.70 +-- static int extos_stat(lua_State *L)
    1.71 +--//--
    1.72 +
    1.73 +
    1.74 +--[[--
    1.75 +filestat_table,  -- table with information on the file, nil on error
    1.76 +errmsg =         -- error message if file information could not be determined
    1.77 +extos.fstat(
    1.78 +  file_handle    -- Lua file handle (e.g. as returned by io.open(...))
    1.79 +)
    1.80 +
    1.81 +Return information on an open file. The file is specified by passing an open file handle to this function. Otherwise this function behaves like extos.stat(...).
    1.82 +
    1.83 +See extos.stat(...) for further information.
    1.84 +
    1.85 +--]]--
    1.86 +-- implemented in extos.c as
    1.87 +-- static int extos_stat(lua_State *L)
    1.88  --//--
    1.89  
    1.90  
     2.1 --- a/libraries/extos/extos.c	Thu Mar 26 12:26:00 2015 +0100
     2.2 +++ b/libraries/extos/extos.c	Thu Mar 26 16:38:30 2015 +0100
     2.3 @@ -296,16 +296,30 @@
     2.4    return 1;
     2.5  }
     2.6  
     2.7 -static int extos_stat(lua_State *L) {
     2.8 -  const char *filename;
     2.9 +#define EXTOS_STAT_FOLLOW -1
    2.10 +#define EXTOS_STAT_NOFOLLOW -2
    2.11 +
    2.12 +static int extos_stat_impl(lua_State *L, int fd) {
    2.13    struct stat sb;
    2.14 -  filename = luaL_checkstring(L, 1);
    2.15 -  if (stat(filename, &sb)) {
    2.16 -    char errmsg[EXTOS_MAX_ERRLEN+1];
    2.17 -    strerror_r(errno, errmsg, EXTOS_MAX_ERRLEN+1);
    2.18 -    lua_pushnil(L);
    2.19 -    lua_pushfstring(L, "Could not get file stats for \"%s\": %s", filename, errmsg);
    2.20 -    return 2;
    2.21 +  if (fd < 0) {
    2.22 +    const char *filename;
    2.23 +    filename = luaL_checkstring(L, 1);
    2.24 +    if (fd == EXTOS_STAT_FOLLOW ? stat(filename, &sb) : lstat(filename, &sb)) {
    2.25 +      char errmsg[EXTOS_MAX_ERRLEN+1];
    2.26 +      strerror_r(errno, errmsg, EXTOS_MAX_ERRLEN+1);
    2.27 +      if (errno == ENOENT) lua_pushboolean(L, 0);
    2.28 +      else lua_pushnil(L);
    2.29 +      lua_pushfstring(L, "Could not get file stats for \"%s\": %s", filename, errmsg);
    2.30 +      return 2;
    2.31 +    }
    2.32 +  } else {
    2.33 +    if (fstat(fd, &sb)) {
    2.34 +      char errmsg[EXTOS_MAX_ERRLEN+1];
    2.35 +      strerror_r(errno, errmsg, EXTOS_MAX_ERRLEN+1);
    2.36 +      lua_pushnil(L);
    2.37 +      lua_pushfstring(L, "Could not get file stats for open file: %s", errmsg);
    2.38 +      return 2;
    2.39 +    }
    2.40    }
    2.41    lua_createtable(L, 0, 19);
    2.42    lua_pushinteger(L, sb.st_dev);
    2.43 @@ -349,6 +363,21 @@
    2.44    return 1;
    2.45  }
    2.46  
    2.47 +static int extos_stat(lua_State *L) {
    2.48 +  return extos_stat_impl(L, EXTOS_STAT_FOLLOW);
    2.49 +}
    2.50 +
    2.51 +static int extos_lstat(lua_State *L) {
    2.52 +  return extos_stat_impl(L, EXTOS_STAT_NOFOLLOW);
    2.53 +}
    2.54 +
    2.55 +static int extos_fstat(lua_State *L) {
    2.56 +  luaL_Stream *stream;
    2.57 +  stream = luaL_checkudata(L, 1, LUA_FILEHANDLE);
    2.58 +  if (!stream->closef) luaL_error(L, "attempt to use a closed file");
    2.59 +  return extos_stat_impl(L, fileno(stream->f));
    2.60 +}
    2.61 +
    2.62  static int extos_crypt(lua_State *L) {
    2.63    const char *key;
    2.64    const char *salt;
    2.65 @@ -386,6 +415,8 @@
    2.66    {"pfilter",              extos_pfilter},
    2.67    {"listdir",              extos_listdir},
    2.68    {"stat",                 extos_stat},
    2.69 +  {"lstat",                extos_lstat},
    2.70 +  {"fstat",                extos_fstat},
    2.71    {"crypt",                extos_crypt},
    2.72    {"hires_time",           extos_hires_time},
    2.73    {"monotonic_hires_time", extos_monotonic_hires_time},

Impressum / About Us