aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Melnichenko <mpeterval@gmail.com>2017-09-14 12:15:58 +0300
committerPeter Melnichenko <mpeterval@gmail.com>2017-09-14 12:15:58 +0300
commitd399961536eee59f891a395271bf12d6a0c70c64 (patch)
tree8f3094937cb09b9e46e00248ccf606922fb69fac
parent8a7796ec170923267a823566424f818037bca82c (diff)
parenta332bde584756b14298f51e913bdc309f87e7ff8 (diff)
downloadluafilesystem-d399961536eee59f891a395271bf12d6a0c70c64.tar.gz
luafilesystem-d399961536eee59f891a395271bf12d6a0c70c64.tar.bz2
luafilesystem-d399961536eee59f891a395271bf12d6a0c70c64.zip
Merge branch '1.7'
-rw-r--r--doc/us/manual.html14
-rw-r--r--src/lfs.c108
-rw-r--r--tests/test.lua5
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>
diff --git a/src/lfs.c b/src/lfs.c
index be6977e..dc799a4 100644
--- a/src/lfs.c
+++ b/src/lfs.c
@@ -123,6 +123,13 @@ typedef struct dir_data {
123#define LSTAT_FUNC lstat 123#define LSTAT_FUNC lstat
124#endif 124#endif
125 125
126#ifdef _WIN32
127 #define lfs_mkdir _mkdir
128#else
129 #define lfs_mkdir(path) (mkdir((path), \
130 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH))
131#endif
132
126/* 133/*
127** Utility functions 134** Utility functions
128*/ 135*/
@@ -137,12 +144,13 @@ static int pusherror(lua_State *L, const char *info)
137 return 3; 144 return 3;
138} 145}
139 146
140static int pushresult(lua_State *L, int i, const char *info) 147static int pushresult(lua_State *L, int res, const char *info) {
141{ 148 if (res == -1) {
142 if (i==-1) 149 return pusherror(L, info);
143 return pusherror(L, info); 150 } else {
144 lua_pushinteger(L, i); 151 lua_pushboolean(L, 1);
145 return 1; 152 return 1;
153 }
146} 154}
147 155
148 156
@@ -424,16 +432,20 @@ static int file_unlock (lua_State *L) {
424** @param #2 Name of link. 432** @param #2 Name of link.
425** @param #3 True if link is symbolic (optional). 433** @param #3 True if link is symbolic (optional).
426*/ 434*/
427static int make_link(lua_State *L) 435static int make_link (lua_State *L) {
428{
429#ifndef _WIN32 436#ifndef _WIN32
430 const char *oldpath = luaL_checkstring(L, 1); 437 const char *oldpath = luaL_checkstring(L, 1);
431 const char *newpath = luaL_checkstring(L, 2); 438 const char *newpath = luaL_checkstring(L, 2);
432 return pushresult(L, 439 int res = (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath);
433 (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL); 440 if (res == -1) {
441 return pusherror(L, NULL);
442 } else {
443 lua_pushinteger(L, 0);
444 return 1;
445 }
434#else 446#else
435 errno = ENOSYS; /* = "Function not implemented" */ 447 errno = ENOSYS; /* = "Function not implemented" */
436 return pushresult(L, -1, "make_link is not supported on Windows"); 448 return pushresult(L, -1, "make_link is not supported on Windows");
437#endif 449#endif
438} 450}
439 451
@@ -443,21 +455,8 @@ static int make_link(lua_State *L)
443** @param #1 Directory path. 455** @param #1 Directory path.
444*/ 456*/
445static int make_dir (lua_State *L) { 457static int make_dir (lua_State *L) {
446 const char *path = luaL_checkstring (L, 1); 458 const char *path = luaL_checkstring(L, 1);
447 int fail; 459 return pushresult(L, lfs_mkdir(path), NULL);
448#ifdef _WIN32
449 fail = _mkdir (path);
450#else
451 fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
452 S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
453#endif
454 if (fail) {
455 lua_pushnil (L);
456 lua_pushfstring (L, "%s", strerror(errno));
457 return 2;
458 }
459 lua_pushboolean (L, 1);
460 return 1;
461} 460}
462 461
463 462
@@ -466,18 +465,8 @@ static int make_dir (lua_State *L) {
466** @param #1 Directory path. 465** @param #1 Directory path.
467*/ 466*/
468static int remove_dir (lua_State *L) { 467static int remove_dir (lua_State *L) {
469 const char *path = luaL_checkstring (L, 1); 468 const char *path = luaL_checkstring(L, 1);
470 int fail; 469 return pushresult(L, rmdir(path), NULL);
471
472 fail = rmdir (path);
473
474 if (fail) {
475 lua_pushnil (L);
476 lua_pushfstring (L, "%s", strerror(errno));
477 return 2;
478 }
479 lua_pushboolean (L, 1);
480 return 1;
481} 470}
482 471
483 472
@@ -664,26 +653,24 @@ static const char *mode2string (mode_t mode) {
664 653
665 654
666/* 655/*
667** Set access time and modification values for file 656** Set access time and modification values for a file.
657** @param #1 File path.
658** @param #2 Access time in seconds, current time is used if missing.
659** @param #3 Modification time in seconds, access time is used if missing.
668*/ 660*/
669static int file_utime (lua_State *L) { 661static int file_utime (lua_State *L) {
670 const char *file = luaL_checkstring (L, 1); 662 const char *file = luaL_checkstring(L, 1);
671 struct utimbuf utb, *buf; 663 struct utimbuf utb, *buf;
672 664
673 if (lua_gettop (L) == 1) /* set to current date/time */ 665 if (lua_gettop (L) == 1) /* set to current date/time */
674 buf = NULL; 666 buf = NULL;
675 else { 667 else {
676 utb.actime = (time_t)luaL_optnumber (L, 2, 0); 668 utb.actime = (time_t) luaL_optnumber(L, 2, 0);
677 utb.modtime = (time_t) luaL_optinteger (L, 3, utb.actime); 669 utb.modtime = (time_t) luaL_optinteger(L, 3, utb.actime);
678 buf = &utb; 670 buf = &utb;
679 } 671 }
680 if (utime (file, buf)) { 672
681 lua_pushnil (L); 673 return pushresult(L, utime(file, buf), NULL);
682 lua_pushfstring (L, "%s", strerror (errno));
683 return 2;
684 }
685 lua_pushboolean (L, 1);
686 return 1;
687} 674}
688 675
689 676
@@ -820,7 +807,8 @@ static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) {
820 if (st(file, &info)) { 807 if (st(file, &info)) {
821 lua_pushnil(L); 808 lua_pushnil(L);
822 lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno)); 809 lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno));
823 return 2; 810 lua_pushinteger(L, errno);
811 return 3;
824 } 812 }
825 if (lua_isstring (L, 2)) { 813 if (lua_isstring (L, 2)) {
826 const char *member = lua_tostring (L, 2); 814 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(".")
152io.flush() 152io.flush()
153 153
154-- Trying to get attributes of a non-existent file 154-- Trying to get attributes of a non-existent file
155assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file") 155local attr_ok, err, errno = lfs.attributes("this couldn't be an actual file")
156assert(attr_ok == nil, "could get attributes of a non-existent file")
157assert(type(err) == "string", "failed lfs.attributes did not return an error message")
158assert(type(errno) == "number", "failed lfs.attributes did not return error code")
156assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") 159assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory")
157 160
158io.write(".") 161io.write(".")