Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/delete readonly folders #7010

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/libsync/filesystem.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in src/libsync/filesystem.cpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on src/libsync/filesystem.cpp

File src/libsync/filesystem.cpp does not conform to Custom style guidelines. (lines 488)
* Copyright (C) by Daniel Molkentin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -480,6 +480,25 @@
return false;
}
}

FileSystem::FilePermissionsRestore::FilePermissionsRestore(const QString &path, FolderPermissions temporaryPermissions)
: _path(path)
{
const auto stdStrPath = _path.toStdWString();
_initialPermissions = FileSystem::isFolderReadOnly(stdStrPath) ? OCC::FileSystem::FolderPermissions::ReadOnly : OCC::FileSystem::FolderPermissions::ReadWrite;
if (_initialPermissions != temporaryPermissions) {
_rollbackNeeded = true;
FileSystem::setFolderPermissions(_path, temporaryPermissions);
}
}

FileSystem::FilePermissionsRestore::~FilePermissionsRestore()
{
if (_rollbackNeeded) {
FileSystem::setFolderPermissions(_path, _initialPermissions);
}
}

#endif

} // namespace OCC
13 changes: 13 additions & 0 deletions src/libsync/filesystem.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in src/libsync/filesystem.h

View workflow job for this annotation

GitHub Actions / build

Run clang-format on src/libsync/filesystem.h

File src/libsync/filesystem.h does not conform to Custom style guidelines. (lines 44, 45, 46, 47, 49, 51, 52, 53, 54, 55)
* Copyright (C) by Olivier Goffart <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -14,7 +14,7 @@

#pragma once

#include "config.h"

Check failure on line 17 in src/libsync/filesystem.h

View workflow job for this annotation

GitHub Actions / build

src/libsync/filesystem.h:17:10 [clang-diagnostic-error]

'config.h' file not found

#include "owncloudlib.h"
#include "common/filesystembase.h"
Expand All @@ -41,6 +41,19 @@
* @brief This file contains file system helper
*/
namespace FileSystem {
class OWNCLOUDSYNC_EXPORT FilePermissionsRestore {
public:
explicit FilePermissionsRestore(const QString &path,
FileSystem::FolderPermissions temporaryPermissions);

~FilePermissionsRestore();

private:
QString _path;
FileSystem::FolderPermissions _initialPermissions;
bool _rollbackNeeded = false;
};

struct OWNCLOUDSYNC_EXPORT FileLockingInfo {
enum class Type { Unset = -1, Locked, Unlocked };
QString path;
Expand Down
21 changes: 17 additions & 4 deletions src/libsync/propagatorjobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ bool PropagateLocalRemove::removeRecursively(const QString &path)
QStringList errors;
QList<QPair<QString, bool>> deleted;
#if !defined(Q_OS_MACOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15
const auto fileInfo = QFileInfo{absolute};
const auto parentFolderPath = fileInfo.dir().absolutePath();
const auto parentPermissionsHandler = FileSystem::FilePermissionsRestore{parentFolderPath, FileSystem::FolderPermissions::ReadWrite};
FileSystem::setFolderPermissions(absolute, FileSystem::FolderPermissions::ReadWrite);
#endif
bool success = FileSystem::removeRecursively(
Expand Down Expand Up @@ -126,10 +129,18 @@ void PropagateLocalRemove::start()
return;
}
} else {
if (FileSystem::fileExists(filename)
&& !FileSystem::remove(filename, &removeError)) {
done(SyncFileItem::NormalError, removeError, ErrorCategory::GenericError);
return;
if (FileSystem::fileExists(filename)) {
#if !defined(Q_OS_MACOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_15
const auto fileInfo = QFileInfo{filename};
const auto parentFolderPath = fileInfo.dir().absolutePath();

const auto parentPermissionsHandler = FileSystem::FilePermissionsRestore{parentFolderPath, FileSystem::FolderPermissions::ReadWrite};
#endif

if (!FileSystem::remove(filename, &removeError)) {
done(SyncFileItem::NormalError, removeError, ErrorCategory::GenericError);
return;
}
}
}
}
Expand Down Expand Up @@ -368,6 +379,8 @@ void PropagateLocalRename::start()
};
#endif

