From b41b2a1566dd54d933f16cb87fb452e3ce2fa90b Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Mon, 30 Nov 2020 21:51:55 -0500 Subject: [PATCH] [file.lua] Added create_tmp_file() back in. --- lib/dtutils/file.lua | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/dtutils/file.lua b/lib/dtutils/file.lua index 77d310c..f9f1455 100644 --- a/lib/dtutils/file.lua +++ b/lib/dtutils/file.lua @@ -937,6 +937,38 @@ function dtutils_file.rmdir(path) return dsys.external_command(rm_cmd.." "..path) end +dtutils_file.libdoc.functions["create_tmp_file"] = { + Name = [[create_tmp_file]], + Synopsis = [[creates a temporary file]], + Usage = [[local df = require "lib/dtutils.file + + local result = df.create_tmp_file()]], + Description = [[create_tmp_file can be used to create temporary files]], + Return_Value = [[result - string - path to the created temporary file.]], + Limitations = [[]], + Example = [[]], + See_Also = [[]], + Reference = [[]], + License = [[]], + Copyright = [[]], +} + +function dtutils_file.create_tmp_file() + local tmp_file = os.tmpname() + if dt.configuration.running_os == "windows" then + tmp_file = dt.configuration.tmp_dir .. tmp_file -- windows os.tmpname() defaults to root directory + end + + local f = io.open(tmp_file, "w") + if not f then + log.msg(log.error, string.format("Error writing to `%s`", tmp_file)) + os.remove(tmp_file) + return nil + end + + return tmp_file +end + --[[ The new check_if_bin_exists() does multiple calls to the operating system to check if the file exists and is an executable. On windows, each call to the operating system