Port the invalid napme regexp
authorOlivier Goffart <ogoffart@woboq.com>
Tue, 10 Jul 2018 14:21:45 +0000 (16:21 +0200)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:57:57 +0000 (10:57 +0100)
TestSyncEngine::testInvalidFilenameRegex

src/libsync/discovery.cpp
src/libsync/discoveryphase.h
src/libsync/syncengine.cpp

index 3b8d27aa595605978f503f0efb496458edaf0b62..03d97dbb5449399a11cb849c7da729349d8a7332 100644 (file)
@@ -171,6 +171,17 @@ bool ProcessDirectoryJob::handleExcluded(const QString &path, bool isDirectory)
 {
     // FIXME! call directly, without char* conversion
     auto excluded = _discoveryData->_excludes->csyncTraversalMatchFun()(path.toUtf8(), isDirectory ? ItemTypeDirectory : ItemTypeFile);
+
+    // FIXME: move to ExcludedFiles 's regexp ?
+    bool isInvalidPattern = false;
+    if (excluded == CSYNC_NOT_EXCLUDED && !_discoveryData->_invalidFilenamePattern.isEmpty()) {
+        const QRegExp invalidFilenameRx(_discoveryData->_invalidFilenamePattern);
+        if (path.contains(invalidFilenameRx)) {
+            excluded = CSYNC_FILE_EXCLUDE_INVALID_CHAR;
+            isInvalidPattern = true;
+        }
+    }
+
     if (excluded == CSYNC_NOT_EXCLUDED /* FIXME && item->_type != ItemTypeSoftLink */) {
         return false;
     } else if (excluded == CSYNC_FILE_SILENTLY_EXCLUDED || excluded == CSYNC_FILE_EXCLUDE_AND_REMOVE) {
@@ -208,6 +219,9 @@ bool ProcessDirectoryJob::handleExcluded(const QString &path, bool isDirectory)
             if (invalid) {
                 item->_errorString = tr("File names containing the character '%1' are not supported on this file system.")
                                          .arg(QLatin1Char(invalid));
+            }
+            if (isInvalidPattern) {
+                item->_errorString = tr("File name contains at least one invalid character");
             } else {
                 item->_errorString = tr("The file name is a reserved name on this file system.");
             }
index a0b387104fd1fc0a4d62fe8388794854029342f6..35973378f02b8885ca433b8a6d4fe9b178ed537c 100644 (file)
@@ -109,6 +109,7 @@ public:
     QStringList _selectiveSyncBlackList;
     QStringList _selectiveSyncWhiteList;
     ExcludedFiles *_excludes;
+    QString _invalidFilenamePattern; // FIXME: maybe move in ExcludedFiles
 
     bool isInSelectiveSyncBlackList(const QString &path) const;
     bool checkSelectiveSyncNewFolder(const QString &path, RemotePermissions rp);
index 07fbb652ec5c909172b430d34fdd5682b2b8f6be..e564f769ea63ffe5af893727e38c472e2b20f340 100644 (file)
@@ -858,11 +858,11 @@ void SyncEngine::slotStartDiscovery()
     ddata->_account = _account;
     ddata->_excludes = _excludedFiles.data();
     ddata->_statedb = _journal;
-    ddata->_selectiveSyncBlackList = selectiveSyncBlackList;
-    ddata->_selectiveSyncWhiteList = _journal->getSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, &ok);
     ddata->_localDir = _localPath;
     ddata->_remoteFolder = _remotePath;
     ddata->_syncOptions = _syncOptions;
+    ddata->_selectiveSyncBlackList = selectiveSyncBlackList;
+    ddata->_selectiveSyncWhiteList = _journal->getSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, &ok);
     if (!ok) {
         qCWarning(lcEngine) << "Unable to read selective sync list, aborting.";
         csyncError(tr("Unable to read from the sync journal."));
@@ -870,6 +870,19 @@ void SyncEngine::slotStartDiscovery()
         return;
     }
 
+    // Check for invalid character in old server version
+    QString invalidFilenamePattern = _account->capabilities().invalidFilenameRegex();
+    if (invalidFilenamePattern.isNull()
+        && _account->serverVersionInt() < Account::makeServerVersion(8, 1, 0)) {
+        // Server versions older than 8.1 don't support some characters in filenames.
+        // If the capability is not set, default to a pattern that avoids uploading
+        // files with names that contain these.
+        // It's important to respect the capability also for older servers -- the
+        // version check doesn't make sense for custom servers.
+        invalidFilenamePattern = "[\\\\:?*\"<>|]";
+    }
+    ddata->_invalidFilenamePattern = invalidFilenamePattern;
+
     connect(ddata.data(), &DiscoveryPhase::folderDiscovered, this, &SyncEngine::slotFolderDiscovered);
     connect(ddata.data(), &DiscoveryPhase::newBigFolder, this, &SyncEngine::newBigFolder);
 
@@ -1037,29 +1050,6 @@ void SyncEngine::slotDiscoveryJobFinished(int /*discoveryResult*/)
         syncItem->_file = adjustRenamedPath(syncItem->_file);
     }
 
-    // Check for invalid character in old server version
-    QString invalidFilenamePattern = _account->capabilities().invalidFilenameRegex();
-    if (invalidFilenamePattern.isNull()
-        && _account->serverVersionInt() < Account::makeServerVersion(8, 1, 0)) {
-        // Server versions older than 8.1 don't support some characters in filenames.
-        // If the capability is not set, default to a pattern that avoids uploading
-        // files with names that contain these.
-        // It's important to respect the capability also for older servers -- the
-        // version check doesn't make sense for custom servers.
-        invalidFilenamePattern = R"([\:?*"<>|])";
-    }
-    if (!invalidFilenamePattern.isEmpty()) {
-        const QRegExp invalidFilenameRx(invalidFilenamePattern);
-        for (const auto &syncItem : qAsConst(syncItems)) {
-            if (syncItem->_direction == SyncFileItem::Up
-                && isFileModifyingInstruction(syncItem->_instruction)
-                && syncItem->destination().contains(invalidFilenameRx)) {
-                syncItem->_errorString = tr("File name contains at least one invalid character");
-                syncItem->_instruction = CSYNC_INSTRUCTION_IGNORE;
-            }
-        }
-    }
-
     ConfigFile cfgFile;
     if (!_hasNoneFiles && _hasRemoveFile && cfgFile.promptDeleteFiles()) {
         qCInfo(lcEngine) << "All the files are going to be changed, asking the user";