From d186dda4d7229924678523e5ff59682780a9244f Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Wed, 4 May 2016 14:44:45 +0300 Subject: Return errno from lfs.mkdir on error Change pushresult() to return true on success. Change make_link to keep returning 0. --- src/lfs.c | 57 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/src/lfs.c b/src/lfs.c index 2b85d30..25535ce 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -133,6 +133,13 @@ typedef struct dir_data { #define LSTAT_FUNC lstat #endif +#ifdef _WIN32 + #define lfs_mkdir _mkdir +#else + #define lfs_mkdir(path) (mkdir((path), \ + S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH)) +#endif + /* ** Utility functions */ @@ -147,12 +154,13 @@ static int pusherror(lua_State *L, const char *info) return 3; } -static int pushresult(lua_State *L, int i, const char *info) -{ - if (i==-1) - return pusherror(L, info); - lua_pushinteger(L, i); - return 1; +static int pushresult(lua_State *L, int res, const char *info) { + if (res == -1) { + return pusherror(L, info); + } else { + lua_pushboolean(L, 1); + return 1; + } } @@ -417,16 +425,20 @@ static int file_unlock (lua_State *L) { ** @param #2 Name of link. ** @param #3 True if link is symbolic (optional). */ -static int make_link(lua_State *L) -{ +static int make_link (lua_State *L) { #ifndef _WIN32 - const char *oldpath = luaL_checkstring(L, 1); - const char *newpath = luaL_checkstring(L, 2); - return pushresult(L, - (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL); + const char *oldpath = luaL_checkstring(L, 1); + const char *newpath = luaL_checkstring(L, 2); + int res = (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath); + if (res == -1) { + return pusherror(L, NULL); + } else { + lua_pushinteger(L, 0); + return 1; + } #else - errno = ENOSYS; /* = "Function not implemented" */ - return pushresult(L, -1, "make_link is not supported on Windows"); + errno = ENOSYS; /* = "Function not implemented" */ + return pushresult(L, -1, "make_link is not supported on Windows"); #endif } @@ -436,21 +448,8 @@ static int make_link(lua_State *L) ** @param #1 Directory path. */ static int make_dir (lua_State *L) { - const char *path = luaL_checkstring (L, 1); - int fail; -#ifdef _WIN32 - fail = _mkdir (path); -#else - fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | - S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH ); -#endif - if (fail) { - lua_pushnil (L); - lua_pushfstring (L, "%s", strerror(errno)); - return 2; - } - lua_pushboolean (L, 1); - return 1; + const char *path = luaL_checkstring(L, 1); + return pushresult(L, lfs_mkdir(path), NULL); } -- cgit v1.2.3-55-g6feb From 8b85d257a6c6ed3cbe1c80240ab168b5f3117cc3 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Wed, 4 May 2016 15:08:13 +0300 Subject: Return errno from lfs.rmdir on error --- src/lfs.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/lfs.c b/src/lfs.c index 25535ce..2ead179 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -458,18 +458,8 @@ static int make_dir (lua_State *L) { ** @param #1 Directory path. */ static int remove_dir (lua_State *L) { - const char *path = luaL_checkstring (L, 1); - int fail; - - fail = rmdir (path); - - if (fail) { - lua_pushnil (L); - lua_pushfstring (L, "%s", strerror(errno)); - return 2; - } - lua_pushboolean (L, 1); - return 1; + const char *path = luaL_checkstring(L, 1); + return pushresult(L, rmdir(path), NULL); } -- cgit v1.2.3-55-g6feb From 8f167ef1de6ecbe1a6b4b375c92c370b995c6874 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Wed, 4 May 2016 15:17:21 +0300 Subject: Return errno from lfs.touch on error --- src/lfs.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/lfs.c b/src/lfs.c index 2ead179..d9b21e6 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -646,26 +646,24 @@ static const char *mode2string (mode_t mode) { /* -** Set access time and modification values for file +** Set access time and modification values for a file. +** @param #1 File path. +** @param #2 Access time in seconds, current time is used if missing. +** @param #3 Modification time in seconds, access time is used if missing. */ static int file_utime (lua_State *L) { - const char *file = luaL_checkstring (L, 1); - struct utimbuf utb, *buf; - - if (lua_gettop (L) == 1) /* set to current date/time */ - buf = NULL; - else { - utb.actime = (time_t)luaL_optnumber (L, 2, 0); - utb.modtime = (time_t) luaL_optinteger (L, 3, utb.actime); - buf = &utb; - } - if (utime (file, buf)) { - lua_pushnil (L); - lua_pushfstring (L, "%s", strerror (errno)); - return 2; - } - lua_pushboolean (L, 1); - return 1; + const char *file = luaL_checkstring(L, 1); + struct utimbuf utb, *buf; + + if (lua_gettop (L) == 1) /* set to current date/time */ + buf = NULL; + else { + utb.actime = (time_t) luaL_optnumber(L, 2, 0); + utb.modtime = (time_t) luaL_optinteger(L, 3, utb.actime); + buf = &utb; + } + + return pushresult(L, utime(file, buf), NULL); } -- cgit v1.2.3-55-g6feb From b37e88b3d6128b85586231200c6d1a2f05ecd0d6 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Thu, 5 May 2016 12:29:05 +0300 Subject: Return errno from lfs.attributes on error --- src/lfs.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lfs.c b/src/lfs.c index d9b21e6..0387e15 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -800,7 +800,8 @@ static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { if (st(file, &info)) { lua_pushnil(L); lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno)); - return 2; + lua_pushinteger(L, errno); + return 3; } if (lua_isstring (L, 2)) { const char *member = lua_tostring (L, 2); -- cgit v1.2.3-55-g6feb From 8d5f9661e954be91a17d915f214bd14f44622e99 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Wed, 4 May 2016 15:44:19 +0300 Subject: Test what lfs.attributes returns on error --- tests/test.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test.lua b/tests/test.lua index abfbd4d..5872717 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -136,7 +136,10 @@ io.write(".") io.flush() -- Trying to get attributes of a non-existent file -assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file") +local attr_ok, err, errno = lfs.attributes("this couldn't be an actual file") +assert(attr_ok == nil, "could get attributes of a non-existent file") +assert(type(err) == "string", "failed lfs.attributes did not return an error message") +assert(type(errno) == "number", "failed lfs.attributes did not return error code") assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") io.write(".") -- cgit v1.2.3-55-g6feb From 81e5b165bf324689777b967143c751786c405ce4 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Wed, 4 May 2016 15:49:56 +0300 Subject: Update docs for new return values --- doc/us/manual.html | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/us/manual.html b/doc/us/manual.html index 86c3406..beffe03 100644 --- a/doc/us/manual.html +++ b/doc/us/manual.html @@ -104,7 +104,7 @@ LuaFileSystem offers the following functions:
lfs.attributes (filepath [, aname | atable])
Returns a table with the file attributes corresponding to - filepath (or nil followed by an error message + filepath (or nil followed by an error message and a system-dependent error code in case of error). If the second optional argument is given and is a string, then only the value of the named attribute is returned (this use is equivalent to @@ -222,14 +222,14 @@ LuaFileSystem offers the following functions:
lfs.mkdir (dirname)
Creates a new directory. The argument is the name of the new directory.
- Returns true if the operation was successful; - in case of error, it returns nil plus an error string. + Returns true in case of success or nil, an error message and + a system-dependent error code in case of error.
lfs.rmdir (dirname)
Removes an existing directory. The argument is the name of the directory.
- Returns true if the operation was successful; - in case of error, it returns nil plus an error string.
+ Returns true in case of success or nil, an error message and + a system-dependent error code in case of error.
lfs.setmode (file, mode)
Sets the writing mode for a file. The mode string can be either "binary" or "text". @@ -255,8 +255,8 @@ LuaFileSystem offers the following functions: Lua standard function os.time). If the modification time is omitted, the access time provided is used; if both times are omitted, the current time is used.
- Returns true if the operation was successful; - in case of error, it returns nil plus an error string. + Returns true in case of success or nil, an error message and + a system-dependent error code in case of error.
lfs.unlock (filehandle[, start[, length]])
-- cgit v1.2.3-55-g6feb