From fa386c41f8d84207ee1074e5b922349dd8860316 Mon Sep 17 00:00:00 2001 From: bugdea1er Date: Sat, 19 Apr 2025 15:36:42 +0300 Subject: [PATCH 1/4] Remove `std::error_code` overload for `create_file` --- src/create.cpp | 51 ++++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/src/create.cpp b/src/create.cpp index 311d781d..a845691c 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -154,36 +154,6 @@ std::FILE* create_file(std::ios::openmode mode, std::error_code& ec) { ec.clear(); return handle; } -#else -/// Creates a temporary file in the current user's temporary directory -/// and opens it for reading and writing -/// @param[out] ec Parameter for error reporting -/// @returns A handle to the created temporary file -int create_file(std::error_code& ec) { - fs::path::string_type path = make_pattern(""); - - int handle; -#ifdef O_TMPFILE - handle = open(path.c_str(), O_RDWR | O_TMPFILE, S_IRUSR | S_IWUSR); - if (handle >= 0) { - ec.clear(); - return handle; - } -#endif - - handle = mkstemp(path.data()); - if (handle == -1) { - ec = std::error_code(errno, std::system_category()); - return -1; - } - - unlink(path.c_str()); - // TODO: check that there are no hardlinks to the file - // someone might have created one before we unlinked the file - - ec.clear(); - return handle; -} #endif } // namespace @@ -218,13 +188,28 @@ std::FILE* create_file(std::ios::openmode mode) { } #else int create_file() { - std::error_code ec; - int handle = create_file(ec); + fs::path::string_type path = make_pattern(""); - if (ec) { + int handle; +#ifdef O_TMPFILE + handle = open(path.c_str(), O_RDWR | O_TMPFILE, S_IRUSR | S_IWUSR); + if (handle >= 0) { + return handle; + } +#endif + + handle = mkstemp(path.data()); + errno_t create_error = errno; + + unlink(path.c_str()); + if (handle == -1) { + std::error_code ec = std::error_code(create_error, std::system_category()); throw fs::filesystem_error("Cannot create a temporary file", ec); } + // TODO: check that there are no hardlinks to the file + // someone might have created one before we unlinked the file + return handle; } #endif From 4038941aaafda5d011e87ab5c59672799cad2ccc Mon Sep 17 00:00:00 2001 From: bugdea1er Date: Sat, 19 Apr 2025 15:38:43 +0300 Subject: [PATCH 2/4] Fix local variable type --- src/create.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/create.cpp b/src/create.cpp index a845691c..10fc56e9 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -199,7 +199,7 @@ int create_file() { #endif handle = mkstemp(path.data()); - errno_t create_error = errno; + int create_error = errno; unlink(path.c_str()); if (handle == -1) { From bf56493011d488855b3e1848929501629fac84b1 Mon Sep 17 00:00:00 2001 From: bugdea1er Date: Sat, 19 Apr 2025 15:52:07 +0300 Subject: [PATCH 3/4] Do not try to unlink a non-existent file --- src/create.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/create.cpp b/src/create.cpp index 10fc56e9..306a6fea 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -199,17 +199,15 @@ int create_file() { #endif handle = mkstemp(path.data()); - int create_error = errno; - - unlink(path.c_str()); if (handle == -1) { - std::error_code ec = std::error_code(create_error, std::system_category()); + std::error_code ec = std::error_code(errno, std::system_category()); throw fs::filesystem_error("Cannot create a temporary file", ec); } // TODO: check that there are no hardlinks to the file // someone might have created one before we unlinked the file + unlink(path.c_str()); return handle; } #endif From 07b3e5455a732288b992fd31b6b30f4d1952a4a9 Mon Sep 17 00:00:00 2001 From: bugdea1er Date: Sat, 19 Apr 2025 16:24:49 +0300 Subject: [PATCH 4/4] Remove a todo --- src/create.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/create.cpp b/src/create.cpp index 306a6fea..35b46e2c 100644 --- a/src/create.cpp +++ b/src/create.cpp @@ -14,6 +14,7 @@ #else #include #include +#include #include #endif @@ -204,9 +205,6 @@ int create_file() { throw fs::filesystem_error("Cannot create a temporary file", ec); } - // TODO: check that there are no hardlinks to the file - // someone might have created one before we unlinked the file - unlink(path.c_str()); return handle; }