From 02635c6f2fe9357c88aa896896aec036f92c8d87 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 3 Mar 2016 13:45:48 +0100 Subject: [PATCH] Add locking to the node api --- lib/private/files/node/node.php | 24 ++++++++++++++++ lib/public/files/node.php | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/lib/private/files/node/node.php b/lib/private/files/node/node.php index 5df4f738a4..c4fabfc2e2 100644 --- a/lib/private/files/node/node.php +++ b/lib/private/files/node/node.php @@ -356,4 +356,28 @@ class Node implements \OCP\Files\Node { public function getChecksum() { return; } + + /** + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE + * @throws \OCP\Lock\LockedException + */ + public function lock($type) { + $this->view->lockFile($this->path, $type); + } + + /** + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE + * @throws \OCP\Lock\LockedException + */ + public function changeLock($type) { + $this->view->changeLock($this->path, $type); + } + + /** + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE + * @throws \OCP\Lock\LockedException + */ + public function unlock($type) { + $this->view->unlockFile($this->path, $type); + } } diff --git a/lib/public/files/node.php b/lib/public/files/node.php index ee3f0cb041..c69077c7f2 100644 --- a/lib/public/files/node.php +++ b/lib/public/files/node.php @@ -225,4 +225,55 @@ interface Node extends FileInfo { * @since 6.0.0 */ public function getName(); + + /** + * Acquire a lock on this file or folder. + * + * A shared (read) lock will prevent any exclusive (write) locks from being created but any number of shared locks + * can be active at the same time. + * An exclusive lock will prevent any other lock from being created (both shared and exclusive). + * + * A locked exception will be thrown if any conflicting lock already exists + * + * Note that this uses mandatory locking, if you acquire an exclusive lock on a file it will block *all* + * other operations for that file, even within the same php process. + * + * Acquiring any lock on a file will also create a shared lock on all parent folders of that file. + * + * Note that in most cases you won't need to manually manage the locks for any files you're working with, + * any filesystem operation will automatically acquire the relevant locks for that operation. + * + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE + * @throws \OCP\Lock\LockedException + * @since 9.1.0 + */ + public function lock($type); + + /** + * Check the type of an existing lock. + * + * A shared lock can be changed to an exclusive lock is there is exactly one shared lock on the file, + * an exclusive lock can always be changed to a shared lock since there can only be one exclusive lock int he first place. + * + * A locked exception will be thrown when these preconditions are not met. + * Note that this is also the case if no existing lock exists for the file. + * + * @param int $targetType \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE + * @throws \OCP\Lock\LockedException + * @since 9.1.0 + */ + public function changeLock($targetType); + + /** + * Release an existing lock. + * + * This will also free up the shared locks on any parent folder that were automatically acquired when locking the file. + * + * Note that this method will not give any sort of error when trying to free a lock that doesn't exist. + * + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE + * @throws \OCP\Lock\LockedException + * @since 9.1.0 + */ + public function unlock($type); }