From ccb2e6dc0f0af28e72732e87353ff6f81393616c Mon Sep 17 00:00:00 2001 From: Mathieu Westphal Date: Sun, 5 Jan 2025 08:29:03 +0100 Subject: [PATCH] filesystem: Fix issues related to create_directories --- library/src/image.cxx | 8 ++++++-- library/src/interactor_impl.cxx | 8 ++++++-- library/src/window_impl.cxx | 10 ++++++++++ library/testing/TestSDKEngineExceptions.cxx | 4 ++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/library/src/image.cxx b/library/src/image.cxx index 121b95d6c5..e18211c5a8 100644 --- a/library/src/image.cxx +++ b/library/src/image.cxx @@ -479,8 +479,12 @@ const image& image::save(const fs::path& filePath, SaveFormat format) const try { - // Ensure the directories exists - fs::create_directories(filePath.parent_path()); + // Ensure the directories exists if not empty + fs::path parent = filePath.parent_path(); + if (!parent.empty()) + { + fs::create_directories(parent); + } writer->SetFileName(filePath.string().c_str()); writer->SetInputData(this->Internals->Image); diff --git a/library/src/interactor_impl.cxx b/library/src/interactor_impl.cxx index 13edcdbeb6..6404cee624 100644 --- a/library/src/interactor_impl.cxx +++ b/library/src/interactor_impl.cxx @@ -1185,8 +1185,12 @@ bool interactor_impl::recordInteraction(const fs::path& file) try { - // Ensure parent directories exists - fs::create_directories(file.parent_path()); + // Ensure parent directories exists if not empty + fs::path parent = file.parent_path(); + if (!parent.empty()) + { + fs::create_directories(parent); + } // Make sure the recorder is off and streams are cleared this->Internals->Recorder->Off(); diff --git a/library/src/window_impl.cxx b/library/src/window_impl.cxx index 7e2f8e959c..6f90899467 100644 --- a/library/src/window_impl.cxx +++ b/library/src/window_impl.cxx @@ -553,6 +553,16 @@ void window_impl::SetCachePath(const fs::path& cachePath) { try { + if (cachePath.empty()) + { + throw engine::cache_exception("Provided cache path is empty"); + } + + if (!fs::is_directory(cachePath)) + { + throw engine::cache_exception("Provided cache path is not a directory: " + cachePath.string()); + } + // create directories if they do not exist fs::create_directories(cachePath); } diff --git a/library/testing/TestSDKEngineExceptions.cxx b/library/testing/TestSDKEngineExceptions.cxx index d971404bc9..7245556cda 100644 --- a/library/testing/TestSDKEngineExceptions.cxx +++ b/library/testing/TestSDKEngineExceptions.cxx @@ -22,6 +22,10 @@ int TestSDKEngineExceptions(int argc, char* argv[]) "get non-existent interactor", [&]() { std::ignore = eng.getInteractor(); }); // Test setCachePath error handling + test.expect("set cache path with empty name", + [&]() { eng.setCachePath(""); }); + test.expect("set cache path with non directory name", + [&]() { eng.setCachePath("/path/to/file.ext"); }); test.expect("set cache path with invalid long name", [&]() { eng.setCachePath("/" + std::string(257, 'x')); });