@@ -34,67 +34,67 @@ |
||
| 34 | 34 | use Sabre\HTTP\RequestInterface; |
| 35 | 35 | |
| 36 | 36 | class LockPlugin extends ServerPlugin { |
| 37 | - /** |
|
| 38 | - * Reference to main server object |
|
| 39 | - * |
|
| 40 | - * @var \Sabre\DAV\Server |
|
| 41 | - */ |
|
| 42 | - private $server; |
|
| 37 | + /** |
|
| 38 | + * Reference to main server object |
|
| 39 | + * |
|
| 40 | + * @var \Sabre\DAV\Server |
|
| 41 | + */ |
|
| 42 | + private $server; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * State of the lock |
|
| 46 | - * |
|
| 47 | - * @var bool |
|
| 48 | - */ |
|
| 49 | - private $isLocked; |
|
| 44 | + /** |
|
| 45 | + * State of the lock |
|
| 46 | + * |
|
| 47 | + * @var bool |
|
| 48 | + */ |
|
| 49 | + private $isLocked; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * {@inheritdoc} |
|
| 53 | - */ |
|
| 54 | - public function initialize(\Sabre\DAV\Server $server) { |
|
| 55 | - $this->server = $server; |
|
| 56 | - $this->server->on('beforeMethod:*', [$this, 'getLock'], 50); |
|
| 57 | - $this->server->on('afterMethod:*', [$this, 'releaseLock'], 50); |
|
| 58 | - $this->isLocked = false; |
|
| 59 | - } |
|
| 51 | + /** |
|
| 52 | + * {@inheritdoc} |
|
| 53 | + */ |
|
| 54 | + public function initialize(\Sabre\DAV\Server $server) { |
|
| 55 | + $this->server = $server; |
|
| 56 | + $this->server->on('beforeMethod:*', [$this, 'getLock'], 50); |
|
| 57 | + $this->server->on('afterMethod:*', [$this, 'releaseLock'], 50); |
|
| 58 | + $this->isLocked = false; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - public function getLock(RequestInterface $request) { |
|
| 62 | - // we can't listen on 'beforeMethod:PUT' due to order of operations with setting up the tree |
|
| 63 | - // so instead we limit ourselves to the PUT method manually |
|
| 64 | - if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 65 | - return; |
|
| 66 | - } |
|
| 67 | - try { |
|
| 68 | - $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
| 69 | - } catch (NotFound $e) { |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - if ($node instanceof Node) { |
|
| 73 | - try { |
|
| 74 | - $node->acquireLock(ILockingProvider::LOCK_SHARED); |
|
| 75 | - } catch (LockedException $e) { |
|
| 76 | - throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 77 | - } |
|
| 78 | - $this->isLocked = true; |
|
| 79 | - } |
|
| 80 | - } |
|
| 61 | + public function getLock(RequestInterface $request) { |
|
| 62 | + // we can't listen on 'beforeMethod:PUT' due to order of operations with setting up the tree |
|
| 63 | + // so instead we limit ourselves to the PUT method manually |
|
| 64 | + if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 65 | + return; |
|
| 66 | + } |
|
| 67 | + try { |
|
| 68 | + $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
| 69 | + } catch (NotFound $e) { |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + if ($node instanceof Node) { |
|
| 73 | + try { |
|
| 74 | + $node->acquireLock(ILockingProvider::LOCK_SHARED); |
|
| 75 | + } catch (LockedException $e) { |
|
| 76 | + throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
| 77 | + } |
|
| 78 | + $this->isLocked = true; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - public function releaseLock(RequestInterface $request) { |
|
| 83 | - // don't try to release the lock if we never locked one |
|
| 84 | - if ($this->isLocked === false) { |
|
| 85 | - return; |
|
| 86 | - } |
|
| 87 | - if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 88 | - return; |
|
| 89 | - } |
|
| 90 | - try { |
|
| 91 | - $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
| 92 | - } catch (NotFound $e) { |
|
| 93 | - return; |
|
| 94 | - } |
|
| 95 | - if ($node instanceof Node) { |
|
| 96 | - $node->releaseLock(ILockingProvider::LOCK_SHARED); |
|
| 97 | - $this->isLocked = false; |
|
| 98 | - } |
|
| 99 | - } |
|
| 82 | + public function releaseLock(RequestInterface $request) { |
|
| 83 | + // don't try to release the lock if we never locked one |
|
| 84 | + if ($this->isLocked === false) { |
|
| 85 | + return; |
|
| 86 | + } |
|
| 87 | + if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) { |
|
| 88 | + return; |
|
| 89 | + } |
|
| 90 | + try { |
|
| 91 | + $node = $this->server->tree->getNodeForPath($request->getPath()); |
|
| 92 | + } catch (NotFound $e) { |
|
| 93 | + return; |
|
| 94 | + } |
|
| 95 | + if ($node instanceof Node) { |
|
| 96 | + $node->releaseLock(ILockingProvider::LOCK_SHARED); |
|
| 97 | + $this->isLocked = false; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | 100 | } |
@@ -32,124 +32,124 @@ |
||
| 32 | 32 | use OCP\Lock\LockedException; |
| 33 | 33 | |
| 34 | 34 | class MemcacheLockingProvider extends AbstractLockingProvider { |
| 35 | - /** |
|
| 36 | - * @var \OCP\IMemcache |
|
| 37 | - */ |
|
| 38 | - private $memcache; |
|
| 35 | + /** |
|
| 36 | + * @var \OCP\IMemcache |
|
| 37 | + */ |
|
| 38 | + private $memcache; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param \OCP\IMemcache $memcache |
|
| 42 | - * @param int $ttl |
|
| 43 | - */ |
|
| 44 | - public function __construct(IMemcache $memcache, int $ttl = 3600) { |
|
| 45 | - $this->memcache = $memcache; |
|
| 46 | - $this->ttl = $ttl; |
|
| 47 | - } |
|
| 40 | + /** |
|
| 41 | + * @param \OCP\IMemcache $memcache |
|
| 42 | + * @param int $ttl |
|
| 43 | + */ |
|
| 44 | + public function __construct(IMemcache $memcache, int $ttl = 3600) { |
|
| 45 | + $this->memcache = $memcache; |
|
| 46 | + $this->ttl = $ttl; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - private function setTTL(string $path) { |
|
| 50 | - if ($this->memcache instanceof IMemcacheTTL) { |
|
| 51 | - $this->memcache->setTTL($path, $this->ttl); |
|
| 52 | - } |
|
| 53 | - } |
|
| 49 | + private function setTTL(string $path) { |
|
| 50 | + if ($this->memcache instanceof IMemcacheTTL) { |
|
| 51 | + $this->memcache->setTTL($path, $this->ttl); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * @param string $path |
|
| 57 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 58 | - * @return bool |
|
| 59 | - */ |
|
| 60 | - public function isLocked(string $path, int $type): bool { |
|
| 61 | - $lockValue = $this->memcache->get($path); |
|
| 62 | - if ($type === self::LOCK_SHARED) { |
|
| 63 | - return $lockValue > 0; |
|
| 64 | - } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
| 65 | - return $lockValue === 'exclusive'; |
|
| 66 | - } else { |
|
| 67 | - return false; |
|
| 68 | - } |
|
| 69 | - } |
|
| 55 | + /** |
|
| 56 | + * @param string $path |
|
| 57 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 58 | + * @return bool |
|
| 59 | + */ |
|
| 60 | + public function isLocked(string $path, int $type): bool { |
|
| 61 | + $lockValue = $this->memcache->get($path); |
|
| 62 | + if ($type === self::LOCK_SHARED) { |
|
| 63 | + return $lockValue > 0; |
|
| 64 | + } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
| 65 | + return $lockValue === 'exclusive'; |
|
| 66 | + } else { |
|
| 67 | + return false; |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * @param string $path |
|
| 73 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 74 | - * @param string $readablePath human readable path to use in error messages |
|
| 75 | - * @throws \OCP\Lock\LockedException |
|
| 76 | - */ |
|
| 77 | - public function acquireLock(string $path, int $type, string $readablePath = null) { |
|
| 78 | - if ($type === self::LOCK_SHARED) { |
|
| 79 | - if (!$this->memcache->inc($path)) { |
|
| 80 | - throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath); |
|
| 81 | - } |
|
| 82 | - } else { |
|
| 83 | - $this->memcache->add($path, 0); |
|
| 84 | - if (!$this->memcache->cas($path, 0, 'exclusive')) { |
|
| 85 | - throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - $this->setTTL($path); |
|
| 89 | - $this->markAcquire($path, $type); |
|
| 90 | - } |
|
| 71 | + /** |
|
| 72 | + * @param string $path |
|
| 73 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 74 | + * @param string $readablePath human readable path to use in error messages |
|
| 75 | + * @throws \OCP\Lock\LockedException |
|
| 76 | + */ |
|
| 77 | + public function acquireLock(string $path, int $type, string $readablePath = null) { |
|
| 78 | + if ($type === self::LOCK_SHARED) { |
|
| 79 | + if (!$this->memcache->inc($path)) { |
|
| 80 | + throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath); |
|
| 81 | + } |
|
| 82 | + } else { |
|
| 83 | + $this->memcache->add($path, 0); |
|
| 84 | + if (!$this->memcache->cas($path, 0, 'exclusive')) { |
|
| 85 | + throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + $this->setTTL($path); |
|
| 89 | + $this->markAcquire($path, $type); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * @param string $path |
|
| 94 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 95 | - */ |
|
| 96 | - public function releaseLock(string $path, int $type) { |
|
| 97 | - if ($type === self::LOCK_SHARED) { |
|
| 98 | - $ownSharedLockCount = $this->getOwnSharedLockCount($path); |
|
| 99 | - $newValue = 0; |
|
| 100 | - if ($ownSharedLockCount === 0) { // if we are not holding the lock, don't try to release it |
|
| 101 | - return; |
|
| 102 | - } |
|
| 103 | - if ($ownSharedLockCount === 1) { |
|
| 104 | - $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go |
|
| 105 | - if (!$removed) { //someone else also has a shared lock, decrease only |
|
| 106 | - $newValue = $this->memcache->dec($path); |
|
| 107 | - } |
|
| 108 | - } else { |
|
| 109 | - // if we own more than one lock ourselves just decrease |
|
| 110 | - $newValue = $this->memcache->dec($path); |
|
| 111 | - } |
|
| 92 | + /** |
|
| 93 | + * @param string $path |
|
| 94 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 95 | + */ |
|
| 96 | + public function releaseLock(string $path, int $type) { |
|
| 97 | + if ($type === self::LOCK_SHARED) { |
|
| 98 | + $ownSharedLockCount = $this->getOwnSharedLockCount($path); |
|
| 99 | + $newValue = 0; |
|
| 100 | + if ($ownSharedLockCount === 0) { // if we are not holding the lock, don't try to release it |
|
| 101 | + return; |
|
| 102 | + } |
|
| 103 | + if ($ownSharedLockCount === 1) { |
|
| 104 | + $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go |
|
| 105 | + if (!$removed) { //someone else also has a shared lock, decrease only |
|
| 106 | + $newValue = $this->memcache->dec($path); |
|
| 107 | + } |
|
| 108 | + } else { |
|
| 109 | + // if we own more than one lock ourselves just decrease |
|
| 110 | + $newValue = $this->memcache->dec($path); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - // if we somehow release more locks then exists, reset the lock |
|
| 114 | - if ($newValue < 0) { |
|
| 115 | - $this->memcache->cad($path, $newValue); |
|
| 116 | - } |
|
| 117 | - } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
| 118 | - $this->memcache->cad($path, 'exclusive'); |
|
| 119 | - } |
|
| 120 | - $this->markRelease($path, $type); |
|
| 121 | - } |
|
| 113 | + // if we somehow release more locks then exists, reset the lock |
|
| 114 | + if ($newValue < 0) { |
|
| 115 | + $this->memcache->cad($path, $newValue); |
|
| 116 | + } |
|
| 117 | + } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
| 118 | + $this->memcache->cad($path, 'exclusive'); |
|
| 119 | + } |
|
| 120 | + $this->markRelease($path, $type); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Change the type of an existing lock |
|
| 125 | - * |
|
| 126 | - * @param string $path |
|
| 127 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 128 | - * @throws \OCP\Lock\LockedException |
|
| 129 | - */ |
|
| 130 | - public function changeLock(string $path, int $targetType) { |
|
| 131 | - if ($targetType === self::LOCK_SHARED) { |
|
| 132 | - if (!$this->memcache->cas($path, 'exclusive', 1)) { |
|
| 133 | - throw new LockedException($path, null, $this->getExistingLockForException($path)); |
|
| 134 | - } |
|
| 135 | - } elseif ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 136 | - // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock |
|
| 137 | - if (!$this->memcache->cas($path, 1, 'exclusive')) { |
|
| 138 | - throw new LockedException($path, null, $this->getExistingLockForException($path)); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - $this->setTTL($path); |
|
| 142 | - $this->markChange($path, $targetType); |
|
| 143 | - } |
|
| 123 | + /** |
|
| 124 | + * Change the type of an existing lock |
|
| 125 | + * |
|
| 126 | + * @param string $path |
|
| 127 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 128 | + * @throws \OCP\Lock\LockedException |
|
| 129 | + */ |
|
| 130 | + public function changeLock(string $path, int $targetType) { |
|
| 131 | + if ($targetType === self::LOCK_SHARED) { |
|
| 132 | + if (!$this->memcache->cas($path, 'exclusive', 1)) { |
|
| 133 | + throw new LockedException($path, null, $this->getExistingLockForException($path)); |
|
| 134 | + } |
|
| 135 | + } elseif ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 136 | + // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock |
|
| 137 | + if (!$this->memcache->cas($path, 1, 'exclusive')) { |
|
| 138 | + throw new LockedException($path, null, $this->getExistingLockForException($path)); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + $this->setTTL($path); |
|
| 142 | + $this->markChange($path, $targetType); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - private function getExistingLockForException($path) { |
|
| 146 | - $existing = $this->memcache->get($path); |
|
| 147 | - if (!$existing) { |
|
| 148 | - return 'none'; |
|
| 149 | - } elseif ($existing === 'exclusive') { |
|
| 150 | - return $existing; |
|
| 151 | - } else { |
|
| 152 | - return $existing . ' shared locks'; |
|
| 153 | - } |
|
| 154 | - } |
|
| 145 | + private function getExistingLockForException($path) { |
|
| 146 | + $existing = $this->memcache->get($path); |
|
| 147 | + if (!$existing) { |
|
| 148 | + return 'none'; |
|
| 149 | + } elseif ($existing === 'exclusive') { |
|
| 150 | + return $existing; |
|
| 151 | + } else { |
|
| 152 | + return $existing . ' shared locks'; |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | 155 | } |