const auto folderPermissionsHandler = FileSystem::FilePermissionsRestore{existingFile, FileSystem::FolderPermissions::ReadWrite};

emit propagator()->touchedFile(existingFile);
emit propagator()->touchedFile(targetFile);
if (QString renameError; !FileSystem::rename(existingFile, targetFile, &renameError)) {
Expand Down
134 changes: 134 additions & 0 deletions test/testpermissions.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check notice on line 1 in test/testpermissions.cpp

View workflow job for this annotation

GitHub Actions / build

Run clang-format on test/testpermissions.cpp

File test/testpermissions.cpp does not conform to Custom style guidelines. (lines 796, 826, 860, 896)
* This software is in the public domain, furnished "as is", without technical
* support, and with no warranty, express or implied, as to its usefulness for
* any purpose.
Expand Down Expand Up @@ -769,6 +769,140 @@
QVERIFY(testFolderStatus.permissions() & std::filesystem::perms::owner_read);
QVERIFY(!static_cast<bool>(testFolderStatus.permissions() & std::filesystem::perms::owner_write));
}

void testDeleteChildItemsInReadOnlyFolder()
{
FakeFolder fakeFolder{FileInfo{}};

auto &remote = fakeFolder.remoteModifier();

remote.mkdir("readOnlyFolder");
remote.mkdir("readOnlyFolder/test");
remote.insert("readOnlyFolder/readOnlyFile.txt");

remote.find("readOnlyFolder")->permissions = RemotePermissions::fromServerString("M");
remote.find("readOnlyFolder/test")->permissions = RemotePermissions::fromServerString("m");
remote.find("readOnlyFolder/readOnlyFile.txt")->permissions = RemotePermissions::fromServerString("m");

QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());

remote.remove("readOnlyFolder/test");
remote.remove("readOnlyFolder/readOnlyFile.txt");

QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());

const auto ensureReadOnlyItem = [&fakeFolder] (const auto path) -> bool {
const auto itemStatus = std::filesystem::status(static_cast<QString>(fakeFolder.localPath() + path).toStdWString());
return static_cast<bool>(itemStatus.permissions() & std::filesystem::perms::owner_read);
};
QVERIFY(ensureReadOnlyItem("/readOnlyFolder"));
}

void testRenameChildItemsInReadOnlyFolder()
{
FakeFolder fakeFolder{FileInfo{}};

auto &remote = fakeFolder.remoteModifier();

remote.mkdir("readOnlyFolder");
remote.mkdir("readOnlyFolder/test");
remote.insert("readOnlyFolder/readOnlyFile.txt");

remote.find("readOnlyFolder")->permissions = RemotePermissions::fromServerString("M");
remote.find("readOnlyFolder/test")->permissions = RemotePermissions::fromServerString("m");
remote.find("readOnlyFolder/readOnlyFile.txt")->permissions = RemotePermissions::fromServerString("m");

QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());

remote.rename("readOnlyFolder/test", "readOnlyFolder/testRename");
remote.rename("readOnlyFolder/readOnlyFile.txt", "readOnlyFolder/readOnlyFileRename.txt");

QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());

const auto ensureReadOnlyItem = [&fakeFolder] (const auto path) -> bool {
const auto itemStatus = std::filesystem::status(static_cast<QString>(fakeFolder.localPath() + path).toStdWString());
return static_cast<bool>(itemStatus.permissions() & std::filesystem::perms::owner_read);
};
QVERIFY(ensureReadOnlyItem("/readOnlyFolder"));
QVERIFY(ensureReadOnlyItem("/readOnlyFolder/testRename"));
QVERIFY(ensureReadOnlyItem("/readOnlyFolder/readOnlyFileRename.txt"));
}

