fs/tmp: make temp queries and path generator more robust

This commit is contained in:
Danny Robson 2024-02-21 14:54:53 +10:00
parent 48e71ce0dd
commit cd84d863c2
4 changed files with 100 additions and 2 deletions

View File

@ -127,6 +127,7 @@ if (NOT WIN32)
debug/crash_posix.cpp
debug/fpe_posix.cpp
debug/system_posix.cpp
fs/tmp_posix.cpp
io_posix.cpp
io_posix.hpp
library_posix.hpp
@ -153,6 +154,7 @@ if (WIN32)
debug/fpe_win32.cpp
debug/system_win32.cpp
exe_win32.cpp
fs/tmp_win32.cpp
io_win32.cpp
io_win32.hpp
library_win32.cpp
@ -357,7 +359,6 @@ list (
fourcc.hpp
fs/scoped.cpp
fs/scoped.hpp
fs/tmp_posix.cpp
fs/tmp.hpp
functor.hpp
geom/fwd.hpp

View File

@ -11,6 +11,8 @@
#include "./paths.hpp"
#include "./posix/except.hpp"
#include <unistd.h>
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path
@ -25,8 +27,13 @@ std::filesystem::path
cruft::fs::mktempfile (std::filesystem::path const &dir)
{
auto pattern = (dir / PACKAGE_NAME "-XXXXXX").native ();
if (!mkstemp (pattern.data ()))
auto const fd = mkstemp (pattern.data ());
if (fd < 0)
cruft::posix::error::throw_code ();
cruft::posix::error::try_call(::close, fd);
return pattern;
}

74
fs/tmp_win32.cpp Normal file
View File

@ -0,0 +1,74 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2023, Danny Robson <danny@nerdcruft.net>
*/
#include "./tmp.hpp"
#include "../paths.hpp"
#include "../posix/except.hpp"
#include <io.h>
#include <direct.h>
///////////////////////////////////////////////////////////////////////////////
static std::filesystem::path
_temp_path_at (std::filesystem::path const &root)
{
// The pattern suffix we'll append to the temp directory.
//
// * We might as well append the package name for clarity
// * The libc call REQUIRES the string end with "XXXXXX"
char const SUFFIX[] = "/" PACKAGE_NAME "-XXXXXX";
std::string path;
path.reserve (root.string ().size () + sizeof (SUFFIX));
path = root.string ();
path += SUFFIX;
if (auto const err = _mktemp_s (path.data (), path.size ()); err)
throw cruft::posix::error (err);
return path;
}
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path
cruft::fs::mktempfile (std::filesystem::path const &dir)
{
auto pattern = (dir / PACKAGE_NAME "-XXXXXX").string ();
auto const fd = mkstemp (pattern.data ());
if (fd < 0)
cruft::posix::error::throw_code ();
cruft::posix::error::try_call(::close, fd);
return pattern;
}
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path
cruft::fs::mktmpdir (void)
{
static constexpr int RETRIES = 10;
auto const root = cruft::paths::temp ();
for (int i = 0; i < RETRIES; ++i) {
auto const path = _temp_path_at (root);
if (_mkdir (path.string ().c_str ()))
continue;
return path;
}
throw std::runtime_error ("Unable to create tmpdir");
}

View File

@ -36,3 +36,19 @@ cruft::paths::expand (std::filesystem::path const &val)
return store;
}
///////////////////////////////////////////////////////////////////////////////
std::filesystem::path
cruft::paths::temp (void)
{
std::string value;
value.resize (MAX_PATH + 1);
auto const size = GetTempPathA (value.size (), value.data ());
if (!size)
throw std::runtime_error ("Unable to locate temp");
value.resize (size);
return value;
}