From 3a58eeda07e2b5b16e46ed8444f9f6a4cf096f5a Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Fri, 26 Apr 2013 16:46:01 +0200 Subject: [PATCH] Add error handling to e2lib.mktempfile() Signed-off-by: Tobias Ulmer --- generic/e2lib.lua | 10 +++++----- generic/generic_git.lua | 28 ++++++++++++++++++++++++---- local/e2-new-source.lua | 14 +++++++++++--- local/e2tool.lua | 6 +++++- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/generic/e2lib.lua b/generic/e2lib.lua index 5b80540..5f1ab06 100644 --- a/generic/e2lib.lua +++ b/generic/e2lib.lua @@ -1376,22 +1376,22 @@ end --- Create a temporary file. -- The template string is passed to the mktemp tool, which replaces -- trailing X characters by some random string to create a unique name. --- This function always succeeds (or aborts immediately). -- @param template string: template name (optional) --- @return string: name of the file +-- @return Name of the file or false on error. +-- @return Error object on failure. function e2lib.mktempfile(template) if not template then template = string.format("%s/e2tmp.%d.XXXXXXXX", e2lib.globals.tmpdir, - e2util.getpid()) + e2util.getpid()) end local cmd = string.format("mktemp '%s'", template) local mktemp = io.popen(cmd, "r") if not mktemp then - e2lib.abort("can't mktemp") + return false, err.new("can't mktemp") end local tmp = mktemp:read() if not tmp then - e2lib.abort("can't mktemp") + return false, err.new("can't mktemp") end mktemp:close() -- register tmp for removing with rmtempfiles() later on diff --git a/generic/generic_git.lua b/generic/generic_git.lua index e45fb0c..1b7fd72 100644 --- a/generic/generic_git.lua +++ b/generic/generic_git.lua @@ -129,7 +129,12 @@ end function generic_git.git_rev_list1(gitdir, ref) local e = err.new("git rev-list failed") local rc, re - local tmpfile = e2lib.mktempfile() + + local tmpfile, re = e2lib.mktempfile() + if not tmpfile then + return false, e:cat(re) + end + local args = string.format("--max-count=1 '%s' -- >'%s'", ref, tmpfile) rc, re = e2lib.git(gitdir, "rev-list", args) if not rc then @@ -359,7 +364,12 @@ end function generic_git.git_config(gitdir, query) local rc, re local e = err.new("running git config") - local tmpfile = e2lib.mktempfile() + + local tmpfile, re = e2lib.mktempfile() + if not tmpfile then + return false, e:cat(re) + end + local cmd = string.format("GIT_DIR=%s git config %s > %s", e2lib.shquote(gitdir), e2lib.shquote(query), e2lib.shquote(tmpfile)) local rc, re = e2lib.callcmd_log(cmd) @@ -464,7 +474,12 @@ function generic_git.verify_clean_repository(gitwc) gitwc = gitwc or "." local e = err.new("verifying that repository is clean") local rc, re - local tmp = e2lib.mktempfile() + + local tmp, re = e2lib.mktempfile() + if not tmp then + return false, e:cat(re) + end + rc, re = e2lib.chdir(gitwc) if not rc then return nil, e:cat(re) @@ -534,7 +549,12 @@ function generic_git.verify_head_match_tag(gitwc, verify_tag) gitwc = gitwc or "." local e = err.new("verifying that HEAD matches 'refs/tags/%s'", verify_tag) local rc, re - local tmp = e2lib.mktempfile() + + local tmp, re = e2lib.mktempfile() + if not tmp then + return false, e:cat(re) + end + local args = string.format("--tags --match '%s' >%s", verify_tag, tmp) rc, re = e2lib.chdir(gitwc) if not rc then diff --git a/local/e2-new-source.lua b/local/e2-new-source.lua index 8d46195..0c4fcda 100644 --- a/local/e2-new-source.lua +++ b/local/e2-new-source.lua @@ -45,14 +45,19 @@ local url = require("url") -- @return temporary filename or false. -- @return an error object on failure. local function download(f) + local rc, re local path = e2lib.dirname(f) local fn = e2lib.basename(f) - local tfile = e2lib.mktempfile() + local tfile, re = e2lib.mktempfile() + if not tfile then + return false, re + end + local tpath = e2lib.dirname(tfile) local tfn = e2lib.basename(tfile) - local rc, re = transport.fetch_file(path, fn, tpath, tfn) + rc, re = transport.fetch_file(path, fn, tpath, tfn) if not rc then return rc, re end @@ -120,7 +125,10 @@ local function new_files_source(c, server, location, source_file, checksum_file, -- check for file with identical name on the server local tmpfile = {} - tmpfile.file = e2lib.mktempfile() + tmpfile.file, re = e2lib.mktempfile() + if not tmpfile.file then + return false, e:cat(re) + end tmpfile.base = e2lib.basename(tmpfile.file) tmpfile.dir = e2lib.dirname(tmpfile.file) diff --git a/local/e2tool.lua b/local/e2tool.lua index f8346f7..58024e7 100644 --- a/local/e2tool.lua +++ b/local/e2tool.lua @@ -2029,7 +2029,11 @@ local function verify_remote_fileid(info, file, fileid) return false, e:cat(re) end - local tmpfile = e2lib.mktempfile() + local tmpfile, re = e2lib.mktempfile() + if not tmpfile then + return false, e:cat(re) + end + rc, re = transport.fetch_file(ce.remote_url, file.location, e2lib.dirname(tmpfile), e2lib.basename(tmpfile)) if not rc then -- 2.39.5