Update docs/hooks.txt for ShowSearchHitTitle
[mediawiki.git] / includes / libs / lockmanager / FSLockManager.php
blob7f33a0abdfaa4e2abf0b14f545d5f3a2c57a1841
1 <?php
2 /**
3 * Simple version of LockManager based on using FS lock files.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup LockManager
24 /**
25 * Simple version of LockManager based on using FS lock files.
26 * All locks are non-blocking, which avoids deadlocks.
28 * This should work fine for small sites running off one server.
29 * Do not use this with 'lockDirectory' set to an NFS mount unless the
30 * NFS client is at least version 2.6.12. Otherwise, the BSD flock()
31 * locks will be ignored; see http://nfs.sourceforge.net/#section_d.
33 * @ingroup LockManager
34 * @since 1.19
36 class FSLockManager extends LockManager {
37 /** @var array Mapping of lock types to the type actually used */
38 protected $lockTypeMap = [
39 self::LOCK_SH => self::LOCK_SH,
40 self::LOCK_UW => self::LOCK_SH,
41 self::LOCK_EX => self::LOCK_EX
44 /** @var string Global dir for all servers */
45 protected $lockDir;
47 /** @var array Map of (locked key => lock file handle) */
48 protected $handles = [];
50 /** @var bool */
51 protected $isWindows;
53 /**
54 * Construct a new instance from configuration.
56 * @param array $config Includes:
57 * - lockDirectory : Directory containing the lock files
59 function __construct( array $config ) {
60 parent::__construct( $config );
62 $this->lockDir = $config['lockDirectory'];
63 $this->isWindows = ( strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN' );
66 /**
67 * @see LockManager::doLock()
68 * @param array $paths
69 * @param int $type
70 * @return StatusValue
72 protected function doLock( array $paths, $type ) {
73 $status = StatusValue::newGood();
75 $lockedPaths = []; // files locked in this attempt
76 foreach ( $paths as $path ) {
77 $status->merge( $this->doSingleLock( $path, $type ) );
78 if ( $status->isOK() ) {
79 $lockedPaths[] = $path;
80 } else {
81 // Abort and unlock everything
82 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
84 return $status;
88 return $status;
91 /**
92 * @see LockManager::doUnlock()
93 * @param array $paths
94 * @param int $type
95 * @return StatusValue
97 protected function doUnlock( array $paths, $type ) {
98 $status = StatusValue::newGood();
100 foreach ( $paths as $path ) {
101 $status->merge( $this->doSingleUnlock( $path, $type ) );
104 return $status;
108 * Lock a single resource key
110 * @param string $path
111 * @param int $type
112 * @return StatusValue
114 protected function doSingleLock( $path, $type ) {
115 $status = StatusValue::newGood();
117 if ( isset( $this->locksHeld[$path][$type] ) ) {
118 ++$this->locksHeld[$path][$type];
119 } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
120 $this->locksHeld[$path][$type] = 1;
121 } else {
122 if ( isset( $this->handles[$path] ) ) {
123 $handle = $this->handles[$path];
124 } else {
125 MediaWiki\suppressWarnings();
126 $handle = fopen( $this->getLockPath( $path ), 'a+' );
127 if ( !$handle ) { // lock dir missing?
128 mkdir( $this->lockDir, 0777, true );
129 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
131 MediaWiki\restoreWarnings();
133 if ( $handle ) {
134 // Either a shared or exclusive lock
135 $lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX;
136 if ( flock( $handle, $lock | LOCK_NB ) ) {
137 // Record this lock as active
138 $this->locksHeld[$path][$type] = 1;
139 $this->handles[$path] = $handle;
140 } else {
141 fclose( $handle );
142 $status->fatal( 'lockmanager-fail-acquirelock', $path );
144 } else {
145 $status->fatal( 'lockmanager-fail-openlock', $path );
149 return $status;
153 * Unlock a single resource key
155 * @param string $path
156 * @param int $type
157 * @return StatusValue
159 protected function doSingleUnlock( $path, $type ) {
160 $status = StatusValue::newGood();
162 if ( !isset( $this->locksHeld[$path] ) ) {
163 $status->warning( 'lockmanager-notlocked', $path );
164 } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
165 $status->warning( 'lockmanager-notlocked', $path );
166 } else {
167 $handlesToClose = [];
168 --$this->locksHeld[$path][$type];
169 if ( $this->locksHeld[$path][$type] <= 0 ) {
170 unset( $this->locksHeld[$path][$type] );
172 if ( !count( $this->locksHeld[$path] ) ) {
173 unset( $this->locksHeld[$path] ); // no locks on this path
174 if ( isset( $this->handles[$path] ) ) {
175 $handlesToClose[] = $this->handles[$path];
176 unset( $this->handles[$path] );
179 // Unlock handles to release locks and delete
180 // any lock files that end up with no locks on them...
181 if ( $this->isWindows ) {
182 // Windows: for any process, including this one,
183 // calling unlink() on a locked file will fail
184 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
185 $status->merge( $this->pruneKeyLockFiles( $path ) );
186 } else {
187 // Unix: unlink() can be used on files currently open by this
188 // process and we must do so in order to avoid race conditions
189 $status->merge( $this->pruneKeyLockFiles( $path ) );
190 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
194 return $status;
198 * @param string $path
199 * @param array $handlesToClose
200 * @return StatusValue
202 private function closeLockHandles( $path, array $handlesToClose ) {
203 $status = StatusValue::newGood();
204 foreach ( $handlesToClose as $handle ) {
205 if ( !flock( $handle, LOCK_UN ) ) {
206 $status->fatal( 'lockmanager-fail-releaselock', $path );
208 if ( !fclose( $handle ) ) {
209 $status->warning( 'lockmanager-fail-closelock', $path );
213 return $status;
217 * @param string $path
218 * @return StatusValue
220 private function pruneKeyLockFiles( $path ) {
221 $status = StatusValue::newGood();
222 if ( !isset( $this->locksHeld[$path] ) ) {
223 # No locks are held for the lock file anymore
224 if ( !unlink( $this->getLockPath( $path ) ) ) {
225 $status->warning( 'lockmanager-fail-deletelock', $path );
227 unset( $this->handles[$path] );
230 return $status;
234 * Get the path to the lock file for a key
235 * @param string $path
236 * @return string
238 protected function getLockPath( $path ) {
239 return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock";
243 * Make sure remaining locks get cleared for sanity
245 function __destruct() {
246 while ( count( $this->locksHeld ) ) {
247 foreach ( $this->locksHeld as $path => $locks ) {
248 $this->doSingleUnlock( $path, self::LOCK_EX );
249 $this->doSingleUnlock( $path, self::LOCK_SH );