Debian Codegraph @

lib/3rdParty/composer/symfony/filesystem/Filesystem.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
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 * Provides basic utility to manipulate the file system.
20 *
21 * @author Fabien Potencier <[email protected]>
22 */
23 class Filesystem
24 {
25 private static ?string $lastError = null;
26
27 /**
28 * Copies a file.
29 *
30 * If the target file is older than the origin file, it's always overwritten.
31 * If the target file is newer, it is overwritten only when the
32 * $overwriteNewerFiles option is set to true.
33 *
34 * @throws FileNotFoundException When originFile doesn't exist
35 * @throws IOException When copy fails
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 // https://bugs.php.net/64634
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 // Stream context created to allow files overwrite when using FTP stream wrapper - disabled by default
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 // Like `cp`, preserve the source mode masked by the umask
73 self::box('chmod', $targetFile, fileperms($originFile) & 0o777 & ~umask());
74
75 // Like `cp`, preserve the file modification time
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 * Creates a directory recursively.
87 *
88 * @throws IOException On any directory creation failure
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 * Checks the existence of files or directories.
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 * Sets access and modification time of file.
125 *
126 * @param int|null $time The touch time as a Unix timestamp, if not supplied the current system time is used
127 * @param int|null $atime The access time as a Unix timestamp, if not supplied the current system time is used
128 *
129 * @throws IOException When touch fails
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 * Removes files or directories.
142 *
143 * @throws IOException When removal fails
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 // See https://bugs.php.net/52176
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 * Change mode for an array of files or directories.
204 *
205 * @param int $mode The new mode (octal)
206 * @param int $umask The mode mask (octal)
207 * @param bool $recursive Whether change the mod recursively or not
208 *
209 * @throws IOException When the change fails
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 * Change the owner of an array of files or directories.
225 *
226 * This method always throws on Windows, as the underlying PHP function is not supported.
227 *
228 * @see https://php.net/chown
229 *
230 * @param string|int $user A user name or number
231 * @param bool $recursive Whether change the owner recursively or not
232 *
233 * @throws IOException When the change fails
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 * Change the group of an array of files or directories.
255 *
256 * This method always throws on Windows, as the underlying PHP function is not supported.
257 *
258 * @see https://php.net/chgrp
259 *
260 * @param string|int $group A group name or number
261 * @param bool $recursive Whether change the group recursively or not
262 *
263 * @throws IOException When the change fails
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 * Renames a file or a directory.
285 *
286 * @throws IOException When target file or directory already exists
287 * @throws IOException When origin cannot be renamed
288 */
289 public function rename(string $origin, string $target, bool $overwrite = false): void
290 {
291 // we check that target does not exist
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 // See https://bugs.php.net/54097 & https://php.net/rename#113943
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 * Tells whether a file exists and is readable.
310 *
311 * @throws IOException When windows path is longer than 258 characters
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 * Creates a symbolic link or copy a directory.
326 *
327 * @throws IOException When symlink fails
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 * Creates a hard link, or several hard links to a file.
360 *
361 * @param string|string[] $targetFiles The target file(s)
362 *
363 * @throws FileNotFoundException When original file is missing or not a file
364 * @throws IOException When link fails, including if link already exists
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 * @param string $linkType Name of the link type, typically 'symbolic' or 'hard'
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 * Resolves links in paths.
407 *
408 * With $canonicalize = false (default)
409 * - if $path does not exist or is not a link, returns null
410 * - if $path is a link, returns the next direct target of the link without considering the existence of the target
411 *
412 * With $canonicalize = true
413 * - if $path does not exist, returns null
414 * - if $path exists, returns its absolute fully resolved final version
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 * Given an existing path, convert it to a path relative to a given starting path.
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 // Normalize separators on Windows
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 // End path is on another drive, so no relative path exists
480 return $endDriveLetter.':/'.($endPathArr ? implode('/', $endPathArr).'/' : '');
481 }
482
483 // Find for which directory the common path stops
484 $index = 0;
485 while (isset($startPathArr[$index]) && isset($endPathArr[$index]) && $startPathArr[$index] === $endPathArr[$index]) {
486 ++$index;
487 }
488
489 // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
490 if (1 === \count($startPathArr) && '' === $startPathArr[0]) {
491 $depth = 0;
492 } else {
493 $depth = \count($startPathArr) - $index;
494 }
495
496 // Repeated "../" for each level need to reach the common path
497 $traverser = str_repeat('../', $depth);
498
499 $endPathRemainder = implode('/', \array_slice($endPathArr, $index));
500
501 // Construct $endPath from traversing to the common path, then to the remaining $endPath
502 $relativePath = $traverser.('' !== $endPathRemainder ? $endPathRemainder.'/' : '');
503
504 // Remove ending "/" if $endPath points to an existing file
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 * Mirrors a directory to another.
514 *
515 * Copies files and directories from the origin directory into the target directory. By default:
516 *
517 * - existing files in the target directory will be overwritten, except if they are newer (see the `override` option)
518 * - files in the target directory that do not exist in the source directory will not be deleted (see the `delete` option)
519 *
520 * @param \Traversable|null $iterator Iterator that filters which files and directories to copy, if null a recursive iterator is created
521 * @param array $options An array of boolean options
522 * Valid options are:
523 * - $options['override'] If true, target files newer than origin files are overwritten (see copy(), defaults to false)
524 * - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink(), defaults to false)
525 * - $options['delete'] Whether to delete files that are not in the source directory (defaults to false)
526 *
527 * @throws IOException When file type is unknown
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 // Iterate in destination folder to remove obsolete entries
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 * Returns whether the given path is absolute.
587 */
588 public function isAbsolutePath(string $file): bool
589 {
590 return Path::isAbsolute($file);
591 }
592
593 /**
594 * Creates a temporary file with support for custom stream wrappers.
595 *
596 * @param string $prefix The prefix of the generated temporary filename
597 * Note: Windows uses only the first three characters of prefix
598 * @param string $suffix The suffix of the generated temporary filename
599 *
600 * @return string The new temporary filename (with path), or throw an exception on failure
601 */
602 public function tempnam(string $dir, string $prefix, string $suffix = ''): string
603 {
604 [$scheme, $hierarchy] = $this->getSchemeAndHierarchy($dir);
605
606 // If no scheme or scheme is "file" or "gs" (Google Cloud) create temp file in local filesystem
607 if ((null === $scheme || 'file' === $scheme || 'gs' === $scheme) && '' === $suffix) {
608 // If tempnam failed or no scheme return the filename otherwise prepend the scheme
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 // Loop until we create a valid temp file or have reached 10 attempts
621 for ($i = 0; $i < 10; ++$i) {
622 // Create a unique filename
623 $tmpFile = $dir.'/'.$prefix.bin2hex(random_bytes(4)).$suffix;
624
625 // Use fopen instead of file_exists as some streams do not support stat
626 // Use mode 'x+' to atomically check existence and create to avoid a TOCTOU vulnerability
627 if (!$handle = self::box('fopen', $tmpFile, 'x+')) {
628 continue;
629 }
630
631 // Close the file if it was successfully opened
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 * Atomically dumps content into a file.
642 *
643 * @param string|resource $content The data to write into the file
644 *
645 * @throws IOException if the file cannot be written to
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 // Will create a temp file with 0600 access rights
666 // when the filesystem supports chmod.
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 * Appends content to an existing file.
690 *
691 * @param string|resource $content The content to append
692 * @param bool $lock Whether the file should be locked when writing to it
693 *
694 * @throws IOException If the file is not writable
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 * Returns the content of a file as a string.
715 *
716 * @throws IOException If the file cannot be read
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 * Gets a 2-tuple of scheme (may be null) and hierarchical part of a filename (e.g. file:///tmp -> [file, tmp]).
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 * @internal
769 */
770 public static function handleError(int $type, string $msg): void
771 {
772 self::$lastError = $msg;
773 }
774 }