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

[stable25] fix: catch ManuallyLockedException and use app context #38044

Merged
merged 1 commit into from
May 4, 2023
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
32 changes: 17 additions & 15 deletions apps/files_versions/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,24 +436,26 @@ private static function copyFileContents($view, $path1, $path2) {
$view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
$view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);

// TODO add a proper way of overwriting a file while maintaining file ids
if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) {
$source = $storage1->fopen($internalPath1, 'r');
$target = $storage2->fopen($internalPath2, 'w');
[, $result] = \OC_Helper::streamCopy($source, $target);
fclose($source);
fclose($target);

if ($result !== false) {
$storage1->unlink($internalPath1);
try {
// TODO add a proper way of overwriting a file while maintaining file ids
if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) {

Check notice

Code scanning / Psalm

ArgumentTypeCoercion

Argument 1 of OC\Files\Storage\Storage::instanceOfStorage expects class-string<OCP\Files\Storage\IStorage>, parent type "\\OC\\Files\\ObjectStore\\ObjectStoreStorage" provided

Check notice

Code scanning / Psalm

ArgumentTypeCoercion

Argument 1 of OC\Files\Storage\Storage::instanceOfStorage expects class-string<OCP\Files\Storage\IStorage>, parent type "\\OC\\Files\\ObjectStore\\ObjectStoreStorage" provided
$source = $storage1->fopen($internalPath1, 'r');
$target = $storage2->fopen($internalPath2, 'w');
[, $result] = \OC_Helper::streamCopy($source, $target);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument

Argument 1 of OC_Helper::streamCopy expects resource, possibly different type bool|mixed|resource provided

Check notice

Code scanning / Psalm

PossiblyInvalidArgument

Argument 2 of OC_Helper::streamCopy expects resource, possibly different type bool|resource provided
fclose($source);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument

Argument 1 of fclose expects resource, possibly different type bool|mixed|resource provided
fclose($target);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument

Argument 1 of fclose expects resource, possibly different type bool|resource provided

if ($result !== false) {
$storage1->unlink($internalPath1);
}
} else {
$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
}
} else {
$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
} finally {
$view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
$view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
}

$view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
$view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);

return ($result !== false);
}

Expand Down
51 changes: 50 additions & 1 deletion apps/files_versions/lib/Versions/VersionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@

use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Files\Lock\ILock;
use OCP\Files\Lock\ILockManager;
use OCP\Files\Lock\LockContext;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\Lock\ManuallyLockedException;

class VersionManager implements IVersionManager {
/** @var (IVersionBackend[])[] */
Expand Down Expand Up @@ -94,7 +99,7 @@ public function createVersion(IUser $user, FileInfo $file) {

public function rollback(IVersion $version) {
$backend = $version->getBackend();
return $backend->rollback($version);
return self::handleAppLocks(fn(): ?bool => $backend->rollback($version));
}

public function read(IVersion $version) {
Expand All @@ -110,4 +115,48 @@ public function getVersionFile(IUser $user, FileInfo $sourceFile, $revision): Fi
public function useBackendForStorage(IStorage $storage): bool {
return false;
}

/**
* Catch ManuallyLockedException and retry in app context if possible.
*
* Allow users to go back to old versions via the versions tab in the sidebar
* even when the file is opened in the viewer next to it.
*
* Context: If a file is currently opened for editing
* the files_lock app will throw ManuallyLockedExceptions.
* This prevented the user from rolling an opened file back to a previous version.
*
* Text and Richdocuments can handle changes of open files.
* So we execute the rollback under their lock context
* to let them handle the conflict.
*
* @param callable $callback function to run with app locks handled
* @return bool|null
* @throws ManuallyLockedException
*
*/
private static function handleAppLocks(callable $callback): ?bool {
try {
return $callback();
} catch (ManuallyLockedException $e) {
$owner = (string) $e->getOwner();
$appsThatHandleUpdates = array("text", "richdocuments");
if (!in_array($owner, $appsThatHandleUpdates)) {
throw $e;
}
// The LockWrapper in the files_lock app only compares the lock type and owner
// when checking the lock against the current scope.
// So we do not need to get the actual node here
// and use the root node instead.
$root = \OC::$server->get(IRootFolder::class);
$lockContext = new LockContext($root, ILock::TYPE_APP, $owner);
$lockManager = \OC::$server->get(ILockManager::class);
$result = null;
$lockManager->runInScope($lockContext, function() use ($callback, &$result) {
$result = $callback();
});
return $result;
}
}

}