-
Notifications
You must be signed in to change notification settings - Fork 6
/
LegacyFilesystemLock.php
185 lines (157 loc) · 5.16 KB
/
LegacyFilesystemLock.php
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
<?php
namespace Enqueue\Fs;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
class LegacyFilesystemLock implements Lock
{
/**
* Array key is a destination name. String.
*
* @var LockHandler[]
*/
private $lockHandlers;
public function __construct()
{
$this->lockHandlers = [];
}
/**
* {@inheritdoc}
*/
public function lock(FsDestination $destination)
{
$lockHandler = $this->getLockHandler($destination);
if (false == $lockHandler->lock(true)) {
throw new CannotObtainLockException(sprintf('Cannot obtain the lock for destination %s', $destination->getName()));
}
}
/**
* {@inheritdoc}
*/
public function release(FsDestination $destination)
{
$lockHandler = $this->getLockHandler($destination);
$lockHandler->release();
}
public function releaseAll()
{
foreach ($this->lockHandlers as $lockHandler) {
$lockHandler->release();
}
$this->lockHandlers = [];
}
/**
* @param FsDestination $destination
*
* @return LockHandler
*/
private function getLockHandler(FsDestination $destination)
{
if (false == isset($this->lockHandlers[$destination->getName()])) {
$this->lockHandlers[$destination->getName()] = new LockHandler(
$destination->getName(),
$destination->getFileInfo()->getPath()
);
}
return $this->lockHandlers[$destination->getName()];
}
}
// symfony/lock component works only with 3.x and 4.x Symfony
// For symfony 2.x we should use LockHandler from symfony/component which was removed from 4.x
// because we cannot use both at the same time. I copied and pasted the lock handler here
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* LockHandler class provides a simple abstraction to lock anything by means of
* a file lock.
*
* A locked file is created based on the lock name when calling lock(). Other
* lock handlers will not be able to lock the same name until it is released
* (explicitly by calling release() or implicitly when the instance holding the
* lock is destroyed).
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
* @author Romain Neutron <imprec@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*
* @deprecated since version 3.4, to be removed in 4.0. Use Symfony\Component\Lock\Store\SemaphoreStore or Symfony\Component\Lock\Store\FlockStore instead.
*/
class LockHandler
{
private $file;
private $handle;
/**
* @param string $name The lock name
* @param string|null $lockPath The directory to store the lock. Default values will use temporary directory
*
* @throws IOException If the lock directory could not be created or is not writable
*/
public function __construct($name, $lockPath = null)
{
$lockPath = $lockPath ?: sys_get_temp_dir();
if (!is_dir($lockPath)) {
$fs = new Filesystem();
$fs->mkdir($lockPath);
}
if (!is_writable($lockPath)) {
throw new IOException(sprintf('The directory "%s" is not writable.', $lockPath), 0, null, $lockPath);
}
$this->file = sprintf('%s/sf.%s.%s.lock', $lockPath, preg_replace('/[^a-z0-9\._-]+/i', '-', $name), hash('sha256', $name));
}
/**
* Lock the resource.
*
* @param bool $blocking Wait until the lock is released
*
* @throws IOException If the lock file could not be created or opened
*
* @return bool Returns true if the lock was acquired, false otherwise
*/
public function lock($blocking = false)
{
if ($this->handle) {
return true;
}
$error = null;
// Silence error reporting
set_error_handler(function ($errno, $msg) use (&$error) {
$error = $msg;
});
if (!$this->handle = fopen($this->file, 'r')) {
if ($this->handle = fopen($this->file, 'x')) {
chmod($this->file, 0444);
} elseif (!$this->handle = fopen($this->file, 'r')) {
usleep(100); // Give some time for chmod() to complete
$this->handle = fopen($this->file, 'r');
}
}
restore_error_handler();
if (!$this->handle) {
throw new IOException($error, 0, null, $this->file);
}
// On Windows, even if PHP doc says the contrary, LOCK_NB works, see
// https://bugs.php.net/54129
if (!flock($this->handle, LOCK_EX | ($blocking ? 0 : LOCK_NB))) {
fclose($this->handle);
$this->handle = null;
return false;
}
return true;
}
/**
* Release the resource.
*/
public function release()
{
if ($this->handle) {
flock($this->handle, LOCK_UN | LOCK_NB);
fclose($this->handle);
$this->handle = null;
}
}
}