MediaWiki  1.23.1
FSLockManager.php
Go to the documentation of this file.
1 <?php
36 class FSLockManager extends LockManager {
38  protected $lockTypeMap = array(
39  self::LOCK_SH => self::LOCK_SH,
40  self::LOCK_UW => self::LOCK_SH,
41  self::LOCK_EX => self::LOCK_EX
42  );
43 
44  protected $lockDir; // global dir for all servers
45 
47  protected $handles = array();
48 
55  function __construct( array $config ) {
56  parent::__construct( $config );
57 
58  $this->lockDir = $config['lockDirectory'];
59  }
60 
67  protected function doLock( array $paths, $type ) {
68  $status = Status::newGood();
69 
70  $lockedPaths = array(); // files locked in this attempt
71  foreach ( $paths as $path ) {
72  $status->merge( $this->doSingleLock( $path, $type ) );
73  if ( $status->isOK() ) {
74  $lockedPaths[] = $path;
75  } else {
76  // Abort and unlock everything
77  $status->merge( $this->doUnlock( $lockedPaths, $type ) );
78 
79  return $status;
80  }
81  }
82 
83  return $status;
84  }
85 
92  protected function doUnlock( array $paths, $type ) {
93  $status = Status::newGood();
94 
95  foreach ( $paths as $path ) {
96  $status->merge( $this->doSingleUnlock( $path, $type ) );
97  }
98 
99  return $status;
100  }
101 
109  protected function doSingleLock( $path, $type ) {
110  $status = Status::newGood();
111 
112  if ( isset( $this->locksHeld[$path][$type] ) ) {
113  ++$this->locksHeld[$path][$type];
114  } elseif ( isset( $this->locksHeld[$path][self::LOCK_EX] ) ) {
115  $this->locksHeld[$path][$type] = 1;
116  } else {
117  if ( isset( $this->handles[$path] ) ) {
118  $handle = $this->handles[$path];
119  } else {
121  $handle = fopen( $this->getLockPath( $path ), 'a+' );
123  if ( !$handle ) { // lock dir missing?
124  wfMkdirParents( $this->lockDir );
125  $handle = fopen( $this->getLockPath( $path ), 'a+' ); // try again
126  }
127  }
128  if ( $handle ) {
129  // Either a shared or exclusive lock
130  $lock = ( $type == self::LOCK_SH ) ? LOCK_SH : LOCK_EX;
131  if ( flock( $handle, $lock | LOCK_NB ) ) {
132  // Record this lock as active
133  $this->locksHeld[$path][$type] = 1;
134  $this->handles[$path] = $handle;
135  } else {
136  fclose( $handle );
137  $status->fatal( 'lockmanager-fail-acquirelock', $path );
138  }
139  } else {
140  $status->fatal( 'lockmanager-fail-openlock', $path );
141  }
142  }
143 
144  return $status;
145  }
146 
154  protected function doSingleUnlock( $path, $type ) {
155  $status = Status::newGood();
156 
157  if ( !isset( $this->locksHeld[$path] ) ) {
158  $status->warning( 'lockmanager-notlocked', $path );
159  } elseif ( !isset( $this->locksHeld[$path][$type] ) ) {
160  $status->warning( 'lockmanager-notlocked', $path );
161  } else {
162  $handlesToClose = array();
163  --$this->locksHeld[$path][$type];
164  if ( $this->locksHeld[$path][$type] <= 0 ) {
165  unset( $this->locksHeld[$path][$type] );
166  }
167  if ( !count( $this->locksHeld[$path] ) ) {
168  unset( $this->locksHeld[$path] ); // no locks on this path
169  if ( isset( $this->handles[$path] ) ) {
170  $handlesToClose[] = $this->handles[$path];
171  unset( $this->handles[$path] );
172  }
173  }
174  // Unlock handles to release locks and delete
175  // any lock files that end up with no locks on them...
176  if ( wfIsWindows() ) {
177  // Windows: for any process, including this one,
178  // calling unlink() on a locked file will fail
179  $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
180  $status->merge( $this->pruneKeyLockFiles( $path ) );
181  } else {
182  // Unix: unlink() can be used on files currently open by this
183  // process and we must do so in order to avoid race conditions
184  $status->merge( $this->pruneKeyLockFiles( $path ) );
185  $status->merge( $this->closeLockHandles( $path, $handlesToClose ) );
186  }
187  }
188 
189  return $status;
190  }
191 
197  private function closeLockHandles( $path, array $handlesToClose ) {
198  $status = Status::newGood();
199  foreach ( $handlesToClose as $handle ) {
200  if ( !flock( $handle, LOCK_UN ) ) {
201  $status->fatal( 'lockmanager-fail-releaselock', $path );
202  }
203  if ( !fclose( $handle ) ) {
204  $status->warning( 'lockmanager-fail-closelock', $path );
205  }
206  }
207 
208  return $status;
209  }
210 
215  private function pruneKeyLockFiles( $path ) {
216  $status = Status::newGood();
217  if ( !isset( $this->locksHeld[$path] ) ) {
218  # No locks are held for the lock file anymore
219  if ( !unlink( $this->getLockPath( $path ) ) ) {
220  $status->warning( 'lockmanager-fail-deletelock', $path );
221  }
222  unset( $this->handles[$path] );
223  }
224 
225  return $status;
226  }
227 
233  protected function getLockPath( $path ) {
234  return "{$this->lockDir}/{$this->sha1Base36Absolute( $path )}.lock";
235  }
236 
240  function __destruct() {
241  while ( count( $this->locksHeld ) ) {
242  foreach ( $this->locksHeld as $path => $locks ) {
243  $this->doSingleUnlock( $path, self::LOCK_EX );
244  $this->doSingleUnlock( $path, self::LOCK_SH );
245  }
246  }
247  }
248 }
LockManager
Class for handling resource locking.
Definition: LockManager.php:45
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
FSLockManager
Simple version of LockManager based on using FS lock files.
Definition: FSLockManager.php:36
wfMkdirParents
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Definition: GlobalFunctions.php:2590
LockManager\LOCK_SH
const LOCK_SH
Lock types; stronger locks have higher values.
Definition: LockManager.php:58
FSLockManager\getLockPath
getLockPath( $path)
Get the path to the lock file for a key.
Definition: FSLockManager.php:231
wfSuppressWarnings
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
Definition: GlobalFunctions.php:2387
FSLockManager\pruneKeyLockFiles
pruneKeyLockFiles( $path)
Definition: FSLockManager.php:213
Status\newGood
static newGood( $value=null)
Factory function for good results.
Definition: Status.php:77
FSLockManager\__construct
__construct(array $config)
Construct a new instance from configuration.
Definition: FSLockManager.php:53
FSLockManager\$handles
array $handles
Map of (locked key => lock file handle) *.
Definition: FSLockManager.php:45
wfRestoreWarnings
wfRestoreWarnings()
Restore error level to previous value.
Definition: GlobalFunctions.php:2417
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
FSLockManager\$lockTypeMap
array $lockTypeMap
Mapping of lock types to the type actually used *.
Definition: FSLockManager.php:37
FSLockManager\doSingleUnlock
doSingleUnlock( $path, $type)
Unlock a single resource key.
Definition: FSLockManager.php:152
wfIsWindows
wfIsWindows()
Check if the operating system is Windows.
Definition: GlobalFunctions.php:2524
FSLockManager\doSingleLock
doSingleLock( $path, $type)
Lock a single resource key.
Definition: FSLockManager.php:107
FSLockManager\doUnlock
doUnlock(array $paths, $type)
Definition: FSLockManager.php:90
$path
$path
Definition: NoLocalSettings.php:35
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
FSLockManager\doLock
doLock(array $paths, $type)
Definition: FSLockManager.php:65
LockManager\LOCK_EX
const LOCK_EX
Definition: LockManager.php:60
FSLockManager\$lockDir
$lockDir
Definition: FSLockManager.php:43
FSLockManager\__destruct
__destruct()
Make sure remaining locks get cleared for sanity.
Definition: FSLockManager.php:238
FSLockManager\closeLockHandles
closeLockHandles( $path, array $handlesToClose)
Definition: FSLockManager.php:195
$type
$type
Definition: testCompression.php:46