From 6225c6c4b1960df9da0dc164286c15d953c684eb Mon Sep 17 00:00:00 2001 From: PascalBoeschoten Date: Tue, 30 Jan 2018 15:24:13 +0100 Subject: [PATCH] Fix for getFileSystemType() on OSX (#9) * Fix for getFileSystemType() on OSX * Fix for signal handlers on OSX * Remove unused field * Fix for signal handling --- include/Common/Thread.h | 1 - src/System.cxx | 26 +++++++++++++++++++------- test/TestSystem.cxx | 9 +++++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/include/Common/Thread.h b/include/Common/Thread.h index 6a9dc99..5ab8300 100644 --- a/include/Common/Thread.h +++ b/include/Common/Thread.h @@ -60,7 +60,6 @@ class Thread { private: std::chrono::time_point t0; // time of reset - double tmax; // duration between reset and timeout condition, in seconds }; } // namespace Common diff --git a/src/System.cxx b/src/System.cxx index cf2633a..d478496 100644 --- a/src/System.cxx +++ b/src/System.cxx @@ -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) @@ -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 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")); } diff --git a/test/TestSystem.cxx b/test/TestSystem.cxx index 2977259..a4a5d8d 100644 --- a/test/TestSystem.cxx +++ b/test/TestSystem.cxx @@ -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 }