diff options
-rw-r--r-- | doc/us/manual.html | 14 | ||||
-rw-r--r-- | src/lfs.c | 105 | ||||
-rw-r--r-- | tests/test.lua | 9 |
3 files changed, 56 insertions, 72 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> |
@@ -70,7 +70,6 @@ | |||
70 | #include "lfs.h" | 70 | #include "lfs.h" |
71 | 71 | ||
72 | #define LFS_VERSION "1.6.3" | 72 | #define LFS_VERSION "1.6.3" |
73 | #define LFS_LIBNAME "lfs" | ||
74 | 73 | ||
75 | #if LUA_VERSION_NUM >= 503 /* Lua 5.3 */ | 74 | #if LUA_VERSION_NUM >= 503 /* Lua 5.3 */ |
76 | 75 | ||
@@ -121,6 +120,13 @@ typedef struct dir_data { | |||
121 | #define LSTAT_FUNC lstat | 120 | #define LSTAT_FUNC lstat |
122 | #endif | 121 | #endif |
123 | 122 | ||
123 | #ifdef _WIN32 | ||
124 | #define lfs_mkdir _mkdir | ||
125 | #else | ||
126 | #define lfs_mkdir(path) (mkdir((path), \ | ||
127 | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH)) | ||
128 | #endif | ||
129 | |||
124 | /* | 130 | /* |
125 | ** Utility functions | 131 | ** Utility functions |
126 | */ | 132 | */ |
@@ -135,12 +141,13 @@ static int pusherror(lua_State *L, const char *info) | |||
135 | return 3; | 141 | return 3; |
136 | } | 142 | } |
137 | 143 | ||
138 | static int pushresult(lua_State *L, int i, const char *info) | 144 | static int pushresult(lua_State *L, int res, const char *info) { |
139 | { | 145 | if (res == -1) { |
140 | if (i==-1) | 146 | return pusherror(L, info); |
141 | return pusherror(L, info); | 147 | } else { |
142 | lua_pushinteger(L, i); | 148 | lua_pushboolean(L, 1); |
143 | return 1; | 149 | return 1; |
150 | } | ||
144 | } | 151 | } |
145 | 152 | ||
146 | 153 | ||
@@ -422,16 +429,14 @@ static int file_unlock (lua_State *L) { | |||
422 | ** @param #2 Name of link. | 429 | ** @param #2 Name of link. |
423 | ** @param #3 True if link is symbolic (optional). | 430 | ** @param #3 True if link is symbolic (optional). |
424 | */ | 431 | */ |
425 | static int make_link(lua_State *L) | 432 | static int make_link (lua_State *L) { |
426 | { | ||
427 | #ifndef _WIN32 | 433 | #ifndef _WIN32 |
428 | const char *oldpath = luaL_checkstring(L, 1); | 434 | const char *oldpath = luaL_checkstring(L, 1); |
429 | const char *newpath = luaL_checkstring(L, 2); | 435 | const char *newpath = luaL_checkstring(L, 2); |
430 | return pushresult(L, | 436 | return pushresult(L, (lua_toboolean(L, 3) ? symlink : link)(oldpath, newpath), NULL); |
431 | (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL); | ||
432 | #else | 437 | #else |
433 | errno = ENOSYS; /* = "Function not implemented" */ | 438 | errno = ENOSYS; /* = "Function not implemented" */ |
434 | return pushresult(L, -1, "make_link is not supported on Windows"); | 439 | return pushresult(L, -1, "make_link is not supported on Windows"); |
435 | #endif | 440 | #endif |
436 | } | 441 | } |
437 | 442 | ||
@@ -441,21 +446,8 @@ static int make_link(lua_State *L) | |||
441 | ** @param #1 Directory path. | 446 | ** @param #1 Directory path. |
442 | */ | 447 | */ |
443 | static int make_dir (lua_State *L) { | 448 | static int make_dir (lua_State *L) { |
444 | const char *path = luaL_checkstring (L, 1); | 449 | const char *path = luaL_checkstring(L, 1); |
445 | int fail; | 450 | 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 | } | 451 | } |
460 | 452 | ||
461 | 453 | ||
@@ -464,18 +456,8 @@ static int make_dir (lua_State *L) { | |||
464 | ** @param #1 Directory path. | 456 | ** @param #1 Directory path. |
465 | */ | 457 | */ |
466 | static int remove_dir (lua_State *L) { | 458 | static int remove_dir (lua_State *L) { |
467 | const char *path = luaL_checkstring (L, 1); | 459 | const char *path = luaL_checkstring(L, 1); |
468 | int fail; | 460 | 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 | } | 461 | } |
480 | 462 | ||
481 | 463 | ||
@@ -662,26 +644,24 @@ static const char *mode2string (mode_t mode) { | |||
662 | 644 | ||
663 | 645 | ||
664 | /* | 646 | /* |
665 | ** Set access time and modification values for file | 647 | ** Set access time and modification values for a file. |
648 | ** @param #1 File path. | ||
649 | ** @param #2 Access time in seconds, current time is used if missing. | ||
650 | ** @param #3 Modification time in seconds, access time is used if missing. | ||
666 | */ | 651 | */ |
667 | static int file_utime (lua_State *L) { | 652 | static int file_utime (lua_State *L) { |
668 | const char *file = luaL_checkstring (L, 1); | 653 | const char *file = luaL_checkstring(L, 1); |
669 | struct utimbuf utb, *buf; | 654 | struct utimbuf utb, *buf; |
670 | 655 | ||
671 | if (lua_gettop (L) == 1) /* set to current date/time */ | 656 | if (lua_gettop (L) == 1) /* set to current date/time */ |
672 | buf = NULL; | 657 | buf = NULL; |
673 | else { | 658 | else { |
674 | utb.actime = (time_t)luaL_optnumber (L, 2, 0); | 659 | utb.actime = (time_t) luaL_optnumber(L, 2, 0); |
675 | utb.modtime = (time_t) luaL_optinteger (L, 3, utb.actime); | 660 | utb.modtime = (time_t) luaL_optinteger(L, 3, utb.actime); |
676 | buf = &utb; | 661 | buf = &utb; |
677 | } | 662 | } |
678 | if (utime (file, buf)) { | 663 | |
679 | lua_pushnil (L); | 664 | 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 | } | 665 | } |
686 | 666 | ||
687 | 667 | ||
@@ -818,7 +798,8 @@ static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { | |||
818 | if (st(file, &info)) { | 798 | if (st(file, &info)) { |
819 | lua_pushnil(L); | 799 | lua_pushnil(L); |
820 | lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno)); | 800 | lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno)); |
821 | return 2; | 801 | lua_pushinteger(L, errno); |
802 | return 3; | ||
822 | } | 803 | } |
823 | if (lua_isstring (L, 2)) { | 804 | if (lua_isstring (L, 2)) { |
824 | const char *member = lua_tostring (L, 2); | 805 | const char *member = lua_tostring (L, 2); |
@@ -944,8 +925,6 @@ LFS_EXPORT int luaopen_lfs (lua_State *L) { | |||
944 | dir_create_meta (L); | 925 | dir_create_meta (L); |
945 | lock_create_meta (L); | 926 | lock_create_meta (L); |
946 | luaL_newlib (L, fslib); | 927 | luaL_newlib (L, fslib); |
947 | lua_pushvalue(L, -1); | ||
948 | lua_setglobal(L, LFS_LIBNAME); | ||
949 | set_info (L); | 928 | set_info (L); |
950 | return 1; | 929 | return 1; |
951 | } | 930 | } |
diff --git a/tests/test.lua b/tests/test.lua index 10810fe..fd36ba7 100644 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -88,7 +88,9 @@ io.write(".") | |||
88 | io.flush() | 88 | io.flush() |
89 | 89 | ||
90 | -- Checking link (does not work on Windows) | 90 | -- Checking link (does not work on Windows) |
91 | if lfs.link (tmpfile, "_a_link_for_test_", true) then | 91 | local link_ok = lfs.link (tmpfile, "_a_link_for_test_", true) |
92 | if link_ok then | ||
93 | assert (link_ok == true, "successful lfs.link did not return true") | ||
92 | assert (lfs.attributes"_a_link_for_test_".mode == "file") | 94 | assert (lfs.attributes"_a_link_for_test_".mode == "file") |
93 | assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link") | 95 | assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link") |
94 | assert (lfs.symlinkattributes"_a_link_for_test_".target == tmpfile) | 96 | assert (lfs.symlinkattributes"_a_link_for_test_".target == tmpfile) |
@@ -152,7 +154,10 @@ io.write(".") | |||
152 | io.flush() | 154 | io.flush() |
153 | 155 | ||
154 | -- Trying to get attributes of a non-existent file | 156 | -- 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") | 157 | local attr_ok, err, errno = lfs.attributes("this couldn't be an actual file") |
158 | assert(attr_ok == nil, "could get attributes of a non-existent file") | ||
159 | assert(type(err) == "string", "failed lfs.attributes did not return an error message") | ||
160 | 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") | 161 | assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") |
157 | 162 | ||
158 | io.write(".") | 163 | io.write(".") |