Skip to content

Commit

Permalink
Fix for getFileSystemType() on OSX (#9)
Browse files Browse the repository at this point in the history
* Fix for getFileSystemType() on OSX
* Fix for signal handlers on OSX
* Remove unused field
* Fix for signal handling
  • Loading branch information
PascalBoeschoten authored and awegrzyn committed Jan 30, 2018
1 parent a136f7d commit 6225c6c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
1 change: 0 additions & 1 deletion include/Common/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class Thread {

private:
std::chrono::time_point<std::chrono::high_resolution_clock> t0; // time of reset
double tmax; // duration between reset and timeout condition, in seconds
};

} // namespace Common
Expand Down
26 changes: 19 additions & 7 deletions src/System.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,24 @@ void setSigIntHandler(void(*function)(int))
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = function;
sigfillset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
if (sigfillset(&sa.sa_mask) == -1) {
int err = errno;
throw std::runtime_error((b::format("sigfillset returned error (%d)") % err).str());
}
if (sigaction(SIGINT, &sa, NULL) == -1) {
int err = errno;
throw std::runtime_error((b::format("sigaction returned error (%d) while setting sigint handler") % err).str());
}
}

bool isSigIntHandlerSet()
{
struct sigaction sa;
sigaction(SIGINT, NULL, &sa);
return sa.sa_flags != 0;
if (sigaction(SIGINT, NULL, &sa) == -1) {
int err = errno;
throw std::runtime_error((b::format("sigaction returned error (%d) while getting sigint handler") % err).str());
}
return sa.sa_handler != SIG_DFL && sa.sa_handler != SIG_IGN;
}

void makeParentDirectories(const std::string& path)
Expand Down Expand Up @@ -71,13 +80,16 @@ std::string executeCommand(const std::string& command)
std::string getFileSystemType(const std::string& path)
{
std::string type {""};
std::string result = executeCommand(b::str(b::format("df --output=fstype %s") % path.c_str()));
std::string result = executeCommand(b::str(b::format("df %s") % path));

// We need the second like of the output (first line is a header)
// We need the second line of the output (first line is a header)
std::vector<std::string> splitted;
boost::split(splitted, result, boost::is_any_of("\n"));
if (splitted.size() == 3) {
type = splitted.at(1);
// Then get the first "column" of the second line, which contains the file system type
std::string line = splitted.at(1);
boost::split(splitted, line, boost::is_any_of(" "));
type = splitted.at(0);
} else {
BOOST_THROW_EXCEPTION(std::runtime_error("Unrecognized output from 'df' command"));
}
Expand Down
9 changes: 9 additions & 0 deletions test/TestSystem.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,31 @@ BOOST_AUTO_TEST_CASE(TestExecuteCommand)

BOOST_AUTO_TEST_CASE(TestTouchFile)
{
#ifdef __linux__
auto path = "/tmp/touched_file";
boost::filesystem::remove(path);
System::touchFile("/tmp/touched_file");
BOOST_CHECK(boost::filesystem::exists(path));
#endif
}

BOOST_AUTO_TEST_CASE(TestGetFileSystemType)
{
#ifdef __linux__
BOOST_CHECK(System::getFileSystemType("/sys") == "sysfs");
BOOST_CHECK(System::getFileSystemType("/proc") == "proc");
BOOST_CHECK(System::getFileSystemType("/dev") == "devtmpfs");
BOOST_CHECK(System::getFileSystemType("/dev/shm") == "tmpfs");
#endif
#ifdef __APPLE__
BOOST_CHECK(System::getFileSystemType("/dev") == "devfs");
#endif
}

BOOST_AUTO_TEST_CASE(TestIsFileSystemTypeAnyOf)
{
#ifdef __linux__
BOOST_CHECK(System::isFileSystemTypeAnyOf("/sys", {"sysfs", "ext4", "tmpfs"}).first == true);
BOOST_CHECK(System::isFileSystemTypeAnyOf("/sys", {"blahfs", "ext42"}).first == false);
#endif
}

0 comments on commit 6225c6c

Please sign in to comment.