Fixed undefined defines warnings introduced in change 5131
[mediawiki.git] / includes / filerepo / backend / lockmanager / FSLockManager.php
blobd35a1ae6b2273541fd01c6065cec6ab5bb185155
1 <?php
3 /**
4 * Simple version of LockManager based on using FS lock files.
5 * All locks are non-blocking, which avoids deadlocks.
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.
12 * @ingroup LockManager
13 * @since 1.19
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
23 protected $lockDir; // global dir for all servers
25 /** @var Array Map of (locked key => lock type => lock file handle) */
26 protected $handles = array();
28 /**
29 * Construct a new instance from configuration.
31 * $config includes:
32 * 'lockDirectory' : Directory containing the lock files
34 * @param array $config
36 function __construct( array $config ) {
37 parent::__construct( $config );
39 $this->lockDir = $config['lockDirectory'];
42 /**
43 * @see LockManager::doLock()
44 * @return Status
46 protected function doLock( array $paths, $type ) {
47 $status = Status::newGood();
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;
61 return $status;
64 /**
65 * @see LockManager::doUnlock()
66 * @return Status
68 protected function doUnlock( array $paths, $type ) {
69 $status = Status::newGood();
71 foreach ( $paths as $path ) {
72 $status->merge( $this->doSingleUnlock( $path, $type ) );
75 return $status;
78 /**
79 * Lock a single resource key
81 * @param $path string
82 * @param $type integer
83 * @return Status
85 protected function doSingleLock( $path, $type ) {
86 $status = Status::newGood();
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
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 );
111 } else {
112 $status->fatal( 'lockmanager-fail-openlock', $path );
116 return $status;
120 * Unlock a single resource key
122 * @param $path string
123 * @param $type integer
124 * @return Status
126 protected function doSingleUnlock( $path, $type ) {
127 $status = Status::newGood();
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] );
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 ) );
161 return $status;
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 );
171 if ( !fclose( $handle ) ) {
172 $status->warning( 'lockmanager-fail-closelock', $path );
174 wfRestoreWarnings();
176 return $status;
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 );
187 wfRestoreWarnings();
188 unset( $this->locksHeld[$path] );
189 unset( $this->handles[$path] );
191 return $status;
195 * Get the path to the lock file for a key
196 * @param $path string
197 * @return string
199 protected function getLockPath( $path ) {
200 $hash = self::sha1Base36( $path );
201 return "{$this->lockDir}/{$hash}.lock";
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 );