diff options
| -rw-r--r-- | src/lfs.c | 21 | ||||
| -rw-r--r-- | tests/test.lua | 30 |
2 files changed, 37 insertions, 14 deletions
| @@ -7,10 +7,11 @@ | |||
| 7 | ** lfs.dir (path) | 7 | ** lfs.dir (path) |
| 8 | ** lfs.lock (fh, mode) | 8 | ** lfs.lock (fh, mode) |
| 9 | ** lfs.mkdir (path) | 9 | ** lfs.mkdir (path) |
| 10 | ** lfs.rmdir (path) | ||
| 10 | ** lfs.touch (filepath [, atime [, mtime]]) | 11 | ** lfs.touch (filepath [, atime [, mtime]]) |
| 11 | ** lfs.unlock (fh) | 12 | ** lfs.unlock (fh) |
| 12 | ** | 13 | ** |
| 13 | ** $Id: lfs.c,v 1.21 2005/05/20 18:32:19 uid20006 Exp $ | 14 | ** $Id: lfs.c,v 1.22 2005/06/03 21:50:59 tuler Exp $ |
| 14 | */ | 15 | */ |
| 15 | 16 | ||
| 16 | #include <errno.h> | 17 | #include <errno.h> |
| @@ -250,6 +251,23 @@ static int make_dir (lua_State *L) { | |||
| 250 | return 1; | 251 | return 1; |
| 251 | } | 252 | } |
| 252 | 253 | ||
| 254 | /* | ||
| 255 | ** Removes a directory. | ||
| 256 | ** @param #1 Directory path. | ||
| 257 | */ | ||
| 258 | static int remove_dir (lua_State *L) { | ||
| 259 | const char *path = luaL_checkstring (L, 1); | ||
| 260 | int fail; | ||
| 261 | |||
| 262 | fail = rmdir (path); | ||
| 263 | |||
| 264 | lua_pushboolean (L, !fail); | ||
| 265 | if (fail) { | ||
| 266 | lua_pushfstring (L, "%s", strerror(errno)); | ||
| 267 | return 2; | ||
| 268 | } | ||
| 269 | return 1; | ||
| 270 | } | ||
| 253 | 271 | ||
| 254 | /* | 272 | /* |
| 255 | ** Directory iterator | 273 | ** Directory iterator |
| @@ -519,6 +537,7 @@ static const struct luaL_reg fslib[] = { | |||
| 519 | {"dir", dir_iter_factory}, | 537 | {"dir", dir_iter_factory}, |
| 520 | {"lock", file_lock}, | 538 | {"lock", file_lock}, |
| 521 | {"mkdir", make_dir}, | 539 | {"mkdir", make_dir}, |
| 540 | {"rmdir", remove_dir}, | ||
| 522 | {"touch", file_utime}, | 541 | {"touch", file_utime}, |
| 523 | {"unlock", file_unlock}, | 542 | {"unlock", file_unlock}, |
| 524 | {NULL, NULL}, | 543 | {NULL, NULL}, |
diff --git a/tests/test.lua b/tests/test.lua index 7f981e5..d0018cd 100644 --- a/tests/test.lua +++ b/tests/test.lua | |||
| @@ -9,7 +9,7 @@ require"lfs" | |||
| 9 | function attrdir (path) | 9 | function attrdir (path) |
| 10 | for file in lfs.dir(path) do | 10 | for file in lfs.dir(path) do |
| 11 | if file ~= "." and file ~= ".." then | 11 | if file ~= "." and file ~= ".." then |
| 12 | local f = path..'/'..file | 12 | local f = path..sep..file |
| 13 | print ("\t=> "..f.." <=") | 13 | print ("\t=> "..f.." <=") |
| 14 | local attr = lfs.attributes (f) | 14 | local attr = lfs.attributes (f) |
| 15 | assert (type(attr) == "table") | 15 | assert (type(attr) == "table") |
| @@ -32,30 +32,34 @@ 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 | local tmpdir = tmp.."/lfs_tmp_dir" | 35 | local tmpdir = tmp..sep.."lfs_tmp_dir" |
| 36 | assert (lfs.mkdir (tmpdir), "could not make a new directory") | 36 | assert (lfs.mkdir (tmpdir), "could not make a new directory") |
| 37 | local attrib, errmsg = lfs.attributes (tmpdir) | 37 | -- create a new file |
| 38 | local tmpfile = tmpdir..sep.."lfs_tmp_file" | ||
| 39 | assert (io.open(tmpfile, "w"), "could not make a new file") | ||
| 40 | local attrib, errmsg = lfs.attributes (tmpfile) | ||
| 38 | if not attrib then | 41 | if not attrib then |
| 39 | error ("could not get attributes of file `"..tmpdir.."':\n"..errmsg) | 42 | error ("could not get attributes of file `"..tmpfile.."':\n"..errmsg) |
| 40 | else | 43 | else |
| 41 | -- Change access time | 44 | -- Change access time |
| 42 | assert (lfs.touch (tmpdir, 11)) | 45 | assert (lfs.touch (tmpfile, 11)) |
| 43 | local new_att = assert (lfs.attributes (tmpdir)) | 46 | local new_att = assert (lfs.attributes (tmpfile)) |
| 44 | assert (new_att.access == 11, "could not set access time") | 47 | assert (new_att.access == 11, string.format("could not set access time: %s", tostring(new_att.access))) |
| 45 | assert (new_att.modification == 11, "could not set modification time") | 48 | assert (new_att.modification == 11, "could not set modification time") |
| 46 | -- Change access and modification time | 49 | -- Change access and modification time |
| 47 | assert (lfs.touch (tmpdir, 33, 22)) | 50 | assert (lfs.touch (tmpfile, 33, 22)) |
| 48 | local new_att = assert (lfs.attributes (tmpdir)) | 51 | local new_att = assert (lfs.attributes (tmpfile)) |
| 49 | assert (new_att.access == 33, "could not set access time") | 52 | assert (new_att.access == 33, "could not set access time") |
| 50 | assert (new_att.modification == 22, "could not set modification time") | 53 | assert (new_att.modification == 22, "could not set modification time") |
| 51 | -- Restore access time to current value | 54 | -- Restore access time to current value |
| 52 | assert (lfs.touch (tmpdir)) | 55 | assert (lfs.touch (tmpfile)) |
| 53 | new_att = assert (lfs.attributes (tmpdir)) | 56 | new_att = assert (lfs.attributes (tmpfile)) |
| 54 | assert (new_att.access == attrib.access) | 57 | assert (new_att.access == attrib.access) |
| 55 | assert (new_att.modification == attrib.modification) | 58 | assert (new_att.modification == attrib.modification) |
| 56 | end | 59 | end |
| 57 | assert (os.remove (tmpdir), "could not remove new directory") | 60 | assert (os.remove (tmpfile), "could not remove file") |
| 58 | assert (lfs.mkdir (tmpdir.."/lfs_tmp_dir") == false, "could create a directory inside a non-existent one") | 61 | assert (lfs.rmdir (tmpdir), "could not remove new directory") |
| 62 | assert (lfs.mkdir (tmpdir..sep.."lfs_tmp_dir") == false, "could create a directory inside a non-existent one") | ||
| 59 | -- | 63 | -- |
| 60 | assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file") | 64 | assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file") |
| 61 | assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") | 65 | assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") |
