10000 Verify bytes written in Filesystem::dumpFile() by lstrojny · Pull Request #36952 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Verify bytes written in Filesystem::dumpFile() #36952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Verify bytes written in Filesystem::dumpFile()
  • Loading branch information
lstrojny committed May 25, 2020
commit 576a778f27057f76fb59efbfcf4d48397259a949
14 changes: 10 additions & 4 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,8 @@ public function tempnam($dir, $prefix)
/**
* Atomically dumps content into a file.
*
* @param string $filename The file to be written to
* @param string $content The data to write into the file
* @param string $filename The file to be written to
* @param string|resource|array $content The data to write into the file
*
* @throws IOException if the file cannot be written to
*/
Expand All @@ -684,8 +684,14 @@ public function dumpFile($filename, $content)
// when the filesystem supports chmod.
$tmpFile = $this->tempnam($dir, basename($filename));

if (false === @file_put_contents($tmpFile, $content)) {
throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
if (\is_resource($content)) {
$expectedSize = fstat($content)['size'];
} else {
$expectedSize = array_sum(array_map('strlen', (array) $content));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want to add support for arrays - they worked "by chance" but that's not supported

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are not dealing with arrays in 3.4 it would break the current behavior – the one that is only explicitly deprecated in 4.0. That doesn’t seem the right thing to do.

}

if ($expectedSize !== ($actualSize = @file_put_contents($tmpFile, $content))) {
throw new IOException(sprintf('Failed to write file "%s". Wrote %d of %d bytes.', $filename, $actualSize, $expectedSize), 0, null, $filename);
}

@chmod($tmpFile, file_exists($filename) ? fileperms($filename) : 0666 & ~umask());
Expand Down
0