[FileBackend]
[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 $this->lockDir = $config['lockDirectory'];
39 }
40
41 /**
42 * @see LockManager::doLock()
43 * @return Status
44 */
45 protected function doLock( array $paths, $type ) {
46 $status = Status::newGood();
47
48 $lockedPaths = array(); // files locked in this attempt
49 foreach ( $paths as $path ) {
50 $status->merge( $this->doSingleLock( $path, $type ) );
51 if ( $status->isOK() ) {
52 $lockedPaths[] = $path;
53 } else {
54 // Abort and unlock everything
55 $status->merge( $this->doUnlock( $lockedPaths, $type ) );
56 return $status;
57 }
58 }
59
60 return $status;
61 }
62
63 /**
64 * @see LockManager::doUnlock()
65 * @return Status
66 */
67 protected function doUnlock( array $paths, $type ) {
68 $status = Status::newGood();
69
70 foreach ( $paths as $path ) {
71 $status->merge( $this->doSingleUnlock( $path, $type ) );
72 }
73
74 return $status;
75 }
76
77 /**
78 * Lock a single resource key
79 *
80 * @param $path string
81 * @param $type integer
82 * @return Status
83 */
84 protected function doSingleLock( $path, $type ) {
85 $status = Status::newGood();
86
87 if ( isset( $this->locksHeld[$path][$type] ) ) {
88 ++$this->locksHeld[$path][$type];
89 } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
90 $this->locksHeld[$path][$type] = 1;
91 } else {
92 wfSuppressWarnings();
93 $handle = fopen( $this->getLockPath( $path ), 'a+' );
94 wfRestoreWarnings();
95 if ( !$handle ) { // lock dir missing?
96 wfMkdirParents( $this->lockDir );
97 $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
98 }
99 if ( $handle ) {
100 // Either a shared or exclusive lock
101 $lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX;
102 if ( flock( $handle, $lock | LOCK_NB ) ) {
103 // Record this lock as active
104 $this->locksHeld[$path][$type] = 1;
105 $this->handles[$path][$type] = $handle;
106 } else {
107 fclose( $handle );
108 $status->fatal( 'lockmanager-fail-acquirelock', $path );
109 }
110 } else {
111 $status->fatal( 'lockmanager-fail-openlock', $path );
112 }
113 }
114
115 return $status;
116 }
117
118 /**
119 * Unlock a single resource key
120 *
121 * @param $path string
122 * @param $type integer
123 * @return Status
124 */
125 protected function doSingleUnlock( $path, $type ) {
126 $status = Status::newGood();
127
128 if ( !isset( $this->locksHeld[$path] ) ) {
129 $status->warning( 'lockmanager-notlocked', $path );
130 } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
131 $status->warning( 'lockmanager-notlocked', $path );
132 } else {
133 $handlesToClose = array();
134 --$this->locksHeld[$path][$type];
135 if ( $this->locksHeld[$path][$type] <= 0 ) {
136 unset( $this->locksHeld[$path][$type] );
137 // If a LOCK_SH comes in while we have a LOCK_EX, we don't
138 // actually add a handler, so check for handler existence.
139 if ( isset( $this->handles[$path][$type] ) ) {
140 // Mark this handle to be unlocked and closed
141 $handlesToClose[] = $this->handles[$path][$type];
142 unset( $this->handles[$path][$type] );
143 }
144 }
145 // Unlock handles to release locks and delete
146 // any lock files that end up with no locks on them...
147 if ( wfIsWindows() ) {
148 // Windows: for any process, including this one,
149 // calling unlink() on a locked file will fail
150 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
151 $status->merge( $this->pruneKeyLockFiles( $path ) );
152 } else {
153 // Unix: unlink() can be used on files currently open by this
154 // process and we must do so in order to avoid race conditions
155 $status->merge( $this->pruneKeyLockFiles( $path ) );
156 $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
157 }
158 }
159
160 return $status;
161 }
162
163 private function closeLockHandles( $path, array $handlesToClose ) {
164 $status = Status::newGood();
165 foreach ( $handlesToClose as $handle ) {
166 wfSuppressWarnings();
167 if ( !flock( $handle, LOCK_UN ) ) {
168 $status->fatal( 'lockmanager-fail-releaselock', $path );
169 }
170 if ( !fclose( $handle ) ) {
171 $status->warning( 'lockmanager-fail-closelock', $path );
172 }
173 wfRestoreWarnings();
174 }
175 return $status;
176 }
177
178 private function pruneKeyLockFiles( $path ) {
179 $status = Status::newGood();
180 if ( !count( $this->locksHeld[$path] ) ) {
181 wfSuppressWarnings();
182 # No locks are held for the lock file anymore
183 if ( !unlink( $this->getLockPath( $path ) ) ) {
184 $status->warning( 'lockmanager-fail-deletelock', $path );
185 }
186 wfRestoreWarnings();
187 unset( $this->locksHeld[$path] );
188 unset( $this->handles[$path] );
189 }
190 return $status;
191 }
192
193 /**
194 * Get the path to the lock file for a key
195 * @param $path string
196 * @return string
197 */
198 protected function getLockPath( $path ) {
199 $hash = self::sha1Base36( $path );
200 return "{$this->lockDir}/{$hash}.lock";
201 }
202
203 function __destruct() {
204 // Make sure remaining locks get cleared for sanity
205 foreach ( $this->locksHeld as $path => $locks ) {
206 $this->doSingleUnlock( $path, self::LOCK_EX );
207 $this->doSingleUnlock( $path, self::LOCK_SH );
208 }
209 }
210 }