]> gitweb.hamatoma.de Git - reqt/commitdiff
ReLocalFileSystem::copy() and move() works
authorhama <hama@siduction.net>
Wed, 25 Nov 2015 22:58:03 +0000 (23:58 +0100)
committerhama <hama@siduction.net>
Wed, 25 Nov 2015 22:58:03 +0000 (23:58 +0100)
base/retrace.hpp
cunit/cuReFileSystem.cpp
os/ReFileSystem.cpp
os/ReFileSystem.hpp

index ea3c7925f95db45423eb5e08c865246df9f8d9f5..387763dc6246dd50d1ccb22262c29ab39a6afc76 100644 (file)
@@ -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<const unsigned char*>(arg);
@@ -35,5 +36,6 @@ static QByteArray hexBytes(const void* arg, int length = 8){
        }
        return rc;
 }
+#endif // WITH_TRACE
 #endif // RETRACE_HPP
 
index cd15611297cceb32bb6ab79c17188a949f9ae95a..41d027d6791ab078164e190c6e6dad33ba0a1b72 100644 (file)
@@ -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() {
index b3bd3c3428c4672b5bb32613414124202ed189f2..1c22b6758444e1015eedcdbd5656c00d81273859 100644 (file)
@@ -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<br>
+ *
+ */
+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 {
index 74f2aa9360ffddb820eb09c926939ebf96399798..e46552e63e232ecc9797fceb075c179e07c26e14 100644 (file)
@@ -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);