symphonycms /
symphony-2
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * @package toolkit |
||||||
| 4 | */ |
||||||
| 5 | /** |
||||||
| 6 | * The Mutex class is a crude locking class that generates files |
||||||
| 7 | * with a specific time to live. It has basic functions to create a |
||||||
| 8 | * lock, release a lock or refresh a lock. |
||||||
| 9 | */ |
||||||
| 10 | class Mutex |
||||||
| 11 | { |
||||||
| 12 | /** |
||||||
| 13 | * An associative array of files that have been locked by the Mutex |
||||||
| 14 | * class, with the key the filename, and the values an associative array |
||||||
| 15 | * with `time` and `ttl` values. |
||||||
| 16 | * @var array |
||||||
| 17 | */ |
||||||
| 18 | private static $lockFiles; |
||||||
| 19 | |||||||
| 20 | /** |
||||||
| 21 | * Creates a lock file if one does not already exist with a certain |
||||||
| 22 | * time to live (TTL) at a specific path. If a lock already exists, |
||||||
| 23 | * false will be returned otherwise boolean depending if a lock |
||||||
| 24 | * file was created successfully or not. |
||||||
| 25 | * |
||||||
| 26 | * @param string $id |
||||||
| 27 | * The name of the lock file, which gets obfuscated using |
||||||
| 28 | * generateLockFileName. |
||||||
| 29 | * @param integer $ttl |
||||||
| 30 | * The length, in seconds, that the lock should exist for. Defaults |
||||||
| 31 | * to 5. |
||||||
| 32 | * @param string $path |
||||||
| 33 | * The path the lock should be written, defaults to the current |
||||||
| 34 | * working directory |
||||||
| 35 | * @return boolean |
||||||
| 36 | */ |
||||||
| 37 | public static function acquire($id, $ttl = 5, $path = '.') |
||||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||||
| 38 | { |
||||||
| 39 | $lockFile = self::__generateLockFileName($id, $path); |
||||||
|
0 ignored issues
–
show
The method
__generateLockFileName() does not exist on Mutex.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 40 | |||||||
| 41 | // If this thread already has acquired the lock, return true. |
||||||
| 42 | if (isset(self::$lockFiles[$lockFile])) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 43 | $age = time() - self::$lockFiles[$lockFile]['time']; |
||||||
| 44 | return ($age < $ttl ? false : true); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | // Disable log temporarily because we actually depend on fopen() |
||||||
| 48 | // failing with E_WARNING here and we do not want Symphony to throw |
||||||
| 49 | // errors or spam logfiles. |
||||||
| 50 | try { |
||||||
| 51 | GenericErrorHandler::$logDisabled = true; |
||||||
| 52 | $lock = fopen($lockFile, 'xb'); |
||||||
| 53 | GenericErrorHandler::$logDisabled = false; |
||||||
| 54 | |||||||
| 55 | self::$lockFiles[$lockFile] = array('time' => time(), 'ttl' => $ttl); |
||||||
| 56 | fclose($lock); |
||||||
|
0 ignored issues
–
show
It seems like
$lock can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 57 | |||||||
| 58 | return true; |
||||||
| 59 | } catch (Exception $ex) { |
||||||
| 60 | // If, for some reason, lock file was not unlinked before, |
||||||
| 61 | // remove it if it is old enough. |
||||||
| 62 | if (file_exists($lockFile)) { |
||||||
| 63 | $age = time() - filemtime($lockFile); |
||||||
| 64 | |||||||
| 65 | if ($age > $ttl) { |
||||||
| 66 | unlink($lockFile); |
||||||
| 67 | } |
||||||
| 68 | } |
||||||
| 69 | |||||||
| 70 | // Return false anyway - just in case two or more threads |
||||||
| 71 | // do the same check and unlink at the same time. |
||||||
| 72 | return false; |
||||||
| 73 | } |
||||||
| 74 | } |
||||||
| 75 | |||||||
| 76 | /** |
||||||
| 77 | * Removes a lock file. This is the only way a lock file can be removed |
||||||
| 78 | * |
||||||
| 79 | * @param string $id |
||||||
| 80 | * The original name of the lock file (note that this will be different from |
||||||
| 81 | * the name of the file saved on the file system) |
||||||
| 82 | * @param string $path |
||||||
| 83 | * The path to the lock, defaults to the current working directory |
||||||
| 84 | * @return boolean |
||||||
| 85 | */ |
||||||
| 86 | public static function release($id, $path = '.') |
||||||
|
0 ignored issues
–
show
|
|||||||
| 87 | { |
||||||
| 88 | $lockFile = self::__generateLockFileName($id, $path); |
||||||
| 89 | |||||||
| 90 | if (!empty(self::$lockFiles[$lockFile])) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 91 | unset(self::$lockFiles[$lockFile]); |
||||||
| 92 | return General::deleteFile($lockFile, false); |
||||||
| 93 | } |
||||||
| 94 | |||||||
| 95 | return false; |
||||||
| 96 | } |
||||||
| 97 | |||||||
| 98 | /** |
||||||
| 99 | * Updates a lock file to 'keep alive' for another 'x' seconds. |
||||||
| 100 | * |
||||||
| 101 | * @param string $id |
||||||
| 102 | * The name of the lock file, which gets obfuscated using |
||||||
| 103 | * `__generateLockFileName()`. |
||||||
| 104 | * @param integer $ttl |
||||||
| 105 | * The length, in seconds, that the lock should be extended by. |
||||||
| 106 | * Defaults to 5. |
||||||
| 107 | * @param string $path |
||||||
| 108 | * The path to the lock, defaults to the current working directory |
||||||
| 109 | * @return boolean |
||||||
| 110 | */ |
||||||
| 111 | public static function refresh($id, $ttl = 5, $path = '.') |
||||||
|
0 ignored issues
–
show
|
|||||||
| 112 | { |
||||||
| 113 | return touch(self::__generateLockFileName($id, $path), time() + $ttl, time()); |
||||||
| 114 | } |
||||||
| 115 | |||||||
| 116 | /** |
||||||
| 117 | * Checks if a lock exists, purely on the presence on the lock file. |
||||||
| 118 | * This function takes the unobfuscated lock name |
||||||
| 119 | * Others should not depend on value returned by this function, |
||||||
| 120 | * because by the time it returns, the lock file can be created or deleted |
||||||
| 121 | * by another thread. |
||||||
| 122 | * |
||||||
| 123 | * @since Symphony 2.2 |
||||||
| 124 | * @param string $id |
||||||
| 125 | * The name of the lock file, which gets obfuscated using |
||||||
| 126 | * generateLockFileName. |
||||||
| 127 | * @param string $path |
||||||
| 128 | * The path the lock should be written, defaults to the current |
||||||
| 129 | * working directory |
||||||
| 130 | * @return boolean |
||||||
| 131 | */ |
||||||
| 132 | public static function lockExists($id, $path) |
||||||
| 133 | { |
||||||
| 134 | $lockFile = self::__generateLockFileName($id, $path); |
||||||
| 135 | |||||||
| 136 | return file_exists($lockFile); |
||||||
| 137 | } |
||||||
| 138 | |||||||
| 139 | /** |
||||||
| 140 | * Generates a lock filename using an MD5 hash of the `$id` and |
||||||
| 141 | * `$path`. Lock files are given a .lock extension |
||||||
| 142 | * |
||||||
| 143 | * @param string $id |
||||||
| 144 | * The name of the lock file to be obfuscated |
||||||
| 145 | * @param string $path |
||||||
| 146 | * The path the lock should be written |
||||||
| 147 | * @return string |
||||||
| 148 | */ |
||||||
| 149 | private static function __generateLockFileName($id, $path = null) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 150 | { |
||||||
| 151 | // This function is called from all others, so it is a good point to initialize Mutex handling. |
||||||
| 152 | if (!is_array(self::$lockFiles)) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 153 | self::$lockFiles = array(); |
||||||
| 154 | register_shutdown_function(array(__CLASS__, '__shutdownCleanup')); |
||||||
| 155 | } |
||||||
| 156 | |||||||
| 157 | if (is_null($path)) { |
||||||
| 158 | $path = sys_get_temp_dir(); |
||||||
| 159 | } |
||||||
| 160 | |||||||
| 161 | // Use realpath, because shutdown function may operate in different working directory. |
||||||
| 162 | // So we need to be sure that path is absolute. |
||||||
| 163 | return rtrim(realpath($path), '/') . '/' . md5($id) . '.lock'; |
||||||
| 164 | } |
||||||
| 165 | |||||||
| 166 | /** |
||||||
| 167 | * Releases all locks on expired files. |
||||||
| 168 | * |
||||||
| 169 | * @since Symphony 2.2.2 |
||||||
| 170 | */ |
||||||
| 171 | public static function __shutdownCleanup() |
||||||
| 172 | { |
||||||
| 173 | $now = time(); |
||||||
| 174 | |||||||
| 175 | if (is_array(self::$lockFiles)) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 176 | foreach (self::$lockFiles as $lockFile => $meta) { |
||||||
| 177 | if (($now - $meta['time'] > $meta['ttl']) && file_exists($lockFile)) { |
||||||
| 178 | unlink($lockFile); |
||||||
| 179 | } |
||||||
| 180 | } |
||||||
| 181 | } |
||||||
| 182 | } |
||||||
| 183 | } |
||||||
| 184 |