diff --git a/tempdir.cpp b/tempdir.cpp index 0a16ebd4..946de57f 100644 --- a/tempdir.cpp +++ b/tempdir.cpp @@ -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);