From 5fdb410e9bda666e834a078a5971389c4b6ab3cc Mon Sep 17 00:00:00 2001 From: hama Date: Wed, 25 Nov 2015 23:58:03 +0100 Subject: [PATCH] ReLocalFileSystem::copy() and move() works --- base/retrace.hpp | 2 ++ cunit/cuReFileSystem.cpp | 34 ++++++++++++++++++++++++++++++++++ os/ReFileSystem.cpp | 36 ++++++++++++++++++++++++++++++------ os/ReFileSystem.hpp | 6 ++++-- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/base/retrace.hpp b/base/retrace.hpp index ea3c792..387763d 100644 --- a/base/retrace.hpp +++ b/base/retrace.hpp @@ -24,6 +24,7 @@ #define IF_TRACE(statem) #endif +#ifdef WITH_TRACE static QByteArray hexBytes(const void* arg, int length = 8){ char buffer[16+1]; const unsigned char* src = reinterpret_cast(arg); @@ -35,5 +36,6 @@ static QByteArray hexBytes(const void* arg, int length = 8){ } return rc; } +#endif // WITH_TRACE #endif // RETRACE_HPP diff --git a/cunit/cuReFileSystem.cpp b/cunit/cuReFileSystem.cpp index cd15611..41d027d 100644 --- a/cunit/cuReFileSystem.cpp +++ b/cunit/cuReFileSystem.cpp @@ -216,6 +216,39 @@ protected: checkEqu(123, p2.m_dirMode); checkEqu(7766, p2.m_fileMode); } + void checkMove(const char* node1, const char* node2){ + ReLocalFileSystem fsSource(m_base, &m_logger); + QByteArray base2 = ReFileUtils::tempDir("refilesystem.trg", NULL, + false); + ReFileUtils::deleteTree(base2, false, &m_logger); + ReLocalFileSystem fsTarget(base2, &m_logger); + ReFileMetaData metaSource; + const char* content = "content move1.txt"; + QByteArray fullSrc(fsSource.fullNameAsUTF8(node1)); + ReFileUtils::writeToFile(fullSrc, content); + checkT(fsSource.first(node1, metaSource)); + + QByteArray fullTrg(fsTarget.fullNameAsUTF8(node2 == NULL ? node1 : node2)); + ensureNotExist(fullTrg.constData()); + + checkT(fsSource.exists(node1, &metaSource)); + if (node2 == NULL) + checkEqu(0, fsTarget.move(metaSource, fsSource)); + else + checkEqu(0, fsTarget.move(metaSource, fsSource, node2)); + + checkF(exists(fullSrc)); + checkT(exists(fullTrg)); + checkF(fsSource.exists(node1)); + checkT(fsTarget.exists(node2 == NULL ? node1 : node2)); + QByteArray buffer; + checkEqu(content, ReFileUtils::readFromFile(fullTrg, buffer)); + } + + void testMove(){ + checkMove("move1.txt", "move2.txt"); + checkMove("move1.txt", NULL); + } virtual void run() { testReOSPermissions(); @@ -225,6 +258,7 @@ protected: testSetPropertiesOwner(); testCopy(); testReadWrite(); + testMove(); } }; void testReFileSystem() { diff --git a/os/ReFileSystem.cpp b/os/ReFileSystem.cpp index b3bd3c3..1c22b67 100644 --- a/os/ReFileSystem.cpp +++ b/os/ReFileSystem.cpp @@ -98,15 +98,17 @@ ReFileSystem* ReFileSystem::buildFromUrl(const QString& url) * */ ReFileSystem::ErrorCode ReFileSystem::copy(ReFileMetaData& source, - ReFileSystem& sourceFS) { + ReFileSystem& sourceFS, QString targetNode) { int blocksize = min(m_blocksize, sourceFS.blocksize()); ErrorCode rc = EC_SUCCESS; ErrorCode rc2; int64_t size = 0; + if (targetNode.isEmpty()) + targetNode = source.m_node; ReLeafFile* sourceFile = sourceFS.buildFile(source); ReFileMetaData targetMeta; - if (! exists(source.m_node, &targetMeta)){ - rc = createFile(source.m_node, false, &targetMeta); + if (! exists(targetNode, &targetMeta)){ + rc = createFile(targetNode, false, &targetMeta); } if (rc == EC_SUCCESS){ ReLeafFile* targetFile = buildFile(targetMeta); @@ -123,9 +125,12 @@ ReFileSystem::ErrorCode ReFileSystem::copy(ReFileMetaData& source, } sourceFile->close(); targetFile->close(); - ReFileMetaData target(source.m_node, ReFileUtils::m_undefinedTime, + ReFileMetaData target(targetNode, ReFileUtils::m_undefinedTime, ReFileUtils::m_undefinedTime, m_uid, m_gid); + QString node = source.m_node; + source.m_node = targetNode; setProperties(source, target, false); + source.m_node = node; delete sourceFile; delete targetFile; } @@ -249,6 +254,25 @@ bool ReFileSystem::first(const QString& pattern, ReFileMetaData& file, return rc; } +/** + * Move a file from a source filesystem to the current directory of the instance. + * + * @param source meta data of the source file (in current directory) + * @param sourceFS the filesystem containing the source + * @return EC_SUCCESS: success
+ * + */ +ReFileSystem::ErrorCode ReFileSystem::move(ReFileMetaData& source, + ReFileSystem& sourceFS, QString targetNode) { + if (targetNode.isEmpty()) + targetNode = source.m_node; + ErrorCode rc = copy(source, sourceFS, targetNode); + if (rc == EC_SUCCESS){ + rc = sourceFS.remove(source); + } + return rc; +} + /** * Finds all nodes with a given prefix. * @@ -561,10 +585,10 @@ QString ReLocalFileSystem::canonicalPathOf(const QString& path) ReFileSystem::ErrorCode ReLocalFileSystem::remove(const ReFileMetaData& node) { ErrorCode rc = EC_SUCCESS; if (!m_writeable) { - m_logger->log(LOG_ERROR, LOC_REMOVE_1, "filesystem is readonly"); + m_logger->log(LOG_ERROR, LOC_REMOVE_1, "remove(): filesystem is readonly"); rc = EC_FS_READ_ONLY; } else if (!m_dir.exists(node.m_node)) { - m_logger->logv(LOG_ERROR, LOC_REMOVE_2, "node does not exists: %s", + m_logger->logv(LOG_ERROR, LOC_REMOVE_2, "remove(): node does not exists: %s", fullNameAsUTF8(node.m_node).constData()); rc = EC_NOT_EXISTS; } else { diff --git a/os/ReFileSystem.hpp b/os/ReFileSystem.hpp index 74f2aa9..e46552e 100644 --- a/os/ReFileSystem.hpp +++ b/os/ReFileSystem.hpp @@ -177,9 +177,11 @@ public: ReFileMetaData meta; return exists(node, &meta) ? buildFile(meta) : NULL; } - - virtual ErrorCode copy(ReFileMetaData& source, ReFileSystem& sourceFS); virtual QString errorMessage(ErrorCode rc); + virtual ErrorCode copy(ReFileMetaData& source, ReFileSystem& sourceFS, + QString targetNode = ReQStringUtils::m_empty); + virtual ErrorCode move(ReFileMetaData& source, ReFileSystem& sourceFS, + QString targetNode = ReQStringUtils::m_empty); public: int blocksize() const; bool findByUrl(const QString& url, ReFileMetaData& metaData); -- 2.39.5