From b409fafb5ecff8298ddf0fbf16367b1830a1a452 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Tue, 8 Sep 2020 20:05:58 +0200 Subject: [PATCH] Simplify TemporaryFile --- src/TemporaryFile.cpp | 19 +++---------------- src/TemporaryFile.hpp | 3 --- 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index 285654f48..0c64f5ec3 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -59,14 +59,10 @@ mkstemp(char* name_template) } // namespace TemporaryFile::TemporaryFile(string_view path_prefix) + : path(std::string(path_prefix) + ".XXXXXX") { - if (!initialize(path_prefix) && errno == ENOENT) { - auto dir = Util::dir_name(path); - if (!Util::create_dir(dir)) { - throw Fatal("Failed to create directory {}: {}", dir, strerror(errno)); - } - initialize(path_prefix); - } + Util::ensure_dir_exists(Util::dir_name(path)); + fd = Fd(mkstemp(&path[0])); if (!fd) { throw Fatal( "Failed to create temporary file for {}: {}", path, strerror(errno)); @@ -77,12 +73,3 @@ TemporaryFile::TemporaryFile(string_view path_prefix) fchmod(*fd, 0666 & ~get_umask()); #endif } - -bool -TemporaryFile::initialize(string_view path_prefix) -{ - path = std::string(path_prefix); - path += ".XXXXXX"; - fd = Fd(mkstemp(&path[0])); - return bool(fd); -} diff --git a/src/TemporaryFile.hpp b/src/TemporaryFile.hpp index ca40b4821..3aae4f90e 100644 --- a/src/TemporaryFile.hpp +++ b/src/TemporaryFile.hpp @@ -41,7 +41,4 @@ public: // The actual filename. Empty on error. std::string path; - -private: - bool initialize(nonstd::string_view path_prefix); }; -- 2.47.3