Merge "(Bug 23472) Removed undesirable space after external link url in printout"
[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 return $status;
81 }
82 }
83
84 return $status;
85 }
86
87 /**
88 * @see LockManager::doUnlock()
89 * @param $paths array
90 * @param $type int
91 * @return Status
92 */
93 protected function doUnlock( array $paths, $type ) {
94 $status = Status::newGood();
95
96 foreach ( $paths as $path ) {
97 $status->merge( $this->doSingleUnlock( $path, $type ) );
98 }
99
100 return $status;
101 }
102
103 /**
104 * Lock a single resource key
105 *
106 * @param $path string
107 * @param $type integer
108 * @return Status
109 */
110 protected function doSingleLock( $path, $type ) {
111 $status = Status::newGood();
112
113 if ( isset( $this->locksHeld[$path][$type] ) ) {
114 ++$this->locksHeld[$path][$type];
115 } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
116 $this->locksHeld[$path][$type] = 1;
117 } else {
118 if ( isset( $this->handles[$path] ) ) {
119 $handle = $this->handles[$path];
120 } else {
121 wfSuppressWarnings();
122 $handle = fopen( $this->getLockPath( $path ), 'a+' );
123 wfRestoreWarnings();
124 if ( !$handle ) { // lock dir missing?
125 wfMkdirParents( $this->lockDir );
126 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
127 }
128 }
129 if ( $handle ) {
130 // Either a shared or exclusive lock
131 $lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX;
132 if ( flock( $handle, $lock | LOCK_NB ) ) {
133 // Record this lock as active
134 $this->locksHeld[$path][$type] = 1;
135 $this->handles[$path] = $handle;
136 } else {
137 fclose( $handle );
138 $status->fatal( 'lockmanager-fail-acquirelock', $path );
139 }
140 } else {
141 $status->fatal( 'lockmanager-fail-openlock', $path );
142 }
143 }
144
145 return $status;
146 }
147
148 /**
149 * Unlock a single resource key
150 *
151 * @param $path string
152 * @param $type integer
153 * @return Status
154 */
155 protected function doSingleUnlock( $path, $type ) {
156 $status = Status::newGood();
157
158 if ( !isset( $this->locksHeld[$path] ) ) {
159 $status->warning( 'lockmanager-notlocked', $path );
160 } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
161 $status->warning( 'lockmanager-notlocked', $path );
162 } else {
163 $handlesToClose = array();
164 --$this->locksHeld[$path][$type];
165 if ( $this->locksHeld[$path][$type] <= 0 ) {
166 unset( $this->locksHeld[$path][$type] );
167 }
168 if ( !count( $this->locksHeld[$path] ) ) {
169 unset( $this->locksHeld[$path] ); // no locks on this path
170 if ( isset( $this->handles[$path] ) ) {
171 $handlesToClose[] = $this->handles[$path];
172 unset( $this->handles[$path] );
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 /**
194 * @param $path string
195 * @param $handlesToClose array
196 * @return Status
197 */
198 private function closeLockHandles( $path, array $handlesToClose ) {
199 $status = Status::newGood();
200 foreach ( $handlesToClose as $handle ) {
201 if ( !flock( $handle, LOCK_UN ) ) {
202 $status->fatal( 'lockmanager-fail-releaselock', $path );
203 }
204 if ( !fclose( $handle ) ) {
205 $status->warning( 'lockmanager-fail-closelock', $path );
206 }
207 }
208 return $status;
209 }
210
211 /**
212 * @param $path string
213 * @return Status
214 */
215 private function pruneKeyLockFiles( $path ) {
216 $status = Status::newGood();
217 if ( !isset( $this->locksHeld[$path] ) ) {
218 # No locks are held for the lock file anymore
219 if ( !unlink( $this->getLockPath( $path ) ) ) {
220 $status->warning( 'lockmanager-fail-deletelock', $path );
221 }
222 unset( $this->handles[$path] );
223 }
224 return $status;
225 }
226
227 /**
228 * Get the path to the lock file for a key
229 * @param $path string
230 * @return string
231 */
232 protected function getLockPath( $path ) {
233 return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock";
234 }
235
236 /**
237 * Make sure remaining locks get cleared for sanity
238 */
239 function __destruct() {
240 while ( count( $this->locksHeld ) ) {
241 foreach ( $this->locksHeld as $path => $locks ) {
242 $this->doSingleUnlock( $path, self::LOCK_EX );
243 $this->doSingleUnlock( $path, self::LOCK_SH );
244 }
245 }
246 }
247 }