diff options
Diffstat (limited to 'src/lfs.c')
-rw-r--r-- | src/lfs.c | 105 |
1 files changed, 42 insertions, 63 deletions
@@ -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 | } |