diff --git a/Makefile b/Makefile index 9d2f630..5d1ac6e 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ -# $Id: Makefile,v 1.10 2005/01/18 09:36:11 tomas Exp $ +# $Id: Makefile,v 1.11 2005/01/18 10:48:02 tomas Exp $ T= lfs include ./config -V= 1.0b +V= 1.1b DIST_DIR= luafilesystem-$V TAR_FILE= $(DIST_DIR).tar.gz ZIP_FILE= $(DIST_DIR).zip diff --git a/src/lfs.c b/src/lfs.c index 2d62a66..6f672ac 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -7,9 +7,10 @@ ** lfs.dir (path) ** lfs.mkdir (path) ** lfs.lock (fh, mode) +** lfs.touch (filepath [, atime [, mtime]]) ** lfs.unlock (fh) ** -** $Id: lfs.c,v 1.14 2004/11/17 14:08:04 tomas Exp $ +** $Id: lfs.c,v 1.15 2005/01/18 10:48:02 tomas Exp $ */ #include @@ -17,6 +18,7 @@ #include #include #include +#include #ifdef WIN32 #include @@ -131,7 +133,7 @@ static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long star case 'r': lkmode = _LK_NBLCK; break; case 'w': lkmode = _LK_NBLCK; break; case 'u': lkmode = _LK_UNLCK; break; - default : luaL_error (L, "%s: invalid mode", funcname); + default : return luaL_error (L, "%s: invalid mode", funcname); } if (!len) { fseek (fh, 0L, SEEK_END); @@ -345,13 +347,27 @@ static int dir_create_meta (lua_State *L) { #ifdef _WIN32 - #define S_ISDIR(mode) (mode&_S_IFDIR) - #define S_ISREG(mode) (mode&_S_IFREG) - #define S_ISLNK(mode) (0) - #define S_ISSOCK(mode) (0) - #define S_ISFIFO(mode) (0) - #define S_ISCHR(mode) (mode&_S_IFCHR) - #define S_ISBLK(mode) (0) + #ifndef S_ISDIR + #define S_ISDIR(mode) (mode&_S_IFDIR) + #endif + #ifndef S_ISREG + #define S_ISREG(mode) (mode&_S_IFREG) + #endif + #ifndef S_ISLNK + #define S_ISLNK(mode) (0) + #endif + #ifndef S_ISSOCK + #define S_ISSOCK(mode) (0) + #endif + #ifndef S_ISFIFO + #define S_ISFIFO(mode) (0) + #endif + #ifndef S_ISCHR + #define S_ISCHR(mode) (mode&_S_IFCHR) + #endif + #ifndef S_ISBLK + #define S_ISBLK(mode) (0) + #endif #endif /* ** Convert the inode protection mode to a string. @@ -380,6 +396,30 @@ static const char *mode2string (mode_t mode) { } +/* +** Set access time and modification values for file +*/ +static int file_utime (lua_State *L) { + const char *file = luaL_checkstring (L, 1); + struct utimbuf utb, *buf; + + if (lua_gettop (L) == 1) /* set to current date/time */ + buf = NULL; + else { + utb.actime = (time_t)luaL_optnumber (L, 2, 0); + utb.modtime = (time_t)luaL_optnumber (L, 3, utb.actime); + buf = &utb; + } + if (utime (file, buf)) { + lua_pushnil (L); + lua_pushfstring (L, "%s", strerror (errno)); + return 2; + } + lua_pushboolean (L, 1); + return 1; +} + + /* ** Get file information */ @@ -478,6 +518,7 @@ static const struct luaL_reg fslib[] = { {"dir", dir_iter_factory}, {"lock", file_lock}, {"mkdir", make_dir}, + {"touch", file_utime}, {"unlock", file_unlock}, {NULL, NULL}, }; diff --git a/tests/test.lua b/tests/test.lua index 25a3fd3..7f981e5 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -32,9 +32,31 @@ assert (lfs.chdir (reldir), "could not change back to current directory") assert (lfs.currentdir() == current, "error trying to change directories") assert (lfs.chdir ("this couldn't be an actual directory") == nil, "could change to a non-existent directory") -- Changing creating and removing directories -assert (lfs.mkdir (tmp.."/lfs_tmp_dir"), "could not make a new directory") -assert (os.remove (tmp.."/lfs_tmp_dir"), "could not remove new directory") -assert (lfs.mkdir (tmp.."/lfs_tmp_dir/lfs_tmp_dir") == false, "could create a directory inside a non-existent one") +local tmpdir = tmp.."/lfs_tmp_dir" +assert (lfs.mkdir (tmpdir), "could not make a new directory") +local attrib, errmsg = lfs.attributes (tmpdir) +if not attrib then + error ("could not get attributes of file `"..tmpdir.."':\n"..errmsg) +else + -- Change access time + assert (lfs.touch (tmpdir, 11)) + local new_att = assert (lfs.attributes (tmpdir)) + assert (new_att.access == 11, "could not set access time") + assert (new_att.modification == 11, "could not set modification time") + -- Change access and modification time + assert (lfs.touch (tmpdir, 33, 22)) + local new_att = assert (lfs.attributes (tmpdir)) + assert (new_att.access == 33, "could not set access time") + assert (new_att.modification == 22, "could not set modification time") + -- Restore access time to current value + assert (lfs.touch (tmpdir)) + new_att = assert (lfs.attributes (tmpdir)) + assert (new_att.access == attrib.access) + assert (new_att.modification == attrib.modification) +end +assert (os.remove (tmpdir), "could not remove new directory") +assert (lfs.mkdir (tmpdir.."/lfs_tmp_dir") == false, "could create a directory inside a non-existent one") -- assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file") assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") +print"Ok!"