Skip to content

Commit

Permalink
TempDir handle the case where the temp dir does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkvdb committed Apr 12, 2024
1 parent 030b3df commit 51b1726
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion tempdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ TempDir::TempDir()
TempDir::TempDir(std::string_view name)
{
const auto timestamp = std::chrono::high_resolution_clock::now().time_since_epoch().count();
_path = fs::temp_directory_path() / std::to_string(timestamp);
try {
_path = fs::temp_directory_path() / std::to_string(timestamp);
} catch (const fs::filesystem_error& e) {
if (e.code() == std::errc::not_a_directory && !fs::exists(e.path1())) {
// The temp directory does not exist, so we need to create it.
fs::create_directories(e.path1());
_path = fs::temp_directory_path() / std::to_string(timestamp);
} else {
throw e;
}
}

if (!name.empty()) {
_path /= file::u8path(name);
Expand Down

0 comments on commit 51b1726

Please sign in to comment.