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