@@ -1,5 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 5 | 5 | * |
@@ -31,51 +31,51 @@ |
||
| 31 | 31 | * @since 8.1.0 |
| 32 | 32 | */ |
| 33 | 33 | interface ILockingProvider { |
| 34 | - /** |
|
| 35 | - * @since 8.1.0 |
|
| 36 | - */ |
|
| 37 | - const LOCK_SHARED = 1; |
|
| 38 | - /** |
|
| 39 | - * @since 8.1.0 |
|
| 40 | - */ |
|
| 41 | - const LOCK_EXCLUSIVE = 2; |
|
| 34 | + /** |
|
| 35 | + * @since 8.1.0 |
|
| 36 | + */ |
|
| 37 | + const LOCK_SHARED = 1; |
|
| 38 | + /** |
|
| 39 | + * @since 8.1.0 |
|
| 40 | + */ |
|
| 41 | + const LOCK_EXCLUSIVE = 2; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param string $path |
|
| 45 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 46 | - * @return bool |
|
| 47 | - * @since 8.1.0 |
|
| 48 | - */ |
|
| 49 | - public function isLocked(string $path, int $type): bool; |
|
| 43 | + /** |
|
| 44 | + * @param string $path |
|
| 45 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 46 | + * @return bool |
|
| 47 | + * @since 8.1.0 |
|
| 48 | + */ |
|
| 49 | + public function isLocked(string $path, int $type): bool; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param string $path |
|
| 53 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 54 | - * @throws \OCP\Lock\LockedException |
|
| 55 | - * @since 8.1.0 |
|
| 56 | - */ |
|
| 57 | - public function acquireLock(string $path, int $type); |
|
| 51 | + /** |
|
| 52 | + * @param string $path |
|
| 53 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 54 | + * @throws \OCP\Lock\LockedException |
|
| 55 | + * @since 8.1.0 |
|
| 56 | + */ |
|
| 57 | + public function acquireLock(string $path, int $type); |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * @param string $path |
|
| 61 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 62 | - * @since 8.1.0 |
|
| 63 | - */ |
|
| 64 | - public function releaseLock(string $path, int $type); |
|
| 59 | + /** |
|
| 60 | + * @param string $path |
|
| 61 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 62 | + * @since 8.1.0 |
|
| 63 | + */ |
|
| 64 | + public function releaseLock(string $path, int $type); |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Change the type of an existing lock |
|
| 68 | - * |
|
| 69 | - * @param string $path |
|
| 70 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 71 | - * @throws \OCP\Lock\LockedException |
|
| 72 | - * @since 8.1.0 |
|
| 73 | - */ |
|
| 74 | - public function changeLock(string $path, int $targetType); |
|
| 66 | + /** |
|
| 67 | + * Change the type of an existing lock |
|
| 68 | + * |
|
| 69 | + * @param string $path |
|
| 70 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 71 | + * @throws \OCP\Lock\LockedException |
|
| 72 | + * @since 8.1.0 |
|
| 73 | + */ |
|
| 74 | + public function changeLock(string $path, int $targetType); |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * release all lock acquired by this instance |
|
| 78 | - * @since 8.1.0 |
|
| 79 | - */ |
|
| 80 | - public function releaseAll(); |
|
| 76 | + /** |
|
| 77 | + * release all lock acquired by this instance |
|
| 78 | + * @since 8.1.0 |
|
| 79 | + */ |
|
| 80 | + public function releaseAll(); |
|
| 81 | 81 | } |
@@ -1,5 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 5 | 5 | * |
@@ -28,102 +28,102 @@ |
||
| 28 | 28 | use OCP\IMemcache; |
| 29 | 29 | |
| 30 | 30 | class MemcacheLockingProvider extends AbstractLockingProvider { |
| 31 | - /** |
|
| 32 | - * @var \OCP\IMemcache |
|
| 33 | - */ |
|
| 34 | - private $memcache; |
|
| 31 | + /** |
|
| 32 | + * @var \OCP\IMemcache |
|
| 33 | + */ |
|
| 34 | + private $memcache; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param \OCP\IMemcache $memcache |
|
| 38 | - * @param int $ttl |
|
| 39 | - */ |
|
| 40 | - public function __construct(IMemcache $memcache, int $ttl = 3600) { |
|
| 41 | - $this->memcache = $memcache; |
|
| 42 | - $this->ttl = $ttl; |
|
| 43 | - } |
|
| 36 | + /** |
|
| 37 | + * @param \OCP\IMemcache $memcache |
|
| 38 | + * @param int $ttl |
|
| 39 | + */ |
|
| 40 | + public function __construct(IMemcache $memcache, int $ttl = 3600) { |
|
| 41 | + $this->memcache = $memcache; |
|
| 42 | + $this->ttl = $ttl; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - private function setTTL(string $path) { |
|
| 46 | - if ($this->memcache instanceof IMemcacheTTL) { |
|
| 47 | - $this->memcache->setTTL($path, $this->ttl); |
|
| 48 | - } |
|
| 49 | - } |
|
| 45 | + private function setTTL(string $path) { |
|
| 46 | + if ($this->memcache instanceof IMemcacheTTL) { |
|
| 47 | + $this->memcache->setTTL($path, $this->ttl); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param string $path |
|
| 53 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 54 | - * @return bool |
|
| 55 | - */ |
|
| 56 | - public function isLocked(string $path, int $type): bool { |
|
| 57 | - $lockValue = $this->memcache->get($path); |
|
| 58 | - if ($type === self::LOCK_SHARED) { |
|
| 59 | - return $lockValue > 0; |
|
| 60 | - } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 61 | - return $lockValue === 'exclusive'; |
|
| 62 | - } else { |
|
| 63 | - return false; |
|
| 64 | - } |
|
| 65 | - } |
|
| 51 | + /** |
|
| 52 | + * @param string $path |
|
| 53 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 54 | + * @return bool |
|
| 55 | + */ |
|
| 56 | + public function isLocked(string $path, int $type): bool { |
|
| 57 | + $lockValue = $this->memcache->get($path); |
|
| 58 | + if ($type === self::LOCK_SHARED) { |
|
| 59 | + return $lockValue > 0; |
|
| 60 | + } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 61 | + return $lockValue === 'exclusive'; |
|
| 62 | + } else { |
|
| 63 | + return false; |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @param string $path |
|
| 69 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 70 | - * @throws \OCP\Lock\LockedException |
|
| 71 | - */ |
|
| 72 | - public function acquireLock(string $path, int $type) { |
|
| 73 | - if ($type === self::LOCK_SHARED) { |
|
| 74 | - if (!$this->memcache->inc($path)) { |
|
| 75 | - throw new LockedException($path); |
|
| 76 | - } |
|
| 77 | - } else { |
|
| 78 | - $this->memcache->add($path, 0); |
|
| 79 | - if (!$this->memcache->cas($path, 0, 'exclusive')) { |
|
| 80 | - throw new LockedException($path); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - $this->setTTL($path); |
|
| 84 | - $this->markAcquire($path, $type); |
|
| 85 | - } |
|
| 67 | + /** |
|
| 68 | + * @param string $path |
|
| 69 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 70 | + * @throws \OCP\Lock\LockedException |
|
| 71 | + */ |
|
| 72 | + public function acquireLock(string $path, int $type) { |
|
| 73 | + if ($type === self::LOCK_SHARED) { |
|
| 74 | + if (!$this->memcache->inc($path)) { |
|
| 75 | + throw new LockedException($path); |
|
| 76 | + } |
|
| 77 | + } else { |
|
| 78 | + $this->memcache->add($path, 0); |
|
| 79 | + if (!$this->memcache->cas($path, 0, 'exclusive')) { |
|
| 80 | + throw new LockedException($path); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + $this->setTTL($path); |
|
| 84 | + $this->markAcquire($path, $type); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * @param string $path |
|
| 89 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 90 | - */ |
|
| 91 | - public function releaseLock(string $path, int $type) { |
|
| 92 | - if ($type === self::LOCK_SHARED) { |
|
| 93 | - if ($this->getOwnSharedLockCount($path) === 1) { |
|
| 94 | - $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go |
|
| 95 | - if (!$removed) { //someone else also has a shared lock, decrease only |
|
| 96 | - $this->memcache->dec($path); |
|
| 97 | - } |
|
| 98 | - } else { |
|
| 99 | - // if we own more than one lock ourselves just decrease |
|
| 100 | - $this->memcache->dec($path); |
|
| 101 | - } |
|
| 102 | - } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 103 | - $this->memcache->cad($path, 'exclusive'); |
|
| 104 | - } |
|
| 105 | - $this->markRelease($path, $type); |
|
| 106 | - } |
|
| 87 | + /** |
|
| 88 | + * @param string $path |
|
| 89 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 90 | + */ |
|
| 91 | + public function releaseLock(string $path, int $type) { |
|
| 92 | + if ($type === self::LOCK_SHARED) { |
|
| 93 | + if ($this->getOwnSharedLockCount($path) === 1) { |
|
| 94 | + $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go |
|
| 95 | + if (!$removed) { //someone else also has a shared lock, decrease only |
|
| 96 | + $this->memcache->dec($path); |
|
| 97 | + } |
|
| 98 | + } else { |
|
| 99 | + // if we own more than one lock ourselves just decrease |
|
| 100 | + $this->memcache->dec($path); |
|
| 101 | + } |
|
| 102 | + } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 103 | + $this->memcache->cad($path, 'exclusive'); |
|
| 104 | + } |
|
| 105 | + $this->markRelease($path, $type); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Change the type of an existing lock |
|
| 110 | - * |
|
| 111 | - * @param string $path |
|
| 112 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 113 | - * @throws \OCP\Lock\LockedException |
|
| 114 | - */ |
|
| 115 | - public function changeLock(string $path, int $targetType) { |
|
| 116 | - if ($targetType === self::LOCK_SHARED) { |
|
| 117 | - if (!$this->memcache->cas($path, 'exclusive', 1)) { |
|
| 118 | - throw new LockedException($path); |
|
| 119 | - } |
|
| 120 | - } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 121 | - // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock |
|
| 122 | - if (!$this->memcache->cas($path, 1, 'exclusive')) { |
|
| 123 | - throw new LockedException($path); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - $this->setTTL($path); |
|
| 127 | - $this->markChange($path, $targetType); |
|
| 128 | - } |
|
| 108 | + /** |
|
| 109 | + * Change the type of an existing lock |
|
| 110 | + * |
|
| 111 | + * @param string $path |
|
| 112 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 113 | + * @throws \OCP\Lock\LockedException |
|
| 114 | + */ |
|
| 115 | + public function changeLock(string $path, int $targetType) { |
|
| 116 | + if ($targetType === self::LOCK_SHARED) { |
|
| 117 | + if (!$this->memcache->cas($path, 'exclusive', 1)) { |
|
| 118 | + throw new LockedException($path); |
|
| 119 | + } |
|
| 120 | + } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 121 | + // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock |
|
| 122 | + if (!$this->memcache->cas($path, 1, 'exclusive')) { |
|
| 123 | + throw new LockedException($path); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + $this->setTTL($path); |
|
| 127 | + $this->markChange($path, $targetType); |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -1,5 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 5 | 5 | * |
@@ -30,100 +30,100 @@ |
||
| 30 | 30 | * to release any left over locks at the end of the request |
| 31 | 31 | */ |
| 32 | 32 | abstract class AbstractLockingProvider implements ILockingProvider { |
| 33 | - /** @var int $ttl */ |
|
| 34 | - protected $ttl; // how long until we clear stray locks in seconds |
|
| 33 | + /** @var int $ttl */ |
|
| 34 | + protected $ttl; // how long until we clear stray locks in seconds |
|
| 35 | 35 | |
| 36 | - protected $acquiredLocks = [ |
|
| 37 | - 'shared' => [], |
|
| 38 | - 'exclusive' => [] |
|
| 39 | - ]; |
|
| 36 | + protected $acquiredLocks = [ |
|
| 37 | + 'shared' => [], |
|
| 38 | + 'exclusive' => [] |
|
| 39 | + ]; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Check if we've locally acquired a lock |
|
| 43 | - * |
|
| 44 | - * @param string $path |
|
| 45 | - * @param int $type |
|
| 46 | - * @return bool |
|
| 47 | - */ |
|
| 48 | - protected function hasAcquiredLock(string $path, int $type): bool { |
|
| 49 | - if ($type === self::LOCK_SHARED) { |
|
| 50 | - return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0; |
|
| 51 | - } else { |
|
| 52 | - return isset($this->acquiredLocks['exclusive'][$path]) && $this->acquiredLocks['exclusive'][$path] === true; |
|
| 53 | - } |
|
| 54 | - } |
|
| 41 | + /** |
|
| 42 | + * Check if we've locally acquired a lock |
|
| 43 | + * |
|
| 44 | + * @param string $path |
|
| 45 | + * @param int $type |
|
| 46 | + * @return bool |
|
| 47 | + */ |
|
| 48 | + protected function hasAcquiredLock(string $path, int $type): bool { |
|
| 49 | + if ($type === self::LOCK_SHARED) { |
|
| 50 | + return isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 0; |
|
| 51 | + } else { |
|
| 52 | + return isset($this->acquiredLocks['exclusive'][$path]) && $this->acquiredLocks['exclusive'][$path] === true; |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Mark a locally acquired lock |
|
| 58 | - * |
|
| 59 | - * @param string $path |
|
| 60 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 61 | - */ |
|
| 62 | - protected function markAcquire(string $path, int $type) { |
|
| 63 | - if ($type === self::LOCK_SHARED) { |
|
| 64 | - if (!isset($this->acquiredLocks['shared'][$path])) { |
|
| 65 | - $this->acquiredLocks['shared'][$path] = 0; |
|
| 66 | - } |
|
| 67 | - $this->acquiredLocks['shared'][$path]++; |
|
| 68 | - } else { |
|
| 69 | - $this->acquiredLocks['exclusive'][$path] = true; |
|
| 70 | - } |
|
| 71 | - } |
|
| 56 | + /** |
|
| 57 | + * Mark a locally acquired lock |
|
| 58 | + * |
|
| 59 | + * @param string $path |
|
| 60 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 61 | + */ |
|
| 62 | + protected function markAcquire(string $path, int $type) { |
|
| 63 | + if ($type === self::LOCK_SHARED) { |
|
| 64 | + if (!isset($this->acquiredLocks['shared'][$path])) { |
|
| 65 | + $this->acquiredLocks['shared'][$path] = 0; |
|
| 66 | + } |
|
| 67 | + $this->acquiredLocks['shared'][$path]++; |
|
| 68 | + } else { |
|
| 69 | + $this->acquiredLocks['exclusive'][$path] = true; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Mark a release of a locally acquired lock |
|
| 75 | - * |
|
| 76 | - * @param string $path |
|
| 77 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 78 | - */ |
|
| 79 | - protected function markRelease(string $path, int $type) { |
|
| 80 | - if ($type === self::LOCK_SHARED) { |
|
| 81 | - if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) { |
|
| 82 | - $this->acquiredLocks['shared'][$path]--; |
|
| 83 | - if ($this->acquiredLocks['shared'][$path] === 0) { |
|
| 84 | - unset($this->acquiredLocks['shared'][$path]); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 88 | - unset($this->acquiredLocks['exclusive'][$path]); |
|
| 89 | - } |
|
| 90 | - } |
|
| 73 | + /** |
|
| 74 | + * Mark a release of a locally acquired lock |
|
| 75 | + * |
|
| 76 | + * @param string $path |
|
| 77 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 78 | + */ |
|
| 79 | + protected function markRelease(string $path, int $type) { |
|
| 80 | + if ($type === self::LOCK_SHARED) { |
|
| 81 | + if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) { |
|
| 82 | + $this->acquiredLocks['shared'][$path]--; |
|
| 83 | + if ($this->acquiredLocks['shared'][$path] === 0) { |
|
| 84 | + unset($this->acquiredLocks['shared'][$path]); |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 88 | + unset($this->acquiredLocks['exclusive'][$path]); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Change the type of an existing tracked lock |
|
| 94 | - * |
|
| 95 | - * @param string $path |
|
| 96 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 97 | - */ |
|
| 98 | - protected function markChange(string $path, int $targetType) { |
|
| 99 | - if ($targetType === self::LOCK_SHARED) { |
|
| 100 | - unset($this->acquiredLocks['exclusive'][$path]); |
|
| 101 | - if (!isset($this->acquiredLocks['shared'][$path])) { |
|
| 102 | - $this->acquiredLocks['shared'][$path] = 0; |
|
| 103 | - } |
|
| 104 | - $this->acquiredLocks['shared'][$path]++; |
|
| 105 | - } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 106 | - $this->acquiredLocks['exclusive'][$path] = true; |
|
| 107 | - $this->acquiredLocks['shared'][$path]--; |
|
| 108 | - } |
|
| 109 | - } |
|
| 92 | + /** |
|
| 93 | + * Change the type of an existing tracked lock |
|
| 94 | + * |
|
| 95 | + * @param string $path |
|
| 96 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 97 | + */ |
|
| 98 | + protected function markChange(string $path, int $targetType) { |
|
| 99 | + if ($targetType === self::LOCK_SHARED) { |
|
| 100 | + unset($this->acquiredLocks['exclusive'][$path]); |
|
| 101 | + if (!isset($this->acquiredLocks['shared'][$path])) { |
|
| 102 | + $this->acquiredLocks['shared'][$path] = 0; |
|
| 103 | + } |
|
| 104 | + $this->acquiredLocks['shared'][$path]++; |
|
| 105 | + } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 106 | + $this->acquiredLocks['exclusive'][$path] = true; |
|
| 107 | + $this->acquiredLocks['shared'][$path]--; |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * release all lock acquired by this instance which were marked using the mark* methods |
|
| 113 | - */ |
|
| 114 | - public function releaseAll() { |
|
| 115 | - foreach ($this->acquiredLocks['shared'] as $path => $count) { |
|
| 116 | - for ($i = 0; $i < $count; $i++) { |
|
| 117 | - $this->releaseLock($path, self::LOCK_SHARED); |
|
| 118 | - } |
|
| 119 | - } |
|
| 111 | + /** |
|
| 112 | + * release all lock acquired by this instance which were marked using the mark* methods |
|
| 113 | + */ |
|
| 114 | + public function releaseAll() { |
|
| 115 | + foreach ($this->acquiredLocks['shared'] as $path => $count) { |
|
| 116 | + for ($i = 0; $i < $count; $i++) { |
|
| 117 | + $this->releaseLock($path, self::LOCK_SHARED); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) { |
|
| 122 | - $this->releaseLock($path, self::LOCK_EXCLUSIVE); |
|
| 123 | - } |
|
| 124 | - } |
|
| 121 | + foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) { |
|
| 122 | + $this->releaseLock($path, self::LOCK_EXCLUSIVE); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | - protected function getOwnSharedLockCount(string $path) { |
|
| 127 | - return isset($this->acquiredLocks['shared'][$path]) ? $this->acquiredLocks['shared'][$path] : 0; |
|
| 128 | - } |
|
| 126 | + protected function getOwnSharedLockCount(string $path) { |
|
| 127 | + return isset($this->acquiredLocks['shared'][$path]) ? $this->acquiredLocks['shared'][$path] : 0; |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -1,5 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 5 | 5 | * |
@@ -33,38 +33,38 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class NoopLockingProvider implements ILockingProvider { |
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * {@inheritdoc} |
|
| 38 | - */ |
|
| 39 | - public function isLocked(string $path, int $type): bool { |
|
| 40 | - return false; |
|
| 41 | - } |
|
| 36 | + /** |
|
| 37 | + * {@inheritdoc} |
|
| 38 | + */ |
|
| 39 | + public function isLocked(string $path, int $type): bool { |
|
| 40 | + return false; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * {@inheritdoc} |
|
| 45 | - */ |
|
| 46 | - public function acquireLock(string $path, int $type) { |
|
| 47 | - // do nothing |
|
| 48 | - } |
|
| 43 | + /** |
|
| 44 | + * {@inheritdoc} |
|
| 45 | + */ |
|
| 46 | + public function acquireLock(string $path, int $type) { |
|
| 47 | + // do nothing |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * {@inheritdoc} |
|
| 52 | - */ |
|
| 53 | - public function releaseLock(string $path, int $type) { |
|
| 54 | - // do nothing |
|
| 55 | - } |
|
| 50 | + /** |
|
| 51 | + * {@inheritdoc} |
|
| 52 | + */ |
|
| 53 | + public function releaseLock(string $path, int $type) { |
|
| 54 | + // do nothing |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /**1 |
|
| 57 | + /**1 |
|
| 58 | 58 | * {@inheritdoc} |
| 59 | 59 | */ |
| 60 | - public function releaseAll() { |
|
| 61 | - // do nothing |
|
| 62 | - } |
|
| 60 | + public function releaseAll() { |
|
| 61 | + // do nothing |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * {@inheritdoc} |
|
| 66 | - */ |
|
| 67 | - public function changeLock(string $path, int $targetType) { |
|
| 68 | - // do nothing |
|
| 69 | - } |
|
| 64 | + /** |
|
| 65 | + * {@inheritdoc} |
|
| 66 | + */ |
|
| 67 | + public function changeLock(string $path, int $targetType) { |
|
| 68 | + // do nothing |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -38,246 +38,246 @@ |
||
| 38 | 38 | * Locking provider that stores the locks in the database |
| 39 | 39 | */ |
| 40 | 40 | class DBLockingProvider extends AbstractLockingProvider { |
| 41 | - /** |
|
| 42 | - * @var \OCP\IDBConnection |
|
| 43 | - */ |
|
| 44 | - private $connection; |
|
| 41 | + /** |
|
| 42 | + * @var \OCP\IDBConnection |
|
| 43 | + */ |
|
| 44 | + private $connection; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @var \OCP\ILogger |
|
| 48 | - */ |
|
| 49 | - private $logger; |
|
| 46 | + /** |
|
| 47 | + * @var \OCP\ILogger |
|
| 48 | + */ |
|
| 49 | + private $logger; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @var \OCP\AppFramework\Utility\ITimeFactory |
|
| 53 | - */ |
|
| 54 | - private $timeFactory; |
|
| 51 | + /** |
|
| 52 | + * @var \OCP\AppFramework\Utility\ITimeFactory |
|
| 53 | + */ |
|
| 54 | + private $timeFactory; |
|
| 55 | 55 | |
| 56 | - private $sharedLocks = []; |
|
| 56 | + private $sharedLocks = []; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Check if we have an open shared lock for a path |
|
| 60 | - * |
|
| 61 | - * @param string $path |
|
| 62 | - * @return bool |
|
| 63 | - */ |
|
| 64 | - protected function isLocallyLocked(string $path): bool { |
|
| 65 | - return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * Check if we have an open shared lock for a path |
|
| 60 | + * |
|
| 61 | + * @param string $path |
|
| 62 | + * @return bool |
|
| 63 | + */ |
|
| 64 | + protected function isLocallyLocked(string $path): bool { |
|
| 65 | + return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Mark a locally acquired lock |
|
| 70 | - * |
|
| 71 | - * @param string $path |
|
| 72 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 73 | - */ |
|
| 74 | - protected function markAcquire(string $path, int $type) { |
|
| 75 | - parent::markAcquire($path, $type); |
|
| 76 | - if ($type === self::LOCK_SHARED) { |
|
| 77 | - $this->sharedLocks[$path] = true; |
|
| 78 | - } |
|
| 79 | - } |
|
| 68 | + /** |
|
| 69 | + * Mark a locally acquired lock |
|
| 70 | + * |
|
| 71 | + * @param string $path |
|
| 72 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 73 | + */ |
|
| 74 | + protected function markAcquire(string $path, int $type) { |
|
| 75 | + parent::markAcquire($path, $type); |
|
| 76 | + if ($type === self::LOCK_SHARED) { |
|
| 77 | + $this->sharedLocks[$path] = true; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Change the type of an existing tracked lock |
|
| 83 | - * |
|
| 84 | - * @param string $path |
|
| 85 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 86 | - */ |
|
| 87 | - protected function markChange(string $path, int $targetType) { |
|
| 88 | - parent::markChange($path, $targetType); |
|
| 89 | - if ($targetType === self::LOCK_SHARED) { |
|
| 90 | - $this->sharedLocks[$path] = true; |
|
| 91 | - } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 92 | - $this->sharedLocks[$path] = false; |
|
| 93 | - } |
|
| 94 | - } |
|
| 81 | + /** |
|
| 82 | + * Change the type of an existing tracked lock |
|
| 83 | + * |
|
| 84 | + * @param string $path |
|
| 85 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 86 | + */ |
|
| 87 | + protected function markChange(string $path, int $targetType) { |
|
| 88 | + parent::markChange($path, $targetType); |
|
| 89 | + if ($targetType === self::LOCK_SHARED) { |
|
| 90 | + $this->sharedLocks[$path] = true; |
|
| 91 | + } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 92 | + $this->sharedLocks[$path] = false; |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * @param \OCP\IDBConnection $connection |
|
| 98 | - * @param \OCP\ILogger $logger |
|
| 99 | - * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
| 100 | - * @param int $ttl |
|
| 101 | - */ |
|
| 102 | - public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory, int $ttl = 3600) { |
|
| 103 | - $this->connection = $connection; |
|
| 104 | - $this->logger = $logger; |
|
| 105 | - $this->timeFactory = $timeFactory; |
|
| 106 | - $this->ttl = $ttl; |
|
| 107 | - } |
|
| 96 | + /** |
|
| 97 | + * @param \OCP\IDBConnection $connection |
|
| 98 | + * @param \OCP\ILogger $logger |
|
| 99 | + * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
| 100 | + * @param int $ttl |
|
| 101 | + */ |
|
| 102 | + public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory, int $ttl = 3600) { |
|
| 103 | + $this->connection = $connection; |
|
| 104 | + $this->logger = $logger; |
|
| 105 | + $this->timeFactory = $timeFactory; |
|
| 106 | + $this->ttl = $ttl; |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Insert a file locking row if it does not exists. |
|
| 111 | - * |
|
| 112 | - * @param string $path |
|
| 113 | - * @param int $lock |
|
| 114 | - * @return int number of inserted rows |
|
| 115 | - */ |
|
| 109 | + /** |
|
| 110 | + * Insert a file locking row if it does not exists. |
|
| 111 | + * |
|
| 112 | + * @param string $path |
|
| 113 | + * @param int $lock |
|
| 114 | + * @return int number of inserted rows |
|
| 115 | + */ |
|
| 116 | 116 | |
| 117 | - protected function initLockField(string $path, int $lock = 0): int { |
|
| 118 | - $expire = $this->getExpireTime(); |
|
| 119 | - return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']); |
|
| 120 | - } |
|
| 117 | + protected function initLockField(string $path, int $lock = 0): int { |
|
| 118 | + $expire = $this->getExpireTime(); |
|
| 119 | + return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * @return int |
|
| 124 | - */ |
|
| 125 | - protected function getExpireTime(): int { |
|
| 126 | - return $this->timeFactory->getTime() + $this->ttl; |
|
| 127 | - } |
|
| 122 | + /** |
|
| 123 | + * @return int |
|
| 124 | + */ |
|
| 125 | + protected function getExpireTime(): int { |
|
| 126 | + return $this->timeFactory->getTime() + $this->ttl; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * @param string $path |
|
| 131 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 132 | - * @return bool |
|
| 133 | - */ |
|
| 134 | - public function isLocked(string $path, int $type): bool { |
|
| 135 | - if ($this->hasAcquiredLock($path, $type)) { |
|
| 136 | - return true; |
|
| 137 | - } |
|
| 138 | - $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
|
| 139 | - $query->execute([$path]); |
|
| 140 | - $lockValue = (int)$query->fetchColumn(); |
|
| 141 | - if ($type === self::LOCK_SHARED) { |
|
| 142 | - if ($this->isLocallyLocked($path)) { |
|
| 143 | - // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
| 144 | - return $lockValue > 1; |
|
| 145 | - } else { |
|
| 146 | - return $lockValue > 0; |
|
| 147 | - } |
|
| 148 | - } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 149 | - return $lockValue === -1; |
|
| 150 | - } else { |
|
| 151 | - return false; |
|
| 152 | - } |
|
| 153 | - } |
|
| 129 | + /** |
|
| 130 | + * @param string $path |
|
| 131 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 132 | + * @return bool |
|
| 133 | + */ |
|
| 134 | + public function isLocked(string $path, int $type): bool { |
|
| 135 | + if ($this->hasAcquiredLock($path, $type)) { |
|
| 136 | + return true; |
|
| 137 | + } |
|
| 138 | + $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
|
| 139 | + $query->execute([$path]); |
|
| 140 | + $lockValue = (int)$query->fetchColumn(); |
|
| 141 | + if ($type === self::LOCK_SHARED) { |
|
| 142 | + if ($this->isLocallyLocked($path)) { |
|
| 143 | + // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
| 144 | + return $lockValue > 1; |
|
| 145 | + } else { |
|
| 146 | + return $lockValue > 0; |
|
| 147 | + } |
|
| 148 | + } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 149 | + return $lockValue === -1; |
|
| 150 | + } else { |
|
| 151 | + return false; |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * @param string $path |
|
| 157 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 158 | - * @throws \OCP\Lock\LockedException |
|
| 159 | - */ |
|
| 160 | - public function acquireLock(string $path, int $type) { |
|
| 161 | - $expire = $this->getExpireTime(); |
|
| 162 | - if ($type === self::LOCK_SHARED) { |
|
| 163 | - if (!$this->isLocallyLocked($path)) { |
|
| 164 | - $result = $this->initLockField($path, 1); |
|
| 165 | - if ($result <= 0) { |
|
| 166 | - $result = $this->connection->executeUpdate( |
|
| 167 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` + 1, `ttl` = ? WHERE `key` = ? AND `lock` >= 0', |
|
| 168 | - [$expire, $path] |
|
| 169 | - ); |
|
| 170 | - } |
|
| 171 | - } else { |
|
| 172 | - $result = 1; |
|
| 173 | - } |
|
| 174 | - } else { |
|
| 175 | - $existing = 0; |
|
| 176 | - if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
| 177 | - $existing = 1; |
|
| 178 | - } |
|
| 179 | - $result = $this->initLockField($path, -1); |
|
| 180 | - if ($result <= 0) { |
|
| 181 | - $result = $this->connection->executeUpdate( |
|
| 182 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = ?', |
|
| 183 | - [$expire, $path, $existing] |
|
| 184 | - ); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - if ($result !== 1) { |
|
| 188 | - throw new LockedException($path); |
|
| 189 | - } |
|
| 190 | - $this->markAcquire($path, $type); |
|
| 191 | - } |
|
| 155 | + /** |
|
| 156 | + * @param string $path |
|
| 157 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 158 | + * @throws \OCP\Lock\LockedException |
|
| 159 | + */ |
|
| 160 | + public function acquireLock(string $path, int $type) { |
|
| 161 | + $expire = $this->getExpireTime(); |
|
| 162 | + if ($type === self::LOCK_SHARED) { |
|
| 163 | + if (!$this->isLocallyLocked($path)) { |
|
| 164 | + $result = $this->initLockField($path, 1); |
|
| 165 | + if ($result <= 0) { |
|
| 166 | + $result = $this->connection->executeUpdate( |
|
| 167 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` + 1, `ttl` = ? WHERE `key` = ? AND `lock` >= 0', |
|
| 168 | + [$expire, $path] |
|
| 169 | + ); |
|
| 170 | + } |
|
| 171 | + } else { |
|
| 172 | + $result = 1; |
|
| 173 | + } |
|
| 174 | + } else { |
|
| 175 | + $existing = 0; |
|
| 176 | + if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
| 177 | + $existing = 1; |
|
| 178 | + } |
|
| 179 | + $result = $this->initLockField($path, -1); |
|
| 180 | + if ($result <= 0) { |
|
| 181 | + $result = $this->connection->executeUpdate( |
|
| 182 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = ?', |
|
| 183 | + [$expire, $path, $existing] |
|
| 184 | + ); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + if ($result !== 1) { |
|
| 188 | + throw new LockedException($path); |
|
| 189 | + } |
|
| 190 | + $this->markAcquire($path, $type); |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - /** |
|
| 194 | - * @param string $path |
|
| 195 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 196 | - */ |
|
| 197 | - public function releaseLock(string $path, int $type) { |
|
| 198 | - $this->markRelease($path, $type); |
|
| 193 | + /** |
|
| 194 | + * @param string $path |
|
| 195 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 196 | + */ |
|
| 197 | + public function releaseLock(string $path, int $type) { |
|
| 198 | + $this->markRelease($path, $type); |
|
| 199 | 199 | |
| 200 | - // we keep shared locks till the end of the request so we can re-use them |
|
| 201 | - if ($type === self::LOCK_EXCLUSIVE) { |
|
| 202 | - $this->connection->executeUpdate( |
|
| 203 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = -1', |
|
| 204 | - [$path] |
|
| 205 | - ); |
|
| 206 | - } |
|
| 207 | - } |
|
| 200 | + // we keep shared locks till the end of the request so we can re-use them |
|
| 201 | + if ($type === self::LOCK_EXCLUSIVE) { |
|
| 202 | + $this->connection->executeUpdate( |
|
| 203 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = -1', |
|
| 204 | + [$path] |
|
| 205 | + ); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * Change the type of an existing lock |
|
| 211 | - * |
|
| 212 | - * @param string $path |
|
| 213 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 214 | - * @throws \OCP\Lock\LockedException |
|
| 215 | - */ |
|
| 216 | - public function changeLock(string $path, int $targetType) { |
|
| 217 | - $expire = $this->getExpireTime(); |
|
| 218 | - if ($targetType === self::LOCK_SHARED) { |
|
| 219 | - $result = $this->connection->executeUpdate( |
|
| 220 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = 1, `ttl` = ? WHERE `key` = ? AND `lock` = -1', |
|
| 221 | - [$expire, $path] |
|
| 222 | - ); |
|
| 223 | - } else { |
|
| 224 | - // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
| 225 | - if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
| 226 | - throw new LockedException($path); |
|
| 227 | - } |
|
| 228 | - $result = $this->connection->executeUpdate( |
|
| 229 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = 1', |
|
| 230 | - [$expire, $path] |
|
| 231 | - ); |
|
| 232 | - } |
|
| 233 | - if ($result !== 1) { |
|
| 234 | - throw new LockedException($path); |
|
| 235 | - } |
|
| 236 | - $this->markChange($path, $targetType); |
|
| 237 | - } |
|
| 209 | + /** |
|
| 210 | + * Change the type of an existing lock |
|
| 211 | + * |
|
| 212 | + * @param string $path |
|
| 213 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 214 | + * @throws \OCP\Lock\LockedException |
|
| 215 | + */ |
|
| 216 | + public function changeLock(string $path, int $targetType) { |
|
| 217 | + $expire = $this->getExpireTime(); |
|
| 218 | + if ($targetType === self::LOCK_SHARED) { |
|
| 219 | + $result = $this->connection->executeUpdate( |
|
| 220 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = 1, `ttl` = ? WHERE `key` = ? AND `lock` = -1', |
|
| 221 | + [$expire, $path] |
|
| 222 | + ); |
|
| 223 | + } else { |
|
| 224 | + // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
| 225 | + if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
| 226 | + throw new LockedException($path); |
|
| 227 | + } |
|
| 228 | + $result = $this->connection->executeUpdate( |
|
| 229 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = 1', |
|
| 230 | + [$expire, $path] |
|
| 231 | + ); |
|
| 232 | + } |
|
| 233 | + if ($result !== 1) { |
|
| 234 | + throw new LockedException($path); |
|
| 235 | + } |
|
| 236 | + $this->markChange($path, $targetType); |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * cleanup empty locks |
|
| 241 | - */ |
|
| 242 | - public function cleanExpiredLocks() { |
|
| 243 | - $expire = $this->timeFactory->getTime(); |
|
| 244 | - try { |
|
| 245 | - $this->connection->executeUpdate( |
|
| 246 | - 'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?', |
|
| 247 | - [$expire] |
|
| 248 | - ); |
|
| 249 | - } catch (\Exception $e) { |
|
| 250 | - // If the table is missing, the clean up was successful |
|
| 251 | - if ($this->connection->tableExists('file_locks')) { |
|
| 252 | - throw $e; |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - } |
|
| 239 | + /** |
|
| 240 | + * cleanup empty locks |
|
| 241 | + */ |
|
| 242 | + public function cleanExpiredLocks() { |
|
| 243 | + $expire = $this->timeFactory->getTime(); |
|
| 244 | + try { |
|
| 245 | + $this->connection->executeUpdate( |
|
| 246 | + 'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?', |
|
| 247 | + [$expire] |
|
| 248 | + ); |
|
| 249 | + } catch (\Exception $e) { |
|
| 250 | + // If the table is missing, the clean up was successful |
|
| 251 | + if ($this->connection->tableExists('file_locks')) { |
|
| 252 | + throw $e; |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - /** |
|
| 258 | - * release all lock acquired by this instance which were marked using the mark* methods |
|
| 259 | - * @suppress SqlInjectionChecker |
|
| 260 | - */ |
|
| 261 | - public function releaseAll() { |
|
| 262 | - parent::releaseAll(); |
|
| 257 | + /** |
|
| 258 | + * release all lock acquired by this instance which were marked using the mark* methods |
|
| 259 | + * @suppress SqlInjectionChecker |
|
| 260 | + */ |
|
| 261 | + public function releaseAll() { |
|
| 262 | + parent::releaseAll(); |
|
| 263 | 263 | |
| 264 | - // since we keep shared locks we need to manually clean those |
|
| 265 | - $lockedPaths = array_keys($this->sharedLocks); |
|
| 266 | - $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
| 267 | - return $this->sharedLocks[$path]; |
|
| 268 | - }); |
|
| 264 | + // since we keep shared locks we need to manually clean those |
|
| 265 | + $lockedPaths = array_keys($this->sharedLocks); |
|
| 266 | + $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
| 267 | + return $this->sharedLocks[$path]; |
|
| 268 | + }); |
|
| 269 | 269 | |
| 270 | - $chunkedPaths = array_chunk($lockedPaths, 100); |
|
| 270 | + $chunkedPaths = array_chunk($lockedPaths, 100); |
|
| 271 | 271 | |
| 272 | - foreach ($chunkedPaths as $chunk) { |
|
| 273 | - $builder = $this->connection->getQueryBuilder(); |
|
| 272 | + foreach ($chunkedPaths as $chunk) { |
|
| 273 | + $builder = $this->connection->getQueryBuilder(); |
|
| 274 | 274 | |
| 275 | - $query = $builder->update('file_locks') |
|
| 276 | - ->set('lock', $builder->createFunction('`lock` -1')) |
|
| 277 | - ->where($builder->expr()->in('key', $builder->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 278 | - ->andWhere($builder->expr()->gt('lock', new Literal(0))); |
|
| 275 | + $query = $builder->update('file_locks') |
|
| 276 | + ->set('lock', $builder->createFunction('`lock` -1')) |
|
| 277 | + ->where($builder->expr()->in('key', $builder->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 278 | + ->andWhere($builder->expr()->gt('lock', new Literal(0))); |
|
| 279 | 279 | |
| 280 | - $query->execute(); |
|
| 281 | - } |
|
| 282 | - } |
|
| 280 | + $query->execute(); |
|
| 281 | + } |
|
| 282 | + } |
|
| 283 | 283 | } |
@@ -34,31 +34,31 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class LockedException extends \Exception { |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Locked path |
|
| 39 | - * |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - private $path; |
|
| 37 | + /** |
|
| 38 | + * Locked path |
|
| 39 | + * |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + private $path; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * LockedException constructor. |
|
| 46 | - * |
|
| 47 | - * @param string $path locked path |
|
| 48 | - * @param \Exception|null $previous previous exception for cascading |
|
| 49 | - * |
|
| 50 | - * @since 8.1.0 |
|
| 51 | - */ |
|
| 52 | - public function __construct(string $path, \Exception $previous = null) { |
|
| 53 | - parent::__construct('"' . $path . '" is locked', 0, $previous); |
|
| 54 | - $this->path = $path; |
|
| 55 | - } |
|
| 44 | + /** |
|
| 45 | + * LockedException constructor. |
|
| 46 | + * |
|
| 47 | + * @param string $path locked path |
|
| 48 | + * @param \Exception|null $previous previous exception for cascading |
|
| 49 | + * |
|
| 50 | + * @since 8.1.0 |
|
| 51 | + */ |
|
| 52 | + public function __construct(string $path, \Exception $previous = null) { |
|
| 53 | + parent::__construct('"' . $path . '" is locked', 0, $previous); |
|
| 54 | + $this->path = $path; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return string |
|
| 59 | - * @since 8.1.0 |
|
| 60 | - */ |
|
| 61 | - public function getPath(): string { |
|
| 62 | - return $this->path; |
|
| 63 | - } |
|
| 57 | + /** |
|
| 58 | + * @return string |
|
| 59 | + * @since 8.1.0 |
|
| 60 | + */ |
|
| 61 | + public function getPath(): string { |
|
| 62 | + return $this->path; |
|
| 63 | + } |
|
| 64 | 64 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types=1); |
|
| 2 | +declare(strict_types = 1); |
|
| 3 | 3 | /** |
| 4 | 4 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 5 | 5 | * |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * @since 8.1.0 |
| 51 | 51 | */ |
| 52 | 52 | public function __construct(string $path, \Exception $previous = null) { |
| 53 | - parent::__construct('"' . $path . '" is locked', 0, $previous); |
|
| 53 | + parent::__construct('"'.$path.'" is locked', 0, $previous); |
|
| 54 | 54 | $this->path = $path; |
| 55 | 55 | } |
| 56 | 56 | |
@@ -27,44 +27,44 @@ |
||
| 27 | 27 | use OCP\ILogger; |
| 28 | 28 | |
| 29 | 29 | class FakeDBLockingProvider extends \OC\Lock\DBLockingProvider { |
| 30 | - // Lock for 10 hours just to be sure |
|
| 31 | - const TTL = 36000; |
|
| 30 | + // Lock for 10 hours just to be sure |
|
| 31 | + const TTL = 36000; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Need a new child, because parent::connection is private instead of protected... |
|
| 35 | - * @var IDBConnection |
|
| 36 | - */ |
|
| 37 | - protected $db; |
|
| 33 | + /** |
|
| 34 | + * Need a new child, because parent::connection is private instead of protected... |
|
| 35 | + * @var IDBConnection |
|
| 36 | + */ |
|
| 37 | + protected $db; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param \OCP\IDBConnection $connection |
|
| 41 | - * @param \OCP\ILogger $logger |
|
| 42 | - * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
| 43 | - */ |
|
| 44 | - public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory) { |
|
| 45 | - parent::__construct($connection, $logger, $timeFactory); |
|
| 46 | - $this->db = $connection; |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * @param \OCP\IDBConnection $connection |
|
| 41 | + * @param \OCP\ILogger $logger |
|
| 42 | + * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
| 43 | + */ |
|
| 44 | + public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory) { |
|
| 45 | + parent::__construct($connection, $logger, $timeFactory); |
|
| 46 | + $this->db = $connection; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param string $path |
|
| 52 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 53 | - */ |
|
| 54 | - public function releaseLock(string $path, int $type) { |
|
| 55 | - // we DONT keep shared locks till the end of the request |
|
| 56 | - if ($type === self::LOCK_SHARED) { |
|
| 57 | - $this->db->executeUpdate( |
|
| 58 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = 1', |
|
| 59 | - [$path] |
|
| 60 | - ); |
|
| 61 | - } |
|
| 50 | + /** |
|
| 51 | + * @param string $path |
|
| 52 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 53 | + */ |
|
| 54 | + public function releaseLock(string $path, int $type) { |
|
| 55 | + // we DONT keep shared locks till the end of the request |
|
| 56 | + if ($type === self::LOCK_SHARED) { |
|
| 57 | + $this->db->executeUpdate( |
|
| 58 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = 1', |
|
| 59 | + [$path] |
|
| 60 | + ); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - parent::releaseLock($path, $type); |
|
| 64 | - } |
|
| 63 | + parent::releaseLock($path, $type); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - public function __destruct() { |
|
| 67 | - // Prevent cleaning up at the end of the live time. |
|
| 68 | - // parent::__destruct(); |
|
| 69 | - } |
|
| 66 | + public function __destruct() { |
|
| 67 | + // Prevent cleaning up at the end of the live time. |
|
| 68 | + // parent::__destruct(); |
|
| 69 | + } |
|
| 70 | 70 | } |