webmcp

diff libraries/extos/extos.c @ 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 985ffb3ef69f
children 32b64f641761
line diff
     1.1 --- a/libraries/extos/extos.c	Thu Mar 26 12:26:00 2015 +0100
     1.2 +++ b/libraries/extos/extos.c	Thu Mar 26 16:38:30 2015 +0100
     1.3 @@ -296,16 +296,30 @@
     1.4    return 1;
     1.5  }
     1.6  
     1.7 -static int extos_stat(lua_State *L) {
     1.8 -  const char *filename;
     1.9 +#define EXTOS_STAT_FOLLOW -1
    1.10 +#define EXTOS_STAT_NOFOLLOW -2
    1.11 +
    1.12 +static int extos_stat_impl(lua_State *L, int fd) {
    1.13    struct stat sb;
    1.14 -  filename = luaL_checkstring(L, 1);
    1.15 -  if (stat(filename, &sb)) {
    1.16 -    char errmsg[EXTOS_MAX_ERRLEN+1];
    1.17 -    strerror_r(errno, errmsg, EXTOS_MAX_ERRLEN+1);
    1.18 -    lua_pushnil(L);
    1.19 -    lua_pushfstring(L, "Could not get file stats for \"%s\": %s", filename, errmsg);
    1.20 -    return 2;
    1.21 +  if (fd < 0) {
    1.22 +    const char *filename;
    1.23 +    filename = luaL_checkstring(L, 1);
    1.24 +    if (fd == EXTOS_STAT_FOLLOW ? stat(filename, &sb) : lstat(filename, &sb)) {
    1.25 +      char errmsg[EXTOS_MAX_ERRLEN+1];
    1.26 +      strerror_r(errno, errmsg, EXTOS_MAX_ERRLEN+1);
    1.27 +      if (errno == ENOENT) lua_pushboolean(L, 0);
    1.28 +      else lua_pushnil(L);
    1.29 +      lua_pushfstring(L, "Could not get file stats for \"%s\": %s", filename, errmsg);
    1.30 +      return 2;
    1.31 +    }
    1.32 +  } else {
    1.33 +    if (fstat(fd, &sb)) {
    1.34 +      char errmsg[EXTOS_MAX_ERRLEN+1];
    1.35 +      strerror_r(errno, errmsg, EXTOS_MAX_ERRLEN+1);
    1.36 +      lua_pushnil(L);
    1.37 +      lua_pushfstring(L, "Could not get file stats for open file: %s", errmsg);
    1.38 +      return 2;
    1.39 +    }
    1.40    }
    1.41    lua_createtable(L, 0, 19);
    1.42    lua_pushinteger(L, sb.st_dev);
    1.43 @@ -349,6 +363,21 @@
    1.44    return 1;
    1.45  }
    1.46  
    1.47 +static int extos_stat(lua_State *L) {
    1.48 +  return extos_stat_impl(L, EXTOS_STAT_FOLLOW);
    1.49 +}
    1.50 +
    1.51 +static int extos_lstat(lua_State *L) {
    1.52 +  return extos_stat_impl(L, EXTOS_STAT_NOFOLLOW);
    1.53 +}
    1.54 +
    1.55 +static int extos_fstat(lua_State *L) {
    1.56 +  luaL_Stream *stream;
    1.57 +  stream = luaL_checkudata(L, 1, LUA_FILEHANDLE);
    1.58 +  if (!stream->closef) luaL_error(L, "attempt to use a closed file");
    1.59 +  return extos_stat_impl(L, fileno(stream->f));
    1.60 +}
    1.61 +
    1.62  static int extos_crypt(lua_State *L) {
    1.63    const char *key;
    1.64    const char *salt;
    1.65 @@ -386,6 +415,8 @@
    1.66    {"pfilter",              extos_pfilter},
    1.67    {"listdir",              extos_listdir},
    1.68    {"stat",                 extos_stat},
    1.69 +  {"lstat",                extos_lstat},
    1.70 +  {"fstat",                extos_fstat},
    1.71    {"crypt",                extos_crypt},
    1.72    {"hires_time",           extos_hires_time},
    1.73    {"monotonic_hires_time", extos_monotonic_hires_time},

Impressum / About Us