Remove the encryption flag check from encrypted propagation code
authorKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 8 Dec 2020 15:20:29 +0000 (16:20 +0100)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:59:23 +0000 (10:59 +0100)
If we use those encrypted propagation code paths, we already know from
the discovery phase (and thus the journal db) that the folders are
encrypted so no need to check again.

This will remove another expensive round trip with the server.

Signed-off-by: Kevin Ottens <kevin.ottens@nextcloud.com>
src/libsync/propagatedownload.cpp
src/libsync/propagatedownloadencrypted.cpp
src/libsync/propagatedownloadencrypted.h
src/libsync/propagateremotemkdir.cpp
src/libsync/propagateupload.cpp
src/libsync/propagateuploadencrypted.cpp
src/libsync/propagateuploadencrypted.h

index f56fdcd3da050faa72522f9ad8f824f6c0b2412f..679c5620a39c8584eeb3ba6581e0506708c54f0e 100644 (file)
@@ -381,10 +381,7 @@ void PropagateDownloadFile::start()
         startAfterIsEncryptedIsChecked();
     } else {
         _downloadEncryptedHelper = new PropagateDownloadEncrypted(propagator(), parentPath, _item, this);
-        connect(_downloadEncryptedHelper, &PropagateDownloadEncrypted::folderStatusNotEncrypted, [this] {
-          startAfterIsEncryptedIsChecked();
-        });
-        connect(_downloadEncryptedHelper, &PropagateDownloadEncrypted::folderStatusEncrypted, [this] {
+        connect(_downloadEncryptedHelper, &PropagateDownloadEncrypted::fileMetadataFound, [this] {
           _isEncrypted = true;
           startAfterIsEncryptedIsChecked();
         });
index 6cbdc8402094296cc48461bf57f207681cedeab1..8975faff7c58547d5d3ac8c6d85052e377ad206a 100644 (file)
@@ -15,11 +15,7 @@ PropagateDownloadEncrypted::PropagateDownloadEncrypted(OwncloudPropagator *propa
 {
 }
 
-void PropagateDownloadEncrypted::start() {
-  checkFolderEncryptedStatus();
-}
-
-void PropagateDownloadEncrypted::checkFolderEncryptedStatus()
+void PropagateDownloadEncrypted::start()
 {
     const auto rootPath = [=]() {
         const auto result = _propagator->remotePath();
@@ -33,37 +29,14 @@ void PropagateDownloadEncrypted::checkFolderEncryptedStatus()
     const auto remotePath = QString(rootPath + remoteFilename);
     const auto remoteParentPath = remotePath.left(remotePath.lastIndexOf('/'));
 
-  auto getEncryptedStatus = new GetFolderEncryptStatusJob(_propagator->account(), remoteParentPath, this);
-  connect(getEncryptedStatus, &GetFolderEncryptStatusJob::encryptStatusFolderReceived,
-          this, &PropagateDownloadEncrypted::folderStatusReceived);
-
-  connect(getEncryptedStatus, &GetFolderEncryptStatusJob::encryptStatusError,
-          this, &PropagateDownloadEncrypted::folderStatusError);
-
-  getEncryptedStatus->start();
-}
-
-void PropagateDownloadEncrypted::folderStatusError(int statusCode)
-{
-  qCDebug(lcPropagateDownloadEncrypted) << "Failed to get encrypted status of folder" << statusCode;
-}
-
-void PropagateDownloadEncrypted::folderStatusReceived(const QString &folder, bool isEncrypted)
-{
-  qCDebug(lcPropagateDownloadEncrypted) << "Get Folder is Encrypted Received" << folder << isEncrypted;
-  if (!isEncrypted) {
-      emit folderStatusNotEncrypted();
-      return;
-  }
-
-  // Is encrypted Now we need the folder-id
-  auto job = new LsColJob(_propagator->account(), folder, this);
-  job->setProperties({"resourcetype", "http://owncloud.org/ns:fileid"});
-  connect(job, &LsColJob::directoryListingSubfolders,
-          this, &PropagateDownloadEncrypted::checkFolderId);
-  connect(job, &LsColJob::finishedWithError,
-          this, &PropagateDownloadEncrypted::folderIdError);
-  job->start();
+    // Is encrypted Now we need the folder-id
+    auto job = new LsColJob(_propagator->account(), remoteParentPath, this);
+    job->setProperties({"resourcetype", "http://owncloud.org/ns:fileid"});
+    connect(job, &LsColJob::directoryListingSubfolders,
+            this, &PropagateDownloadEncrypted::checkFolderId);
+    connect(job, &LsColJob::finishedWithError,
+            this, &PropagateDownloadEncrypted::folderIdError);
+    job->start();
 }
 
 void PropagateDownloadEncrypted::folderIdError()
@@ -101,7 +74,7 @@ void PropagateDownloadEncrypted::checkFolderEncryptedMetadata(const QJsonDocumen
       _encryptedInfo = file;
 
       qCDebug(lcPropagateDownloadEncrypted) << "Found matching encrypted metadata for file, starting download";
-      emit folderStatusEncrypted();
+      emit fileMetadataFound();
       return;
     }
   }
index 2d769c746732a574db5a27a15c9a7ffab1f02ea1..4df0446df9cef62872902c242c5460fcbf4bc453 100644 (file)
@@ -17,20 +17,16 @@ class PropagateDownloadEncrypted : public QObject {
 public:
   PropagateDownloadEncrypted(OwncloudPropagator *propagator, const QString &localParentPath, SyncFileItemPtr item, QObject *parent = nullptr);
   void start();
-  void checkFolderId(const QStringList &list);
   bool decryptFile(QFile& tmpFile);
   QString errorString() const;
 
 public slots:
-  void checkFolderEncryptedStatus();
-
+  void checkFolderId(const QStringList &list);
   void checkFolderEncryptedMetadata(const QJsonDocument &json);
-  void folderStatusReceived(const QString &folder, bool isEncrypted);
-  void folderStatusError(int httpErrorCode);
   void folderIdError();
+
 signals:
-  void folderStatusEncrypted();
-  void folderStatusNotEncrypted();
+  void fileMetadataFound();
   void failed();
 
   void decryptionFinished();
index 2f67b9af3074904e3f614d43a32dd6264d90fbb7..4d1703054514150081464eb4b2917542c6626278 100644 (file)
@@ -183,8 +183,6 @@ void PropagateRemoteMkdir::slotMkdir()
     // We should be encrypted as well since our parent is
     const auto remoteParentPath = parentRec._e2eMangledName.isEmpty() ? parentPath : parentRec._e2eMangledName;
     _uploadEncryptedHelper = new PropagateUploadEncrypted(propagator(), remoteParentPath, _item, this);
-    connect(_uploadEncryptedHelper, &PropagateUploadEncrypted::folderNotEncrypted,
-      this, &PropagateRemoteMkdir::slotStartMkcolJob);
     connect(_uploadEncryptedHelper, &PropagateUploadEncrypted::finalized,
       this, &PropagateRemoteMkdir::slotStartEncryptedMkcolJob);
     connect(_uploadEncryptedHelper, &PropagateUploadEncrypted::error,
index 6c8af7f1f6ccd98909b0c62323e5a829519334ff..d2182df815960495da7eb0abafd769ffa1f584f1 100644 (file)
@@ -249,8 +249,6 @@ void PropagateUploadFileCommon::start()
 
     const auto remoteParentPath = parentRec._e2eMangledName.isEmpty() ? parentPath : parentRec._e2eMangledName;
     _uploadEncryptedHelper = new PropagateUploadEncrypted(propagator(), remoteParentPath, _item, this);
-    connect(_uploadEncryptedHelper, &PropagateUploadEncrypted::folderNotEncrypted,
-            this, &PropagateUploadFileCommon::setupUnencryptedFile);
     connect(_uploadEncryptedHelper, &PropagateUploadEncrypted::finalized,
             this, &PropagateUploadFileCommon::setupEncryptedFile);
     connect(_uploadEncryptedHelper, &PropagateUploadEncrypted::error,
index 2bce8b5adb62486f3b0ff875f26a94b1ea617189..d02e7bc373e1d44d72e1b5e69d909dfc9316161c 100644 (file)
@@ -44,48 +44,24 @@ void PropagateUploadEncrypted::start()
     }();
 
 
-  /* If the file is in a encrypted-enabled nextcloud instance, we need to
-      * do the long road: Fetch the folder status of the encrypted bit,
-      * if it's encrypted, find the ID of the folder.
-      * lock the folder using it's id.
-      * download the metadata
-      * update the metadata
-      * upload the file
-      * upload the metadata
-      * unlock the folder.
-      *
-      * If the folder is unencrypted we just follow the old way.
-      */
-      qCDebug(lcPropagateUploadEncrypted) << "Starting to send an encrypted file!";
-      auto getEncryptedStatus = new GetFolderEncryptStatusJob(_propagator->account(), absoluteRemoteParentPath, this);
-
-      connect(getEncryptedStatus, &GetFolderEncryptStatusJob::encryptStatusFolderReceived,
-              this, &PropagateUploadEncrypted::slotFolderEncryptedStatusFetched);
-      connect(getEncryptedStatus, &GetFolderEncryptStatusJob::encryptStatusError,
-             this, &PropagateUploadEncrypted::slotFolderEncryptedStatusError);
-      getEncryptedStatus->start();
-}
-
-void PropagateUploadEncrypted::slotFolderEncryptedStatusFetched(const QString &folder, bool isEncrypted)
-{
-  qCDebug(lcPropagateUploadEncrypted) << "Encrypted Status Fetched" << folder << isEncrypted;
-
-  /* We are inside an encrypted folder, we need to find it's Id. */
-  if (isEncrypted) {
-      qCDebug(lcPropagateUploadEncrypted) << "Folder is encrypted, let's get the Id from it.";
-      auto job = new LsColJob(_propagator->account(), folder, this);
-      job->setProperties({"resourcetype", "http://owncloud.org/ns:fileid"});
-      connect(job, &LsColJob::directoryListingSubfolders, this, &PropagateUploadEncrypted::slotFolderEncryptedIdReceived);
-      connect(job, &LsColJob::finishedWithError, this, &PropagateUploadEncrypted::slotFolderEncryptedIdError);
-      job->start();
-  } else {
-    qCDebug(lcPropagateUploadEncrypted) << "Folder is not encrypted, getting back to default.";
-    emit folderNotEncrypted();
-  }
+    /* If the file is in a encrypted folder, which we know, we wouldn't be here otherwise,
+     * we need to do the long road:
+     * find the ID of the folder.
+     * lock the folder using it's id.
+     * download the metadata
+     * update the metadata
+     * upload the file
+     * upload the metadata
+     * unlock the folder.
+     */
+    qCDebug(lcPropagateUploadEncrypted) << "Folder is encrypted, let's get the Id from it.";
+    auto job = new LsColJob(_propagator->account(), absoluteRemoteParentPath, this);
+    job->setProperties({"resourcetype", "http://owncloud.org/ns:fileid"});
+    connect(job, &LsColJob::directoryListingSubfolders, this, &PropagateUploadEncrypted::slotFolderEncryptedIdReceived);
+    connect(job, &LsColJob::finishedWithError, this, &PropagateUploadEncrypted::slotFolderEncryptedIdError);
+    job->start();
 }
 
-
-
 /* We try to lock a folder, if it's locked we try again in one second.
  * if it's still locked we try again in one second. looping untill one minute.
  *                                                                      -> fail.
@@ -286,11 +262,6 @@ void PropagateUploadEncrypted::slotFolderEncryptedIdError(QNetworkReply *r)
     qCDebug(lcPropagateUploadEncrypted) << "Error retrieving the Id of the encrypted folder.";
 }
 
-void PropagateUploadEncrypted::slotFolderEncryptedStatusError(int error)
-{
-    qCDebug(lcPropagateUploadEncrypted) << "Failed to retrieve the status of the folders." << error;
-}
-
 void PropagateUploadEncrypted::unlockFolder()
 {
     qDebug() << "Calling Unlock";
index 33ddbb59753a001a670ef8e1c10f39cd8a89ce40..e281a17b15d73a834f7fe65d372cf62f6112b60f 100644 (file)
@@ -42,8 +42,6 @@ public:
   QByteArray _folderId;
 
 private slots:
-    void slotFolderEncryptedStatusFetched(const QString &folder, bool isEncrypted);
-    void slotFolderEncryptedStatusError(int error);
     void slotFolderEncryptedIdReceived(const QStringList &list);
     void slotFolderEncryptedIdError(QNetworkReply *r);
     void slotFolderLockedSuccessfully(const QByteArray& fileId, const QByteArray& token);
@@ -59,9 +57,6 @@ signals:
     void finalized(const QString& path, const QString& filename, quint64 size);
     void error();
 
-    // Emited if the file is not in a encrypted folder.
-    void folderNotEncrypted();
-
 private:
   OwncloudPropagator *_propagator;
   QString _remoteParentPath;