‪TYPO3CMS  ‪main
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  public const ‪FILE_LOCK_FOLDER = 'lock/';
33  public 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  ‪$filePointer = fopen($this->filePath, 'c');
106  if (‪$filePointer === false) {
107  throw new LockAcquireException('Lock file could not be opened', 1294586099);
108  }
109  $this->filePointer = ‪$filePointer;
110  ‪GeneralUtility::fixPermissions($this->filePath);
111 
112  $operation = $mode & self::LOCK_CAPABILITY_EXCLUSIVE ? LOCK_EX : LOCK_SH;
113  if ($mode & self::LOCK_CAPABILITY_NOBLOCK) {
114  $operation |= LOCK_NB;
115  }
116 
117  $wouldBlock = 0;
118  $this->‪isAcquired = flock($this->filePointer, $operation, $wouldBlock);
119 
120  if (!$this->‪isAcquired) {
121  // Make sure to cleanup any dangling resources for this process/thread, which are not needed any longer
122  fclose($this->filePointer);
123  }
124  if ($mode & self::LOCK_CAPABILITY_NOBLOCK && !$this->‪isAcquired && $wouldBlock) {
125  throw new LockAcquireWouldBlockException('Failed to acquire lock because the request would block.', 1428700748);
126  }
127 
128  return ‪$this->isAcquired;
129  }
130 
136  public function ‪release()
137  {
138  if (!$this->‪isAcquired) {
139  return true;
140  }
141  $success = true;
142  if (is_resource($this->filePointer)) {
143  if (flock($this->filePointer, LOCK_UN) === false) {
144  $success = false;
145  }
146  fclose($this->filePointer);
147  }
148  $this->‪isAcquired = false;
149  return $success;
150  }
151 
157  public function ‪isAcquired()
158  {
159  return ‪$this->isAcquired;
160  }
161 
165  public static function ‪getPriority()
166  {
167  return ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['locking']['strategies'][self::class]['priority']
169  }
170 
174  public static function ‪getCapabilities()
175  {
176  if (PHP_SAPI === 'isapi') {
177  // From php docs: When using a multi-threaded server API like ISAPI you may not be able to rely on flock()
178  // to protect files against other PHP scripts running in parallel threads of the same server instance!
179  return 0;
180  }
181  $capabilities = self::LOCK_CAPABILITY_EXCLUSIVE | self::LOCK_CAPABILITY_SHARED | ‪self::LOCK_CAPABILITY_NOBLOCK;
182 
183  return $capabilities;
184  }
185 
189  public function ‪destroy()
190  {
191  @unlink($this->filePath);
192  }
193 }
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir(string $newFolder)
Definition: GeneralUtility.php:1638
‪TYPO3\CMS\Core\Locking\FileLockStrategy\__construct
‪__construct($subject)
Definition: FileLockStrategy.php:51
‪TYPO3\CMS\Core\Locking\FileLockStrategy\destroy
‪destroy()
Definition: FileLockStrategy.php:186
‪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\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException
Definition: LockAcquireWouldBlockException.php:21
‪TYPO3\CMS\Core\Locking\FileLockStrategy\release
‪bool release()
Definition: FileLockStrategy.php:133
‪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:160
‪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:162
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireException
Definition: LockAcquireException.php:23
‪TYPO3\CMS\Core\Locking\Exception\LockCreateException
Definition: LockCreateException.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixPermissions
‪static bool fixPermissions(string $path, bool $recursive=false)
Definition: GeneralUtility.php:1496
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪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:52
‪TYPO3\CMS\Core\Locking\FileLockStrategy\isAcquired
‪bool isAcquired()
Definition: FileLockStrategy.php:154
‪TYPO3\CMS\Core\Locking\FileLockStrategy
Definition: FileLockStrategy.php:29
‪TYPO3\CMS\Core\Locking\FileLockStrategy\getCapabilities
‪static int getCapabilities()
Definition: FileLockStrategy.php:171