|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Robin Appelman <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|
6
|
|
|
* @license AGPL-3.0 |
|
7
|
|
|
* |
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
10
|
|
|
* as published by the Free Software Foundation. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OC\Lock; |
|
23
|
|
|
|
|
24
|
|
|
use OCP\IMemcacheTTL; |
|
25
|
|
|
use OCP\Lock\LockedException; |
|
26
|
|
|
use OCP\IMemcache; |
|
27
|
|
|
|
|
28
|
|
|
class MemcacheLockingProvider extends AbstractLockingProvider { |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \OCP\IMemcache |
|
31
|
|
|
*/ |
|
32
|
|
|
private $memcache; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param \OCP\IMemcache $memcache |
|
36
|
|
|
*/ |
|
37
|
23 |
|
public function __construct(IMemcache $memcache) { |
|
38
|
23 |
|
$this->memcache = $memcache; |
|
39
|
23 |
|
} |
|
40
|
|
|
|
|
41
|
894 |
|
private function setTTL($path) { |
|
42
|
894 |
|
if ($this->memcache instanceof IMemcacheTTL) { |
|
43
|
|
|
$this->memcache->setTTL($path, self::TTL); |
|
44
|
|
|
} |
|
45
|
894 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $path |
|
49
|
|
|
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
17 |
|
public function isLocked($path, $type) { |
|
53
|
17 |
|
$lockValue = $this->memcache->get($path); |
|
54
|
17 |
|
if ($type === self::LOCK_SHARED) { |
|
55
|
13 |
|
return $lockValue > 0; |
|
56
|
14 |
|
} else if ($type === self::LOCK_EXCLUSIVE) { |
|
57
|
14 |
|
return $lockValue === 'exclusive'; |
|
58
|
|
|
} else { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $path |
|
65
|
|
|
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
66
|
|
|
* @throws \OCP\Lock\LockedException |
|
67
|
|
|
*/ |
|
68
|
894 |
|
public function acquireLock($path, $type) { |
|
69
|
894 |
|
if (strlen($path) > 64) { // max length in file_locks |
|
70
|
889 |
|
throw new \InvalidArgumentException("Lock key length too long"); |
|
71
|
24 |
|
} |
|
72
|
|
|
if ($type === self::LOCK_SHARED) { |
|
73
|
887 |
|
if (!$this->memcache->inc($path)) { |
|
74
|
106 |
|
throw new LockedException($path); |
|
75
|
106 |
|
} |
|
76
|
27 |
|
} else { |
|
77
|
|
|
$this->memcache->add($path, 0); |
|
78
|
|
|
if (!$this->memcache->cas($path, 0, 'exclusive')) { |
|
79
|
894 |
|
throw new LockedException($path); |
|
80
|
894 |
|
} |
|
81
|
894 |
|
} |
|
82
|
|
|
$this->setTTL($path); |
|
83
|
|
|
$this->markAcquire($path, $type); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
881 |
|
* @param string $path |
|
88
|
881 |
|
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
89
|
880 |
|
*/ |
|
90
|
880 |
|
public function releaseLock($path, $type) { |
|
91
|
880 |
|
if ($type === self::LOCK_SHARED) { |
|
92
|
880 |
|
if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) { |
|
93
|
881 |
|
$this->memcache->dec($path); |
|
94
|
502 |
|
$this->memcache->cad($path, 0); |
|
95
|
502 |
|
} |
|
96
|
881 |
|
} else if ($type === self::LOCK_EXCLUSIVE) { |
|
97
|
881 |
|
$this->memcache->cad($path, 'exclusive'); |
|
98
|
|
|
} |
|
99
|
|
|
$this->markRelease($path, $type); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Change the type of an existing lock |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $path |
|
106
|
818 |
|
* @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
107
|
818 |
|
* @throws \OCP\Lock\LockedException |
|
108
|
813 |
|
*/ |
|
109
|
2 |
|
public function changeLock($path, $targetType) { |
|
110
|
|
|
if ($targetType === self::LOCK_SHARED) { |
|
111
|
816 |
|
if (!$this->memcache->cas($path, 'exclusive', 1)) { |
|
112
|
|
|
throw new LockedException($path); |
|
113
|
815 |
|
} |
|
114
|
6 |
|
} else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
115
|
|
|
// we can only change a shared lock to an exclusive if there's only a single owner of the shared lock |
|
116
|
812 |
|
if (!$this->memcache->cas($path, 1, 'exclusive')) { |
|
117
|
813 |
|
throw new LockedException($path); |
|
118
|
813 |
|
} |
|
119
|
813 |
|
} |
|
120
|
|
|
$this->setTTL($path); |
|
121
|
|
|
$this->markChange($path, $targetType); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|