Skip to content

Commit

Permalink
fix: catch ManuallyLockedException and use app context
Browse files Browse the repository at this point in the history
The files_lock app may throw ManuallyLockedExceptions
when attempting to revert a file that is currently opened.
This would prevent the user from rolling back a opened file.

Text and Richdocuments handle changes of the file while editing.
Allow reverting files even when they are locked by these apps
and let the apps handle the conflict.

Signed-off-by: Max <max@nextcloud.com>
  • Loading branch information
max-nextcloud committed Apr 18, 2023
1 parent b21c5c8 commit 52db34b
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 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, INameableVersionBackend, IDeletableVersionBackend {
/** @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();
$result = $backend->rollback($version);
$result = self::handleAppLocks(fn(): ?bool => $backend->rollback($version));
// rollback doesn't have a return type yet and some implementations don't return anything
if ($result === null || $result === true) {
\OC_Hook::emit('\OCP\Versions', 'rollback', [
Expand Down Expand Up @@ -133,4 +138,45 @@ public function deleteVersion(IVersion $version): void {
$backend->deleteVersion($version);
}
}

/**
* Catch ManuallyLockedException and retry in app context if possible.
*
* The files_lock app may throw ManuallyLockedExceptions
* when attempting to revert a file that is currently opened.
* This would prevent the user from rolling back a opened file.
*
* Text and Richdocuments handle changes of the file while editing.
* Allow reverting files even when they are locked by these apps
* and let the apps 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;
}
}

}

0 comments on commit 52db34b

Please sign in to comment.