8000 [Filesystem] improve messages on failure · symfony/symfony@56b8b9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 56b8b9c

Browse files
[Filesystem] improve messages on failure
1 parent 386555b commit 56b8b9c

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public function copy(string $originFile, string $targetFile, bool $overwriteNewe
5050

5151
if ($doCopy) {
5252
// https://bugs.php.net/64634
53-
if (false === $source = @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), 0, null, $originFile);
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);
5555
}
5656

5757
// Stream context created to allow files overwrite when using FTP stream wrapper - disabled by default
58-
if (false === $target = @fopen($targetFile, 'w', null, 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), 0, null, $originFile);
58+
if (!$target = self::box('fopen', $targetFile, 'w', null, 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);
6060
}
6161

6262
$bytesCopied = stream_copy_to_stream($source, $target);
@@ -70,7 +70,7 @@ public function copy(string $originFile, string $targetFile, bool $overwriteNewe
7070

7171
if ($originIsLocal) {
7272
// Like `cp`, preserve executable permission bits
73-
@chmod($targetFile, fileperms($targetFile) | (fileperms($originFile) & 0111));
73+
self::box('chmod', $targetFile, fileperms($targetFile) | (fileperms($originFile) & 0111));
7474

7575
if ($bytesCopied !== $bytesOrigin = filesize($originFile)) {
7676
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);
@@ -93,14 +93,8 @@ public function mkdir($dirs, int $mode = 0777)
9393
continue;
9494
}
9595

96-
if (!self::box('mkdir', $dir, $mode, true)) {
97-
if (!is_dir($dir)) {
98-
// The directory was not created by a concurrent process. Let's throw an exception with a developer friendly error message if we have one
99-
if (self::$lastError) {
100-
throw new IOException(sprintf('Failed to create "%s": ', $dir).self::$lastError, 0, null, $dir);
101-
}
102-
throw new IOException(sprintf('Failed to create "%s".', $dir), 0, null, $dir);
103-
}
96+
if (!self::box('mkdir', $dir, $mode, true) && !is_dir($dir)) {
97+
throw new IOException(sprintf('Failed to create "%s": ', $dir).self::$lastError, 0, null, $dir);
10498
}
10599
}
106100
}
@@ -141,9 +135,8 @@ public function exists($files)
141135
public function touch($files, int $time = null, int $atime = null)
142136
{
143137
foreach ($this->toIterable($files) as $file) {
144-
$touch = $time ? @touch($file, $time, $atime) : @touch($file);
145-
if (true !== $touch) {
146-
throw new IOException(sprintf('Failed to touch "%s".', $file), 0, null, $file);
138+
if (!($time ? self::box('touch', $file, $time, $atime) : self::box('touch', $file))) {
139+
throw new IOException(sprintf('Failed to touch "%s": ', $file).self::$lastError, 0, null, $file);
147140
}
148141
}
149142
}
@@ -194,8 +187,8 @@ public function remove($files)
194187
public function chmod($files, int $mode, int $umask = 0000, bool $recursive = false)
195188
{
196189
foreach ($this->toIterable($files) as $file) {
197-
if ((\PHP_VERSION_ID < 80000 || \is_int($mode)) && true !== @chmod($file, $mode & ~$umask)) {
198-
throw new IOException(sprintf('Failed to chmod file "%s".', $file), 0, null, $file);
190+
if ((\PHP_VERSION_ID < 80000 || \is_int($mode)) && !self::box('chmod', $file, $mode & ~$umask)) {
191+
throw new IOException(sprintf('Failed to chmod file "%s": ', $file).self::$lastError, 0, null, $file);
199192
}
200193
if ($recursive && is_dir($file) && !is_link($file)) {
201194
$this->chmod(new \FilesystemIterator($file), $mode, $umask, true);
@@ -219,12 +212,12 @@ public function chown($files, $user, bool $recursive = false)
219212
$this->chown(new \FilesystemIterator($file), $user, true);
220213
}
221214
if (is_link($file) && \function_exists('lchown')) {
222-
if (true !== @lchown($file, $user)) {
223-
throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file);
215+
if (!self::box('lchown', $file, $user)) {
216+
throw new IOException(sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file);
224217
}
225218
} else {
226-
if (true !== @chown($file, $user)) {
227-
throw new IOException(sprintf('Failed to chown file "%s".', $file), 0, null, $file);
219+
if (!self::box('chown', $file, $user)) {
220+
throw new IOException(sprintf('Failed to chown file "%s": ', $file).self::$lastError, 0, null, $file);
228221
}
229222
}
230223
}
@@ -246,12 +239,12 @@ public function chgrp($files, $group, bool $recursive = false)
246239
$this->chgrp(new \FilesystemIterator($file), $group, true);
247240
}
248241
if (is_link($file) && \function_exists('lchgrp')) {
249-
if (true !== @lchgrp($file, $group)) {
250-
throw new IOException(sprintf('Failed to chgrp file "%s".', $file), 0, null, $file);
242+
if (!self::box('lchgrp', $file, $group)) {
243+
throw new IOException(sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file);
251244
}
252245
} else {
253-
if (true !== @chgrp($file, $group)) {
254-
throw new IOException(sprintf('Failed to chgrp file "%s".', $file), 0, null, $file);
246+
if (!self::box('chgrp', $file, $group)) {
247+
throw new IOException(sprintf('Failed to chgrp file "%s": ', $file).self::$lastError, 0, null, $file);
255248
}
256249
}
257250
}
@@ -270,15 +263,15 @@ public function rename(string $origin, string $target, bool $overwrite = false)
270263
throw new IOException(sprintf('Cannot rename because the target "%s" already exists.', $target), 0, null, $target);
271264
}
272265

273-
if (true !== @rename($origin, $target)) {
266+
if (!self::box('rename', $origin, $target)) {
274267
if (is_dir($origin)) {
275268
// See https://bugs.php.net/54097 & https://php.net/rename#113943
276269
$this->mirror($origin, $target, null, ['override' => $overwrite, 'delete' => $overwrite]);
277270
$this->remove($origin);
278271

279272
return;
280273
}
281-
throw new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target), 0, null, $target);
274+
throw new IOException(sprintf('Cannot rename "%s" to "%s": ', $origin, $target).self::$lastError, 0, null, $target);
282275
}
283276
}
284277

@@ -372,7 +365,7 @@ private function linkException(string $origin, string $target, string $linkType)
372365
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);
373366
}
374367
}
375-
throw new IOException(sprintf('Failed to create "%s" link from "%s" to "%s".', $linkType, $origin, $target), 0, null, $target);
368+
throw new IOException(sprintf('Failed to create "%s" link from "%s" to "%s": ', $linkType, $origin, $target).self::$lastError, 0, null, $target);
376369
}
377370

