diff options
-rw-r--r-- | doc/us/manual.html | 14 | ||||
-rw-r--r-- | src/lfs.c | 108 | ||||
-rw-r--r-- | tests/test.lua | 5 |
3 files changed, 59 insertions, 68 deletions
diff --git a/doc/us/manual.html b/doc/us/manual.html index 0ecb625..3555e3d 100644 --- a/doc/us/manual.html +++ b/doc/us/manual.html | |||
@@ -104,7 +104,7 @@ LuaFileSystem offers the following functions: | |||
104 | <dl class="reference"> | 104 | <dl class="reference"> |
105 | <dt><a name="attributes"></a><strong><code>lfs.attributes (filepath [, aname | atable])</code></strong></dt> | 105 | <dt><a name="attributes"></a><strong><code>lfs.attributes (filepath [, aname | atable])</code></strong></dt> |
106 | <dd>Returns a table with the file attributes corresponding to | 106 | <dd>Returns a table with the file attributes corresponding to |
107 | <code>filepath</code> (or <code>nil</code> followed by an error message | 107 | <code>filepath</code> (or <code>nil</code> followed by an error message and a system-dependent error code |
108 | in case of error). | 108 | in case of error). |
109 | If the second optional argument is given and is a string, then only the value of the | 109 | If the second optional argument is given and is a string, then only the value of the |
110 | named attribute is returned (this use is equivalent to | 110 | named attribute is returned (this use is equivalent to |
@@ -222,14 +222,14 @@ LuaFileSystem offers the following functions: | |||
222 | <dt><a name="mkdir"></a><strong><code>lfs.mkdir (dirname)</code></strong></dt> | 222 | <dt><a name="mkdir"></a><strong><code>lfs.mkdir (dirname)</code></strong></dt> |
223 | <dd>Creates a new directory. The argument is the name of the new | 223 | <dd>Creates a new directory. The argument is the name of the new |
224 | directory.<br /> | 224 | directory.<br /> |
225 | Returns <code>true</code> if the operation was successful; | 225 | Returns <code>true</code> in case of success or <code>nil</code>, an error message and |
226 | in case of error, it returns <code>nil</code> plus an error string. | 226 | a system-dependent error code in case of error. |
227 | </dd> | 227 | </dd> |
228 | 228 | ||
229 | <dt><a name="rmdir"></a><strong><code>lfs.rmdir (dirname)</code></strong></dt> | 229 | <dt><a name="rmdir"></a><strong><code>lfs.rmdir (dirname)</code></strong></dt> |
230 | <dd>Removes an existing directory. The argument is the name of the directory.<br /> | 230 | <dd>Removes an existing directory. The argument is the name of the directory.<br /> |
231 | Returns <code>true</code> if the operation was successful; | 231 | Returns <code>true</code> in case of success or <code>nil</code>, an error message and |
232 | in case of error, it returns <code>nil</code> plus an error string.</dd> | 232 | a system-dependent error code in case of error. |
233 | 233 | ||
234 | <dt><a name="setmode"></a><strong><code>lfs.setmode (file, mode)</code></strong></dt> | 234 | <dt><a name="setmode"></a><strong><code>lfs.setmode (file, mode)</code></strong></dt> |
235 | <dd>Sets the writing mode for a file. The mode string can be either <code>"binary"</code> or <code>"text"</code>. | 235 | <dd>Sets the writing mode for a file. The mode string can be either <code>"binary"</code> or <code>"text"</code>. |
@@ -257,8 +257,8 @@ LuaFileSystem offers the following functions: | |||
257 | Lua standard function <code>os.time</code>). | 257 | Lua standard function <code>os.time</code>). |
258 | If the modification time is omitted, the access time provided is used; | 258 | If the modification time is omitted, the access time provided is used; |
259 | if both times are omitted, the current time is used.<br /> | 259 | if both times are omitted, the current time is used.<br /> |
260 | Returns <code>true</code> if the operation was successful; | 260 | Returns <code>true</code> in case of success or <code>nil</code>, an error message and |
261 | in case of error, it returns <code>nil</code> plus an error string. | 261 | a system-dependent error code in case of error. |
262 | </dd> | 262 | </dd> |
263 | 263 | ||
264 | <dt><a name="unlock"></a><strong><code>lfs.unlock (filehandle[, start[, length]])</code></strong></dt> | 264 | <dt><a name="unlock"></a><strong><code>lfs.unlock (filehandle[, start[, length]])</code></strong></dt> |
@@ -121,6 +121,13 @@ typedef struct dir_data { | |||
121 | #define LSTAT_FUNC lstat | 121 | #define LSTAT_FUNC lstat |
122 | #endif | 122 | #endif |
123 | 123 | ||
124 | #ifdef _WIN32 | ||
125 | #define lfs_mkdir _mkdir | ||
126 | #else | ||
127 | #define lfs_mkdir(path) (mkdir((path), \ | ||
128 | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH)) | ||
129 | #endif | ||
130 | |||
124 | /* | 131 | /* |
125 | ** Utility functions | 132 | ** Utility functions |
126 | */ | 133 | */ |
@@ -135,12 +142,13 @@ static int pusherror(lua_State *L, const char *info) | |||
135 | return 3; | 142 | return 3; |
136 | } | 143 | } |
137 | 144 | ||
138 | static int pushresult(lua_State *L, int i, const char *info) | 145 | static int pushresult(lua_State *L, int res, const char *info) { |
139 | { | 146 | if (res == -1) { |
140 | if (i==-1) | 147 | return pusherror(L, info); |
141 | return pusherror(L, info); | 148 | } else { |
142 | lua_pushinteger(L, i); | 149 | lua_pushboolean(L, 1); |
143 | return 1; | 150 | return 1; |
151 | } | ||
144 | } | 152 | } |
145 | 153 | ||
146 | 154 | ||
@@ -422,16 +430,20 @@ static int file_unlock (lua_State *L) { | |||
422 | ** @param #2 Name of link. | 430 | ** @param #2 Name of link. |
423 | ** @param #3 True if link is symbolic (optional). | 431 | ** @param #3 True if link is symbolic (optional). |
424 | */ | 432 | */ |
425 | static int make_link(lua_State *L) | 433 | static int make_link (lua_State *L) { |
426 | { | ||
427 | #ifndef _WIN32 | 434 | #ifndef _WIN32 |
428 | const char *oldpath = luaL_checkstring(L, 1); | 435 | const char *oldpath = luaL_checkstring(L, 1); |
429 | const char *newpath = luaL_checkstring(L, 2); | 436 | const char *newpath = luaL_checkstring(L, 2); |
430 | return pushresult(L, | 437 | int res = (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath); |
431 | (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL); | 438 | if (res == -1) { |
439 | return pusherror(L, NULL); | ||
440 | } else { | ||
441 | lua_pushinteger(L, 0); | ||
442 | return 1; | ||
443 | } | ||
432 | #else | 444 | #else |
433 | errno = ENOSYS; /* = "Function not implemented" */ | 445 | errno = ENOSYS; /* = "Function not implemented" */ |
434 | return pushresult(L, -1, "make_link is not supported on Windows"); | 446 | return pushresult(L, -1, "make_link is not supported on Windows"); |
435 | #endif | 447 | #endif |
436 | } | 448 | } |
437 | 449 | ||
@@ -441,21 +453,8 @@ static int make_link(lua_State *L) | |||
441 | ** @param #1 Directory path. | 453 | ** @param #1 Directory path. |
442 | */ | 454 | */ |
443 | static int make_dir (lua_State *L) { | 455 | static int make_dir (lua_State *L) { |
444 | const char *path = luaL_checkstring (L, 1); | 456 | const char *path = luaL_checkstring(L, 1); |
445 | int fail; | 457 | return pushresult(L, lfs_mkdir(path), NULL); |
446 | #ifdef _WIN32 | ||
447 | fail = _mkdir (path); | ||
448 | #else | ||
449 | fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | | ||
450 | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH ); | ||
451 | #endif | ||
452 | if (fail) { | ||
453 | lua_pushnil (L); | ||
454 | lua_pushfstring (L, "%s", strerror(errno)); | ||
455 | return 2; | ||
456 | } | ||
457 | lua_pushboolean (L, 1); | ||
458 | return 1; | ||
459 | } | 458 | } |
460 | 459 | ||
461 | 460 | ||
@@ -464,18 +463,8 @@ static int make_dir (lua_State *L) { | |||
464 | ** @param #1 Directory path. | 463 | ** @param #1 Directory path. |
465 | */ | 464 | */ |
466 | static int remove_dir (lua_State *L) { | 465 | static int remove_dir (lua_State *L) { |
467 | const char *path = luaL_checkstring (L, 1); | 466 | const char *path = luaL_checkstring(L, 1); |
468 | int fail; | 467 | return pushresult(L, rmdir(path), NULL); |
469 | |||
470 | fail = rmdir (path); | ||
471 | |||
472 | if (fail) { | ||
473 | lua_pushnil (L); | ||
474 | lua_pushfstring (L, "%s", strerror(errno)); | ||
475 | return 2; | ||
476 | } | ||
477 | lua_pushboolean (L, 1); | ||
478 | return 1; | ||
479 | } | 468 | } |
480 | 469 | ||
481 | 470 | ||
@@ -662,26 +651,24 @@ static const char *mode2string (mode_t mode) { | |||
662 | 651 | ||
663 | 652 | ||
664 | /* | 653 | /* |
665 | ** Set access time and modification values for file | 654 | ** Set access time and modification values for a file. |
655 | ** @param #1 File path. | ||
656 | ** @param #2 Access time in seconds, current time is used if missing. | ||
657 | ** @param #3 Modification time in seconds, access time is used if missing. | ||
666 | */ | 658 | */ |
667 | static int file_utime (lua_State *L) { | 659 | static int file_utime (lua_State *L) { |
668 | const char *file = luaL_checkstring (L, 1); | 660 | const char *file = luaL_checkstring(L, 1); |
669 | struct utimbuf utb, *buf; | 661 | struct utimbuf utb, *buf; |
670 | 662 | ||
671 | if (lua_gettop (L) == 1) /* set to current date/time */ | 663 | if (lua_gettop (L) == 1) /* set to current date/time */ |
672 | buf = NULL; | 664 | buf = NULL; |
673 | else { | 665 | else { |
674 | utb.actime = (time_t)luaL_optnumber (L, 2, 0); | 666 | utb.actime = (time_t) luaL_optnumber(L, 2, 0); |
675 | utb.modtime = (time_t) luaL_optinteger (L, 3, utb.actime); | 667 | utb.modtime = (time_t) luaL_optinteger(L, 3, utb.actime); |
676 | buf = &utb; | 668 | buf = &utb; |
677 | } | 669 | } |
678 | if (utime (file, buf)) { | 670 | |
679 | lua_pushnil (L); | 671 | return pushresult(L, utime(file, buf), NULL); |
680 | lua_pushfstring (L, "%s", strerror (errno)); | ||
681 | return 2; | ||
682 | } | ||
683 | lua_pushboolean (L, 1); | ||
684 | return 1; | ||
685 | } | 672 | } |
686 | 673 | ||
687 | 674 | ||
@@ -818,7 +805,8 @@ static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { | |||
818 | if (st(file, &info)) { | 805 | if (st(file, &info)) { |
819 | lua_pushnil(L); | 806 | lua_pushnil(L); |
820 | lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno)); | 807 | lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno)); |
821 | return 2; | 808 | lua_pushinteger(L, errno); |
809 | return 3; | ||
822 | } | 810 | } |
823 | if (lua_isstring (L, 2)) { | 811 | if (lua_isstring (L, 2)) { |
824 | const char *member = lua_tostring (L, 2); | 812 | const char *member = lua_tostring (L, 2); |
diff --git a/tests/test.lua b/tests/test.lua index 10810fe..591ee25 100644 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -152,7 +152,10 @@ io.write(".") | |||
152 | io.flush() | 152 | io.flush() |
153 | 153 | ||
154 | -- Trying to get attributes of a non-existent file | 154 | -- Trying to get attributes of a non-existent file |
155 | assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file") | 155 | local attr_ok, err, errno = lfs.attributes("this couldn't be an actual file") |
156 | assert(attr_ok == nil, "could get attributes of a non-existent file") | ||
157 | assert(type(err) == "string", "failed lfs.attributes did not return an error message") | ||
158 | assert(type(errno) == "number", "failed lfs.attributes did not return error code") | ||
156 | assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") | 159 | assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") |
157 | 160 | ||
158 | io.write(".") | 161 | io.write(".") |