From 6582e99bbb8427fba41b4fc469aa46ac6a370a65 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Tue, 12 Nov 2013 15:51:43 +0100 Subject: [PATCH] Replace shell command mktemp -d with a mkdtemp() call. One more io.popen() is gone. Signed-off-by: Tobias Ulmer --- generic/e2lib.lua | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 4b4f9f8..e8c2b3c 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -1508,30 +1508,29 @@ function e2lib.rmtempfile(tmpfile) end end ---- Create a temporary directory. --- The template string is passed to the mktemp tool, which replaces --- trailing X characters by some random string to create a unique name. --- @param template string: template name (optional) +--- Create a unique temporary directory. +-- The template string must contain at least six trailing "X" characters +-- which are replaced with a random and unique string. +-- @param template Template string or nil for the default. -- @return Name of the directory (string) or false on error. -- @return Error object on failure. function e2lib.mktempdir(template) + local rc, errstring, tmpdir if not template then template = string.format("%s/e2tmp.%d.XXXXXXXX", e2lib.globals.tmpdir, - e2lib.getpid()) - end - local cmd = string.format("mktemp -d '%s'", template) - local mktemp = io.popen(cmd, "r") - if not mktemp then - return false, err.new("can't mktemp") + e2lib.getpid()) end - local tmpdir = mktemp:read() - if not tmpdir then - return false, err.new("can't mktemp") + + rc, errstring, tmpdir = le2lib.mkdtemp(template) + if not rc then + return false, err.new("could not create temporary directory: %s", + errstring) end - mktemp:close() - -- register tmpdir for removing with rmtempdirs() later on + + -- register tmpdir for removal with rmtempdirs() later on table.insert(e2lib.globals.tmpdirs, tmpdir) - e2lib.logf(4, "creating temporary directory: %s", tmpdir) + e2lib.logf(4, "e2lib.mktempdir: created %q", tmpdir) + return tmpdir end -- 2.39.5