‪TYPO3CMS  9.5
SimpleLockStrategy.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 
27 {
29 
30  const ‪FILE_LOCK_FOLDER = 'lock/';
31 
35  protected ‪$filePath;
36 
40  protected ‪$isAcquired = false;
41 
45  protected ‪$loops = 150;
46 
50  protected ‪$step = 200;
51 
56  public function ‪__construct($subject)
57  {
58  // Tests if the directory for simple locks is available.
59  // If not, the directory will be created. The lock path is usually
60  // below typo3temp/var, typo3temp/var itself should exist already (or getProjectPath . /var/ respectively)
62  if (!is_dir($path)) {
63  // Not using mkdir_deep on purpose here, if typo3temp/var itself
64  // does not exist, this issue should be solved on a different
65  // level of the application.
66  if (!GeneralUtility::mkdir($path)) {
67  throw new ‪LockCreateException('Cannot create directory ' . $path, 1460976286);
68  }
69  }
70  if (!is_writable($path)) {
71  throw new ‪LockCreateException('Cannot write to directory ' . $path, 1460976340);
72  }
73  $this->filePath = $path . 'simple_' . md5((string)$subject);
74  }
75 
80  public function ‪init(‪$loops = 0, ‪$step = 0)
81  {
82  $this->loops = (int)‪$loops;
83  $this->step = (int)‪$step;
84  }
85 
90  public function ‪__destruct()
91  {
92  $this->‪release();
93  }
94 
100  public function ‪release()
101  {
102  if (!$this->‪isAcquired) {
103  return true;
104  }
105 
106  $success = true;
107  if (
108  GeneralUtility::isAllowedAbsPath($this->filePath)
109  && GeneralUtility::isFirstPartOfStr($this->filePath, ‪Environment::getVarPath() . '/' . self::FILE_LOCK_FOLDER)
110  ) {
111  if (@unlink($this->filePath) === false) {
112  $success = false;
113  }
114  }
115 
116  $this->‪isAcquired = false;
117  return $success;
118  }
119 
125  public function ‪isAcquired()
126  {
127  return ‪$this->isAcquired;
128  }
129 
133  public static function ‪getCapabilities()
134  {
135  return self::LOCK_CAPABILITY_EXCLUSIVE | ‪self::LOCK_CAPABILITY_NOBLOCK;
136  }
137 
145  public function ‪acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
146  {
147  if ($this->‪isAcquired) {
148  return true;
149  }
150 
151  if (file_exists($this->filePath)) {
152  $maxExecutionTime = (int)ini_get('max_execution_time');
153  $maxAge = time() - ($maxExecutionTime ?: 120);
154  if (@filectime($this->filePath) < $maxAge) {
155  // Remove stale lock file
156  @unlink($this->filePath);
157  }
158  }
159 
160  $this->‪isAcquired = false;
161  $wouldBlock = false;
162  for ($i = 0; $i < ‪$this->loops; $i++) {
163  $filePointer = @fopen($this->filePath, 'x');
164  if ($filePointer !== false) {
165  fclose($filePointer);
166  GeneralUtility::fixPermissions($this->filePath);
167  $this->‪isAcquired = true;
168  break;
169  }
170  if ($mode & self::LOCK_CAPABILITY_NOBLOCK) {
171  $wouldBlock = true;
172  break;
173  }
174  usleep($this->step * 1000);
175  }
176 
177  if ($mode & self::LOCK_CAPABILITY_NOBLOCK && !$this->‪isAcquired && $wouldBlock) {
178  throw new LockAcquireWouldBlockException('Failed to acquire lock because the request would block.', 1460976403);
179  }
180 
181  return ‪$this->isAcquired;
182  }
183 
187  public static function ‪getPriority()
188  {
189  return 50;
190  }
191 
195  public function ‪destroy()
196  {
197  @unlink($this->filePath);
198  }
199 }
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\acquire
‪bool acquire($mode=self::LOCK_CAPABILITY_EXCLUSIVE)
Definition: SimpleLockStrategy.php:141
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\__destruct
‪__destruct()
Definition: SimpleLockStrategy.php:86
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_NOBLOCK
‪const LOCK_CAPABILITY_NOBLOCK
Definition: LockingStrategyInterface.php:39
‪TYPO3\CMS\Core\Locking
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\release
‪bool release()
Definition: SimpleLockStrategy.php:96
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface
Definition: LockingStrategyInterface.php:25
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\getPriority
‪static int getPriority()
Definition: SimpleLockStrategy.php:183
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException
Definition: LockAcquireWouldBlockException.php:21
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\$step
‪int $step
Definition: SimpleLockStrategy.php:46
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\init
‪init($loops=0, $step=0)
Definition: SimpleLockStrategy.php:76
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\destroy
‪destroy()
Definition: SimpleLockStrategy.php:191
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\FILE_LOCK_FOLDER
‪const FILE_LOCK_FOLDER
Definition: SimpleLockStrategy.php:30
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy
Definition: SimpleLockStrategy.php:27
‪TYPO3\CMS\Core\Locking\Exception\LockCreateException
Definition: LockCreateException.php:21
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\$isAcquired
‪bool $isAcquired
Definition: SimpleLockStrategy.php:38
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\getCapabilities
‪static int getCapabilities()
Definition: SimpleLockStrategy.php:129
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\$loops
‪int $loops
Definition: SimpleLockStrategy.php:42
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\isAcquired
‪bool isAcquired()
Definition: SimpleLockStrategy.php:121
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\$filePath
‪string $filePath
Definition: SimpleLockStrategy.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\__construct
‪__construct($subject)
Definition: SimpleLockStrategy.php:52
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165