Merge "(bug 19195) Make user IDs more readily available with the API"
[lhc/web/wiklou.git] / includes / filerepo / backend / lockmanager / FSLockManager.php
1 <?php
2 /**
3 * Simple version of LockManager based on using FS lock files.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup LockManager
22 */
23
24 /**
25 * Simple version of LockManager based on using FS lock files.
26 * All locks are non-blocking, which avoids deadlocks.
27 *
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.
32 *
33 * @ingroup LockManager
34 * @since 1.19
35 */
36 class FSLockManager extends LockManager {
37 /** @var Array Mapping of lock types to the type actually used */
38 protected $lockTypeMap = array(
39 self::LOCK_SH => self::LOCK_SH,
40 self::LOCK_UW => self::LOCK_SH,
41 self::LOCK_EX => self::LOCK_EX
42 );
43
44 protected $lockDir; // global dir for all servers
45
46 /** @var Array Map of (locked key => lock type => lock file handle) */
47 protected $handles = array();
48
49 /**
50 * Construct a new instance from configuration.
51 *
52 * $config includes:
53 * 'lockDirectory' : Directory containing the lock files
54 *
55 * @param array $config
56 */
57 function __construct( array $config ) {
58 parent::__construct( $config );
59
60 $this->lockDir = $config['lockDirectory'];
61 }
62
63 /**
64 * @see LockManager::doLock()
65 * @return Status
66 */
67 protected function doLock( array $paths, $type ) {
68 $status = Status::newGood();
69
70 $lockedPaths = array(); // files locked in this attempt
71 foreach ( $paths as $path ) {
72 $status->merge( $this->doSingleLock( $path, $type ) );
73 if ( $status->isOK() ) {
74 $lockedPaths[] = $path;
75 } else {
76 // Abort and unlock everything
77 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
78 return $status;
79 }
80 }
81
82 return $status;
83 }
84
85 /**
86 * @see LockManager::doUnlock()
87 * @return Status
88 */
89 protected function doUnlock( array $paths, $type ) {
90 $status = Status::newGood();
91
92 foreach ( $paths as $path ) {
93 $status->merge( $this->doSingleUnlock( $path, $type ) );
94 }
95
96 return $status;
97 }
98
99 /**
100 * Lock a single resource key
101 *
102 * @param $path string
103 * @param $type integer
104 * @return Status
105 */
106 protected function doSingleLock( $path, $type ) {
107 $status = Status::newGood();
108
109 if ( isset( $this->locksHeld[$path][$type] ) ) {
110 ++$this->locksHeld[$path][$type];
111 } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
112 $this->locksHeld[$path][$type] = 1;
113 } else {
114 wfSuppressWarnings();
115 $handle = fopen( $this->getLockPath( $path ), 'a+' );
116 wfRestoreWarnings();
117 if ( !$handle ) { // lock dir missing?
118 wfMkdirParents( $this->lockDir );
119 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
120 }
121 if ( $handle ) {
122 // Either a shared or exclusive lock
123 $lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX;
124 if ( flock( $handle, $lock | LOCK_NB ) ) {
125 // Record this lock as active
126 $this->locksHeld[$path][$type] = 1;
127 $this->handles[$path][$type] = $handle;
128 } else {
129 fclose( $handle );
130 $status->fatal( 'lockmanager-fail-acquirelock', $path );
131 }
132 } else {
133 $status->fatal( 'lockmanager-fail-openlock', $path );
134 }
135 }
136
137 return $status;
138 }
139
140 /**
141 * Unlock a single resource key
142 *
143 * @param $path string
144 * @param $type integer
145 * @return Status
146 */
147 protected function doSingleUnlock( $path, $type ) {
148 $status = Status::newGood();
149
150 if ( !isset( $this->locksHeld[$path] ) ) {
151 $status->warning( 'lockmanager-notlocked', $path );
152 } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
153 $status->warning( 'lockmanager-notlocked', $path );
154 } else {
155 $handlesToClose = array();
156 --$this->locksHeld[$path][$type];
157 if ( $this->locksHeld[$path][$type] <= 0 ) {
158 unset( $this->locksHeld[$path][$type] );
159 // If a LOCK_SH comes in while we have a LOCK_EX, we don't
160 // actually add a handler, so check for handler existence.
161 if ( isset( $this->handles[$path][$type] ) ) {
162 if ( $type === self::LOCK_EX
163 && isset( $this->locksHeld[$path][self::LOCK_SH] )
164 && !isset( $this->handles[$path][self::LOCK_SH] ) )
165 {
166 // EX lock came first: move this handle to the SH one
167 $this->handles[$path][self::LOCK_SH] = $this->handles[$path][$type];
168 } else {
169 // Mark this handle to be unlocked and closed
170 $handlesToClose[] = $this->handles[$path][$type];
171 }
172 unset( $this->handles[$path][$type] );
173 }
174 }
175 // Unlock handles to release locks and delete
176 // any lock files that end up with no locks on them...
177 if ( wfIsWindows() ) {
178 // Windows: for any process, including this one,
179 // calling unlink() on a locked file will fail
180 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
181 $status->merge( $this->pruneKeyLockFiles( $path ) );
182 } else {
183 // Unix: unlink() can be used on files currently open by this
184 // process and we must do so in order to avoid race conditions
185 $status->merge( $this->pruneKeyLockFiles( $path ) );
186 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
187 }
188 }
189
190 return $status;
191 }
192
193 private function closeLockHandles( $path, array $handlesToClose ) {
194 $status = Status::newGood();
195 foreach ( $handlesToClose as $handle ) {
196 if ( !flock( $handle, LOCK_UN ) ) {
197 $status->fatal( 'lockmanager-fail-releaselock', $path );
198 }
199 if ( !fclose( $handle ) ) {
200 $status->warning( 'lockmanager-fail-closelock', $path );
201 }
202 }
203 return $status;
204 }
205
206 private function pruneKeyLockFiles( $path ) {
207 $status = Status::newGood();
208 if ( !count( $this->locksHeld[$path] ) ) {
209 # No locks are held for the lock file anymore
210 if ( !unlink( $this->getLockPath( $path ) ) ) {
211 $status->warning( 'lockmanager-fail-deletelock', $path );
212 }
213 unset( $this->locksHeld[$path] );
214 unset( $this->handles[$path] );
215 }
216 return $status;
217 }
218
219 /**
220 * Get the path to the lock file for a key
221 * @param $path string
222 * @return string
223 */
224 protected function getLockPath( $path ) {
225 $hash = self::sha1Base36( $path );
226 return "{$this->lockDir}/{$hash}.lock";
227 }
228
229 function __destruct() {
230 // Make sure remaining locks get cleared for sanity
231 foreach ( $this->locksHeld as $path => $locks ) {
232 $this->doSingleUnlock( $path, self::LOCK_EX );
233 $this->doSingleUnlock( $path, self::LOCK_SH );
234 }
235 }
236 }