-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 $content The data to write into the file | ||
* | ||
* @throws IOException if the file cannot be written to | ||
*/ | ||
|
@@ -684,8 +684,20 @@ 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); | ||
$expectedSize = false; | ||
|
||
if (\is_resource($content)) { | ||
$stat = fstat($content); | ||
if (false !== $stat) { | ||
$expectedSize = $stat['size'] - ftell($content); | ||
} | ||
} else { | ||
$expectedSize = array_sum(array_map('strlen', (array) $content)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
||
$actualSize = @file_put_contents($tmpFile, $content); | ||
if ((false === $expectedSize && false === $actualSize) && ($actualSize !== $expectedSize)) { | ||
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()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry that's not generic enough: a userland stream wrapper could very well decode to return 0 ou -1 here. The only safe way would be to stream "manually".
BUT I would really like to know how PHP behaves in out of space situations here.
Could you give it a try? Mounting a loopback filesystem could be a way to try maybe? (I don't know how better than Google sorry).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea to check again! Here is an example using a loopback device:
It even leaves behind a file:
If this is indeed cross-platform behavior than I misdiagnosed the issue here – in fact there wouldn't be an issue. The tempfile couldn’t be fully written and
file_put_contents()
returns false, the exception is thrown, everything is fine.Setup for the loopback device:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks for double-checking. This means we can close as everything is fine: an error will happen as expected, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that behavior is consistent across platform, yes. I don’t have access to Windows nor would I know how to simulate this. Do you have anybody in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would make sense that this behaves the same on Windows.
I'm not able to verify. But let's close until someone proves there is something to do.
Thanks for having a look!