378371
/**
@@ -594,18 +587,16 @@ public function tempnam(string $dir, string $prefix/*, string $suffix = ''*/)
594587

595588
// If no scheme or scheme is "file" or "gs" (Google Cloud) create temp file in local filesystem
596589
if ((null === $scheme || 'file' === $scheme || 'gs' === $scheme) && '' === $suffix) {
597-
$tmpFile = @tempnam($hierarchy, $prefix);
598-
599590
// If tempnam failed or no scheme return the filename otherwise prepend the scheme
600-
if (false !== $tmpFile) {
591+
if ($tmpFile = self::box('tempnam', $hierarchy, $prefix)) {
601592
if (null !== $scheme && 'gs' !== $scheme) {
602593
return $scheme.'://'.$tmpFile;
603594
}
604595

605596
return $tmpFile;
606597
}
607598

608-
throw new IOException('A temporary file could not be created.');
599+
throw new IOException('A temporary file could not be created: '.self::$lastError);
609600
}
610601

611602
// Loop until we create a valid temp file or have reached 10 attempts
@@ -615,20 +606,17 @@ public function tempnam(string $dir, string $prefix/*, string $suffix = ''*/)
615606

616607
// Use fopen instead of file_exists as some streams do not support stat
617608
// Use mode 'x+' to atomically check existence and create to avoid a TOCTOU vulnerability
618-
$handle = @fopen($tmpFile, 'x+');
619-
620-
// If unsuccessful restart the loop
621-
if (false === $handle) {
609+
if (!$handle = self::box('fopen', $tmpFile, 'x+')) {
622610
continue;
623611
}
624612

625613
// Close the file if it was successfully opened
626-
@fclose($handle);
614+
self::box('fclose', $handle);
627615

628616
return $tmpFile;
629617
}
630618

631-
throw new IOException('A temporary file could not be created.');
619+
throw new IOException('A temporary file could not be created: '.self::$lastError);
632620
}
633621

634622
/**
@@ -659,16 +647,16 @@ public function dumpFile(string $filename, $content)
659647
$tmpFile = $this->tempnam($dir, basename($filename));
660648

661649
try {
662-
if (false === @file_put_contents($tmpFile, $content)) {
663-
throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
650+
if (!self::box('file_put_contents', $tmpFile, $content)) {
651+
throw new IOException(sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
664652
}
665653

666-
@chmod($tmpFile, file_exists($filename) ? fileperms($filename) : 0666 & ~umask());
654+
self::box('chmod', $tmpFile, file_exists($filename) ? fileperms($filename) : 0666 & ~umask());
667655

668656
$this->rename($tmpFile, $filename, true);
669657
} finally {
670658
if (file_exists($tmpFile)) {
671-
@unlink($tmpFile);
659+< A0AA div class="diff-text-inner"> self::box('unlink', $tmpFile);
672660
}
673661
}
674662
}
@@ -696,8 +684,8 @@ public function appendToFile(string $filename, $content)
696684
throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
697685
}
698686

699-
if (false === @file_put_contents($filename, $content, \FILE_APPEND)) {
700-
throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
687+
if (!self::box('file_put_contents', $filename, $content, \FILE_APPEND)) {
688+
throw new IOException(sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
701689
}
702690
}
703691

0 commit comments

Comments
 (0)
0