Skip to content

Commit ab33266

Browse files
authored
Merge pull request #2381 from SCIInstitute/2379-image-crash
Fix 2379. It appears that stat on windows is not thread-safe.
2 parents 7496946 + 557b830 commit ab33266

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

Libs/Common/ShapeworksUtils.cpp

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,31 @@
55
#include <tbb/global_control.h>
66
#include <tbb/info.h>
77

8-
namespace shapeworks {
9-
10-
// Windows doesn't have S_ISDIR and S_ISREG macros
11-
#if !defined(S_ISREG) && defined(S_IFMT) && defined(S_IFREG)
12-
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
13-
#endif
8+
#include <boost/filesystem.hpp>
149

15-
#if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
16-
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
17-
#endif
10+
namespace shapeworks {
1811

1912
unsigned ShapeWorksUtils::rng_seed_ = std::chrono::system_clock::now().time_since_epoch().count();
2013
std::mt19937 ShapeWorksUtils::mt_;
2114
std::unique_ptr<tbb::global_control> ShapeWorksUtils::tbb_global_control_;
2215

23-
//-----------------------------------------------------------------------------
24-
/// looks at the pathname to see if it's a file or a directory or neither
25-
static bool stat_path(const std::string& pathname, bool isdir = false) {
26-
struct stat info;
27-
if (stat(pathname.c_str(), &info) != 0) {
28-
return false;
29-
} else {
30-
return isdir ? S_ISDIR(info.st_mode) : S_ISREG(info.st_mode);
31-
}
32-
}
33-
3416
//-----------------------------------------------------------------------------
3517
void ShapeWorksUtils::set_rng_seed(const unsigned seed) {
3618
rng_seed_ = seed;
3719
mt_.seed(rng_seed_);
3820
}
3921

4022
//-----------------------------------------------------------------------------
41-
bool ShapeWorksUtils::is_directory(const std::string& pathname) { return stat_path(pathname, true); }
23+
bool ShapeWorksUtils::is_directory(const std::string& pathname) {
24+
boost::system::error_code ec;
25+
return boost::filesystem::is_directory(pathname, ec);
26+
}
4227

4328
//-----------------------------------------------------------------------------
44-
bool ShapeWorksUtils::file_exists(const std::string& filename) { return stat_path(filename, false); }
29+
bool ShapeWorksUtils::file_exists(const std::string& filename) {
30+
boost::system::error_code ec;
31+
return boost::filesystem::is_regular_file(filename, ec);
32+
}
4533

4634
//-----------------------------------------------------------------------------
4735
void ShapeWorksUtils::setup_console_logging(bool show_progress, bool xml_status) {

Libs/Image/Image.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ Image::ImageType::Pointer Image::read(const std::string& pathname) {
107107
throw std::invalid_argument("Empty pathname");
108108
}
109109

110-
if (ShapeWorksUtils::is_directory(pathname)) return readDICOMImage(pathname);
110+
if (ShapeWorksUtils::is_directory(pathname)) {
111+
return readDICOMImage(pathname);
112+
}
111113

112114
// check if it exists
113115
if (!boost::filesystem::exists(pathname)) {

0 commit comments

Comments
 (0)