Skip to content

Commit bb69609

Browse files
mgalliencamilasan
authored andcommitted
set the parent folder read/write always when downloading a new file
that would also cover code paths for virtual files Signed-off-by: Matthieu Gallien <[email protected]>
1 parent 866a36c commit bb69609

File tree

2 files changed

+55
-28
lines changed

2 files changed

+55
-28
lines changed

src/libsync/propagatedownload.cpp

+50-28
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ void PropagateDownloadFile::startAfterIsEncryptedIsChecked()
490490

491491
// For virtual files just dehydrate or create the file and be done
492492
if (_item->_type == ItemTypeVirtualFileDehydration) {
493-
QString fsPath = propagator()->fullLocalPath(_item->_file);
493+
const auto fsPath = propagator()->fullLocalPath(_item->_file);
494+
makeParentFolderModifiable(fsPath);
494495
if (!FileSystem::verifyFileUnchanged(fsPath, _item->_previousSize, _item->_previousModtime)) {
495496
propagator()->_anotherSyncNeeded = true;
496497
done(SyncFileItem::SoftError, tr("File has changed since discovery"), ErrorCategory::GenericError);
@@ -499,6 +500,7 @@ void PropagateDownloadFile::startAfterIsEncryptedIsChecked()
499500

500501
qCDebug(lcPropagateDownload) << "dehydrating file" << _item->_file;
501502
if (FileSystem::isLnkFile(fsPath)) {
503+
makeParentFolderModifiable(_tmpFile.fileName());
502504
const auto convertResult = vfs->convertToPlaceholder(fsPath, *_item);
503505
if (!convertResult) {
504506
qCCritical(lcPropagateDownload()) << "error when converting a shortcut file to placeholder" << convertResult.error();
@@ -532,6 +534,10 @@ void PropagateDownloadFile::startAfterIsEncryptedIsChecked()
532534
}
533535
if (_item->_type == ItemTypeVirtualFile && !propagator()->localFileNameClash(_item->_file)) {
534536
qCDebug(lcPropagateDownload) << "creating virtual file" << _item->_file;
537+
538+
const auto fsPath = propagator()->fullLocalPath(_item->_file);
539+
makeParentFolderModifiable(fsPath);
540+
535541
// do a klaas' case clash check.
536542
if (propagator()->localFileNameClash(_item->_file)) {
537543
done(SyncFileItem::FileNameClash, tr("File %1 can not be downloaded because of a local file name clash!").arg(QDir::toNativeSeparators(_item->_file)), ErrorCategory::GenericError);
@@ -666,6 +672,7 @@ void PropagateDownloadFile::startDownload()
666672
tmpFileName = createDownloadTmpFileName(_item->_file);
667673
}
668674
_tmpFile.setFileName(propagator()->fullLocalPath(tmpFileName));
675+
makeParentFolderModifiable(_tmpFile.fileName());
669676

670677
_resumeStart = _tmpFile.size();
671678
if (_resumeStart > 0 && _resumeStart == _item->_size) {
@@ -680,32 +687,6 @@ void PropagateDownloadFile::startDownload()
680687
FileSystem::setFileReadOnly(_tmpFile.fileName(), false);
681688
}
682689

683-
#if !defined(Q_OS_MACOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15
684-
try {
685-
const auto newDirPath = std::filesystem::path{_tmpFile.fileName().toStdWString()};
686-
Q_ASSERT(newDirPath.has_parent_path());
687-
_parentPath = newDirPath.parent_path();
688-
}
689-
catch (const std::filesystem::filesystem_error &e)
690-
{
691-
qCWarning(lcPropagateDownload) << "exception when checking parent folder access rights" << e.what() << e.path1().c_str() << e.path2().c_str();
692-
}
693-
catch (const std::system_error &e)
694-
{
695-
qCWarning(lcPropagateDownload) << "exception when checking parent folder access rights" << e.what();
696-
}
697-
catch (...)
698-
{
699-
qCWarning(lcPropagateDownload) << "exception when checking parent folder access rights";
700-
}
701-
702-
if (FileSystem::isFolderReadOnly(_parentPath)) {
703-
FileSystem::setFolderPermissions(QString::fromStdWString(_parentPath.wstring()), FileSystem::FolderPermissions::ReadWrite);
704-
emit propagator()->touchedFile(QString::fromStdWString(_parentPath.wstring()));
705-
_needParentFolderRestorePermissions = true;
706-
}
707-
#endif
708-
709690
if (!_tmpFile.open(QIODevice::Append | QIODevice::Unbuffered)) {
710691
qCWarning(lcPropagateDownload) << "could not open temporary file" << _tmpFile.fileName();
711692
done(SyncFileItem::NormalError, _tmpFile.errorString(), ErrorCategory::GenericError);
@@ -786,6 +767,47 @@ void PropagateDownloadFile::setDeleteExistingFolder(bool enabled)
786767
_deleteExisting = enabled;
787768
}
788769

770+
void PropagateDownloadFile::done(const SyncFileItem::Status status, const QString &errorString, const ErrorCategory category)
771+
{
772+
#if !defined(Q_OS_MACOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15
773+
if (_needParentFolderRestorePermissions) {
774+
FileSystem::setFolderPermissions(QString::fromStdWString(_parentPath.wstring()), FileSystem::FolderPermissions::ReadOnly);
775+
emit propagator()->touchedFile(QString::fromStdWString(_parentPath.wstring()));
776+
_needParentFolderRestorePermissions = false;
777+
}
778+
#endif
779+
PropagateItemJob::done(status, errorString, category);
780+
}
781+
782+
void PropagateDownloadFile::makeParentFolderModifiable(const QString &fileName)
783+
{
784+
#if !defined(Q_OS_MACOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15
785+
try {
786+
const auto newDirPath = std::filesystem::path{fileName.toStdWString()};
787+
Q_ASSERT(newDirPath.has_parent_path());
788+
_parentPath = newDirPath.parent_path();
789+
}
790+
catch (const std::filesystem::filesystem_error &e)
791+
{
792+
qCWarning(lcPropagateDownload) << "exception when checking parent folder access rights" << e.what() << e.path1().c_str() << e.path2().c_str();
793+
}
794+
catch (const std::system_error &e)
795+
{
796+
qCWarning(lcPropagateDownload) << "exception when checking parent folder access rights" << e.what();
797+
}
798+
catch (...)
799+
{
800+
qCWarning(lcPropagateDownload) << "exception when checking parent folder access rights";
801+
}
802+
803+
if (FileSystem::isFolderReadOnly(_parentPath)) {
804+
FileSystem::setFolderPermissions(QString::fromStdWString(_parentPath.wstring()), FileSystem::FolderPermissions::ReadWrite);
805+
emit propagator()->touchedFile(QString::fromStdWString(_parentPath.wstring()));
806+
_needParentFolderRestorePermissions = true;
807+
}
808+
#endif
809+
}
810+
789811
const char owncloudCustomSoftErrorStringC[] = "owncloud-custom-soft-error-string";
790812
void PropagateDownloadFile::slotGetFinished()
791813
{
@@ -1328,7 +1350,7 @@ void PropagateDownloadFile::downloadFinished()
13281350

13291351
#if !defined(Q_OS_MACOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15
13301352
if (_needParentFolderRestorePermissions) {
1331-
FileSystem::setFolderPermissions(QString::fromStdWString(_parentPath.wstring()), FileSystem::FolderPermissions::ReadWrite);
1353+
FileSystem::setFolderPermissions(QString::fromStdWString(_parentPath.wstring()), FileSystem::FolderPermissions::ReadOnly);
13321354
emit propagator()->touchedFile(QString::fromStdWString(_parentPath.wstring()));
13331355
_needParentFolderRestorePermissions = false;
13341356
}

src/libsync/propagatedownload.h

+5
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ class PropagateDownloadFile : public PropagateItemJob
220220
*/
221221
void setDeleteExistingFolder(bool enabled);
222222

223+
protected:
224+
void done(const SyncFileItem::Status status, const QString &errorString, const ErrorCategory category) override;
225+
226+
void makeParentFolderModifiable(const QString &fileName);
227+
223228
private slots:
224229
/// Called when ComputeChecksum on the local file finishes,
225230
/// maybe the local and remote checksums are identical?

0 commit comments

Comments
 (0)