‪TYPO3CMS  10.4
FileLockStrategy.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 
24 
29 {
31 
32  const ‪FILE_LOCK_FOLDER = 'lock/';
33  const ‪DEFAULT_PRIORITY = 75;
34 
38  protected ‪$filePointer;
39 
43  protected ‪$filePath;
44 
48  protected ‪$isAcquired = false;
49 
54  public function ‪__construct($subject)
55  {
56  /*
57  * Tests if the directory for file locks is available.
58  * If not, the directory will be created. The lock path is usually
59  * below typo3temp/var, typo3temp/var itself should exist already (or root-path/var/ respectively)
60  */
61  if (‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'] ?? false) {
62  $path = ‪Environment::getProjectPath() . '/'
63  . trim(‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['lockFileDir'], ' /')
64  . '/';
65  } else {
67  }
68  if (!is_dir($path)) {
69  // Not using mkdir_deep on purpose here, if typo3temp itself
70  // does not exist, this issue should be solved on a different
71  // level of the application.
72  if (!‪GeneralUtility::mkdir($path)) {
73  throw new ‪LockCreateException('Cannot create directory ' . $path, 1395140007);
74  }
75  }
76  if (!is_writable($path)) {
77  throw new ‪LockCreateException('Cannot write to directory ' . $path, 1396278700);
78  }
79  $this->filePath = $path . 'flock_' . md5((string)$subject);
80  }
81 
86  public function ‪__destruct()
87  {
88  $this->‪release();
89  }
90 
99  public function ‪acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
100  {
101  if ($this->‪isAcquired) {
102  return true;
103  }
104 
105  $this->filePointer = fopen($this->filePath, 'c');
106  if ($this->filePointer === false) {
107  throw new LockAcquireException('Lock file could not be opened', 1294586099);
108  }
109  ‪GeneralUtility::fixPermissions($this->filePath);
110 
111  $operation = $mode & self::LOCK_CAPABILITY_EXCLUSIVE ? LOCK_EX : LOCK_SH;
112  if ($mode & self::LOCK_CAPABILITY_NOBLOCK) {
113  $operation |= LOCK_NB;
114  }
115 
116  $wouldBlock = 0;
117  $this->‪isAcquired = flock($this->filePointer, $operation, $wouldBlock);
118 
119  if (!$this->‪isAcquired) {
120  // Make sure to cleanup any dangling resources for this process/thread, which are not needed any longer
121  fclose($this->filePointer);
122  }
123  if ($mode & self::LOCK_CAPABILITY_NOBLOCK && !$this->‪isAcquired && $wouldBlock) {
124  throw new LockAcquireWouldBlockException('Failed to acquire lock because the request would block.', 1428700748);
125  }
126 
127  return ‪$this->isAcquired;
128  }
129 
135  public function ‪release()
136  {
137  if (!$this->‪isAcquired) {
138  return true;
139  }
140  $success = true;
141  if (is_resource($this->filePointer)) {
142  if (flock($this->filePointer, LOCK_UN) === false) {
143  $success = false;
144  }
145  fclose($this->filePointer);
146  }
147  $this->‪isAcquired = false;
148  return $success;
149  }
150 
156  public function ‪isAcquired()
157  {
158  return ‪$this->isAcquired;
159  }
160 
164  public static function ‪getPriority()
165  {
166  return ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['priority']
168  }
169 
173  public static function ‪getCapabilities()
174  {
175  if (PHP_SAPI === 'isapi') {
176  // From php docs: When using a multi-threaded server API like ISAPI you may not be able to rely on flock()
177  // to protect files against other PHP scripts running in parallel threads of the same server instance!
178  return 0;
179  }
180  $capabilities = self::LOCK_CAPABILITY_EXCLUSIVE | self::LOCK_CAPABILITY_SHARED | ‪self::LOCK_CAPABILITY_NOBLOCK;
181 
182  return $capabilities;
183  }
184 
188  public function ‪destroy()
189  {
190  @unlink($this->filePath);
191  }
192 }
‪TYPO3\CMS\Core\Locking\FileLockStrategy\__construct
‪__construct($subject)
Definition: FileLockStrategy.php:51
‪TYPO3\CMS\Core\Locking\FileLockStrategy\destroy
‪destroy()
Definition: FileLockStrategy.php:185
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_NOBLOCK
‪const LOCK_CAPABILITY_NOBLOCK
Definition: LockingStrategyInterface.php:40
‪TYPO3\CMS\Core\Locking\FileLockStrategy\__destruct
‪__destruct()
Definition: FileLockStrategy.php:83
‪TYPO3\CMS\Core\Locking
‪TYPO3\CMS\Core\Locking\FileLockStrategy\DEFAULT_PRIORITY
‪const DEFAULT_PRIORITY
Definition: FileLockStrategy.php:33
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface
Definition: LockingStrategyInterface.php:26
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException
Definition: LockAcquireWouldBlockException.php:22
‪TYPO3\CMS\Core\Locking\FileLockStrategy\release
‪bool release()
Definition: FileLockStrategy.php:132
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Locking\FileLockStrategy\$filePointer
‪resource $filePointer
Definition: FileLockStrategy.php:37
‪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\FileLockStrategy\$isAcquired
‪bool $isAcquired
Definition: FileLockStrategy.php:45
‪TYPO3\CMS\Core\Locking\FileLockStrategy\acquire
‪bool acquire($mode=self::LOCK_CAPABILITY_EXCLUSIVE)
Definition: FileLockStrategy.php:96
‪TYPO3\CMS\Core\Locking\FileLockStrategy\getPriority
‪static int getPriority()
Definition: FileLockStrategy.php:161
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireException
Definition: LockAcquireException.php:24
‪TYPO3\CMS\Core\Locking\Exception\LockCreateException
Definition: LockCreateException.php:24
‪$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\FileLockStrategy\$filePath
‪string $filePath
Definition: FileLockStrategy.php:41
‪TYPO3\CMS\Core\Locking\FileLockStrategy\FILE_LOCK_FOLDER
‪const FILE_LOCK_FOLDER
Definition: FileLockStrategy.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\FileLockStrategy\isAcquired
‪bool isAcquired()
Definition: FileLockStrategy.php:153
‪TYPO3\CMS\Core\Locking\FileLockStrategy
Definition: FileLockStrategy.php:29
‪TYPO3\CMS\Core\Locking\FileLockStrategy\getCapabilities
‪static int getCapabilities()
Definition: FileLockStrategy.php:170
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192