diff options
author | Peter Melnichenko <mpeterval@gmail.com> | 2016-05-04 14:44:45 +0300 |
---|---|---|
committer | Peter Melnichenko <mpeterval@gmail.com> | 2016-05-05 12:38:28 +0300 |
commit | d186dda4d7229924678523e5ff59682780a9244f (patch) | |
tree | 46e8a30973063bc37fbab87e8002f4b586354f9b | |
parent | 1937ba848b275408e837fb49dc8a83eca6a031fe (diff) | |
download | luafilesystem-d186dda4d7229924678523e5ff59682780a9244f.tar.gz luafilesystem-d186dda4d7229924678523e5ff59682780a9244f.tar.bz2 luafilesystem-d186dda4d7229924678523e5ff59682780a9244f.zip |
Return errno from lfs.mkdir on error
Change pushresult() to return true on success.
Change make_link to keep returning 0.
-rw-r--r-- | src/lfs.c | 57 |
1 files changed, 28 insertions, 29 deletions
@@ -133,6 +133,13 @@ typedef struct dir_data { | |||
133 | #define LSTAT_FUNC lstat | 133 | #define LSTAT_FUNC lstat |
134 | #endif | 134 | #endif |
135 | 135 | ||
136 | #ifdef _WIN32 | ||
137 | #define lfs_mkdir _mkdir | ||
138 | #else | ||
139 | #define lfs_mkdir(path) (mkdir((path), \ | ||
140 | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH)) | ||
141 | #endif | ||
142 | |||
136 | /* | 143 | /* |
137 | ** Utility functions | 144 | ** Utility functions |
138 | */ | 145 | */ |
@@ -147,12 +154,13 @@ static int pusherror(lua_State *L, const char *info) | |||
147 | return 3; | 154 | return 3; |
148 | } | 155 | } |
149 | 156 | ||
150 | static int pushresult(lua_State *L, int i, const char *info) | 157 | static int pushresult(lua_State *L, int res, const char *info) { |
151 | { | 158 | if (res == -1) { |
152 | if (i==-1) | 159 | return pusherror(L, info); |
153 | return pusherror(L, info); | 160 | } else { |
154 | lua_pushinteger(L, i); | 161 | lua_pushboolean(L, 1); |
155 | return 1; | 162 | return 1; |
163 | } | ||
156 | } | 164 | } |
157 | 165 | ||
158 | 166 | ||
@@ -417,16 +425,20 @@ static int file_unlock (lua_State *L) { | |||
417 | ** @param #2 Name of link. | 425 | ** @param #2 Name of link. |
418 | ** @param #3 True if link is symbolic (optional). | 426 | ** @param #3 True if link is symbolic (optional). |
419 | */ | 427 | */ |
420 | static int make_link(lua_State *L) | 428 | static int make_link (lua_State *L) { |
421 | { | ||
422 | #ifndef _WIN32 | 429 | #ifndef _WIN32 |
423 | const char *oldpath = luaL_checkstring(L, 1); | 430 | const char *oldpath = luaL_checkstring(L, 1); |
424 | const char *newpath = luaL_checkstring(L, 2); | 431 | const char *newpath = luaL_checkstring(L, 2); |
425 | return pushresult(L, | 432 | int res = (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath); |
426 | (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL); | 433 | if (res == -1) { |
434 | return pusherror(L, NULL); | ||
435 | } else { | ||
436 | lua_pushinteger(L, 0); | ||
437 | return 1; | ||
438 | } | ||
427 | #else | 439 | #else |
428 | errno = ENOSYS; /* = "Function not implemented" */ | 440 | errno = ENOSYS; /* = "Function not implemented" */ |
429 | return pushresult(L, -1, "make_link is not supported on Windows"); | 441 | return pushresult(L, -1, "make_link is not supported on Windows"); |
430 | #endif | 442 | #endif |
431 | } | 443 | } |
432 | 444 | ||
@@ -436,21 +448,8 @@ static int make_link(lua_State *L) | |||
436 | ** @param #1 Directory path. | 448 | ** @param #1 Directory path. |
437 | */ | 449 | */ |
438 | static int make_dir (lua_State *L) { | 450 | static int make_dir (lua_State *L) { |
439 | const char *path = luaL_checkstring (L, 1); | 451 | const char *path = luaL_checkstring(L, 1); |
440 | int fail; | 452 | return pushresult(L, lfs_mkdir(path), NULL); |
441 | #ifdef _WIN32 | ||
442 | fail = _mkdir (path); | ||
443 | #else | ||
444 | fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | | ||
445 | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH ); | ||
446 | #endif | ||
447 | if (fail) { | ||
448 | lua_pushnil (L); | ||
449 | lua_pushfstring (L, "%s", strerror(errno)); | ||
450 | return 2; | ||
451 | } | ||
452 | lua_pushboolean (L, 1); | ||
453 | return 1; | ||
454 | } | 453 | } |
455 | 454 | ||
456 | 455 | ||