void testMoveChildItemsInReadOnlyFolder()
{
FakeFolder fakeFolder{FileInfo{}};

auto &remote = fakeFolder.remoteModifier();

remote.mkdir("readOnlyFolder");
remote.mkdir("readOnlyFolder/child");
remote.mkdir("readOnlyFolder/test");
remote.insert("readOnlyFolder/readOnlyFile.txt");

remote.find("readOnlyFolder")->permissions = RemotePermissions::fromServerString("M");
remote.find("readOnlyFolder/child")->permissions = RemotePermissions::fromServerString("m");
remote.find("readOnlyFolder/test")->permissions = RemotePermissions::fromServerString("m");
remote.find("readOnlyFolder/readOnlyFile.txt")->permissions = RemotePermissions::fromServerString("m");

QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());

remote.rename("readOnlyFolder/test", "readOnlyFolder/child/test");
remote.rename("readOnlyFolder/readOnlyFile.txt", "readOnlyFolder/child/readOnlyFile.txt");

QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());

const auto ensureReadOnlyItem = [&fakeFolder] (const auto path) -> bool {
const auto itemStatus = std::filesystem::status(static_cast<QString>(fakeFolder.localPath() + path).toStdWString());
return static_cast<bool>(itemStatus.permissions() & std::filesystem::perms::owner_read);
};
QVERIFY(ensureReadOnlyItem("/readOnlyFolder"));
QVERIFY(ensureReadOnlyItem("/readOnlyFolder/child"));
QVERIFY(ensureReadOnlyItem("/readOnlyFolder/child/test"));
QVERIFY(ensureReadOnlyItem("/readOnlyFolder/child/readOnlyFile.txt"));
}

void testModifyChildItemsInReadOnlyFolder()
{
FakeFolder fakeFolder{FileInfo{}};

auto &remote = fakeFolder.remoteModifier();

remote.mkdir("readOnlyFolder");
remote.mkdir("readOnlyFolder/test");
remote.insert("readOnlyFolder/readOnlyFile.txt");

remote.find("readOnlyFolder")->permissions = RemotePermissions::fromServerString("M");
remote.find("readOnlyFolder/test")->permissions = RemotePermissions::fromServerString("m");
remote.find("readOnlyFolder/readOnlyFile.txt")->permissions = RemotePermissions::fromServerString("m");

QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());

remote.insert("readOnlyFolder/test/newFile.txt");
remote.find("readOnlyFolder/test/newFile.txt")->permissions = RemotePermissions::fromServerString("m");
remote.mkdir("readOnlyFolder/test/newFolder");
remote.find("readOnlyFolder/test/newFolder")->permissions = RemotePermissions::fromServerString("m");
remote.appendByte("readOnlyFolder/readOnlyFile.txt");

QVERIFY(fakeFolder.syncOnce());
QCOMPARE(fakeFolder.currentLocalState(), fakeFolder.currentRemoteState());

const auto ensureReadOnlyItem = [&fakeFolder] (const auto path) -> bool {
const auto itemStatus = std::filesystem::status(static_cast<QString>(fakeFolder.localPath() + path).toStdWString());
return static_cast<bool>(itemStatus.permissions() & std::filesystem::perms::owner_read);
};
QVERIFY(ensureReadOnlyItem("/readOnlyFolder"));
QVERIFY(ensureReadOnlyItem("/readOnlyFolder/test"));
QVERIFY(ensureReadOnlyItem("/readOnlyFolder/readOnlyFile.txt"));
QVERIFY(ensureReadOnlyItem("/readOnlyFolder/test/newFile.txt"));
QVERIFY(ensureReadOnlyItem("/readOnlyFolder/newFolder"));
}
#endif
};

Expand Down
Loading