21 #ifndef LIB_TEST_TEMP_DIR_H 22 #define LIB_TEST_TEMP_DIR_H 47 Literal TEMPFILE_PREFIX =
"Lux";
61 : loc_{establishNewDirectory()}
66 if (fs::exists (loc_))
67 destroyTempDirectory();
71 operator fs::path
const& ()
const 77 makeFile (
string name =
"")
80 return establishNewFile (
string{TEMPFILE_PREFIX});
82 auto newFile = loc_ /
name;
83 if (fs::exists (newFile))
84 return establishNewFile (
name);
86 std::ofstream{newFile};
87 if (fs::exists (newFile) and fs::is_empty(newFile))
90 throw error::Fatal{
_Fmt{
"Failed to create unique new file %s in TempDir."} % newFile};
96 establishNewDirectory()
98 auto tmpDir = fs::temp_directory_path();
99 for (uint attempt=0; attempt<LUMIERA_MAX_COMPETITION; ++attempt)
101 auto randName = TEMPFILE_PREFIX + util::showHash (
entropyGen.
u64());
102 auto newPath = tmpDir / randName;
104 if (fs::create_directory (newPath)
105 and has_perm (newPath, fs::perms::owner_all)
106 and fs::is_empty (newPath)
110 throw error::Fatal{
_Fmt{
"Failed to create unique new TempDir after %d attempts."}
111 % LUMIERA_MAX_COMPETITION
112 ,error::LUMIERA_ERROR_SAFETY_LIMIT };
116 establishNewFile (
string prefix)
118 for (uint attempt=0; attempt<LUMIERA_MAX_COMPETITION; ++attempt)
120 auto randName = prefix +
"." + util::showHash (
entropyGen.
u64());
121 auto newPath = loc_ / randName;
123 if (fs::exists(newPath))
125 std::ofstream{newPath};
126 if (fs::exists(newPath) and fs::is_empty (newPath))
129 throw error::Fatal{
_Fmt{
"Failed to create unique new file at %s after %d attempts."}
130 % loc_ % LUMIERA_MAX_COMPETITION
131 ,error::LUMIERA_ERROR_SAFETY_LIMIT };
135 destroyTempDirectory()
137 fs::remove_all (loc_);
138 ENSURE (not fs::exists(loc_));
#define ERROR_LOG_AND_IGNORE(_FLAG_, _OP_DESCR_)
convenience shortcut for a sequence of catch blocks just logging and consuming an error...
Includes the C++ Filesystem library and provides some convenience helpers.
inline string literal This is a marker type to indicate that
Types marked with this mix-in may be moved but not copied.
hard wired safety limits.
Random entropyGen
a global RandomSequencer seeded with real entropy
A front-end for using printf-style formatting.
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera's exception hierarchy.
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
uint64_t u64()
random 64bit number from full range.
A RAII style temporary directory.
Lumiera error handling (C++ interface).
Generating (pseudo) random numbers with controlled seed.