Commit 937daad6 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner

lua: chdir and getcwd

parent 7903d6f3
......@@ -305,6 +305,32 @@ static int lua_mkdtemp(lua_State *L) {
}
}
static int lua_chdir(lua_State *L) {
int param_count = lua_gettop(L);
if (param_count != 1)
return luaL_error(L, "chdir expects 1 parameter");
const char *path = luaL_checkstring(L, 1);
int result = chdir(path);
if (result == -1)
return luaL_error(L, "chdir to %s: %s", path, strerror(errno));
return 0;
}
static int lua_getcwd(lua_State *L) {
const char *result = NULL;
// An arbitrary length.
size_t s = 16;
while (!result) {
s *= 2;
char *buf = alloca(s);
result = getcwd(buf, s);
if (!result && errno != ERANGE)
return luaL_error(L, "getcwd: %s", strerror(errno));
}
lua_pushstring(L, result);
return 1;
}
struct injected_func {
int (*func)(lua_State *);
const char *name;
......@@ -314,7 +340,9 @@ static const struct injected_func injected_funcs[] = {
{ lua_log, "log" },
{ lua_run_command, "run_command" },
{ lua_events_wait, "events_wait" },
{ lua_mkdtemp, "mkdtemp" }
{ lua_mkdtemp, "mkdtemp" },
{ lua_chdir, "chdir" },
{ lua_getcwd, "getcwd" }
/*
* Note: watch_cancel is not provided, because it would be hell to
* manage the dynamically allocated memory correctly and there doesn't
......
......@@ -84,3 +84,9 @@ mkdtemp([directory])::
created as a subdirectory of the given directory, otherwise it is
created inside `/tmp`. It returns path to the new directory, or
`nil` and an error message.
chdir(directory)::
Change the current working directory to the one provided.
getcwd()::
Get the current working directory.
......@@ -18,7 +18,8 @@ C_TESTS := \
LUA_TESTS := \
backend \
events
events \
interpreter
define DO_C_TEST
......
--[[
Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)
This file is part of the turris updater.
Updater is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Updater is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Updater. If not, see <http://www.gnu.org/licenses/>.
]]--
require 'lunit'
-- Some of the interpreter tests are in C, some are easier written in lua
module("interpreter-tests", package.seeall, lunit.testcase)
function test_dirs()
local top = os.getenv("S") or "."
chdir(top)
chdir("tests")
local dir = getcwd()
-- The current directory should be in tests now
assert_equal("/tests", dir:sub(-6))
end
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