-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathFileLockStore.php
More file actions
189 lines (149 loc) · 4.93 KB
/
FileLockStore.php
File metadata and controls
189 lines (149 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Cache\LockStores;
use CodeIgniter\Cache\Handlers\FileHandler;
use CodeIgniter\Cache\LockStoreInterface;
use CodeIgniter\I18n\Time;
use Throwable;
class FileLockStore implements LockStoreInterface
{
public function __construct(
private readonly string $path,
private readonly int $mode,
private readonly string $prefix = '',
) {
}
public function acquireLock(string $key, string $owner, int $ttl): bool
{
return $this->withLockFile($key, static function ($handle) use ($owner, $ttl): bool {
$data = self::readLockData($handle);
$now = Time::now()->getTimestamp();
if ($data !== null && $data['expires'] > $now) {
return false;
}
return self::writeLockData($handle, $owner, $now + $ttl);
});
}
public function releaseLock(string $key, string $owner): bool
{
return $this->withLockFile($key, static function ($handle) use ($owner): bool {
$data = self::readLockData($handle);
if ($data === null || $data['owner'] !== $owner) {
return false;
}
return self::clearLockFile($handle);
});
}
public function forceReleaseLock(string $key): bool
{
return ! is_file($this->path . FileHandler::validateKey($key, $this->prefix))
|| $this->withLockFile($key, static fn ($handle): bool => self::clearLockFile($handle), false);
}
public function refreshLock(string $key, string $owner, int $ttl): bool
{
return $this->withLockFile($key, static function ($handle) use ($owner, $ttl): bool {
$data = self::readLockData($handle);
$now = Time::now()->getTimestamp();
if ($data === null || $data['owner'] !== $owner || $data['expires'] <= $now) {
return false;
}
return self::writeLockData($handle, $owner, $now + $ttl);
});
}
public function getLockOwner(string $key): ?string
{
$owner = null;
$this->withLockFile($key, static function ($handle) use (&$owner): bool {
$data = self::readLockData($handle);
if ($data === null) {
return true;
}
if ($data['expires'] <= Time::now()->getTimestamp()) {
self::clearLockFile($handle);
return true;
}
$owner = $data['owner'];
return true;
}, false);
return $owner;
}
/**
* @param callable(resource): bool $callback
*/
private function withLockFile(string $key, callable $callback, bool $create = true): bool
{
$key = FileHandler::validateKey($key, $this->prefix);
$handle = @fopen($this->path . $key, $create ? 'c+b' : 'r+b');
if ($handle === false) {
return false;
}
try {
if (! flock($handle, LOCK_EX)) {
return false;
}
return $callback($handle);
} finally {
flock($handle, LOCK_UN);
fclose($handle);
if (is_file($this->path . $key)) {
try {
chmod($this->path . $key, $this->mode);
} catch (Throwable $e) {
log_message('debug', 'Failed to set mode on cache lock file: ' . $e);
}
}
}
}
/**
* @param resource $handle
*
* @return array{owner: string, expires: int}|null
*/
private static function readLockData($handle): ?array
{
rewind($handle);
$content = stream_get_contents($handle);
if ($content === false || $content === '') {
return null;
}
try {
$data = unserialize($content);
} catch (Throwable) {
return null;
}
if (! is_array($data) || ! isset($data['owner'], $data['expires']) || ! is_string($data['owner']) || ! is_int($data['expires'])) {
return null;
}
return $data;
}
/**
* @param resource $handle
*/
private static function writeLockData($handle, string $owner, int $expires): bool
{
rewind($handle);
if (! ftruncate($handle, 0)) {
return false;
}
if (fwrite($handle, serialize(['owner' => $owner, 'expires' => $expires])) === false) {
return false;
}
return fflush($handle);
}
/**
* @param resource $handle
*/
private static function clearLockFile($handle): bool
{
rewind($handle);
return ftruncate($handle, 0) && fflush($handle);
}
}