diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lfs.c | 108 |
1 files changed, 48 insertions, 60 deletions
@@ -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); |