From 9f1d72e5573fbd47108858ce568d9bd70b488767 Mon Sep 17 00:00:00 2001 From: Hanif Koh Date: Wed, 26 Aug 2026 18:01:34 +0800 Subject: [PATCH] Create Directories Recursively, and early exit gracefully if failed to create --- src/OrcaSlicer.cpp | 23 +++++++++++++++++++++-- src/libslic3r/utils.cpp | 6 +++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/OrcaSlicer.cpp b/src/OrcaSlicer.cpp index 8176e9f444..2bd0b22ae4 100644 --- a/src/OrcaSlicer.cpp +++ b/src/OrcaSlicer.cpp @@ -7311,6 +7311,10 @@ bool CLI::setup(int argc, char **argv) m_config.option(optdef.first, true); set_data_dir(m_config.opt_string("datadir")); + if (!data_dir().empty() && !boost::filesystem::exists(data_dir())) { + boost::nowide::cerr << "Could not create data directory: " << data_dir() << std::endl; + return false; + } //FIXME Validating at this stage most likely does not make sense, as the config is not fully initialized yet. if (!validity.empty()) { @@ -7423,6 +7427,10 @@ bool CLI::export_models(IO::ExportFormat format, std::string path_dir) for (ModelObject* model_object : model.objects) { const std::string path = this->output_filepath(*model_object, index++, format, path_dir); + if (path.empty()) { + boost::nowide::cerr << "Could not create output directory for STL export" << std::endl; + return false; + } success = Slic3r::store_stl(path.c_str(), model_object, true); if (success) BOOST_LOG_TRIVIAL(info) << "Model successfully exported to " << path << std::endl; @@ -7548,8 +7556,19 @@ std::string CLI::output_filepath(const ModelObject &object, unsigned int index, output_path = subdir + "/"+file_name; boost::filesystem::path subdir_path(subdir); - if (!boost::filesystem::exists(subdir_path)) - boost::filesystem::create_directory(subdir_path); + if (!boost::filesystem::exists(subdir_path)) { + try { + boost::filesystem::create_directories(subdir_path); + } catch (const boost::filesystem::filesystem_error &ex) { + BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": failed to create output directory " << subdir_path.string() << ": " << ex.what(); + } + if (!boost::filesystem::exists(subdir_path)) { + // Directory creation failed and won't succeed on a retry (same path, same cause) - + // signal failure now instead of letting every object in the model repeat the same + // doomed attempt and fail with a less specific "export failed" error later. + return std::string(); + } + } return output_path; } diff --git a/src/libslic3r/utils.cpp b/src/libslic3r/utils.cpp index 58ec8318a6..5f4baac951 100644 --- a/src/libslic3r/utils.cpp +++ b/src/libslic3r/utils.cpp @@ -310,7 +310,11 @@ void set_data_dir(const std::string &dir) { g_data_dir = dir; if (!g_data_dir.empty() && !boost::filesystem::exists(g_data_dir)) { - boost::filesystem::create_directory(g_data_dir); + try { + boost::filesystem::create_directories(g_data_dir); + } catch (const boost::filesystem::filesystem_error &ex) { + BOOST_LOG_TRIVIAL(error) << "set_data_dir: failed to create data directory " << g_data_dir << ": " << ex.what(); + } } }