| 1 |
<?php |
| 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 |
class FSLockManager extends LockManager { |
| 35 |
|
| 36 |
protected $lockTypeMap = [ |
| 37 |
self::LOCK_SH => self::LOCK_SH, |
| 38 |
self::LOCK_UW => self::LOCK_SH, |
| 39 |
self::LOCK_EX => self::LOCK_EX |
| 40 |
]; |
| 41 |
|
| 42 |
|
| 43 |
protected $lockDir; |
| 44 |
|
| 45 |
|
| 46 |
protected $handles = []; |
| 47 |
|
| 48 |
|
| 49 |
protected $isWindows; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __construct( array $config ) { |
| 58 |
parent::__construct( $config ); |
| 59 |
|
| 60 |
$this->lockDir = $config['lockDirectory']; |
| 61 |
$this->isWindows = ( PHP_OS_FAMILY === 'Windows' ); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
protected function doLock( array $paths, $type ) { |
| 71 |
$status = StatusValue::newGood(); |
| 72 |
|
| 73 |
$lockedPaths = []; |
| 74 |
foreach ( $paths as $path ) { |
| 75 |
$status->merge( $this->doSingleLock( $path, $type ) ); |
| 76 |
if ( $status->isOK() ) { |
| 77 |
$lockedPaths[] = $path; |
| 78 |
} else { |
| 79 |
|
| 80 |
$status->merge( $this->doUnlock( $lockedPaths, $type ) ); |
| 81 |
|
| 82 |
return $status; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $status; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
protected function doUnlock( array $paths, $type ) { |
| 96 |
$status = StatusValue::newGood(); |
| 97 |
|
| 98 |
foreach ( $paths as $path ) { |
| 99 |
$status->merge( $this->doSingleUnlock( $path, $type ) ); |
| 100 |
} |
| 101 |
|
| 102 |
return $status; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
protected function doSingleLock( $path, $type ) { |
| 113 |
$status = StatusValue::newGood(); |
| 114 |
|
| 115 |
if ( isset( $this->locksHeld[$path][$type] ) ) { |
| 116 |
++$this->locksHeld[$path][$type]; |
| 117 |
} elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) { |
| 118 |
$this->locksHeld[$path][$type] = 1; |
| 119 |
} else { |
| 120 |
if ( isset( $this->handles[$path] ) ) { |
| 121 |
$handle = $this->handles[$path]; |
| 122 |
} else { |
| 123 |
|
| 124 |
$handle = @fopen( $this->getLockPath( $path ), 'a+' ); |
| 125 |
if ( !$handle && !is_dir( $this->lockDir ) ) { |
| 126 |
|
| 127 |
|
| 128 |
if ( @mkdir( $this->lockDir, 0777, true ) ) { |
| 129 |
|
| 130 |
$handle = @fopen( $this->getLockPath( $path ), 'a+' ); |
| 131 |
} else { |
| 132 |
$this->logger->error( "Cannot create directory '{$this->lockDir}'." ); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
if ( $handle ) { |
| 137 |
|
| 138 |
$lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX; |
| 139 |
if ( flock( $handle, $lock | LOCK_NB ) ) { |
| 140 |
|
| 141 |
$this->locksHeld[$path][$type] = 1; |
| 142 |
$this->handles[$path] = $handle; |
| 143 |
} else { |
| 144 |
fclose( $handle ); |
| 145 |
$status->fatal( 'lockmanager-fail-conflict' ); |
| 146 |
} |
| 147 |
} else { |
| 148 |
$status->fatal( 'lockmanager-fail-openlock', $path ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return $status; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
protected function doSingleUnlock( $path, $type ) { |
| 163 |
$status = StatusValue::newGood(); |
| 164 |
|
| 165 |
if ( !isset( $this->locksHeld[$path] ) ) { |
| 166 |
$status->warning( 'lockmanager-notlocked', $path ); |
| 167 |
} elseif ( !isset( $this->locksHeld[$path][$type] ) ) { |
| 168 |
$status->warning( 'lockmanager-notlocked', $path ); |
| 169 |
} else { |
| 170 |
$handlesToClose = []; |
| 171 |
--$this->locksHeld[$path][$type]; |
| 172 |
if ( $this->locksHeld[$path][$type] <= 0 ) { |
| 173 |
unset( $this->locksHeld[$path][$type] ); |
| 174 |
} |
| 175 |
if ( $this->locksHeld[$path] === [] ) { |
| 176 |
unset( $this->locksHeld[$path] ); |
| 177 |
if ( isset( $this->handles[$path] ) ) { |
| 178 |
$handlesToClose[] = $this->handles[$path]; |
| 179 |
unset( $this->handles[$path] ); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
if ( $this->isWindows ) { |
| 185 |
|
| 186 |
|
| 187 |
$status->merge( $this->closeLockHandles( $path, $handlesToClose ) ); |
| 188 |
$status->merge( $this->pruneKeyLockFiles( $path ) ); |
| 189 |
} else { |
| 190 |
|
| 191 |
|
| 192 |
$status->merge( $this->pruneKeyLockFiles( $path ) ); |
| 193 |
$status->merge( $this->closeLockHandles( $path, $handlesToClose ) ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return $status; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private function closeLockHandles( $path, array $handlesToClose ) { |
| 206 |
$status = StatusValue::newGood(); |
| 207 |
foreach ( $handlesToClose as $handle ) { |
| 208 |
if ( !flock( $handle, LOCK_UN ) ) { |
| 209 |
$status->fatal( 'lockmanager-fail-releaselock', $path ); |
| 210 |
} |
| 211 |
if ( !fclose( $handle ) ) { |
| 212 |
$status->warning( 'lockmanager-fail-closelock', $path ); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
return $status; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private function pruneKeyLockFiles( $path ) { |
| 224 |
$status = StatusValue::newGood(); |
| 225 |
if ( !isset( $this->locksHeld[$path] ) ) { |
| 226 |
|
| 227 |
if ( !unlink( $this->getLockPath( $path ) ) ) { |
| 228 |
$status->warning( 'lockmanager-fail-deletelock', $path ); |
| 229 |
} |
| 230 |
unset( $this->handles[$path] ); |
| 231 |
} |
| 232 |
|
| 233 |
return $status; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
protected function getLockPath( $path ) { |
| 242 |
return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock"; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public function __destruct() { |
| 249 |
while ( count( $this->locksHeld ) ) { |
| 250 |
foreach ( $this->locksHeld as $path => $locks ) { |
| 251 |
$this->doSingleUnlock( $path, self::LOCK_EX ); |
| 252 |
$this->doSingleUnlock( $path, self::LOCK_SH ); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |