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