Commit 8488c991 authored by alex@thinkpad's avatar alex@thinkpad
Browse files

Lua: dryos.rename (including API tests)

--HG--
branch : lua_fix
parent 984c36f7e1eb
......@@ -148,6 +148,25 @@ static int luaCB_dryos_remove(lua_State * L)
return 1;
}
/***
Renames/moves a file on the card (or between cards).
@tparam string filename
@treturn bool success
@function rename
*/
static int luaCB_dryos_rename(lua_State * L)
{
LUA_PARAM_STRING(src, 1);
LUA_PARAM_STRING(dst, 2);
int err = FIO_RenameFile(src, dst);
if (err)
{
err = FIO_MoveFile(src, dst);
}
lua_pushboolean(L, err == 0);
return 1;
}
static void setfield (lua_State *L, const char *key, int value) {
lua_pushinteger(L, value);
lua_setfield(L, -2, key);
......@@ -496,6 +515,7 @@ const luaL_Reg dryoslib[] =
{"call", luaCB_dryos_call},
{"directory", luaCB_dryos_directory},
{"remove", luaCB_dryos_remove},
{"rename", luaCB_dryos_rename},
{NULL, NULL}
};
......
......@@ -178,10 +178,50 @@ function append_test(file)
printf("Append test OK\n")
end
function rename_test(src, dst)
printf("Rename test: %s -> %s\n", src, dst)
local data = "Millions saw the apple fall, " ..
"but Newton was the one who asked why."
-- create the source file
local fout = io.open(src, "w")
fout:write(data)
fout:close()
-- verify the contents
local fin = io.open(src, "r")
local check = fin:read("*all")
fin:close()
assert(check == data)
-- rename it
assert(dryos.rename(src, dst) == true)
-- verify the contents
fin = io.open(dst, "r")
check = fin:read("*all")
fin:close()
assert(check == data)
-- check if the source file was deleted
assert(io.open(src, "rb") == nil)
-- cleanup: delete the renamed file
assert(dryos.remove(dst) == true)
-- check if it was deleted
assert(io.open(dst, "rb") == nil)
printf("Rename test OK\n")
end
function test_io()
stdio_test()
copy_test("autoexec.bin", "tmp.bin")
append_test("tmp.txt")
rename_test("apple.txt", "banana.txt")
rename_test("apple.txt", "ML/banana.txt")
end
function test_keys()
......
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