]> gitweb.hamatoma.de Git - reqt/commitdiff
+ synchronize()
authorhama <hama@siduction.net>
Fri, 27 Nov 2015 23:49:55 +0000 (00:49 +0100)
committerhama <hama@siduction.net>
Fri, 27 Nov 2015 23:49:55 +0000 (00:49 +0100)
os/ReCryptFileSystem.cpp
os/ReCryptFileSystem.hpp
os/ReFileSystem.cpp
os/ReFileSystem.hpp

index f41c1d905ad2731374fcad38d7bfee8af80d842f..f70a1e1c12a527202204702120e423cc11a05bc1 100644 (file)
@@ -108,7 +108,8 @@ ReCryptFileSystem::~ReCryptFileSystem() {
  */
 ReLeafFile* ReCryptFileSystem::buildFile(const ReFileMetaData& metadata)
 {
-       return new ReCryptLeafFile(metadata, fullName(metadata.m_node), m_logger2);
+       return new ReCryptLeafFile(metadata, fullName(metadata.m_node),
+               *this, m_logger2);
 }
 
 QString ReCryptFileSystem::canonicalPathOf(const QString& path)
@@ -635,8 +636,6 @@ bool ReCryptDirectory::writeMetaFile()
  *                                     entries are deleted from the buffer. block.length() < MAX_ENTRY_SIZE
  */
 void ReCryptDirectory::splitBlock(bool isLast, QByteArray& block){
-       const FileEntry_t* src = reinterpret_cast<const FileEntry_t*>
-                       (block.constData());
        ReFileMetaData file;
        TRACE("splitBlock:\n");
        IF_TRACE(int ix = 0);
@@ -731,13 +730,17 @@ ReFileSystem::ErrorCode ReCryptDirectory::writeFileBlock(const QString& target,
 /**
  * Constructor.
  *
- * @param meta         the file infos
- * @param directory    the assoziated directory
+ * @param metadata             the file infos
+ * @param fullName             the filename with path
+ * @param directory            the assoziated directory
+ * @param logger               the logger
+ *
  */
-ReCryptFile::ReCryptFile(const ReFileMetaData& meta, ReCryptDirectory& directory) :
-       m_metaData(meta),
+ReCryptLeafFile::ReCryptLeafFile(const ReFileMetaData& metadata,
+               const QString& fullName, ReCryptDirectory& directory, ReLogger* logger) :
+       ReLeafFile(metadata, fullName, logger),
        m_fullHostedName((directory.parentFS()->host().directory()
-                       + directory.parentFS()->buildHostedNode(meta.m_id)).toUtf8()),
+                       + directory.parentFS()->buildHostedNode(metadata.m_id)).toUtf8()),
        m_fileHeader(),
        m_dataSum(0x7b644ac5d1187d25L, 0x6b85115d6064365bL),
        m_sumOfEncrypted(0x7b644ac5d1187d25L, 0x6b85115d6064365bL),
@@ -750,7 +753,7 @@ ReCryptFile::ReCryptFile(const ReFileMetaData& meta, ReCryptDirectory& directory
 /**
  * Destructor.
  */
-ReCryptFile::~ReCryptFile()
+ReCryptLeafFile::~ReCryptLeafFile()
 {
        close();
 }
@@ -762,7 +765,7 @@ ReCryptFile::~ReCryptFile()
  * @return                     EC_SUCCESS: success<br>
  *                                     x
  */
-ReFileSystem::ErrorCode ReCryptFile::open(bool writeable)
+ReFileSystem::ErrorCode ReCryptLeafFile::open(bool writeable)
 {
        ReFileSystem::ErrorCode rc = ReFileSystem::EC_SUCCESS;
        close();
@@ -781,7 +784,7 @@ ReFileSystem::ErrorCode ReCryptFile::open(bool writeable)
  * @param data data to write
  * @return             EC_SUCCESS: success
  */
-ReFileSystem::ErrorCode ReCryptFile::writeBlock(const QByteArray& data){
+ReFileSystem::ErrorCode ReCryptLeafFile::writeBlock(const QByteArray& data){
        ReFileSystem::ErrorCode rc = ReFileSystem::EC_SUCCESS;
        if (m_fp != NULL && fwrite(data.constData(), data.length(), 1, m_fp)
                        != size_t(data.length())){
@@ -800,7 +803,7 @@ ReFileSystem::ErrorCode ReCryptFile::writeBlock(const QByteArray& data){
  * @param data
  * @return
  */
-ReFileSystem::ErrorCode ReCryptFile::write(const QByteArray& data)
+ReFileSystem::ErrorCode ReCryptLeafFile::write(const QByteArray& data)
 {
        ReFileSystem::ErrorCode rc = ReFileSystem::EC_SUCCESS;
        m_dataSum.updateBlock(data);
@@ -830,7 +833,7 @@ ReFileSystem::ErrorCode ReCryptFile::write(const QByteArray& data)
  * @param length       the 64 bit length
  * @return                     the dynamic length
  */
-uint32_t ReCryptFile::dynamicLength(int64_t length) {
+uint32_t ReCryptLeafFile::dynamicLength(int64_t length) {
        while (length > 0xFFFFffffL)
                length >>= 1;
        return uint64_t(length);
@@ -841,14 +844,14 @@ uint32_t ReCryptFile::dynamicLength(int64_t length) {
  *
  * @return EC_SUCCESS: success
  */
-ReFileSystem::ErrorCode ReCryptFile::close()
+ReFileSystem::ErrorCode ReCryptLeafFile::close()
 {
        ReFileSystem::ErrorCode rc = ReFileSystem::EC_SUCCESS;
        if (m_fp != NULL){
                QByteArray& target = m_directory.fileBuffer();
                int blockSize = m_directory.blockSize();
                bool doFlush = m_dataSize > 0 && m_dataSize <= blockSize;
-               m_metaData.m_size = m_dataSize;
+               m_meta.m_size = m_dataSize;
                QByteArray checkSum = m_dataSum.digest();
 
                // Add the data checksum at the end of the data block:
@@ -885,47 +888,16 @@ ReFileSystem::ErrorCode ReCryptFile::close()
 }
 
 /**
- * Constructor.
+ * Reads data from the current position into a buffer.
  *
- * @param metaData     metadata of the file
- * @param fullName     filename with path
- * @param logger       the logger
- */
-ReCryptLeafFile::ReCryptLeafFile(const ReFileMetaData& metaData,
-                                                                const QString& fullName, ReLogger* logger) :
-       ReLeafFile(metaData, fullName, logger)
-{
-
-}
-
-/**
- * Destructor.
+ * @param maxSize      number of bytes to read
+ * @param buffer       OUT: content of the file
+ * @return                     EC_SUCCESS or error code
  */
-ReCryptLeafFile::~ReCryptLeafFile()
-{
-
-}
-
-ReFileSystem::ErrorCode ReCryptLeafFile::open(bool writeable)
-{
-       ReFileSystem::ErrorCode rc = ReFileSystem::EC_SUCCESS;
-       return rc;
-}
-
-ReFileSystem::ErrorCode ReCryptLeafFile::close()
-{
-       ReFileSystem::ErrorCode rc = ReFileSystem::EC_SUCCESS;
-       return rc;
-}
-
 ReFileSystem::ErrorCode ReCryptLeafFile::read(int size, QByteArray& buffer)
 {
        ReFileSystem::ErrorCode rc = ReFileSystem::EC_SUCCESS;
-       return rc;
-}
 
-ReFileSystem::ErrorCode ReCryptLeafFile::write(const QByteArray& buffer)
-{
-       ReFileSystem::ErrorCode rc = ReFileSystem::EC_SUCCESS;
        return rc;
 }
+
index 5ede305a94e2991e79ab5ec5f58bd9d8594de685..6cf9aa24ae9b2c7113d02cf680b5dd55996ca89b 100644 (file)
 #define RECRYPTFILESYSTEM_HPP
 
 class ReCryptFileSystem;
-
 class ReCryptDirectory;
+
 /**
  * Administrates an encrypted file for reading / writing.
+ *
+ * A leaf file could not be a directory.
  */
-class ReCryptFile {
+class ReCryptLeafFile : public ReLeafFile{
 public:
-       ReCryptFile(const ReFileMetaData& metaData, ReCryptDirectory& directory);
-       ~ReCryptFile();
+       ReCryptLeafFile(const ReFileMetaData& metaData, const QString& fullName,
+                                       ReCryptDirectory& directory, ReLogger* logger);
+       virtual ~ReCryptLeafFile();
 public:
-       ReFileSystem::ErrorCode open(bool writeable);
-       ReFileSystem::ErrorCode write(const QByteArray& data);
-       ReFileSystem::ErrorCode close();
+       virtual ReFileSystem::ErrorCode open(bool writeable);
+       virtual ReFileSystem::ErrorCode close();
+       virtual ReFileSystem::ErrorCode read(int size, QByteArray& buffer);
+       virtual ReFileSystem::ErrorCode write(const QByteArray& buffer);
 public:
        static uint32_t dynamicLength(int64_t length);
 protected:
        ReFileSystem::ErrorCode writeBlock(const QByteArray& data);
 private:
-       ReFileMetaData m_metaData;
        QByteArray m_fullHostedName;
        QByteArray m_fileHeader;
        /// checksum of the unencrypted data
@@ -110,23 +113,6 @@ protected:
        int m_maxFileId;
 };
 
-/**
- * An abstract base class for leafs of the tree spanned by a filesystem.
- *
- * A leaf file could not be a directory.
- */
-class ReCryptLeafFile : public ReLeafFile{
-public:
-       ReCryptLeafFile(const ReFileMetaData& metaData, const QString& fullName,
-                                       ReLogger* logger);
-       virtual ~ReCryptLeafFile();
-public:
-       virtual ReFileSystem::ErrorCode open(bool writeable);
-       virtual ReFileSystem::ErrorCode close();
-       virtual ReFileSystem::ErrorCode read(int size, QByteArray& buffer);
-       virtual ReFileSystem::ErrorCode write(const QByteArray& buffer);
-};
-
 /**
  * A filesystem with encrypted filenames and file content.
  *
index 1c22b6758444e1015eedcdbd5656c00d81273859..28bd748d2b775c1e3a23c85da260c9be913eb842 100644 (file)
@@ -341,6 +341,76 @@ void ReFileSystem::setOsPermissions(const ReOSPermissions& osPermissions)
 void ReFileSystem::setWriteable(bool writeable) {
        m_writeable = writeable;
 }
+
+/**
+ * Synchronizes recursivly the instances with another filesystem.
+ *
+ * The newer files in the source directory will be copied and files
+ * which does not exist in the target.
+ *
+ * @param fileMatcher  only files matched by this will be processed
+ * @param dirMatcher   only subdirectories matched by this will be processed
+ * @param verboseLevel defines the logging output (to stdout)
+ * @param source               the source filesystem
+ */
+void ReFileSystem::synchronize(ReIncludeExcludeMatcher& fileMatcher,
+       ReIncludeExcludeMatcher& dirMatcher, VerboseLevel verboseLevel,
+       ReFileSystem& source)
+{
+       ReFileMetaDataList sourceList;
+       ReFileMetaData metaTarget;
+       QByteArray dir;
+       if (verboseLevel > V_SILENT)
+               dir = directory().toUtf8();
+       if (source.listInfos(fileMatcher, sourceList, LO_FILES) > 0){
+               QString sourceDir = source.directory();
+               QString targetDir = directory();
+               ReFileMetaDataList::iterator it;
+               for (it = sourceList.begin(); it != sourceList.end(); ++it){
+                       bool alreadyExists = exists(it->m_node, &metaTarget);
+                       if (! alreadyExists
+                                       // precision of 2 seconds:
+                                       || it->m_modified.currentMSecsSinceEpoch() - 2*1000
+                                       > metaTarget.m_modified.currentMSecsSinceEpoch()){
+                               if (verboseLevel > V_SILENT)
+                                       printf("%c%s%s\n", alreadyExists ? '<' : '+',
+                                                  dir.constData(), it->m_node.toUtf8().constData());
+                               copy(*it, source);
+                       } else if (verboseLevel > V_IMPORTANT){
+                               printf("%c%s%s\n",
+                                          it->m_modified == metaTarget.m_modified ? '=' : '>',
+                                          dir.constData(), it->m_node.toUtf8().constData());
+                       }
+               }
+       }
+       if (source.listInfos(dirMatcher, sourceList, LO_ONLY_DIRS_WITH_NAMEFILTER) > 0){
+               QString sourceDir = source.directory();
+               QString targetDir = directory();
+               ReFileMetaDataList::const_iterator it;
+               for (it = sourceList.cbegin(); it != sourceList.cend(); ++it){
+                       bool alreadyExists = exists(it->m_node, &metaTarget);
+                       if (! alreadyExists && S_ISDIR(metaTarget.m_mode)){
+                               if (verboseLevel > V_SILENT)
+                                       printf("-%s%s\n", dir.constData(),
+                                                  it->m_node.toUtf8().constData());
+                               remove(metaTarget);
+                               alreadyExists = exists(it->m_node, &metaTarget);
+                       }
+                       if (! alreadyExists){
+                               if (verboseLevel > V_SILENT)
+                                       printf("&%s%s\n", dir.constData(),
+                                                  it->m_node.toUtf8().constData());
+                               if (makeDir(it->m_node) != EC_SUCCESS)
+                                       continue;
+                       }
+                       if (source.setDirectory(it->m_node) == EC_SUCCESS
+                                       && setDirectory(it->m_node) == EC_SUCCESS)
+                               synchronize(fileMatcher, dirMatcher, verboseLevel, source);
+                       source.setDirectory(sourceDir);
+                       setDirectory(targetDir);
+               }
+       }
+}
 /**
  * Sets the size of the internal buffer for copy operations.
  *
index e46552e63e232ecc9797fceb075c179e07c26e14..b2675b49ce8004d762b2fc67fc95857e0a98173f 100644 (file)
@@ -61,6 +61,7 @@ public:
                LO_DIRS = 2,
                LO_ALL = 3,
                LO_NAME_FILTER_FOR_DIRS = 4,
+               LO_ONLY_DIRS_WITH_NAMEFILTER = 6,
                LO_ALL_AND_NAME_FILTER_FOR_DIRS = 7,
        };
 
@@ -85,6 +86,12 @@ public:
                EC_INVALID_STATE,
                EC_ALREADY_EXISTS,
        };
+       enum VerboseLevel {
+               V_SILENT,
+               V_IMPORTANT,
+               V_VERBOSE,
+               V_DEBUG
+       };
 
 public:
        ReFileSystem(const QString& name, ReLogger* logger);
@@ -209,6 +216,10 @@ public:
        void setBlocksize(int blocksize);
        void setOsPermissions(const ReOSPermissions& osPermissions);
        void setWriteable(bool writeable);
+       void synchronize(ReIncludeExcludeMatcher& fileMatcher,
+                                        ReIncludeExcludeMatcher& dirMatcher,
+                                        VerboseLevel verboseLevel,
+                                        ReFileSystem& source);
        bool writeable() const;
 public:
        static ReFileSystem* buildFromUrl(const QString& url);