mirror of
https://github.com/lunarmodules/luafilesystem.git
synced 2026-07-23 19:43:06 +00:00
new function lfs.rmdir
This commit is contained in:
21
src/lfs.c
21
src/lfs.c
@@ -7,10 +7,11 @@
|
|||||||
** lfs.dir (path)
|
** lfs.dir (path)
|
||||||
** lfs.lock (fh, mode)
|
** lfs.lock (fh, mode)
|
||||||
** lfs.mkdir (path)
|
** lfs.mkdir (path)
|
||||||
|
** lfs.rmdir (path)
|
||||||
** lfs.touch (filepath [, atime [, mtime]])
|
** lfs.touch (filepath [, atime [, mtime]])
|
||||||
** lfs.unlock (fh)
|
** lfs.unlock (fh)
|
||||||
**
|
**
|
||||||
** $Id: lfs.c,v 1.21 2005/05/20 18:32:19 uid20006 Exp $
|
** $Id: lfs.c,v 1.22 2005/06/03 21:50:59 tuler Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@@ -250,6 +251,23 @@ static int make_dir (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Removes a directory.
|
||||||
|
** @param #1 Directory path.
|
||||||
|
*/
|
||||||
|
static int remove_dir (lua_State *L) {
|
||||||
|
const char *path = luaL_checkstring (L, 1);
|
||||||
|
int fail;
|
||||||
|
|
||||||
|
fail = rmdir (path);
|
||||||
|
|
||||||
|
lua_pushboolean (L, !fail);
|
||||||
|
if (fail) {
|
||||||
|
lua_pushfstring (L, "%s", strerror(errno));
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Directory iterator
|
** Directory iterator
|
||||||
@@ -519,6 +537,7 @@ static const struct luaL_reg fslib[] = {
|
|||||||
{"dir", dir_iter_factory},
|
{"dir", dir_iter_factory},
|
||||||
{"lock", file_lock},
|
{"lock", file_lock},
|
||||||
{"mkdir", make_dir},
|
{"mkdir", make_dir},
|
||||||
|
{"rmdir", remove_dir},
|
||||||
{"touch", file_utime},
|
{"touch", file_utime},
|
||||||
{"unlock", file_unlock},
|
{"unlock", file_unlock},
|
||||||
{NULL, NULL},
|
{NULL, NULL},
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ require"lfs"
|
|||||||
function attrdir (path)
|
function attrdir (path)
|
||||||
for file in lfs.dir(path) do
|
for file in lfs.dir(path) do
|
||||||
if file ~= "." and file ~= ".." then
|
if file ~= "." and file ~= ".." then
|
||||||
local f = path..'/'..file
|
local f = path..sep..file
|
||||||
print ("\t=> "..f.." <=")
|
print ("\t=> "..f.." <=")
|
||||||
local attr = lfs.attributes (f)
|
local attr = lfs.attributes (f)
|
||||||
assert (type(attr) == "table")
|
assert (type(attr) == "table")
|
||||||
@@ -32,30 +32,34 @@ assert (lfs.chdir (reldir), "could not change back to current directory")
|
|||||||
assert (lfs.currentdir() == current, "error trying to change directories")
|
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")
|
assert (lfs.chdir ("this couldn't be an actual directory") == nil, "could change to a non-existent directory")
|
||||||
-- Changing creating and removing directories
|
-- Changing creating and removing directories
|
||||||
local tmpdir = tmp.."/lfs_tmp_dir"
|
local tmpdir = tmp..sep.."lfs_tmp_dir"
|
||||||
assert (lfs.mkdir (tmpdir), "could not make a new directory")
|
assert (lfs.mkdir (tmpdir), "could not make a new directory")
|
||||||
local attrib, errmsg = lfs.attributes (tmpdir)
|
-- create a new file
|
||||||
|
local tmpfile = tmpdir..sep.."lfs_tmp_file"
|
||||||
|
assert (io.open(tmpfile, "w"), "could not make a new file")
|
||||||
|
local attrib, errmsg = lfs.attributes (tmpfile)
|
||||||
if not attrib then
|
if not attrib then
|
||||||
error ("could not get attributes of file `"..tmpdir.."':\n"..errmsg)
|
error ("could not get attributes of file `"..tmpfile.."':\n"..errmsg)
|
||||||
else
|
else
|
||||||
-- Change access time
|
-- Change access time
|
||||||
assert (lfs.touch (tmpdir, 11))
|
assert (lfs.touch (tmpfile, 11))
|
||||||
local new_att = assert (lfs.attributes (tmpdir))
|
local new_att = assert (lfs.attributes (tmpfile))
|
||||||
assert (new_att.access == 11, "could not set access time")
|
assert (new_att.access == 11, string.format("could not set access time: %s", tostring(new_att.access)))
|
||||||
assert (new_att.modification == 11, "could not set modification time")
|
assert (new_att.modification == 11, "could not set modification time")
|
||||||
-- Change access and modification time
|
-- Change access and modification time
|
||||||
assert (lfs.touch (tmpdir, 33, 22))
|
assert (lfs.touch (tmpfile, 33, 22))
|
||||||
local new_att = assert (lfs.attributes (tmpdir))
|
local new_att = assert (lfs.attributes (tmpfile))
|
||||||
assert (new_att.access == 33, "could not set access time")
|
assert (new_att.access == 33, "could not set access time")
|
||||||
assert (new_att.modification == 22, "could not set modification time")
|
assert (new_att.modification == 22, "could not set modification time")
|
||||||
-- Restore access time to current value
|
-- Restore access time to current value
|
||||||
assert (lfs.touch (tmpdir))
|
assert (lfs.touch (tmpfile))
|
||||||
new_att = assert (lfs.attributes (tmpdir))
|
new_att = assert (lfs.attributes (tmpfile))
|
||||||
assert (new_att.access == attrib.access)
|
assert (new_att.access == attrib.access)
|
||||||
assert (new_att.modification == attrib.modification)
|
assert (new_att.modification == attrib.modification)
|
||||||
end
|
end
|
||||||
assert (os.remove (tmpdir), "could not remove new directory")
|
assert (os.remove (tmpfile), "could not remove file")
|
||||||
assert (lfs.mkdir (tmpdir.."/lfs_tmp_dir") == false, "could create a directory inside a non-existent one")
|
assert (lfs.rmdir (tmpdir), "could not remove new directory")
|
||||||
|
assert (lfs.mkdir (tmpdir..sep.."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 (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")
|
assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory")
|
||||||
|
|||||||
Reference in New Issue
Block a user