‪TYPO3CMS  10.4
SimpleLockStrategy.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
28 {
30 
31  const ‪FILE_LOCK_FOLDER = 'lock/';
32  const ‪DEFAULT_PRIORITY = 50;
33 
37  protected ‪$filePath;
38 
42  protected ‪$isAcquired = false;
43 
47  protected ‪$loops = 150;
48 
52  protected ‪$step = 200;
53 
58  public function ‪__construct($subject)
59  {
60  // Tests if the directory for simple locks is available.
61  // If not, the directory will be created. The lock path is usually
62  // below typo3temp/var, typo3temp/var itself should exist already (or getProjectPath . /var/ respectively)
63  if (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'] ?? false) {
64  $path = ‪Environment::getProjectPath() . '/'
65  . trim(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'], ' /')
66  . '/';
67  } else {
69  }
70  if (!is_dir($path)) {
71  // Not using mkdir_deep on purpose here, if typo3temp/var itself
72  // does not exist, this issue should be solved on a different
73  // level of the application.
74  if (!‪GeneralUtility::mkdir($path)) {
75  throw new ‪LockCreateException('Cannot create directory ' . $path, 1460976286);
76  }
77  }
78  if (!is_writable($path)) {
79  throw new ‪LockCreateException('Cannot write to directory ' . $path, 1460976340);
80  }
81  $this->filePath = $path . 'simple_' . md5((string)$subject);
82  }
83 
88  public function ‪init(‪$loops = 0, ‪$step = 0)
89  {
90  $this->loops = (int)‪$loops;
91  $this->step = (int)‪$step;
92  }
93 
98  public function ‪__destruct()
99  {
100  $this->‪release();
101  }
102 
108  public function ‪release()
109  {
110  if (!$this->‪isAcquired) {
111  return true;
112  }
113 
114  $success = true;
115  if (
116  GeneralUtility::isAllowedAbsPath($this->filePath)
117  && GeneralUtility::isFirstPartOfStr($this->filePath, ‪Environment::getVarPath() . '/' . self::FILE_LOCK_FOLDER)
118  ) {
119  if (@unlink($this->filePath) === false) {
120  $success = false;
121  }
122  }
123 
124  $this->‪isAcquired = false;
125  return $success;
126  }
127 
133  public function ‪isAcquired()
134  {
135  return ‪$this->isAcquired;
136  }
137 
141  public static function ‪getCapabilities()
142  {
143  return self::LOCK_CAPABILITY_EXCLUSIVE | ‪self::LOCK_CAPABILITY_NOBLOCK;
144  }
145 
153  public function ‪acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
154  {
155  if ($this->‪isAcquired) {
156  return true;
157  }
158 
159  if (file_exists($this->filePath)) {
160  $maxExecutionTime = (int)ini_get('max_execution_time');
161  $maxAge = time() - ($maxExecutionTime ?: 120);
162  if (@filectime($this->filePath) < $maxAge) {
163  // Remove stale lock file
164  @unlink($this->filePath);
165  }
166  }
167 
168  $this->‪isAcquired = false;
169  $wouldBlock = false;
170  for ($i = 0; $i < ‪$this->loops; $i++) {
171  $filePointer = @fopen($this->filePath, 'x');
172  if ($filePointer !== false) {
173  fclose($filePointer);
174  ‪GeneralUtility::fixPermissions($this->filePath);
175  $this->‪isAcquired = true;
176  break;
177  }
178  if ($mode & self::LOCK_CAPABILITY_NOBLOCK) {
179  $wouldBlock = true;
180  break;
181  }
182  usleep($this->step * 1000);
183  }
184 
185  if ($mode & self::LOCK_CAPABILITY_NOBLOCK && !$this->‪isAcquired && $wouldBlock) {
186  throw new LockAcquireWouldBlockException('Failed to acquire lock because the request would block.', 1460976403);
187  }
188 
189  return ‪$this->isAcquired;
190  }
191 
195  public static function ‪getPriority()
196  {
197  return ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['priority']
199  }
200 
204  public function ‪destroy()
205  {
206  @unlink($this->filePath);
207  }
208 }
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\acquire
‪bool acquire($mode=self::LOCK_CAPABILITY_EXCLUSIVE)
Definition: SimpleLockStrategy.php:149
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\__destruct
‪__destruct()
Definition: SimpleLockStrategy.php:94
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_NOBLOCK
‪const LOCK_CAPABILITY_NOBLOCK
Definition: LockingStrategyInterface.php:40
‪TYPO3\CMS\Core\Locking
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\release
‪bool release()
Definition: SimpleLockStrategy.php:104
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface
Definition: LockingStrategyInterface.php:26
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\getPriority
‪static int getPriority()
Definition: SimpleLockStrategy.php:191
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException
Definition: LockAcquireWouldBlockException.php:22
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\$step
‪int $step
Definition: SimpleLockStrategy.php:48
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\init
‪init($loops=0, $step=0)
Definition: SimpleLockStrategy.php:84
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\destroy
‪destroy()
Definition: SimpleLockStrategy.php:200
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixPermissions
‪static mixed fixPermissions($path, $recursive=false)
Definition: GeneralUtility.php:1863
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\FILE_LOCK_FOLDER
‪const FILE_LOCK_FOLDER
Definition: SimpleLockStrategy.php:31
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy
Definition: SimpleLockStrategy.php:28
‪TYPO3\CMS\Core\Locking\Exception\LockCreateException
Definition: LockCreateException.php:24
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\$isAcquired
‪bool $isAcquired
Definition: SimpleLockStrategy.php:40
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\getCapabilities
‪static int getCapabilities()
Definition: SimpleLockStrategy.php:137
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\$loops
‪int $loops
Definition: SimpleLockStrategy.php:44
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\isAcquired
‪bool isAcquired()
Definition: SimpleLockStrategy.php:129
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\$filePath
‪string $filePath
Definition: SimpleLockStrategy.php:36
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\DEFAULT_PRIORITY
‪const DEFAULT_PRIORITY
Definition: SimpleLockStrategy.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir($newFolder)
Definition: GeneralUtility.php:2005
‪TYPO3\CMS\Core\Locking\SimpleLockStrategy\__construct
‪__construct($subject)
Definition: SimpleLockStrategy.php:54
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192