diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/lfs.c | 59 | ||||
-rw-r--r-- | tests/test.lua | 28 |
3 files changed, 77 insertions, 14 deletions
@@ -1,10 +1,10 @@ | |||
1 | # $Id: Makefile,v 1.10 2005/01/18 09:36:11 tomas Exp $ | 1 | # $Id: Makefile,v 1.11 2005/01/18 10:48:02 tomas Exp $ |
2 | 2 | ||
3 | T= lfs | 3 | T= lfs |
4 | 4 | ||
5 | include ./config | 5 | include ./config |
6 | 6 | ||
7 | V= 1.0b | 7 | V= 1.1b |
8 | DIST_DIR= luafilesystem-$V | 8 | DIST_DIR= luafilesystem-$V |
9 | TAR_FILE= $(DIST_DIR).tar.gz | 9 | TAR_FILE= $(DIST_DIR).tar.gz |
10 | ZIP_FILE= $(DIST_DIR).zip | 10 | ZIP_FILE= $(DIST_DIR).zip |
@@ -7,9 +7,10 @@ | |||
7 | ** lfs.dir (path) | 7 | ** lfs.dir (path) |
8 | ** lfs.mkdir (path) | 8 | ** lfs.mkdir (path) |
9 | ** lfs.lock (fh, mode) | 9 | ** lfs.lock (fh, mode) |
10 | ** lfs.touch (filepath [, atime [, mtime]]) | ||
10 | ** lfs.unlock (fh) | 11 | ** lfs.unlock (fh) |
11 | ** | 12 | ** |
12 | ** $Id: lfs.c,v 1.14 2004/11/17 14:08:04 tomas Exp $ | 13 | ** $Id: lfs.c,v 1.15 2005/01/18 10:48:02 tomas Exp $ |
13 | */ | 14 | */ |
14 | 15 | ||
15 | #include <errno.h> | 16 | #include <errno.h> |
@@ -17,6 +18,7 @@ | |||
17 | #include <string.h> | 18 | #include <string.h> |
18 | #include <time.h> | 19 | #include <time.h> |
19 | #include <sys/stat.h> | 20 | #include <sys/stat.h> |
21 | #include <utime.h> | ||
20 | 22 | ||
21 | #ifdef WIN32 | 23 | #ifdef WIN32 |
22 | #include <direct.h> | 24 | #include <direct.h> |
@@ -131,7 +133,7 @@ static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long star | |||
131 | case 'r': lkmode = _LK_NBLCK; break; | 133 | case 'r': lkmode = _LK_NBLCK; break; |
132 | case 'w': lkmode = _LK_NBLCK; break; | 134 | case 'w': lkmode = _LK_NBLCK; break; |
133 | case 'u': lkmode = _LK_UNLCK; break; | 135 | case 'u': lkmode = _LK_UNLCK; break; |
134 | default : luaL_error (L, "%s: invalid mode", funcname); | 136 | default : return luaL_error (L, "%s: invalid mode", funcname); |
135 | } | 137 | } |
136 | if (!len) { | 138 | if (!len) { |
137 | fseek (fh, 0L, SEEK_END); | 139 | fseek (fh, 0L, SEEK_END); |
@@ -345,13 +347,27 @@ static int dir_create_meta (lua_State *L) { | |||
345 | 347 | ||
346 | 348 | ||
347 | #ifdef _WIN32 | 349 | #ifdef _WIN32 |
348 | #define S_ISDIR(mode) (mode&_S_IFDIR) | 350 | #ifndef S_ISDIR |
349 | #define S_ISREG(mode) (mode&_S_IFREG) | 351 | #define S_ISDIR(mode) (mode&_S_IFDIR) |
350 | #define S_ISLNK(mode) (0) | 352 | #endif |
351 | #define S_ISSOCK(mode) (0) | 353 | #ifndef S_ISREG |
352 | #define S_ISFIFO(mode) (0) | 354 | #define S_ISREG(mode) (mode&_S_IFREG) |
353 | #define S_ISCHR(mode) (mode&_S_IFCHR) | 355 | #endif |
354 | #define S_ISBLK(mode) (0) | 356 | #ifndef S_ISLNK |
357 | #define S_ISLNK(mode) (0) | ||
358 | #endif | ||
359 | #ifndef S_ISSOCK | ||
360 | #define S_ISSOCK(mode) (0) | ||
361 | #endif | ||
362 | #ifndef S_ISFIFO | ||
363 | #define S_ISFIFO(mode) (0) | ||
364 | #endif | ||
365 | #ifndef S_ISCHR | ||
366 | #define S_ISCHR(mode) (mode&_S_IFCHR) | ||
367 | #endif | ||
368 | #ifndef S_ISBLK | ||
369 | #define S_ISBLK(mode) (0) | ||
370 | #endif | ||
355 | #endif | 371 | #endif |
356 | /* | 372 | /* |
357 | ** Convert the inode protection mode to a string. | 373 | ** Convert the inode protection mode to a string. |
@@ -381,6 +397,30 @@ static const char *mode2string (mode_t mode) { | |||
381 | 397 | ||
382 | 398 | ||
383 | /* | 399 | /* |
400 | ** Set access time and modification values for file | ||
401 | */ | ||
402 | static int file_utime (lua_State *L) { | ||
403 | const char *file = luaL_checkstring (L, 1); | ||
404 | struct utimbuf utb, *buf; | ||
405 | |||
406 | if (lua_gettop (L) == 1) /* set to current date/time */ | ||
407 | buf = NULL; | ||
408 | else { | ||
409 | utb.actime = (time_t)luaL_optnumber (L, 2, 0); | ||
410 | utb.modtime = (time_t)luaL_optnumber (L, 3, utb.actime); | ||
411 | buf = &utb; | ||
412 | } | ||
413 | if (utime (file, buf)) { | ||
414 | lua_pushnil (L); | ||
415 | lua_pushfstring (L, "%s", strerror (errno)); | ||
416 | return 2; | ||
417 | } | ||
418 | lua_pushboolean (L, 1); | ||
419 | return 1; | ||
420 | } | ||
421 | |||
422 | |||
423 | /* | ||
384 | ** Get file information | 424 | ** Get file information |
385 | */ | 425 | */ |
386 | static int file_info (lua_State *L) { | 426 | static int file_info (lua_State *L) { |
@@ -478,6 +518,7 @@ static const struct luaL_reg fslib[] = { | |||
478 | {"dir", dir_iter_factory}, | 518 | {"dir", dir_iter_factory}, |
479 | {"lock", file_lock}, | 519 | {"lock", file_lock}, |
480 | {"mkdir", make_dir}, | 520 | {"mkdir", make_dir}, |
521 | {"touch", file_utime}, | ||
481 | {"unlock", file_unlock}, | 522 | {"unlock", file_unlock}, |
482 | {NULL, NULL}, | 523 | {NULL, NULL}, |
483 | }; | 524 | }; |
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") | |||
32 | assert (lfs.currentdir() == current, "error trying to change directories") | 32 | assert (lfs.currentdir() == current, "error trying to change directories") |
33 | assert (lfs.chdir ("this couldn't be an actual directory") == nil, "could change to a non-existent directory") | 33 | assert (lfs.chdir ("this couldn't be an actual directory") == nil, "could change to a non-existent directory") |
34 | -- Changing creating and removing directories | 34 | -- Changing creating and removing directories |
35 | assert (lfs.mkdir (tmp.."/lfs_tmp_dir"), "could not make a new directory") | 35 | local tmpdir = tmp.."/lfs_tmp_dir" |
36 | assert (os.remove (tmp.."/lfs_tmp_dir"), "could not remove new directory") | 36 | assert (lfs.mkdir (tmpdir), "could not make a new directory") |
37 | assert (lfs.mkdir (tmp.."/lfs_tmp_dir/lfs_tmp_dir") == false, "could create a directory inside a non-existent one") | 37 | local attrib, errmsg = lfs.attributes (tmpdir) |
38 | if not attrib then | ||
39 | error ("could not get attributes of file `"..tmpdir.."':\n"..errmsg) | ||
40 | else | ||
41 | -- Change access time | ||
42 | assert (lfs.touch (tmpdir, 11)) | ||
43 | local new_att = assert (lfs.attributes (tmpdir)) | ||
44 | assert (new_att.access == 11, "could not set access time") | ||
45 | assert (new_att.modification == 11, "could not set modification time") | ||
46 | -- Change access and modification time | ||
47 | assert (lfs.touch (tmpdir, 33, 22)) | ||
48 | local new_att = assert (lfs.attributes (tmpdir)) | ||
49 | assert (new_att.access == 33, "could not set access time") | ||
50 | assert (new_att.modification == 22, "could not set modification time") | ||
51 | -- Restore access time to current value | ||
52 | assert (lfs.touch (tmpdir)) | ||
53 | new_att = assert (lfs.attributes (tmpdir)) | ||
54 | assert (new_att.access == attrib.access) | ||
55 | assert (new_att.modification == attrib.modification) | ||
56 | end | ||
57 | assert (os.remove (tmpdir), "could not remove new directory") | ||
58 | assert (lfs.mkdir (tmpdir.."/lfs_tmp_dir") == false, "could create a directory inside a non-existent one") | ||
38 | -- | 59 | -- |
39 | assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file") | 60 | assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file") |
40 | assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") | 61 | assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") |
62 | print"Ok!" | ||