| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Filesystem; |
| 13 |
|
| 14 |
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
| 15 |
use Symfony\Component\Filesystem\Exception\InvalidArgumentException; |
| 16 |
use Symfony\Component\Filesystem\Exception\IOException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class Filesystem |
| 24 |
{ |
| 25 |
private static ?string $lastError = null; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function copy(string $originFile, string $targetFile, bool $overwriteNewerFiles = false): void |
| 38 |
{ |
| 39 |
$originIsLocal = stream_is_local($originFile) || 0 === stripos($originFile, 'file://'); |
| 40 |
if ($originIsLocal && !is_file($originFile)) { |
| 41 |
throw new FileNotFoundException(\sprintf('Failed to copy "%s" because file does not exist.', $originFile), 0, null, $originFile); |
| 42 |
} |
| 43 |
|
| 44 |
$this->mkdir(\dirname($targetFile)); |
| 45 |
|
| 46 |
$doCopy = true; |
| 47 |
if (!$overwriteNewerFiles && !parse_url($originFile, \PHP_URL_HOST) && is_file($targetFile)) { |
| 48 |
$doCopy = filemtime($originFile) > filemtime($targetFile); |
| 49 |
} |
| 50 |
|
| 51 |
if ($doCopy) { |
| 52 |
|
| 53 |
if (!$source = self::box('fopen', $originFile, 'r')) { |
| 54 |
throw new IOException(\sprintf('Failed to copy "%s" to "%s" because source file could not be opened for reading: ', $originFile, $targetFile).self::$lastError, 0, null, $originFile); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
if (!$target = self::box('fopen', $targetFile, 'w', false, stream_context_create(['ftp' => ['overwrite' => true]]))) { |
| 59 |
throw new IOException(\sprintf('Failed to copy "%s" to "%s" because target file could not be opened for writing: ', $originFile, $targetFile).self::$lastError, 0, null, $originFile); |
| 60 |
} |
| 61 |
|
| 62 |
$bytesCopied = stream_copy_to_stream($source, $target); |
| 63 |
fclose($source); |
| 64 |
fclose($target); |
| 65 |
unset($source, $target); |
| 66 |
|
| 67 |
if (!is_file($targetFile)) { |
| 68 |
throw new IOException(\sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile); |
| 69 |
} |
| 70 |
|
| 71 |
if ($originIsLocal) { |
| 72 |
|
| 73 |
self::box('chmod', $targetFile, fileperms($originFile) & 0o777 & ~umask()); |
| 74 |
|
| 75 |
|
| 76 |
self::box('touch', $targetFile, filemtime($originFile)); |
| 77 |
|
| 78 |
if ($bytesCopied !== $bytesOrigin = filesize($originFile)) { |
| 79 |
throw new IOException(\sprintf('Failed to copy the whole content of "%s" to "%s" (%g of %g bytes copied).', $originFile, $targetFile, $bytesCopied, $bytesOrigin), 0, null, $originFile); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function mkdir(string|iterable $dirs, int $mode = 0o777): void |
| 91 |
{ |
| 92 |
foreach ($this->toIterable($dirs) as $dir) { |
| 93 |
if (is_dir($dir)) { |
| 94 |
continue; |
| 95 |
} |
| 96 |
|
| 97 |
if (!self::box('mkdir', $dir, $mode, true) && !is_dir($dir)) { |
| 98 |
throw new IOException(\sprintf('Failed to create "%s": ', $dir).self::$lastError, 0, null, $dir); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function exists(string|iterable $files): bool |
| 107 |
{ |
| 108 |
$maxPathLength = \PHP_MAXPATHLEN - 2; |
| 109 |
|
| 110 |
foreach ($this->toIterable($files) as $file) { |
| 111 |
if (\strlen($file) > $maxPathLength) { |
| 112 |
throw new IOException(\sprintf('Could not check if file exist because path length exceeds %d characters.', $maxPathLength), 0, null, $file); |
| 113 |
} |
| 114 |
|
| 115 |
if (!file_exists($file)) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function touch(string|iterable $files, ?int $time = null, ?int $atime = null): void |
| 132 |
{ |
| 133 |
foreach ($this->toIterable($files) as $file) { |
| 134 |
if (!($time ? self::box('touch', $file, $time, $atime) : self::box('touch', $file))) { |
| 135 |
throw new IOException(\sprintf('Failed to touch "%s": ', $file).self::$lastError, 0, null, $file); |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function remove(string|iterable $files): void |
| 146 |
{ |
| 147 |
if ($files instanceof \Traversable) { |
| 148 |
$files = iterator_to_array($files, false); |
| 149 |
} elseif (!\is_array($files)) { |
| 150 |
$files = [$files]; |
| 151 |
} |
| 152 |
|
| 153 |
self::doRemove($files, false); |
| 154 |
} |
| 155 |
|
| 156 |
private static function doRemove(array $files, bool $isRecursive): void |
| 157 |
{ |
| 158 |
$files = array_reverse($files); |
| 159 |
foreach ($files as $file) { |
| 160 |
if (is_link($file)) { |
| 161 |
|
| 162 |
if (!(self::box('unlink', $file) || '\\' !== \DIRECTORY_SEPARATOR || self::box('rmdir', $file)) && file_exists($file)) { |
| 163 |
throw new IOException(\sprintf('Failed to remove symlink "%s": ', $file).self::$lastError); |
| 164 |
} |
| 165 |
} elseif (is_dir($file)) { |
| 166 |
if (!$isRecursive) { |
| 167 |
$tmpName = \dirname(realpath($file)).'/.!'.strrev(strtr(base64_encode(random_bytes(2)), '/=', '-!')); |
| 168 |
|
| 169 |
if (file_exists($tmpName)) { |
| 170 |
try { |
| 171 |
self::doRemove([$tmpName], true); |
| 172 |
} catch (IOException) { |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (!file_exists($tmpName) && self::box('rename', $file, $tmpName)) { |
| 177 |
$origFile = $file; |
| 178 |
$file = $tmpName; |
| 179 |
} else { |
| 180 |
$origFile = null; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$filesystemIterator = new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS); |
| 185 |
self::doRemove(iterator_to_array($filesystemIterator, true), true); |
| 186 |
|
| 187 |
if (!self::box('rmdir', $file) && file_exists($file) && !$isRecursive) { |
| 188 |
$lastError = self::$lastError; |
| 189 |
|
| 190 |
if (null !== $origFile && self::box('rename', $file, $origFile)) { |
| 191 |
$file = $origFile; |
| 192 |
} |
| 193 |
|
| 194 |
throw new IOException(\sprintf('Failed to remove directory "%s": ', $file).$lastError); |
| 195 |
} |
| 196 |
} elseif (!self::box('unlink', $file) && ((self::$lastError && str_contains(self::$lastError, 'Permission denied')) || file_exists($file))) { |
| 197 |
throw new IOException(\sprintf('Failed to remove file "%s": ', $file).self::$lastError); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function chmod(string|iterable $files, int $mode, int $umask = 0o000, bool $recursive = false): void |
| 212 |
{ |
| 213 |
foreach ($this->toIterable($files) as $file) { |
| 214 |
if (!self::box('chmod', $file, $mode & ~$umask)) { |
| 215 |
throw new IOException(\sprintf('Failed to chmod file "%s": ', $file).self::$lastError, 0, null, $file); |
| 216 |
} |
| 217 |
if ($recursive && is_dir($file) && !is_link($file)) { |
| 218 |
$this->chmod(new \FilesystemIterator($file), $mode, $umask, true); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public function chown(string|iterable $files, string|int $user, bool $recursive = false): void |
| 236 |
{ |
| 237 |
foreach ($this->toIterable($files) as $file) { |
| 238 |
if ($recursive && is_dir($file) && !is_link($file)) { |
| 239 |
$this->chown(new \FilesystemIterator($file), $user, true); |
| 240 |
} |
| 241 |
if (is_link($file) && \function_exists('lchown')) { |
| 242 |
if (!self::box('lchown', $file, $user)) { |
| 243 |
throw new IOException(\sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file); |
| 244 |
} |
| 245 |
} else { |
| 246 |
if (!self::box('chown', $file, $user)) { |
| 247 |
throw new IOException(\sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public function chgrp(string|iterable $files, string|int $group, bool $recursive = false): void |
| 266 |
{ |
| 267 |
foreach ($this->toIterable($files) as $file) { |
| 268 |
if ($recursive && is_dir($file) && !is_link($file)) { |
| 269 |
$this->chgrp(new \FilesystemIterator($file), $group, true); |
| 270 |
} |
| 271 |
if (is_link($file) && \function_exists('lchgrp')) { |
| 272 |
if (!self::box('lchgrp', $file, $group)) { |
| 273 |
throw new IOException(\sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file); |
| 274 |
} |
| 275 |
} else { |
| 276 |
if (!self::box('chgrp', $file, $group)) { |
| 277 |
throw new IOException(\sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function rename(string $origin, string $target, bool $overwrite = false): void |
| 290 |
{ |
| 291 |
|
| 292 |
if (!$overwrite && $this->isReadable($target)) { |
| 293 |
throw new IOException(\sprintf('Cannot rename because the target "%s" already exists.', $target), 0, null, $target); |
| 294 |
} |
| 295 |
|
| 296 |
if (!self::box('rename', $origin, $target)) { |
| 297 |
if (is_dir($origin)) { |
| 298 |
|
| 299 |
$this->mirror($origin, $target, null, ['override' => $overwrite, 'delete' => $overwrite]); |
| 300 |
$this->remove($origin); |
| 301 |
|
| 302 |
return; |
| 303 |
} |
| 304 |
throw new IOException(\sprintf('Cannot rename "%s" to "%s": ', $origin, $target).self::$lastError, 0, null, $target); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
private function isReadable(string $filename): bool |
| 314 |
{ |
| 315 |
$maxPathLength = \PHP_MAXPATHLEN - 2; |
| 316 |
|
| 317 |
if (\strlen($filename) > $maxPathLength) { |
| 318 |
throw new IOException(\sprintf('Could not check if file is readable because path length exceeds %d characters.', $maxPathLength), 0, null, $filename); |
| 319 |
} |
| 320 |
|
| 321 |
return is_readable($filename); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public function symlink(string $originDir, string $targetDir, bool $copyOnWindows = false): void |
| 330 |
{ |
| 331 |
self::assertFunctionExists('symlink'); |
| 332 |
|
| 333 |
if ('\\' === \DIRECTORY_SEPARATOR) { |
| 334 |
$originDir = strtr($originDir, '/', '\\'); |
| 335 |
$targetDir = strtr($targetDir, '/', '\\'); |
| 336 |
|
| 337 |
if ($copyOnWindows) { |
| 338 |
$this->mirror($originDir, $targetDir); |
| 339 |
|
| 340 |
return; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
$this->mkdir(\dirname($targetDir)); |
| 345 |
|
| 346 |
if (is_link($targetDir)) { |
| 347 |
if (readlink($targetDir) === $originDir) { |
| 348 |
return; |
| 349 |
} |
| 350 |
$this->remove($targetDir); |
| 351 |
} |
| 352 |
|
| 353 |
if (!self::box('symlink', $originDir, $targetDir)) { |
| 354 |
$this->linkException($originDir, $targetDir, 'symbolic'); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public function hardlink(string $originFile, string|iterable $targetFiles): void |
| 367 |
{ |
| 368 |
self::assertFunctionExists('link'); |
| 369 |
|
| 370 |
if (!$this->exists($originFile)) { |
| 371 |
throw new FileNotFoundException(null, 0, null, $originFile); |
| 372 |
} |
| 373 |
|
| 374 |
if (!is_file($originFile)) { |
| 375 |
throw new FileNotFoundException(\sprintf('Origin file "%s" is not a file.', $originFile)); |
| 376 |
} |
| 377 |
|
| 378 |
foreach ($this->toIterable($targetFiles) as $targetFile) { |
| 379 |
if (is_file($targetFile)) { |
| 380 |
if (fileinode($originFile) === fileinode($targetFile)) { |
| 381 |
continue; |
| 382 |
} |
| 383 |
$this->remove($targetFile); |
| 384 |
} |
| 385 |
|
| 386 |
if (!self::box('link', $originFile, $targetFile)) { |
| 387 |
$this->linkException($originFile, $targetFile, 'hard'); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
private function linkException(string $origin, string $target, string $linkType): never |
| 396 |
{ |
| 397 |
if (self::$lastError) { |
| 398 |
if ('\\' === \DIRECTORY_SEPARATOR && str_contains(self::$lastError, 'error code(1314)')) { |
| 399 |
throw new IOException(\sprintf('Unable to create "%s" link due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?', $linkType), 0, null, $target); |
| 400 |
} |
| 401 |
} |
| 402 |
throw new IOException(\sprintf('Failed to create "%s" link from "%s" to "%s": ', $linkType, $origin, $target).self::$lastError, 0, null, $target); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public function readlink(string $path, bool $canonicalize = false): ?string |
| 417 |
{ |
| 418 |
if (!$canonicalize && !is_link($path)) { |
| 419 |
return null; |
| 420 |
} |
| 421 |
|
| 422 |
if ($canonicalize) { |
| 423 |
if (!$this->exists($path)) { |
| 424 |
return null; |
| 425 |
} |
| 426 |
|
| 427 |
return realpath($path); |
| 428 |
} |
| 429 |
|
| 430 |
return readlink($path); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public function makePathRelative(string $endPath, string $startPath): string |
| 437 |
{ |
| 438 |
if (!$this->isAbsolutePath($startPath)) { |
| 439 |
throw new InvalidArgumentException(\sprintf('The start path "%s" is not absolute.', $startPath)); |
| 440 |
} |
| 441 |
|
| 442 |
if (!$this->isAbsolutePath($endPath)) { |
| 443 |
throw new InvalidArgumentException(\sprintf('The end path "%s" is not absolute.', $endPath)); |
| 444 |
} |
| 445 |
|
| 446 |
$originalEndPath = $endPath; |
| 447 |
|
| 448 |
|
| 449 |
if ('\\' === \DIRECTORY_SEPARATOR) { |
| 450 |
$endPath = str_replace('\\', '/', $endPath); |
| 451 |
$startPath = str_replace('\\', '/', $startPath); |
| 452 |
} |
| 453 |
|
| 454 |
$splitDriveLetter = static fn ($path) => (\strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) |
| 455 |
? [substr($path, 2), strtoupper($path[0])] |
| 456 |
: [$path, null]; |
| 457 |
|
| 458 |
$splitPath = static function ($path) { |
| 459 |
$result = []; |
| 460 |
|
| 461 |
foreach (explode('/', trim($path, '/')) as $segment) { |
| 462 |
if ('..' === $segment) { |
| 463 |
array_pop($result); |
| 464 |
} elseif ('.' !== $segment && '' !== $segment) { |
| 465 |
$result[] = $segment; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
return $result; |
| 470 |
}; |
| 471 |
|
| 472 |
[$endPath, $endDriveLetter] = $splitDriveLetter($endPath); |
| 473 |
[$startPath, $startDriveLetter] = $splitDriveLetter($startPath); |
| 474 |
|
| 475 |
$startPathArr = $splitPath($startPath); |
| 476 |
$endPathArr = $splitPath($endPath); |
| 477 |
|
| 478 |
if ($endDriveLetter && $startDriveLetter && $endDriveLetter != $startDriveLetter) { |
| 479 |
|
| 480 |
return $endDriveLetter.':/'.($endPathArr ? implode('/', $endPathArr).'/' : ''); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
$index = 0; |
| 485 |
while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) { |
| 486 |
++$index; |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
if (1 === \count($startPathArr) && '' === $startPathArr[0]) { |
| 491 |
$depth = 0; |
| 492 |
} else { |
| 493 |
$depth = \count($startPathArr) - $index; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
$traverser = str_repeat('../', $depth); |
| 498 |
|
| 499 |
$endPathRemainder = implode('/', \array_slice($endPathArr, $index)); |
| 500 |
|
| 501 |
|
| 502 |
$relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : ''); |
| 503 |
|
| 504 |
|
| 505 |
if (str_ends_with($relativePath, '/') && is_file($originalEndPath)) { |
| 506 |
$relativePath = substr($relativePath, 0, -1); |
| 507 |
} |
| 508 |
|
| 509 |
return '' === $relativePath ? './' : $relativePath; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
public function mirror(string $originDir, string $targetDir, ?\Traversable $iterator = null, array $options = []): void |
| 530 |
{ |
| 531 |
$targetDir = rtrim($targetDir, '/\\'); |
| 532 |
$originDir = rtrim($originDir, '/\\'); |
| 533 |
$originDirLen = \strlen($originDir); |
| 534 |
|
| 535 |
if (!$this->exists($originDir)) { |
| 536 |
throw new IOException(\sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir); |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) { |
| 541 |
$deleteIterator = $iterator; |
| 542 |
if (null === $deleteIterator) { |
| 543 |
$flags = \FilesystemIterator::SKIP_DOTS; |
| 544 |
$deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST); |
| 545 |
} |
| 546 |
$targetDirLen = \strlen($targetDir); |
| 547 |
foreach ($deleteIterator as $file) { |
| 548 |
$origin = $originDir.substr($file->getPathname(), $targetDirLen); |
| 549 |
if (!$this->exists($origin)) { |
| 550 |
$this->remove($file); |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
$copyOnWindows = $options['copy_on_windows'] ?? false; |
| 556 |
|
| 557 |
if (null === $iterator) { |
| 558 |
$flags = $copyOnWindows ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS; |
| 559 |
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST); |
| 560 |
} |
| 561 |
|
| 562 |
$this->mkdir($targetDir); |
| 563 |
$filesCreatedWhileMirroring = []; |
| 564 |
|
| 565 |
foreach ($iterator as $file) { |
| 566 |
if ($file->getPathname() === $targetDir || $file->getRealPath() === $targetDir || isset($filesCreatedWhileMirroring[$file->getRealPath()])) { |
| 567 |
continue; |
| 568 |
} |
| 569 |
|
| 570 |
$target = $targetDir.substr($file->getPathname(), $originDirLen); |
| 571 |
$filesCreatedWhileMirroring[$target] = true; |
| 572 |
|
| 573 |
if (!$copyOnWindows && is_link($file)) { |
| 574 |
$this->symlink($file->getLinkTarget(), $target); |
| 575 |
} elseif (is_dir($file)) { |
| 576 |
$this->mkdir($target); |
| 577 |
} elseif (is_file($file)) { |
| 578 |
$this->copy($file, $target, $options['override'] ?? false); |
| 579 |
} else { |
| 580 |
throw new IOException(\sprintf('Unable to guess "%s" file type.', $file), 0, null, $file); |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
public function isAbsolutePath(string $file): bool |
| 589 |
{ |
| 590 |
return Path::isAbsolute($file); |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public function tempnam(string $dir, string $prefix, string $suffix = ''): string |
| 603 |
{ |
| 604 |
[$scheme, $hierarchy] = $this->getSchemeAndHierarchy($dir); |
| 605 |
|
| 606 |
|
| 607 |
if ((null === $scheme || 'file' === $scheme || 'gs' === $scheme) && '' === $suffix) { |
| 608 |
|
| 609 |
if ($tmpFile = self::box('tempnam', $hierarchy, $prefix)) { |
| 610 |
if (null !== $scheme && 'gs' !== $scheme) { |
| 611 |
return $scheme.'://'.$tmpFile; |
| 612 |
} |
| 613 |
|
| 614 |
return $tmpFile; |
| 615 |
} |
| 616 |
|
| 617 |
throw new IOException('A temporary file could not be created: '.self::$lastError); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
for ($i = 0; $i < 10; ++$i) { |
| 622 |
|
| 623 |
$tmpFile = $dir.'/'.$prefix.bin2hex(random_bytes(4)).$suffix; |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
if (!$handle = self::box('fopen', $tmpFile, 'x+')) { |
| 628 |
continue; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
self::box('fclose', $handle); |
| 633 |
|
| 634 |
return $tmpFile; |
| 635 |
} |
| 636 |
|
| 637 |
throw new IOException('A temporary file could not be created: '.self::$lastError); |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
public function dumpFile(string $filename, $content): void |
| 648 |
{ |
| 649 |
if (\is_array($content)) { |
| 650 |
throw new \TypeError(\sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__)); |
| 651 |
} |
| 652 |
|
| 653 |
$dir = \dirname($filename); |
| 654 |
|
| 655 |
if (is_link($filename) && $linkTarget = $this->readlink($filename)) { |
| 656 |
$this->dumpFile(Path::makeAbsolute($linkTarget, $dir), $content); |
| 657 |
|
| 658 |
return; |
| 659 |
} |
| 660 |
|
| 661 |
if (!is_dir($dir)) { |
| 662 |
$this->mkdir($dir); |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
$tmpFile = $this->tempnam($dir, basename($filename)); |
| 668 |
|
| 669 |
try { |
| 670 |
if (false === self::box('file_put_contents', $tmpFile, $content)) { |
| 671 |
throw new IOException(\sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename); |
| 672 |
} |
| 673 |
|
| 674 |
self::box('chmod', $tmpFile, self::box('fileperms', $filename) ?: 0o666 & ~umask()); |
| 675 |
|
| 676 |
$this->rename($tmpFile, $filename, true); |
| 677 |
} finally { |
| 678 |
if (file_exists($tmpFile)) { |
| 679 |
if ('\\' === \DIRECTORY_SEPARATOR && !is_writable($tmpFile)) { |
| 680 |
self::box('chmod', $tmpFile, self::box('fileperms', $tmpFile) | 0o200); |
| 681 |
} |
| 682 |
|
| 683 |
self::box('unlink', $tmpFile); |
| 684 |
} |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
public function appendToFile(string $filename, $content, bool $lock = false): void |
| 697 |
{ |
| 698 |
if (\is_array($content)) { |
| 699 |
throw new \TypeError(\sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__)); |
| 700 |
} |
| 701 |
|
| 702 |
$dir = \dirname($filename); |
| 703 |
|
| 704 |
if (!is_dir($dir)) { |
| 705 |
$this->mkdir($dir); |
| 706 |
} |
| 707 |
|
| 708 |
if (false === self::box('file_put_contents', $filename, $content, \FILE_APPEND | ($lock ? \LOCK_EX : 0))) { |
| 709 |
throw new IOException(\sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
public function readFile(string $filename): string |
| 719 |
{ |
| 720 |
if (is_dir($filename)) { |
| 721 |
throw new IOException(\sprintf('Failed to read file "%s": File is a directory.', $filename)); |
| 722 |
} |
| 723 |
|
| 724 |
$content = self::box('file_get_contents', $filename); |
| 725 |
if (false === $content) { |
| 726 |
throw new IOException(\sprintf('Failed to read file "%s": ', $filename).self::$lastError, 0, null, $filename); |
| 727 |
} |
| 728 |
|
| 729 |
return $content; |
| 730 |
} |
| 731 |
|
| 732 |
private function toIterable(string|iterable $files): iterable |
| 733 |
{ |
| 734 |
return is_iterable($files) ? $files : [$files]; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
private function getSchemeAndHierarchy(string $filename): array |
| 741 |
{ |
| 742 |
$components = explode('://', $filename, 2); |
| 743 |
|
| 744 |
return 2 === \count($components) ? [$components[0], $components[1]] : [null, $components[0]]; |
| 745 |
} |
| 746 |
|
| 747 |
private static function assertFunctionExists(string $func): void |
| 748 |
{ |
| 749 |
if (!\function_exists($func)) { |
| 750 |
throw new IOException(\sprintf('Unable to perform filesystem operation because the "%s()" function has been disabled.', $func)); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
private static function box(string $func, mixed ...$args): mixed |
| 755 |
{ |
| 756 |
self::assertFunctionExists($func); |
| 757 |
|
| 758 |
self::$lastError = null; |
| 759 |
set_error_handler(self::handleError(...)); |
| 760 |
try { |
| 761 |
return $func(...$args); |
| 762 |
} finally { |
| 763 |
restore_error_handler(); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
public static function handleError(int $type, string $msg): void |
| 771 |
{ |
| 772 |
self::$lastError = $msg; |
| 773 |
} |
| 774 |
} |