diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2016-05-03 16:12:06 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2016-05-03 16:12:06 -0300 |
commit | 0d11ac64fc6b513cf7940699ff57303366bccbe2 (patch) | |
tree | d3b1a78ebf5220e7146c268d60a529adef0690fd | |
parent | a05da8546fca83d2d54304d29a216fbd52406ec7 (diff) | |
parent | f1fbc5c0c4f50e16e1f82ce9374080659320a899 (diff) | |
download | luarocks-0d11ac64fc6b513cf7940699ff57303366bccbe2.tar.gz luarocks-0d11ac64fc6b513cf7940699ff57303366bccbe2.tar.bz2 luarocks-0d11ac64fc6b513cf7940699ff57303366bccbe2.zip |
Merge pull request #504 from Alloyed/fix-temp-files
Use mkstemp()/mkdtemp() where available
-rw-r--r-- | src/luarocks/cfg.lua | 1 | ||||
-rw-r--r-- | src/luarocks/fs/lua.lua | 36 | ||||
-rw-r--r-- | src/luarocks/fs/unix.lua | 2 | ||||
-rw-r--r-- | src/luarocks/fs/unix/tools.lua | 18 | ||||
-rw-r--r-- | src/luarocks/fs/win32.lua | 19 |
5 files changed, 55 insertions, 21 deletions
diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index f01fd205..c9fc00eb 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua | |||
@@ -362,6 +362,7 @@ local defaults = { | |||
362 | FIND = "find", | 362 | FIND = "find", |
363 | TEST = "test", | 363 | TEST = "test", |
364 | CHMOD = "chmod", | 364 | CHMOD = "chmod", |
365 | MKTEMP = "mktemp", | ||
365 | 366 | ||
366 | ZIP = "zip", | 367 | ZIP = "zip", |
367 | UNZIP = "unzip -n", | 368 | UNZIP = "unzip -n", |
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 1d303c67..8f45a829 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
@@ -28,8 +28,6 @@ local patch = require("luarocks.tools.patch") | |||
28 | 28 | ||
29 | local dir_stack = {} | 29 | local dir_stack = {} |
30 | 30 | ||
31 | math.randomseed(os.time()) | ||
32 | |||
33 | local dir_separator = "/" | 31 | local dir_separator = "/" |
34 | 32 | ||
35 | --- Quote argument for shell processing. | 33 | --- Quote argument for shell processing. |
@@ -67,23 +65,6 @@ function fs_lua.is_writable(file) | |||
67 | return result | 65 | return result |
68 | end | 66 | end |
69 | 67 | ||
70 | --- Create a temporary directory. | ||
71 | -- @param name string: name pattern to use for avoiding conflicts | ||
72 | -- when creating temporary directory. | ||
73 | -- @return string or (nil, string): name of temporary directory or (nil, error message) on failure. | ||
74 | function fs_lua.make_temp_dir(name) | ||
75 | assert(type(name) == "string") | ||
76 | name = dir.normalize(name) | ||
77 | |||
78 | local temp_dir = (os.getenv("TMP") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-" .. tostring(math.floor(math.random() * 10000)) | ||
79 | local ok, err = fs.make_dir(temp_dir) | ||
80 | if ok then | ||
81 | return temp_dir | ||
82 | else | ||
83 | return nil, err | ||
84 | end | ||
85 | end | ||
86 | |||
87 | local function quote_args(command, ...) | 68 | local function quote_args(command, ...) |
88 | local out = { command } | 69 | local out = { command } |
89 | for _, arg in ipairs({...}) do | 70 | for _, arg in ipairs({...}) do |
@@ -785,6 +766,23 @@ function fs_lua.get_permissions(file) | |||
785 | return posix.stat(file, "mode") | 766 | return posix.stat(file, "mode") |
786 | end | 767 | end |
787 | 768 | ||
769 | --- Create a temporary directory. | ||
770 | -- @param name string: name pattern to use for avoiding conflicts | ||
771 | -- when creating temporary directory. | ||
772 | -- @return string or (nil, string): name of temporary directory or (nil, error message) on failure. | ||
773 | function fs_lua.make_temp_dir(name) | ||
774 | assert(type(name) == "string") | ||
775 | name = dir.normalize(name) | ||
776 | |||
777 | local template = (os.getenv("TMPDIR") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-XXXXXX" | ||
778 | local temp_dir, err = posix.mkdtemp(template) | ||
779 | if temp_dir then | ||
780 | return temp_dir | ||
781 | else | ||
782 | return nil, err | ||
783 | end | ||
784 | end | ||
785 | |||
788 | end | 786 | end |
789 | 787 | ||
790 | --------------------------------------------------------------------- | 788 | --------------------------------------------------------------------- |
diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index 8eb3386a..570b26e4 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua | |||
@@ -9,8 +9,6 @@ local cfg = require("luarocks.cfg") | |||
9 | local dir = require("luarocks.dir") | 9 | local dir = require("luarocks.dir") |
10 | local util = require("luarocks.util") | 10 | local util = require("luarocks.util") |
11 | 11 | ||
12 | math.randomseed(os.time()) | ||
13 | |||
14 | --- Annotate command string for quiet execution. | 12 | --- Annotate command string for quiet execution. |
15 | -- @param cmd string: A command-line string. | 13 | -- @param cmd string: A command-line string. |
16 | -- @return string: The command-line, with silencing annotation. | 14 | -- @return string: The command-line, with silencing annotation. |
diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 767bae4d..ab55897e 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua | |||
@@ -357,4 +357,22 @@ function tools.set_time(file, time) | |||
357 | return fs.execute(vars.TOUCH, "-d", "@"..tostring(time), file) | 357 | return fs.execute(vars.TOUCH, "-d", "@"..tostring(time), file) |
358 | end | 358 | end |
359 | 359 | ||
360 | --- Create a temporary directory. | ||
361 | -- @param name string: name pattern to use for avoiding conflicts | ||
362 | -- when creating temporary directory. | ||
363 | -- @return string or (nil, string): name of temporary directory or (nil, error message) on failure. | ||
364 | function tools.make_temp_dir(name) | ||
365 | assert(type(name) == "string") | ||
366 | name = dir.normalize(name) | ||
367 | |||
368 | local template = (os.getenv("TMPDIR") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-XXXXXX" | ||
369 | local pipe = io.popen(vars.MKTEMP.." -d "..fs.Q(template)) | ||
370 | local dirname = pipe:read("*l") | ||
371 | pipe:close() | ||
372 | if dirname and dirname:match("^/") then | ||
373 | return dirname | ||
374 | end | ||
375 | return nil, "Failed to create temporary directory "..tostring(dirname) | ||
376 | end | ||
377 | |||
360 | return tools | 378 | return tools |
diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index 0c8cc9e9..c14c421b 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua | |||
@@ -10,6 +10,8 @@ local cfg = require("luarocks.cfg") | |||
10 | local dir = require("luarocks.dir") | 10 | local dir = require("luarocks.dir") |
11 | local util = require("luarocks.util") | 11 | local util = require("luarocks.util") |
12 | 12 | ||
13 | math.randomseed(os.time()) | ||
14 | |||
13 | -- Monkey patch io.popen and os.execute to make sure quoting | 15 | -- Monkey patch io.popen and os.execute to make sure quoting |
14 | -- works as expected. | 16 | -- works as expected. |
15 | -- See http://lua-users.org/lists/lua-l/2013-11/msg00367.html | 17 | -- See http://lua-users.org/lists/lua-l/2013-11/msg00367.html |
@@ -221,6 +223,23 @@ function win32.is_writable(file) | |||
221 | return result | 223 | return result |
222 | end | 224 | end |
223 | 225 | ||
226 | --- Create a temporary directory. | ||
227 | -- @param name string: name pattern to use for avoiding conflicts | ||
228 | -- when creating temporary directory. | ||
229 | -- @return string or (nil, string): name of temporary directory or (nil, error message) on failure. | ||
230 | function win32.make_temp_dir(name) | ||
231 | assert(type(name) == "string") | ||
232 | name = dir.normalize(name) | ||
233 | |||
234 | local temp_dir = os.getenv("TMP") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-" .. tostring(math.floor(math.random() * 10000)) | ||
235 | local ok, err = fs.make_dir(temp_dir) | ||
236 | if ok then | ||
237 | return temp_dir | ||
238 | else | ||
239 | return nil, err | ||
240 | end | ||
241 | end | ||
242 | |||
224 | function win32.tmpname() | 243 | function win32.tmpname() |
225 | return os.getenv("TMP")..os.tmpname() | 244 | return os.getenv("TMP")..os.tmpname() |
226 | end | 245 | end |