From 557b830e900dc26735490a393e27d4906ee86708 Mon Sep 17 00:00:00 2001 From: Alan Morris Date: Wed, 28 May 2025 10:54:46 -0600 Subject: [PATCH] Fix 2379. It appears that stat on windows is not thread-safe. --- Libs/Common/ShapeworksUtils.cpp | 32 ++++++++++---------------------- Libs/Image/Image.cpp | 4 +++- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/Libs/Common/ShapeworksUtils.cpp b/Libs/Common/ShapeworksUtils.cpp index fde56ae216..7cc3dafc04 100644 --- a/Libs/Common/ShapeworksUtils.cpp +++ b/Libs/Common/ShapeworksUtils.cpp @@ -5,32 +5,14 @@ #include #include -namespace shapeworks { - -// Windows doesn't have S_ISDIR and S_ISREG macros -#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG) -#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) -#endif +#include -#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR) -#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) -#endif +namespace shapeworks { unsigned ShapeWorksUtils::rng_seed_ = std::chrono::system_clock::now().time_since_epoch().count(); std::mt19937 ShapeWorksUtils::mt_; std::unique_ptr ShapeWorksUtils::tbb_global_control_; -//----------------------------------------------------------------------------- -/// looks at the pathname to see if it's a file or a directory or neither -static bool stat_path(const std::string& pathname, bool isdir = false) { - struct stat info; - if (stat(pathname.c_str(), &info) != 0) { - return false; - } else { - return isdir ? S_ISDIR(info.st_mode) : S_ISREG(info.st_mode); - } -} - //----------------------------------------------------------------------------- void ShapeWorksUtils::set_rng_seed(const unsigned seed) { rng_seed_ = seed; @@ -38,10 +20,16 @@ void ShapeWorksUtils::set_rng_seed(const unsigned seed) { } //----------------------------------------------------------------------------- -bool ShapeWorksUtils::is_directory(const std::string& pathname) { return stat_path(pathname, true); } +bool ShapeWorksUtils::is_directory(const std::string& pathname) { + boost::system::error_code ec; + return boost::filesystem::is_directory(pathname, ec); +} //----------------------------------------------------------------------------- -bool ShapeWorksUtils::file_exists(const std::string& filename) { return stat_path(filename, false); } +bool ShapeWorksUtils::file_exists(const std::string& filename) { + boost::system::error_code ec; + return boost::filesystem::is_regular_file(filename, ec); +} //----------------------------------------------------------------------------- void ShapeWorksUtils::setup_console_logging(bool show_progress, bool xml_status) { diff --git a/Libs/Image/Image.cpp b/Libs/Image/Image.cpp index 5d54699acf..6b2dca2906 100644 --- a/Libs/Image/Image.cpp +++ b/Libs/Image/Image.cpp @@ -107,7 +107,9 @@ Image::ImageType::Pointer Image::read(const std::string& pathname) { throw std::invalid_argument("Empty pathname"); } - if (ShapeWorksUtils::is_directory(pathname)) return readDICOMImage(pathname); + if (ShapeWorksUtils::is_directory(pathname)) { + return readDICOMImage(pathname); + } // check if it exists if (!boost::filesystem::exists(pathname)) {