summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-09-12 11:20:12 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-09-12 11:20:12 +0100
commit1bdbe528d4050d770e76b5a166e324ef6ce39d35 (patch)
tree14c546de6c0c1f427e638af4e241eb209b3a3fa2
parentdc2e3274d0147db4e8d2f8e846419dadc053ebcc (diff)
downloadluxio-1bdbe528d4050d770e76b5a166e324ef6ce39d35.tar.gz
Add cwd='/path/to/foo' support to luxio.subprocess.spawn
-rw-r--r--luxio/subprocess.lua15
1 files changed, 15 insertions, 0 deletions
diff --git a/luxio/subprocess.lua b/luxio/subprocess.lua
index d0a7c91..3da5993 100644
--- a/luxio/subprocess.lua
+++ b/luxio/subprocess.lua
@@ -45,6 +45,9 @@
-- If you want the child (during pre-exec) to close additional FDs then
-- you can specify them in a table called close_in_child
--
+-- If you want to change directory before exec in the child, you can pass
+-- cwd="/some/path".
+--
-- spawn_simple has one additional tweak. If 'stdin' is provided as
-- a string then it will automatically write that string to the stdin
-- of the child (and close the FD afterwards) for you automatically.
@@ -92,6 +95,8 @@ local dup2 = l.dup2
local fcntl = l.fcntl
local bclear = l.bit.bclear
local wrap_fd = sio.wrap_fd
+local chdir = sio.chdir
+local stderr_fh = sio.stderr
local setenv = l.setenv
local unsetenv = l.unsetenv
local _exit = l._exit
@@ -295,6 +300,16 @@ local function _spawn(t)
end
end
+ -- Change directory if needed
+ if proc.args.cwd then
+ local cwd = tostring(proc.args.cwd)
+ local ret, err = chdir(cwd)
+ if ret ~= 0 then
+ stderr_fh:write("chdir(" .. cwd .. "): " .. err)
+ _exit(1)
+ end
+ end
+
-- Run the child process
exec_fn(exe, unpack(proc.args))