]> gitweb.hamatoma.de Git - reqt/commitdiff
copy()+createFile() works
authorhama <hama@siduction.net>
Tue, 24 Nov 2015 23:06:55 +0000 (00:06 +0100)
committerhama <hama@siduction.net>
Tue, 24 Nov 2015 23:06:55 +0000 (00:06 +0100)
base/ReTest.cpp
base/ReTest.hpp
cunit/allTests.cpp
cunit/cuReFileSystem.cpp
os/ReFileSystem.cpp
os/ReFileSystem.hpp

index 40dd4ecc7f84797bf402641b77216cea783c4b59..ab5007c5cfd0c004901f1bb9b1d171d068fa559a 100644 (file)
@@ -414,6 +414,21 @@ bool ReTest::assertEqualFiles(const char* expected, const char* current,
        return rc;
 }
 
+/**
+  * Ensures that the file (or the directory) does not exist.
+  *
+ * @param fullname     filename with path
+ */
+void ReTest::ensureNotExist(const char* fullname)
+{
+       if (exists(fullname))
+               unlink(fullname);
+       if (exists(fullname) && exists(fullname, true))
+               rmdir(fullname);
+       if (exists(fullname))
+               error("cannot delete (%d): %s", errno, fullname);
+}
+
 /**
  * @brief Writes an error.
  *
@@ -430,6 +445,21 @@ bool ReTest::error(const char* format, ...) {
        return false;
 }
 
+/**
+ * Tests whether a file exists.
+ * @param fullname     the filename with path
+ * @param isDir                <code>true</code>: the file must exist and be a directory
+ * @return                     <code>true</code>: the file exists.
+ */
+bool ReTest::exists(const char* fullname, bool isDir)
+{
+       struct stat info;
+       bool rc = stat(fullname, &info) == 0;
+       if (! rc && isDir && ! S_ISDIR(info.st_mode))
+               rc = false;
+       return rc;
+}
+
 /**
  * @brief Writes an info.
  *
index 009ec26310f1bfebe0ccc2cbf190d1e8452f9025..1f6e5d13ccde876f677cfe6c0b5cb7b847466f36 100644 (file)
@@ -53,7 +53,9 @@ public:
        bool assertNotNull(const void* ptr, const char* file, int lineNo);
        bool assertEqualFiles(const char* expected, const char* current,
                const char* file, int lineNo);
+       void ensureNotExist(const char* fullname);
        bool error(const char* message, ...);
+       bool exists(const char* fullname, bool isDir = false);
        bool log(const char* message);
        bool logv(const char* format...);
        QByteArray getTempDir(const char* node, const char* parent = NULL,
index e0bdf0c4eb1313499d0ebde98baa008838bde5c4..de40ce22d7f0c4f8751bc421f4c741fa4a3b2afe 100644 (file)
@@ -95,12 +95,12 @@ static void testOs() {
        void testReFileSystem();
        void testReCryptFileSystem();
 
-       testReCryptFileSystem();
        testReFileSystem();
+       testReCryptFileSystem();
 }
 void allTests() {
-       testExpr();
        testOs();
+       testExpr();
        testBase();
        testGui();
        if (s_allTest) {
index b1fce075992033d82094e8a560ec53b476cd27de..cd15611297cceb32bb6ab79c17188a949f9ae95a 100644 (file)
@@ -66,55 +66,64 @@ protected:
        void testReadWrite() {
                ReLocalFileSystem fs(m_base, &m_logger);
                QByteArray buffer;
+               const char* node1 = "abc.txt";
                buffer.append("abcdefghijklmnopqrstuvwxyz");
+               QByteArray full(fs.fullNameAsUTF8(QString(node1)));
 
-               //checkEqu(0, fs.write("abc.txt", 0LL, buffer));
-               //checkEqu(0, fs.write("abc.txt", 26LL, buffer));
+               ReFileUtils::writeToFile(full.constData(), buffer.constData());
                QByteArray buffer2;
                ReFileMetaDataList nodes;
                QStringList names;
-               names.append("abc.txt");
-               names.append("new.txt");
+               names.append(node1);
+               names.append("not_exists.txt");
                ReIncludeExcludeMatcher matcher(names, ReQStringUtils::m_emptyList,
                        Qt::CaseInsensitive, true);
-               checkEqu(1, fs.listInfos(matcher, nodes, ReFileSystem::LO_UNDEF));
+               checkEqu(1, fs.listInfos(matcher, nodes, ReFileSystem::LO_FILES));
                checkEqu(1, nodes.size());
                QByteArray content("This is a content\nLine 2");
-               checkEqu(0, fs.createFile("new.txt", false));
+               const char* node2 = "new.txt";
+               ensureNotExist(fs.fullNameAsUTF8(node2));
+               checkEqu(0, fs.createFile(node2, false));
                ReFileMetaData meta;
-               checkT(fs.exists("new.txt", &meta));
+               checkT(fs.exists(node1, &meta));
                ReLeafFile* leaf1 = fs.buildFile(meta);
-               leaf1->open(true);
-               leaf1->write(content);
-               leaf1->close();
+               checkEqu(0, leaf1->open(true));
+               checkEqu(0, leaf1->write(content));
+               checkEqu(0, leaf1->close());
                delete leaf1;
-               checkT(fs.exists("new.txt", &meta));
+               checkT(fs.exists(node1, &meta));
                ReLeafFile* leaf2 = fs.buildFile(meta);
-               leaf2->open(false);
-               leaf2->read(500, buffer);
-               leaf2->close();
-               delete leaf2;
+               checkEqu(0, leaf2->open(false));
+               checkEqu(0, leaf2->read(500, buffer));
+               checkEqu(0, leaf2->close());
+               checkEqu(content.constData(), buffer);
 
-               //checkEqu(0, fs.read(nodes.at(0), 0LL, 3, buffer2));
-               checkEqu("abc", buffer2);
-               //checkEqu(0, fs.read(nodes.at(0), 3LL, 7, buffer2));
-               checkEqu("defghij", buffer2);
-               //checkEqu(0, fs.read(nodes.at(0), 10LL, 99, buffer2));
-               checkEqu("klmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", buffer2);
+               checkEqu(0, leaf2->open(false));
+               checkEqu(0, leaf2->read(3, buffer));
+               checkEqu(content.mid(0, 3).constData(), buffer);
+               checkEqu(0, leaf2->read(8, buffer));
+               checkEqu(content.mid(3, 8).constData(), buffer);
+               checkEqu(0, leaf2->close());
+               delete leaf2;
        }
        void testSetProperties() {
                ReLocalFileSystem fs(m_base, &m_logger);
                ReFileMetaData meta1;
-               const char* trgNode = "later.txt";
-               ReFileUtils::tempFile(trgNode, "refilesystem", true);
+               const char* node1 = "later.txt";
+               const char* node2 = "properties.txt";
+               QByteArray full(fs.fullNameAsUTF8(node1));
+               ensureNotExist(full);
+               full = fs.fullNameAsUTF8(node2);
+               ReFileUtils::writeToFile(full, node2);
+               ReFileUtils::tempFile(node1, "refilesystem", true);
                QDateTime modified = QDateTime::fromString("2015.09.12 11:44:55.765",
                        "yyyy.MM.dd hh:mm:ss.zzz");
-               ReFileMetaData meta2(trgNode, modified, ReFileUtils::m_undefinedTime,
+               ReFileMetaData meta2(node1, modified, ReFileUtils::m_undefinedTime,
                        -1, -1, (mode_t) - 1, 1);
-               checkT(fs.first("test1.txt", meta1));
+               checkT(fs.first(node2, meta1, ReFileSystem::LO_FILES));
                checkEqu(0, fs.setProperties(meta2, meta1, true));
                ReFileMetaData meta3;
-               checkT(fs.first(trgNode, meta3));
+               checkT(fs.first(node1, meta3));
                checkEqu(meta3.m_modified, modified);
        }
        void testSetPropertiesOwner() {
@@ -157,10 +166,16 @@ protected:
                ReFileUtils::deleteTree(base2, false, &m_logger);
                ReLocalFileSystem fsTarget(base2, &m_logger);
                ReFileMetaData metaSource;
-               checkT(fsSource.first("test3.txt", metaSource));
+               const char* node = "test3.txt";
+               checkT(fsSource.first(node, metaSource));
+
+               QByteArray full(fsTarget.fullNameAsUTF8(node));
+               ensureNotExist(full.constData());
+
                checkEqu(0, fsTarget.copy(metaSource, fsSource));
+
                ReFileMetaData metaTarget;
-               checkT(fsTarget.first("test3.txt", metaTarget));
+               checkT(fsTarget.first(node, metaTarget));
                compareMeta(metaSource, metaTarget);
                checkEqu(0, fsTarget.makeDir("dir.01"));
                checkEqu(0, fsTarget.setDirectory("dir.01"));
index 962256f4d834b56b485afd721bd0b51e64b50755..b3bd3c3428c4672b5bb32613414124202ed189f2 100644 (file)
@@ -231,16 +231,18 @@ bool ReFileSystem::findByUrl(const QString& url, ReFileMetaData& metaData) {
  *
  * @param pattern      pattern to find
  * @param file         OUT: the found file (valid only if return code is <code>true</code>
+ * @param options      defines type of the found file, e.g. LO_FILE
  * @return                     <code>true</code>: at least one file found<br>
  *                                     <code>false</code>: no file found
  */
-bool ReFileSystem::first(const QString& pattern, ReFileMetaData& file) {
+bool ReFileSystem::first(const QString& pattern, ReFileMetaData& file,
+                                                ListOptions options) {
        ReFileMetaDataList list;
        QStringList names;
        names.append(pattern);
        ReIncludeExcludeMatcher matcher(names, ReQStringUtils::m_emptyList,
                Qt::CaseInsensitive, true);
-       listInfos(matcher, list);
+       listInfos(matcher, list, options);
        bool rc = list.size() > 0;
        if (rc)
                file = list.at(0);
@@ -376,12 +378,9 @@ const QString& ReLocalFileSystem::basePath() const {
  */
 bool ReLocalFileSystem::exists(const QString& node, ReFileMetaData* metaData) const
 {
-       QByteArray full;
-       full.resize(m_directory.length() + 10 + node.length());
-       full = m_directory.toUtf8();
-       full.append(node.toUtf8());
+       QByteArray full(fullNameAsUTF8(node));
        struct stat info;
-       bool rc = stat(full.constData(), &info) != 0;
+       bool rc = stat(full.constData(), &info) == 0;
        if (rc && metaData != NULL){
                metaData->m_node = node;
                metaData->m_modified = QDateTime::fromTime_t(info.st_mtime);
@@ -417,7 +416,6 @@ int ReLocalFileSystem::listInfos(const ReIncludeExcludeMatcher& matcher,
                        ? m_dir.entryList() : m_dir.entryList(patterns);
        QStringList::const_iterator it;
        QByteArray full = m_directory.toUtf8();
-       full.append(OS_SEPARATOR);
        int pathLength = full.length();
        struct stat info;
        const ReListMatcher& excludeMatcher = matcher.excludes();
@@ -486,12 +484,13 @@ ReFileSystem::ErrorCode ReLocalFileSystem::setDirectory(const QString& path) {
        if (ReFileUtils::isAbsolutPath(path)){
                m_dir.setPath(path);
                rc = m_dir.exists() ? EC_SUCCESS : EC_PATH_NOT_FOUND;
-       } else
-               rc = m_dir.setCurrent(ReFileUtils::pathAppend(
-                       m_dir.absolutePath(), path))
-                               ? EC_SUCCESS : EC_PATH_NOT_FOUND;
+       } else {
+               QString dir = ReFileUtils::pathAppend(m_directory, path);
+               m_dir.setPath(dir);
+               rc = m_dir.exists() ? EC_SUCCESS : EC_PATH_NOT_FOUND;
+       }
        if (rc == EC_SUCCESS){
-               m_directory = m_dir.absolutePath();
+               m_directory = m_dir.path();
                ReQStringUtils::ensureLastChar(m_directory, OS_SEPARATOR);
        }
        return rc;
index 2f99cb0c09ffaadb0ea078f6c4917ff403999dc9..74f2aa9360ffddb820eb09c926939ebf96399798 100644 (file)
@@ -61,6 +61,7 @@ public:
                LO_DIRS = 2,
                LO_ALL = 3,
                LO_NAME_FILTER_FOR_DIRS = 4,
+               LO_ALL_AND_NAME_FILTER_FOR_DIRS = 7,
        };
 
        enum ErrorCode {
@@ -182,7 +183,8 @@ public:
 public:
        int blocksize() const;
        bool findByUrl(const QString& url, ReFileMetaData& metaData);
-       bool first(const QString& pattern, ReFileMetaData& file);
+       bool first(const QString& pattern, ReFileMetaData& file,
+                          ListOptions options = LO_ALL_AND_NAME_FILTER_FOR_DIRS);
        /** Returns the full name (with path).
         * @param node  the name without path
         * @return              the full filename
@@ -195,7 +197,9 @@ public:
         * @return              the full filename
         */
        QByteArray fullNameAsUTF8(const QString& node) const {
-               return (m_directory + node).toUtf8();
+               QString full;
+               full.reserve(m_directory.length() + node.length() + 32);
+               return full.append(m_directory).append(node).toUtf8();
        }
        int nodesByPrefix(QString& prefix, ListOptions options, QStringList& list);
        ReOSPermissions osPermissions() const;