PropagateDownload: Don't try to open readonly temporaries
authorChristian Kamm <mail@ckamm.de>
Fri, 23 Aug 2019 12:49:24 +0000 (14:49 +0200)
committerKevin Ottens <kevin.ottens@nextcloud.com>
Tue, 15 Dec 2020 09:58:58 +0000 (10:58 +0100)
This situation could arrise when receiving a read-only share and the
temporary rename failed for some reason.

See #7419

src/libsync/propagatedownload.cpp

index 1201703822500136552835816be1f13922b3f236..7ac691df1be3bce0e67893364f1ebd5b1bfbb985 100644 (file)
@@ -523,25 +523,27 @@ void PropagateDownloadFile::startDownload()
     if (tmpFileName.isEmpty()) {
         tmpFileName = createDownloadTmpFileName(_item->_file);
     }
-
     _tmpFile.setFileName(propagator()->getFilePath(tmpFileName));
+
+    _resumeStart = _tmpFile.size();
+    if (_resumeStart > 0 && _resumeStart == _item->_size) {
+        qCInfo(lcPropagateDownload) << "File is already complete, no need to download";
+        downloadFinished();
+        return;
+    }
+
+    // Can't open(Append) read-only files, make sure to make
+    // file writable if it exists.
+    if (_tmpFile.exists())
+        FileSystem::setFileReadOnly(_tmpFile.fileName(), false);
     if (!_tmpFile.open(QIODevice::Append | QIODevice::Unbuffered)) {
+        qCWarning(lcPropagateDownload) << "could not open temporary file" << _tmpFile.fileName();
         done(SyncFileItem::NormalError, _tmpFile.errorString());
         return;
     }
-
+    // Hide temporary after creation
     FileSystem::setFileHidden(_tmpFile.fileName(), true);
 
-    _resumeStart = _tmpFile.size();
-    if (_resumeStart > 0) {
-        if (_resumeStart == _item->_size) {
-            qCInfo(lcPropagateDownload) << "File is already complete, no need to download";
-            _tmpFile.close();
-            downloadFinished();
-            return;
-        }
-    }
-
     // If there's not enough space to fully download this file, stop.
     const auto diskSpaceResult = propagator()->diskSpaceCheck();
     if (diskSpaceResult != OwncloudPropagator::DiskSpaceOk) {