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