aboutsummaryrefslogtreecommitdiff
path: root/src/lfs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lfs.c')
-rw-r--r--src/lfs.c108
1 files changed, 48 insertions, 60 deletions
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);