From f1fbc5c0c4f50e16e1f82ce9374080659320a899 Mon Sep 17 00:00:00 2001 From: Kyle McLamb Date: Fri, 19 Feb 2016 02:53:35 -0500 Subject: Use mkdtemp() where available --- src/luarocks/cfg.lua | 1 + src/luarocks/fs/lua.lua | 36 +++++++++++++++++------------------- src/luarocks/fs/unix.lua | 2 -- src/luarocks/fs/unix/tools.lua | 18 ++++++++++++++++++ src/luarocks/fs/win32.lua | 19 +++++++++++++++++++ 5 files changed, 55 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/luarocks/cfg.lua b/src/luarocks/cfg.lua index 0099ec3a..de0a8e65 100644 --- a/src/luarocks/cfg.lua +++ b/src/luarocks/cfg.lua @@ -362,6 +362,7 @@ local defaults = { FIND = "find", TEST = "test", CHMOD = "chmod", + MKTEMP = "mktemp", ZIP = "zip", 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") local dir_stack = {} -math.randomseed(os.time()) - local dir_separator = "/" --- Quote argument for shell processing. @@ -67,23 +65,6 @@ function fs_lua.is_writable(file) return result end ---- Create a temporary directory. --- @param name string: name pattern to use for avoiding conflicts --- when creating temporary directory. --- @return string or (nil, string): name of temporary directory or (nil, error message) on failure. -function fs_lua.make_temp_dir(name) - assert(type(name) == "string") - name = dir.normalize(name) - - local temp_dir = (os.getenv("TMP") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-" .. tostring(math.floor(math.random() * 10000)) - local ok, err = fs.make_dir(temp_dir) - if ok then - return temp_dir - else - return nil, err - end -end - local function quote_args(command, ...) local out = { command } for _, arg in ipairs({...}) do @@ -785,6 +766,23 @@ function fs_lua.get_permissions(file) return posix.stat(file, "mode") end +--- Create a temporary directory. +-- @param name string: name pattern to use for avoiding conflicts +-- when creating temporary directory. +-- @return string or (nil, string): name of temporary directory or (nil, error message) on failure. +function fs_lua.make_temp_dir(name) + assert(type(name) == "string") + name = dir.normalize(name) + + local template = (os.getenv("TMPDIR") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-XXXXXX" + local temp_dir, err = posix.mkdtemp(template) + if temp_dir then + return temp_dir + else + return nil, err + end +end + end --------------------------------------------------------------------- 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") local dir = require("luarocks.dir") local util = require("luarocks.util") -math.randomseed(os.time()) - --- Annotate command string for quiet execution. -- @param cmd string: A command-line string. -- @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 442004ce..c6673ad5 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -351,4 +351,22 @@ function tools.set_time(file, time) return fs.execute(vars.TOUCH, "-d", "@"..tostring(time), file) end +--- Create a temporary directory. +-- @param name string: name pattern to use for avoiding conflicts +-- when creating temporary directory. +-- @return string or (nil, string): name of temporary directory or (nil, error message) on failure. +function tools.make_temp_dir(name) + assert(type(name) == "string") + name = dir.normalize(name) + + local template = (os.getenv("TMPDIR") or "/tmp") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-XXXXXX" + local pipe = io.popen(vars.MKTEMP.." -d "..fs.Q(template)) + local dirname = pipe:read("*l") + pipe:close() + if dirname and dirname:match("^/") then + return dirname + end + return nil, "Failed to create temporary directory "..tostring(dirname) +end + 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") local dir = require("luarocks.dir") local util = require("luarocks.util") +math.randomseed(os.time()) + -- Monkey patch io.popen and os.execute to make sure quoting -- works as expected. -- See http://lua-users.org/lists/lua-l/2013-11/msg00367.html @@ -221,6 +223,23 @@ function win32.is_writable(file) return result end +--- Create a temporary directory. +-- @param name string: name pattern to use for avoiding conflicts +-- when creating temporary directory. +-- @return string or (nil, string): name of temporary directory or (nil, error message) on failure. +function win32.make_temp_dir(name) + assert(type(name) == "string") + name = dir.normalize(name) + + local temp_dir = os.getenv("TMP") .. "/luarocks_" .. name:gsub(dir.separator, "_") .. "-" .. tostring(math.floor(math.random() * 10000)) + local ok, err = fs.make_dir(temp_dir) + if ok then + return temp_dir + else + return nil, err + end +end + function win32.tmpname() return os.getenv("TMP")..os.tmpname() end -- cgit v1.2.3-55-g6feb