Merge "Update formatting of file backend classes"
[lhc/web/wiklou.git] / includes / filebackend / 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 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 * @param $paths array
66 * @param $type int
67 * @return Status
68 */
69 protected function doLock( array $paths, $type ) {
70 $status = Status::newGood();
71
72 $lockedPaths = array(); // files locked in this attempt
73 foreach ( $paths as $path ) {
74 $status->merge( $this->doSingleLock( $path, $type ) );
75 if ( $status->isOK() ) {
76 $lockedPaths[] = $path;
77 } else {
78 // Abort and unlock everything
79 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
80
81 return $status;
82 }
83 }
84
85 return $status;
86 }
87
88 /**
89 * @see LockManager::doUnlock()
90 * @param $paths array
91 * @param $type int
92 * @return Status
93 */
94 protected function doUnlock( array $paths, $type ) {
95 $status = Status::newGood();
96
97 foreach ( $paths as $path ) {
98 $status->merge( $this->doSingleUnlock( $path, $type ) );
99 }
100
101 return $status;
102 }
103
104 /**
105 * Lock a single resource key
106 *
107 * @param $path string
108 * @param $type integer
109 * @return Status
110 */
111 protected function doSingleLock( $path, $type ) {
112 $status = Status::newGood();
113
114 if ( isset( $this->locksHeld[$path][$type] ) ) {
115 ++$this->locksHeld[$path][$type];
116 } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
117 $this->locksHeld[$path][$type] = 1;
118 } else {
119 if ( isset( $this->handles[$path] ) ) {
120 $handle = $this->handles[$path];
121 } else {
122 wfSuppressWarnings();
123 $handle = fopen( $this->getLockPath( $path ), 'a+' );
124 wfRestoreWarnings();
125 if ( !$handle ) { // lock dir missing?
126 wfMkdirParents( $this->lockDir );
127 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
128 }
129 }
130 if ( $handle ) {
131 // Either a shared or exclusive lock
132 $lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX;
133 if ( flock( $handle, $lock | LOCK_NB ) ) {
134 // Record this lock as active
135 $this->locksHeld[$path][$type] = 1;
136 $this->handles[$path] = $handle;
137 } else {
138 fclose( $handle );
139 $status->fatal( 'lockmanager-fail-acquirelock', $path );
140 }
141 } else {
142 $status->fatal( 'lockmanager-fail-openlock', $path );
143 }
144 }
145
146 return $status;
147 }
148
149 /**
150 * Unlock a single resource key
151 *
152 * @param $path string
153 * @param $type integer
154 * @return Status
155 */
156 protected function doSingleUnlock( $path, $type ) {
157 $status = Status::newGood();
158
159 if ( !isset( $this->locksHeld[$path] ) ) {
160 $status->warning( 'lockmanager-notlocked', $path );
161 } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
162 $status->warning( 'lockmanager-notlocked', $path );
163 } else {
164 $handlesToClose = array();
165 --$this->locksHeld[$path][$type];
166 if ( $this->locksHeld[$path][$type] <= 0 ) {
167 unset( $this->locksHeld[$path][$type] );
168 }
169 if ( !count( $this->locksHeld[$path] ) ) {
170 unset( $this->locksHeld[$path] ); // no locks on this path
171 if ( isset( $this->handles[$path] ) ) {
172 $handlesToClose[] = $this->handles[$path];
173 unset( $this->handles[$path] );
174 }
175 }
176 // Unlock handles to release locks and delete
177 // any lock files that end up with no locks on them...
178 if ( wfIsWindows() ) {
179 // Windows: for any process, including this one,
180 // calling unlink() on a locked file will fail
181 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
182 $status->merge( $this->pruneKeyLockFiles( $path ) );
183 } else {
184 // Unix: unlink() can be used on files currently open by this
185 // process and we must do so in order to avoid race conditions
186 $status->merge( $this->pruneKeyLockFiles( $path ) );
187 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
188 }
189 }
190
191 return $status;
192 }
193
194 /**
195 * @param $path string
196 * @param $handlesToClose array
197 * @return Status
198 */
199 private function closeLockHandles( $path, array $handlesToClose ) {
200 $status = Status::newGood();
201 foreach ( $handlesToClose as $handle ) {
202 if ( !flock( $handle, LOCK_UN ) ) {
203 $status->fatal( 'lockmanager-fail-releaselock', $path );
204 }
205 if ( !fclose( $handle ) ) {
206 $status->warning( 'lockmanager-fail-closelock', $path );
207 }
208 }
209
210 return $status;
211 }
212
213 /**
214 * @param $path string
215 * @return Status
216 */
217 private function pruneKeyLockFiles( $path ) {
218 $status = Status::newGood();
219 if ( !isset( $this->locksHeld[$path] ) ) {
220 # No locks are held for the lock file anymore
221 if ( !unlink( $this->getLockPath( $path ) ) ) {
222 $status->warning( 'lockmanager-fail-deletelock', $path );
223 }
224 unset( $this->handles[$path] );
225 }
226
227 return $status;
228 }
229
230 /**
231 * Get the path to the lock file for a key
232 * @param $path string
233 * @return string
234 */
235 protected function getLockPath( $path ) {
236 return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock";
237 }
238
239 /**
240 * Make sure remaining locks get cleared for sanity
241 */
242 function __destruct() {
243 while ( count( $this->locksHeld ) ) {
244 foreach ( $this->locksHeld as $path => $locks ) {
245 $this->doSingleUnlock( $path, self::LOCK_EX );
246 $this->doSingleUnlock( $path, self::LOCK_SH );
247 }
248 }
249 }
250 }