30 #ifndef LIB_TEST_TEMP_DIR_H 31 #define LIB_TEST_TEMP_DIR_H 56 Literal TEMPFILE_PREFIX =
"Lux";
70 : loc_{establishNewDirectory()}
75 if (fs::exists (loc_))
76 destroyTempDirectory();
80 operator fs::path
const& ()
const 86 makeFile (
string name =
"")
89 return establishNewFile (
string{TEMPFILE_PREFIX});
91 auto newFile = loc_ /
name;
92 if (fs::exists (newFile))
93 return establishNewFile (
name);
95 std::ofstream{newFile};
96 if (fs::exists (newFile) and fs::is_empty(newFile))
99 throw error::Fatal{
_Fmt{
"Failed to create unique new file %s in TempDir."} % newFile};
105 establishNewDirectory()
107 auto tmpDir = fs::temp_directory_path();
108 for (uint attempt=0; attempt<LUMIERA_MAX_COMPETITION; ++attempt)
110 auto randName = TEMPFILE_PREFIX + util::showHash (
entropyGen.u64());
111 auto newPath = tmpDir / randName;
113 if (fs::create_directory (newPath)
114 and has_perm (newPath, fs::perms::owner_all)
115 and fs::is_empty (newPath)
119 throw error::Fatal{
_Fmt{
"Failed to create unique new TempDir after %d attempts."}
120 % LUMIERA_MAX_COMPETITION
121 ,error::LUMIERA_ERROR_SAFETY_LIMIT };
125 establishNewFile (
string prefix)
127 for (uint attempt=0; attempt<LUMIERA_MAX_COMPETITION; ++attempt)
129 auto randName = prefix +
"." + util::showHash (
entropyGen.u64());
130 auto newPath = loc_ / randName;
132 if (fs::exists(newPath))
134 std::ofstream{newPath};
135 if (fs::exists(newPath) and fs::is_empty (newPath))
138 throw error::Fatal{
_Fmt{
"Failed to create unique new file at %s after %d attempts."}
139 % loc_ % LUMIERA_MAX_COMPETITION
140 ,error::LUMIERA_ERROR_SAFETY_LIMIT };
144 destroyTempDirectory()
146 fs::remove_all (loc_);
147 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...
A RAII style temporary directory.
Lumiera error handling (C++ interface).
Generating (pseudo) random numbers with controlled seed.