From 94b80585f1c433e38c9d1d8456011b89160c2896 Mon Sep 17 00:00:00 2001 From: Steven Murray <steven.murray@cern.ch> Date: Thu, 1 Sep 2016 11:41:27 +0200 Subject: [PATCH] Moved SmartFd into CTA common --- common/CMakeLists.txt | 2 ++ .../castor/utils => common}/SmartFd.cpp | 19 +++++++++---------- .../castor/utils => common}/SmartFd.hpp | 7 ++----- .../castor/utils => common}/SmartFdTest.cpp | 12 ++++++------ tapeserver/castor/io/IoTest.cpp | 10 +++++----- tapeserver/castor/io/io.cpp | 6 +++--- tapeserver/castor/legacymsg/RmcProxyTcpIp.cpp | 4 ++-- tapeserver/castor/legacymsg/RmcProxyTcpIp.hpp | 4 ++-- .../daemon/ProcessForkerConnectionHandler.cpp | 2 +- .../tapeserver/daemon/ProcessForkerTest.cpp | 18 +++++++++--------- .../tape/tapeserver/daemon/TapeDaemon.cpp | 2 +- tapeserver/castor/utils/CMakeLists.txt | 2 -- 12 files changed, 42 insertions(+), 46 deletions(-) rename {tapeserver/castor/utils => common}/SmartFd.cpp (88%) rename {tapeserver/castor/utils => common}/SmartFd.hpp (98%) rename {tapeserver/castor/utils => common}/SmartFdTest.cpp (86%) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 9b1969c91c..f5a78911d2 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -100,6 +100,7 @@ set (COMMON_LIB_SRC_FILES remoteFS/RemoteFileStatus.cpp remoteFS/RemotePath.cpp remoteFS/RemotePathAndStatus.cpp + SmartFd.cpp threading/ChildProcess.cpp threading/Daemon.cpp threading/Mutex.cpp @@ -136,6 +137,7 @@ set (COMMON_UNIT_TESTS_LIB_SRC_FILES log/SyslogLoggerTest.cpp log/StringLoggerTest.cpp remoteFS/RemotePathTest.cpp + SmartFdTest.cpp threading/DaemonTest.cpp threading/SocketPairTest.cpp utils/UtilsTest.cpp diff --git a/tapeserver/castor/utils/SmartFd.cpp b/common/SmartFd.cpp similarity index 88% rename from tapeserver/castor/utils/SmartFd.cpp rename to common/SmartFd.cpp index f492622ebb..09c1dda747 100644 --- a/tapeserver/castor/utils/SmartFd.cpp +++ b/common/SmartFd.cpp @@ -22,7 +22,7 @@ * @author Castor Dev team, castor-dev@cern.ch *****************************************************************************/ -#include "castor/utils/SmartFd.hpp" +#include "common/SmartFd.hpp" #include <errno.h> #include <unistd.h> @@ -30,21 +30,21 @@ //----------------------------------------------------------------------------- // constructor //----------------------------------------------------------------------------- -castor::utils::SmartFd::SmartFd() throw(): +cta::SmartFd::SmartFd() throw(): m_fd(-1), m_closedCallback(NULL) { } //----------------------------------------------------------------------------- // constructor //----------------------------------------------------------------------------- -castor::utils::SmartFd::SmartFd(const int fd) throw(): +cta::SmartFd::SmartFd(const int fd) throw(): m_fd(fd), m_closedCallback(NULL) { } //----------------------------------------------------------------------------- // setClosedCallback //----------------------------------------------------------------------------- -void castor::utils::SmartFd::setClosedCallback(ClosedCallback closedCallback) +void cta::SmartFd::setClosedCallback(ClosedCallback closedCallback) throw() { m_closedCallback = closedCallback; } @@ -52,7 +52,7 @@ void castor::utils::SmartFd::setClosedCallback(ClosedCallback closedCallback) //----------------------------------------------------------------------------- // reset //----------------------------------------------------------------------------- -void castor::utils::SmartFd::reset(const int fd = -1) throw() { +void cta::SmartFd::reset(const int fd = -1) throw() { // If the new file descriptor is not the one already owned if(fd != m_fd) { @@ -78,8 +78,7 @@ void castor::utils::SmartFd::reset(const int fd = -1) throw() { //----------------------------------------------------------------------------- // SmartFd assignment operator //----------------------------------------------------------------------------- -castor::utils::SmartFd &castor::utils::SmartFd::operator=(SmartFd& obj) - { +cta::SmartFd &cta::SmartFd::operator=(SmartFd& obj) { reset(obj.release()); return *this; } @@ -87,21 +86,21 @@ castor::utils::SmartFd &castor::utils::SmartFd::operator=(SmartFd& obj) //----------------------------------------------------------------------------- // destructor //----------------------------------------------------------------------------- -castor::utils::SmartFd::~SmartFd() { +cta::SmartFd::~SmartFd() { reset(); } //----------------------------------------------------------------------------- // get //----------------------------------------------------------------------------- -int castor::utils::SmartFd::get() const throw() { +int cta::SmartFd::get() const throw() { return m_fd; } //----------------------------------------------------------------------------- // release //----------------------------------------------------------------------------- -int castor::utils::SmartFd::release() { +int cta::SmartFd::release() { // If this SmartFd does not own a file descriptor if(m_fd < 0) { cta::exception::NotAnOwner ex; diff --git a/tapeserver/castor/utils/SmartFd.hpp b/common/SmartFd.hpp similarity index 98% rename from tapeserver/castor/utils/SmartFd.hpp rename to common/SmartFd.hpp index 189cbfabef..e00d1d104b 100644 --- a/tapeserver/castor/utils/SmartFd.hpp +++ b/common/SmartFd.hpp @@ -25,8 +25,7 @@ #include "common/exception/NotAnOwner.hpp" -namespace castor { -namespace utils { +namespace cta { /** * A smart file descriptor that owns a basic file descriptor. When the smart @@ -149,6 +148,4 @@ private: }; // class SmartFd -} // namespace utils -} // namespace castor - +} // namespace cta diff --git a/tapeserver/castor/utils/SmartFdTest.cpp b/common/SmartFdTest.cpp similarity index 86% rename from tapeserver/castor/utils/SmartFdTest.cpp rename to common/SmartFdTest.cpp index 6cd927eee9..4dc3194733 100644 --- a/tapeserver/castor/utils/SmartFdTest.cpp +++ b/common/SmartFdTest.cpp @@ -21,7 +21,7 @@ * @author Castor Dev team, castor-dev@cern.ch *****************************************************************************/ -#include "castor/utils/SmartFd.hpp" +#include "common/SmartFd.hpp" #include <gtest/gtest.h> #include <sys/types.h> @@ -31,7 +31,7 @@ namespace unitTests { -class castor_utils_SmartFdTest : public ::testing::Test { +class cta_SmartFdTest : public ::testing::Test { protected: static int s_fd; static bool s_closedCallbackWasCalled; @@ -50,11 +50,11 @@ protected: } }; -int castor_utils_SmartFdTest::s_fd = -1; -bool castor_utils_SmartFdTest::s_closedCallbackWasCalled = false; +int cta_SmartFdTest::s_fd = -1; +bool cta_SmartFdTest::s_closedCallbackWasCalled = false; -TEST_F(castor_utils_SmartFdTest, testClosedCallback) { - using namespace castor::utils; +TEST_F(cta_SmartFdTest, testClosedCallback) { + using namespace cta; ASSERT_EQ(-1, s_fd); ASSERT_FALSE(s_closedCallbackWasCalled); diff --git a/tapeserver/castor/io/IoTest.cpp b/tapeserver/castor/io/IoTest.cpp index 9a9504f48b..d956b21f92 100644 --- a/tapeserver/castor/io/IoTest.cpp +++ b/tapeserver/castor/io/IoTest.cpp @@ -22,9 +22,9 @@ *****************************************************************************/ #include "castor/io/io.hpp" -#include "castor/utils/SmartFd.hpp" #include "castor/io/marshall.h" #include "common/exception/Errnum.hpp" +#include "common/SmartFd.hpp" #include <fcntl.h> #include <gtest/gtest.h> @@ -58,7 +58,7 @@ protected: unlink(listenSockPath); // Create the socket - castor::utils::SmartFd smartListenSock(socket(PF_LOCAL, SOCK_STREAM, 0)); + cta::SmartFd smartListenSock(socket(PF_LOCAL, SOCK_STREAM, 0)); if(-1 == smartListenSock.get()) { char strErrBuf[256]; if(0 != strerror_r(errno, strErrBuf, sizeof(strErrBuf))) { @@ -112,11 +112,11 @@ protected: }; // class castor_io_IoTest TEST_F(castor_io_IoTest, connectWithTimeout) { - castor::utils::SmartFd smartListenSock; + cta::SmartFd smartListenSock; ASSERT_NO_THROW(smartListenSock.reset( createLocalListenSocket(m_listenSockPath))); - castor::utils::SmartFd smartClientConnectionSock; + cta::SmartFd smartClientConnectionSock; { const int sockDomain = PF_LOCAL; const int sockType = SOCK_STREAM; @@ -138,7 +138,7 @@ TEST_F(castor_io_IoTest, connectWithTimeout) { timeout))); } - castor::utils::SmartFd smartServerConnectionSock; + cta::SmartFd smartServerConnectionSock; { const time_t acceptTimeout = 10; // Timeout is in seconds ASSERT_NO_THROW(smartServerConnectionSock.reset( diff --git a/tapeserver/castor/io/io.cpp b/tapeserver/castor/io/io.cpp index c10b8d5539..93dc94b57e 100644 --- a/tapeserver/castor/io/io.cpp +++ b/tapeserver/castor/io/io.cpp @@ -24,9 +24,9 @@ #include "common/exception/InvalidArgument.hpp" #include "castor/io/io.hpp" -#include "castor/utils/SmartFd.hpp" #include "castor/utils/utils.hpp" #include "common/exception/Exception.hpp" +#include "common/SmartFd.hpp" #include "common/utils/utils.hpp" #include "common/Timer.hpp" #include "common/exception/Errnum.hpp" @@ -129,7 +129,7 @@ int castor::io::createListenerSock( } // Create a socket - utils::SmartFd sock(socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)); + cta::SmartFd sock(socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)); if(sock.get() < 0) { cta::exception::Exception ex; ex.getMessage() << ": Failed to create socket: " @@ -909,7 +909,7 @@ int castor::io::connectWithTimeout( throw(cta::exception::TimeOut, cta::exception::Exception) { // Create the socket for the new connection - utils::SmartFd smartSock(socket(sockDomain, sockType, sockProtocol)); + cta::SmartFd smartSock(socket(sockDomain, sockType, sockProtocol)); if(-1 == smartSock.get()) { cta::exception::Exception ex; ex.getMessage() << diff --git a/tapeserver/castor/legacymsg/RmcProxyTcpIp.cpp b/tapeserver/castor/legacymsg/RmcProxyTcpIp.cpp index efe19ae245..26c68ee2ec 100644 --- a/tapeserver/castor/legacymsg/RmcProxyTcpIp.cpp +++ b/tapeserver/castor/legacymsg/RmcProxyTcpIp.cpp @@ -23,9 +23,9 @@ #include "castor/legacymsg/CommonMarshal.hpp" #include "castor/legacymsg/RmcMarshal.hpp" #include "castor/legacymsg/RmcProxyTcpIp.hpp" -#include "castor/utils/SmartFd.hpp" #include "castor/utils/utils.hpp" #include "Castor_limits.h" +#include "common/SmartFd.hpp" #include "rmc_constants.h" //------------------------------------------------------------------------------ @@ -117,7 +117,7 @@ void castor::legacymsg::RmcProxyTcpIp::forceDismountTape(const std::string &vid, int castor::legacymsg::RmcProxyTcpIp::connectToRmc() const { const std::string rmcHost = "localhost"; - castor::utils::SmartFd smartConnectSock; + cta::SmartFd smartConnectSock; try { smartConnectSock.reset(io::connectWithTimeout(rmcHost, m_rmcPort, m_netTimeout)); diff --git a/tapeserver/castor/legacymsg/RmcProxyTcpIp.hpp b/tapeserver/castor/legacymsg/RmcProxyTcpIp.hpp index 4bac1c34b0..af89a513a2 100644 --- a/tapeserver/castor/legacymsg/RmcProxyTcpIp.hpp +++ b/tapeserver/castor/legacymsg/RmcProxyTcpIp.hpp @@ -26,8 +26,8 @@ #include "castor/legacymsg/RmcMountMsgBody.hpp" #include "castor/legacymsg/RmcProxy.hpp" #include "castor/legacymsg/RmcUnmountMsgBody.hpp" -#include "castor/utils/SmartFd.hpp" #include "castor/utils/utils.hpp" +#include "common/SmartFd.hpp" #include "h/rmc_constants.h" #include <unistd.h> @@ -240,7 +240,7 @@ protected: template<typename T> int rmcSendRecv(const T &rqstBody, std::ostringstream &rmcErrorStream) { // Connect to rmcd and send request - castor::utils::SmartFd fd(connectToRmc()); + cta::SmartFd fd(connectToRmc()); { char buf[RMC_MSGBUFSIZ]; const size_t len = marshal(buf, rqstBody); diff --git a/tapeserver/castor/tape/tapeserver/daemon/ProcessForkerConnectionHandler.cpp b/tapeserver/castor/tape/tapeserver/daemon/ProcessForkerConnectionHandler.cpp index 980a82d921..bbbdedb457 100644 --- a/tapeserver/castor/tape/tapeserver/daemon/ProcessForkerConnectionHandler.cpp +++ b/tapeserver/castor/tape/tapeserver/daemon/ProcessForkerConnectionHandler.cpp @@ -24,7 +24,7 @@ #include "castor/tape/tapeserver/daemon/ProcessForkerConnectionHandler.hpp" #include "castor/tape/tapeserver/daemon/ProcessForkerUtils.hpp" #include "castor/tape/tapeserver/daemon/Session.hpp" -#include "castor/utils/SmartFd.hpp" +#include "common/SmartFd.hpp" //------------------------------------------------------------------------------ // constructor diff --git a/tapeserver/castor/tape/tapeserver/daemon/ProcessForkerTest.cpp b/tapeserver/castor/tape/tapeserver/daemon/ProcessForkerTest.cpp index 651656057d..696cab3050 100644 --- a/tapeserver/castor/tape/tapeserver/daemon/ProcessForkerTest.cpp +++ b/tapeserver/castor/tape/tapeserver/daemon/ProcessForkerTest.cpp @@ -24,7 +24,7 @@ #include "castor/log/DummyLogger.hpp" #include "castor/tape/tapeserver/daemon/ProcessForker.hpp" #include "castor/tape/tapeserver/daemon/ProcessForkerProxySocket.hpp" -#include "castor/utils/SmartFd.hpp" +#include "common/SmartFd.hpp" #include <gtest/gtest.h> #include <memory> @@ -48,13 +48,13 @@ TEST_F(castor_tape_tapeserver_daemon_ProcessForkerTest, constructor) { int cmdPair[2] = {-1, -1}; ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, cmdPair)); - castor::utils::SmartFd cmdSenderSocket(cmdPair[0]); - castor::utils::SmartFd cmdReceiverSocket(cmdPair[1]); + cta::SmartFd cmdSenderSocket(cmdPair[0]); + cta::SmartFd cmdReceiverSocket(cmdPair[1]); int reaperPair[2] = {-1, -1}; ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, reaperPair)); - castor::utils::SmartFd reaperSenderSocket(reaperPair[0]); - castor::utils::SmartFd reaperReceiverSocket(reaperPair[1]); + cta::SmartFd reaperSenderSocket(reaperPair[0]); + cta::SmartFd reaperReceiverSocket(reaperPair[1]); const std::string programName = "unittests"; const std::string hostName = "hostName"; @@ -73,13 +73,13 @@ TEST_F(castor_tape_tapeserver_daemon_ProcessForkerTest, socketproxy) { int cmdPair[2] = {-1, -1}; ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, cmdPair)); - castor::utils::SmartFd cmdSenderSocket(cmdPair[0]); - castor::utils::SmartFd cmdReceiverSocket(cmdPair[1]); + cta::SmartFd cmdSenderSocket(cmdPair[0]); + cta::SmartFd cmdReceiverSocket(cmdPair[1]); int reaperPair[2] = {-1, -1}; ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, reaperPair)); - castor::utils::SmartFd reaperSenderSocket(reaperPair[0]); - castor::utils::SmartFd reaperReceiverSocket(reaperPair[1]); + cta::SmartFd reaperSenderSocket(reaperPair[0]); + cta::SmartFd reaperReceiverSocket(reaperPair[1]); const std::string programName = "unittests"; const std::string hostName = "hostName"; diff --git a/tapeserver/castor/tape/tapeserver/daemon/TapeDaemon.cpp b/tapeserver/castor/tape/tapeserver/daemon/TapeDaemon.cpp index 64014a907f..d49761e8cb 100644 --- a/tapeserver/castor/tape/tapeserver/daemon/TapeDaemon.cpp +++ b/tapeserver/castor/tape/tapeserver/daemon/TapeDaemon.cpp @@ -39,8 +39,8 @@ #include "castor/tape/tapeserver/daemon/TapeMessageHandler.hpp" #include "castor/tape/tapeserver/file/File.hpp" #include "castor/tape/tapeserver/TapeBridgeConstants.hpp" -#include "castor/utils/SmartFd.hpp" #include "castor/utils/utils.hpp" +#include "common/SmartFd.hpp" #include "rmc_constants.h" #include <algorithm> diff --git a/tapeserver/castor/utils/CMakeLists.txt b/tapeserver/castor/utils/CMakeLists.txt index c6ea056822..899fab9a20 100644 --- a/tapeserver/castor/utils/CMakeLists.txt +++ b/tapeserver/castor/utils/CMakeLists.txt @@ -24,7 +24,6 @@ include_directories(${PROJECT_SOURCE_DIR}/tapeserver) set_source_files_properties(CRC.cpp PROPERTIES COMPILE_FLAGS -O2) set (UTILS_LIB_SRC_FILES - SmartFd.cpp SmartFILEPtr.cpp Timer.cpp CRC.cpp @@ -50,7 +49,6 @@ install(TARGETS ctautils DESTINATION usr/${CMAKE_INSTALL_LIBDIR}) add_library (ctautilsunittests SHARED SmartArrayPtrTest.cpp - SmartFdTest.cpp UtilsTest.cpp) install(TARGETS ctautilsunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR}) -- GitLab