Commit 628c7bd0 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner

lua: Use os.remove instead of own unlink and rmdir

parent 374911c9
......@@ -26,6 +26,7 @@ local require = require
local next = next
local unpack = unpack
local io = io
local os = os
local table = table
local mkdtemp = mkdtemp
local chdir = chdir
......@@ -34,8 +35,6 @@ local events_wait = events_wait
local stat = stat
local mkdir = mkdir
local move = move
local unlink = unlink
local rmdir = rmdir
local ls = ls
local DBG = DBG
local WARN = WARN
......@@ -512,7 +511,7 @@ function pkg_cleanup_files(files)
f = f:gsub("/+", "/")
path = root_dir .. f
DBG("Removing file " .. path)
local ok, err = pcall(function () unlink(path) end)
local ok, err = pcall(function () os.remove(path) end)
-- If it failed because the file didn't exist, that's OK. Mostly.
if not ok then
local tp = stat(path)
......@@ -536,7 +535,7 @@ function pkg_cleanup_files(files)
break
else
DBG("Removing empty directory " .. root_dir .. parent)
local ok, err = pcall(function () rmdir(root_dir .. parent) end)
local ok, err = pcall(function () os.remove(root_dir .. parent) end)
if not ok then
-- It is an error, but we don't want to give up on the rest of the operation because of that
ERROR("Failed to removed empty " .. parent .. ", ignoring")
......
......@@ -387,22 +387,6 @@ static int lua_move(lua_State *L) {
return 0;
}
static int lua_unlink(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
int result = unlink(path);
if (result == -1)
return luaL_error(L, "Failed to unlink '%s': %s", path, strerror(errno));
return 0;
}
static int lua_rmdir(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
int result = rmdir(path);
if (result == -1)
return luaL_error(L, "Failed to remove directory '%s': %s", path, strerror(errno));
return 0;
}
static const char *stat2str(const struct stat *buf) {
switch (buf->st_mode & S_IFMT) {
case S_IFSOCK:
......@@ -507,8 +491,6 @@ static const struct injected_func injected_funcs[] = {
{ lua_getcwd, "getcwd" },
{ lua_mkdir, "mkdir" },
{ lua_move, "move" },
{ lua_unlink, "unlink" },
{ lua_rmdir, "rmdir" },
{ lua_ls, "ls" },
{ lua_stat, "stat" }
/*
......
......@@ -99,12 +99,6 @@ move(old, new)::
Move a file from the old location to the new. It tries to cope with
paths on different devices.
unlink(path)::
Remove a file or something like that.
rmdir(path)::
Remove (an empty) directory.
ls(directory)::
Return a table with file names under the given directory (acts like
shell's `ls -a`, omitting the `.` and `..` entries). Value of
......
......@@ -60,19 +60,12 @@ function test_fsutils()
f:close()
-- The file exists
assert_table_equal({["x"] = "r"}, ls(dir .. "/d2"))
-- Can't remove non-empty dir
assert_error(function () rmdir(dir .. "/d2") end)
-- A directory on another file system than tmp (likely)
local ldir = mkdtemp(getcwd())
table.insert(tmp_dirs, ldir)
-- Cross-device move
move(dir .. "/d2/x", ldir .. "/x")
assert_table_equal({["x"] = "r"}, ls(ldir))
unlink(ldir .. "/x")
-- d2 is now empty, so it can be removed
rmdir(dir .. "/d2")
-- Nothing left now
assert_table_equal({}, ls(dir))
end
function teardown()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment