‪TYPO3CMS  9.5
FileLockStrategy.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 
23 
28 {
30 
31  const ‪FILE_LOCK_FOLDER = 'lock/';
32 
36  protected ‪$filePointer;
37 
41  protected ‪$filePath;
42 
46  protected ‪$isAcquired = false;
47 
52  public function ‪__construct($subject)
53  {
54  /*
55  * Tests if the directory for simple locks is available.
56  * If not, the directory will be created. The lock path is usually
57  * below typo3temp/var, typo3temp/var itself should exist already (or root-path/var/ respectively)
58  */
60  if (!is_dir($path)) {
61  // Not using mkdir_deep on purpose here, if typo3temp itself
62  // does not exist, this issue should be solved on a different
63  // level of the application.
64  if (!GeneralUtility::mkdir($path)) {
65  throw new ‪LockCreateException('Cannot create directory ' . $path, 1395140007);
66  }
67  }
68  if (!is_writable($path)) {
69  throw new ‪LockCreateException('Cannot write to directory ' . $path, 1396278700);
70  }
71  $this->filePath = $path . 'flock_' . md5((string)$subject);
72  }
73 
78  public function ‪__destruct()
79  {
80  $this->‪release();
81  }
82 
91  public function ‪acquire($mode = self::LOCK_CAPABILITY_EXCLUSIVE)
92  {
93  if ($this->‪isAcquired) {
94  return true;
95  }
96 
97  $this->filePointer = fopen($this->filePath, 'c');
98  if ($this->filePointer === false) {
99  throw new LockAcquireException('Lock file could not be opened', 1294586099);
100  }
101  GeneralUtility::fixPermissions($this->filePath);
102 
103  $operation = $mode & self::LOCK_CAPABILITY_EXCLUSIVE ? LOCK_EX : LOCK_SH;
104  if ($mode & self::LOCK_CAPABILITY_NOBLOCK) {
105  $operation |= LOCK_NB;
106  }
107 
108  $wouldBlock = 0;
109  $this->‪isAcquired = flock($this->filePointer, $operation, $wouldBlock);
110 
111  if (!$this->‪isAcquired) {
112  // Make sure to cleanup any dangling resources for this process/thread, which are not needed any longer
113  fclose($this->filePointer);
114  }
115  if ($mode & self::LOCK_CAPABILITY_NOBLOCK && !$this->‪isAcquired && $wouldBlock) {
116  throw new LockAcquireWouldBlockException('Failed to acquire lock because the request would block.', 1428700748);
117  }
118 
119  return ‪$this->isAcquired;
120  }
121 
127  public function ‪release()
128  {
129  if (!$this->‪isAcquired) {
130  return true;
131  }
132  $success = true;
133  if (is_resource($this->filePointer)) {
134  if (flock($this->filePointer, LOCK_UN) === false) {
135  $success = false;
136  }
137  fclose($this->filePointer);
138  }
139  $this->‪isAcquired = false;
140  return $success;
141  }
142 
148  public function ‪isAcquired()
149  {
150  return ‪$this->isAcquired;
151  }
152 
156  public static function ‪getPriority()
157  {
158  return 75;
159  }
160 
164  public static function ‪getCapabilities()
165  {
166  if (PHP_SAPI === 'isapi') {
167  // From php docs: When using a multi-threaded server API like ISAPI you may not be able to rely on flock()
168  // to protect files against other PHP scripts running in parallel threads of the same server instance!
169  return 0;
170  }
171  $capabilities = self::LOCK_CAPABILITY_EXCLUSIVE | self::LOCK_CAPABILITY_SHARED | ‪self::LOCK_CAPABILITY_NOBLOCK;
172 
173  return $capabilities;
174  }
175 
179  public function ‪destroy()
180  {
181  @unlink($this->filePath);
182  }
183 }
‪TYPO3\CMS\Core\Locking\FileLockStrategy\__construct
‪__construct($subject)
Definition: FileLockStrategy.php:49
‪TYPO3\CMS\Core\Locking\FileLockStrategy\destroy
‪destroy()
Definition: FileLockStrategy.php:176
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface\LOCK_CAPABILITY_NOBLOCK
‪const LOCK_CAPABILITY_NOBLOCK
Definition: LockingStrategyInterface.php:39
‪TYPO3\CMS\Core\Locking\FileLockStrategy\__destruct
‪__destruct()
Definition: FileLockStrategy.php:75
‪TYPO3\CMS\Core\Locking
‪TYPO3\CMS\Core\Locking\LockingStrategyInterface
Definition: LockingStrategyInterface.php:25
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireWouldBlockException
Definition: LockAcquireWouldBlockException.php:21
‪TYPO3\CMS\Core\Locking\FileLockStrategy\release
‪bool release()
Definition: FileLockStrategy.php:124
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Core\Locking\FileLockStrategy\$filePointer
‪resource $filePointer
Definition: FileLockStrategy.php:35
‪TYPO3\CMS\Core\Locking\FileLockStrategy\$isAcquired
‪bool $isAcquired
Definition: FileLockStrategy.php:43
‪TYPO3\CMS\Core\Locking\FileLockStrategy\acquire
‪bool acquire($mode=self::LOCK_CAPABILITY_EXCLUSIVE)
Definition: FileLockStrategy.php:88
‪TYPO3\CMS\Core\Locking\FileLockStrategy\getPriority
‪static int getPriority()
Definition: FileLockStrategy.php:153
‪TYPO3\CMS\Core\Locking\Exception\LockAcquireException
Definition: LockAcquireException.php:21
‪TYPO3\CMS\Core\Locking\Exception\LockCreateException
Definition: LockCreateException.php:21
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Locking\FileLockStrategy\$filePath
‪string $filePath
Definition: FileLockStrategy.php:39
‪TYPO3\CMS\Core\Locking\FileLockStrategy\FILE_LOCK_FOLDER
‪const FILE_LOCK_FOLDER
Definition: FileLockStrategy.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Locking\FileLockStrategy\isAcquired
‪bool isAcquired()
Definition: FileLockStrategy.php:145
‪TYPO3\CMS\Core\Locking\FileLockStrategy
Definition: FileLockStrategy.php:28
‪TYPO3\CMS\Core\Locking\FileLockStrategy\getCapabilities
‪static int getCapabilities()
Definition: FileLockStrategy.php:161
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165