Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
U
updater
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Michal Čihař
updater
Commits
937daad6
Verified
Commit
937daad6
authored
Feb 18, 2016
by
Michal 'vorner' Vaner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lua: chdir and getcwd
parent
7903d6f3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
2 deletions
+69
-2
src/lib/interpreter.c
src/lib/interpreter.c
+29
-1
src/lib/lua_funcs.txt
src/lib/lua_funcs.txt
+6
-0
tests/Makefile.dir
tests/Makefile.dir
+2
-1
tests/interpreter.lua
tests/interpreter.lua
+32
-0
No files found.
src/lib/interpreter.c
View file @
937daad6
...
...
@@ -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
...
...
src/lib/lua_funcs.txt
View file @
937daad6
...
...
@@ -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.
tests/Makefile.dir
View file @
937daad6
...
...
@@ -18,7 +18,8 @@ C_TESTS := \
LUA_TESTS
:=
\
backend
\
events
events
\
interpreter
define
DO_C_TEST
...
...
tests/interpreter.lua
0 → 100644
View file @
937daad6
--[[
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment