From: hama Date: Tue, 24 Nov 2015 23:06:55 +0000 (+0100) Subject: copy()+createFile() works X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=e7850ca70ccde02901081168e7340e3971f0a2b7;p=reqt copy()+createFile() works --- diff --git a/base/ReTest.cpp b/base/ReTest.cpp index 40dd4ec..ab5007c 100644 --- a/base/ReTest.cpp +++ b/base/ReTest.cpp @@ -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 true: the file must exist and be a directory + * @return true: 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. * diff --git a/base/ReTest.hpp b/base/ReTest.hpp index 009ec26..1f6e5d1 100644 --- a/base/ReTest.hpp +++ b/base/ReTest.hpp @@ -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, diff --git a/cunit/allTests.cpp b/cunit/allTests.cpp index e0bdf0c..de40ce2 100644 --- a/cunit/allTests.cpp +++ b/cunit/allTests.cpp @@ -95,12 +95,12 @@ static void testOs() { void testReFileSystem(); void testReCryptFileSystem(); - testReCryptFileSystem(); testReFileSystem(); + testReCryptFileSystem(); } void allTests() { - testExpr(); testOs(); + testExpr(); testBase(); testGui(); if (s_allTest) { diff --git a/cunit/cuReFileSystem.cpp b/cunit/cuReFileSystem.cpp index b1fce07..cd15611 100644 --- a/cunit/cuReFileSystem.cpp +++ b/cunit/cuReFileSystem.cpp @@ -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")); diff --git a/os/ReFileSystem.cpp b/os/ReFileSystem.cpp index 962256f..b3bd3c3 100644 --- a/os/ReFileSystem.cpp +++ b/os/ReFileSystem.cpp @@ -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 true + * @param options defines type of the found file, e.g. LO_FILE * @return true: at least one file found
* false: 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; diff --git a/os/ReFileSystem.hpp b/os/ReFileSystem.hpp index 2f99cb0..74f2aa9 100644 --- a/os/ReFileSystem.hpp +++ b/os/ReFileSystem.hpp @@ -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;