diff --git a/common/RemotePath.cpp b/common/RemotePath.cpp index 48a26fc027cbad506bdf082d1911d7c2cdbecc62..38d400c16202e5d9c5cc48451428590b924d6776 100644 --- a/common/RemotePath.cpp +++ b/common/RemotePath.cpp @@ -57,6 +57,7 @@ cta::RemotePath::RemotePath(const std::string &raw): } m_scheme = raw.substr(0, pos); + m_afterScheme = raw.substr(pos + 1, indexOfLastChar - pos); } //------------------------------------------------------------------------------ @@ -73,16 +74,45 @@ bool cta::RemotePath::operator<(const RemotePath &rhs) const { return m_raw < rhs.m_raw; } +//------------------------------------------------------------------------------ +// empty +//------------------------------------------------------------------------------ +bool cta::RemotePath::empty() const { + return m_raw.empty(); +} + //------------------------------------------------------------------------------ // getRaw //------------------------------------------------------------------------------ -const std::string &cta::RemotePath::getRaw() const throw() { +const std::string &cta::RemotePath::getRaw() const { + if(empty()) { + throw exception::Exception(std::string(__FUNCTION__) + + ": Empty remote path"); + } + return m_raw; } //------------------------------------------------------------------------------ // getScheme //------------------------------------------------------------------------------ -const std::string &cta::RemotePath::getScheme() const throw() { +const std::string &cta::RemotePath::getScheme() const { + if(empty()) { + throw exception::Exception(std::string(__FUNCTION__) + + ": Empty remote path"); + } + return m_scheme; } + +//------------------------------------------------------------------------------ +// getAfterScheme +//------------------------------------------------------------------------------ +const std::string &cta::RemotePath::getAfterScheme() const { + if(empty()) { + throw exception::Exception(std::string(__FUNCTION__) + + ": Empty remote path"); + } + + return m_afterScheme; +} diff --git a/common/RemotePath.hpp b/common/RemotePath.hpp index 54f52000efb2c653de0c677f6a0c444ae20d75ea..28e90e12d851ed3a4d1951839f355be24a1c2722 100644 --- a/common/RemotePath.hpp +++ b/common/RemotePath.hpp @@ -37,7 +37,7 @@ public: /** * Constructor. * - * @param raw The raw path in the form "scheme:hierarchical_part". + * @param raw The raw path in the form "scheme:after_scheme". */ RemotePath(const std::string &raw); @@ -52,19 +52,33 @@ public: bool operator<(const RemotePath &rhs) const; /** - * Returns the raw path in the form "scheme:hierarchical_part". + * Returns true if the remote path is empty. */ - const std::string &getRaw() const throw(); + bool empty() const; + + /** + * Returns the raw path in the form "scheme:after_schem". + */ + const std::string &getRaw() const; /** * Returns the scheme part of the remote path. */ - const std::string &getScheme() const throw(); + const std::string &getScheme() const; + + /** + * Returns the part of the remote path after the scheme as in the raw path + * "scheme:after_scheme" would result in "after_scheme" being returned. + * + * @return The part of the remote path after the scheme as in the raw path + * "scheme:after_scheme" would result in "after_scheme" being returned. + */ + const std::string &getAfterScheme() const; private: /** - * The raw path in the form "scheme:hierarchical_part". + * The raw path in the form "scheme:after_scheme". */ std::string m_raw; @@ -73,6 +87,12 @@ private: */ std::string m_scheme; + /** + * The part of the remote path after the scheme. For example the part after + * the scheme for the raw path "scheme:after_scheme" would be "after_scheme". + */ + std::string m_afterScheme; + }; // class RemotePath } // namespace cta diff --git a/common/RemotePathTest.cpp b/common/RemotePathTest.cpp index c562d28492abbb0512210d958d4ee35d92e07c86..0c85097d04583d83c0efc5cd9f21b24b23c31754 100644 --- a/common/RemotePathTest.cpp +++ b/common/RemotePathTest.cpp @@ -35,13 +35,25 @@ protected: } }; +TEST_F(cta_RemotePathTest, default_constructor) { + using namespace cta; + + RemotePath remotePath; + ASSERT_TRUE(remotePath.empty()); + ASSERT_THROW(remotePath.getRaw(), std::exception); + ASSERT_THROW(remotePath.getScheme(), std::exception); + ASSERT_THROW(remotePath.getAfterScheme(), std::exception); +} + TEST_F(cta_RemotePathTest, raw_path_constructor) { using namespace cta; const RemotePath remotePath("xroot://abc.com:1234:the_file"); + ASSERT_FALSE(remotePath.empty()); ASSERT_EQ(std::string("xroot://abc.com:1234:the_file"), remotePath.getRaw()); ASSERT_EQ(std::string("xroot"), remotePath.getScheme()); + ASSERT_EQ(std::string("//abc.com:1234:the_file"), remotePath.getAfterScheme()); } TEST_F(cta_RemotePathTest, raw_path_constructor_empty_string) { diff --git a/remotens/MockRemoteNS.cpp b/remotens/MockRemoteNS.cpp index feafdb2165601329caf7e8ec5a85d88d4caa6388..35e51f9e84cba311ce518fcfd334e0ea901d6119 100644 --- a/remotens/MockRemoteNS.cpp +++ b/remotens/MockRemoteNS.cpp @@ -74,7 +74,8 @@ bool cta::MockRemoteNS::dirExists(const RemotePath &remoteFile) const { //------------------------------------------------------------------------------ // rename //------------------------------------------------------------------------------ -void cta::MockRemoteNS::rename(const RemotePath &remoteFile, const RemotePath &newRemoteFile) { +void cta::MockRemoteNS::rename(const RemotePath &remoteFile, + const RemotePath &newRemoteFile) { auto it = m_entries.find(newRemoteFile); if(m_entries.end() != it) { throw exception::Exception("MockRemoteNS: destination path exists, cannot overwrite it"); @@ -87,13 +88,22 @@ void cta::MockRemoteNS::rename(const RemotePath &remoteFile, const RemotePath &n m_entries.erase(remoteFile); } +//------------------------------------------------------------------------------ +// getFilename +//------------------------------------------------------------------------------ +std::string cta::MockRemoteNS::getFilename(const RemotePath &remoteFile) const { + const std::string afterScheme = remoteFile.getAfterScheme(); + throw exception::Exception(std::string(__FUNCTION__) + " not implemented"); +} + //------------------------------------------------------------------------------ // createEntry //------------------------------------------------------------------------------ -void cta::MockRemoteNS::createEntry(const RemotePath &path, const RemoteFileStatus &status){ +void cta::MockRemoteNS::createEntry(const RemotePath &path, + const RemoteFileStatus &status){ auto it = m_entries.find(path); if(m_entries.end() != it) { throw exception::Exception("MockRemoteNS: path exists, cannot overwrite it"); } m_entries[path] = status; -} \ No newline at end of file +} diff --git a/remotens/MockRemoteNS.hpp b/remotens/MockRemoteNS.hpp index 0d771aa86e30339cb994d970e33ee43494e54d76..8040ce11c5d27157223e78a9c7bdb738c8cd24d4 100644 --- a/remotens/MockRemoteNS.hpp +++ b/remotens/MockRemoteNS.hpp @@ -69,6 +69,14 @@ public: */ void rename(const RemotePath &remoteFile, const RemotePath &newRemoteFile); + + /** + * Parses the specified remote file and returns the file name. + * + * @param path The absolute path of the file. + * @return The file name. + */ + std::string getFilename(const RemotePath &remoteFile) const; /** * Adds a directory or file to the remote NS with the desired status diff --git a/remotens/RemoteNS.hpp b/remotens/RemoteNS.hpp index c4df3d397f1a7363e7d6b40a229501ee873f5594..b61f49e3ec40af49533c5198951b3c9defb1156c 100644 --- a/remotens/RemoteNS.hpp +++ b/remotens/RemoteNS.hpp @@ -69,6 +69,14 @@ public: virtual void rename(const RemotePath &remoteFile, const RemotePath &newRemoteFile) = 0; + /** + * Parses the specified remote file and returns the file name. + * + * @param path The absolute path of the file. + * @return The file name. + */ + virtual std::string getFilename(const RemotePath &remoteFile) const = 0; + }; // class RemoteNS } // namespace cta diff --git a/remotens/RemoteNSDispatcher.cpp b/remotens/RemoteNSDispatcher.cpp index b4fa2722a6de0b0e51b9a502d1182a483529b2a2..0ab855440f79c3209196a69eeaa0c4954fe35ebb 100644 --- a/remotens/RemoteNSDispatcher.cpp +++ b/remotens/RemoteNSDispatcher.cpp @@ -125,3 +125,11 @@ void cta::RemoteNSDispatcher::rename(const RemotePath &remoteFile, const RemotePath &newRemoteFile) { getHandler(remoteFile.getScheme()).rename(remoteFile, newRemoteFile); } + +//------------------------------------------------------------------------------ +// getFilename +//------------------------------------------------------------------------------ +std::string cta::RemoteNSDispatcher::getFilename(const RemotePath &remoteFile) + const { + return getHandler(remoteFile.getScheme()).getFilename(remoteFile); +} diff --git a/remotens/RemoteNSDispatcher.hpp b/remotens/RemoteNSDispatcher.hpp index af5d8b1423af80b90e3a235d1e48a364bcac9d3b..6c39cf80f3cadacbc6251697d2eaaa1749dfd9ec 100644 --- a/remotens/RemoteNSDispatcher.hpp +++ b/remotens/RemoteNSDispatcher.hpp @@ -83,6 +83,14 @@ public: void rename(const RemotePath &remoteFile, const RemotePath &newRemoteFile); + /** + * Parses the specified remote file and returns the file name. + * + * @param path The absolute path of the file. + * @return The file name. + */ + std::string getFilename(const RemotePath &remoteFile) const; + private: /** diff --git a/remotens/RemoteNSDispatcherTest.cpp b/remotens/RemoteNSDispatcherTest.cpp index 46277a05939683e874f213ecd6ba1a446fe40528..d3f3a22796db3923cb98f5ccfe5ce2026ac5ca21 100644 --- a/remotens/RemoteNSDispatcherTest.cpp +++ b/remotens/RemoteNSDispatcherTest.cpp @@ -90,6 +90,11 @@ protected: throw cta::exception::Exception(msg.str()); } + std::string getFilename(const cta::RemotePath &remoteFile) const { + incCallCounter("getFilename"); + return "always_the_same_filename"; + } + uint64_t getCallCounter(const std::string &method) const { auto itor = m_callCounts.find(method); if(m_callCounts.end() == itor) {