[Filesystem] Throw Exception on copying from an unreadable file or to an unwritable file

This commit is contained in:
David Otton 2014-05-21 19:12:51 +01:00 committed by Fabien Potencier
parent 85e08278ca
commit cd5da9b3c8
2 changed files with 54 additions and 2 deletions

View File

@ -51,8 +51,12 @@ class Filesystem
if ($doCopy) {
// https://bugs.php.net/bug.php?id=64634
$source = fopen($originFile, 'r');
$target = fopen($targetFile, 'w');
if (false === $source = @fopen($originFile, 'r')) {
throw new IOException(sprintf('Failed to copy "%s" to "%s" because source file could not be opened for reading.', $originFile, $targetFile), 0, null, $originFile);
}
if (false === $target = @fopen($targetFile, 'w')) {
throw new IOException(sprintf('Failed to copy "%s" to "%s" because target file could not be opened for writing.', $originFile, $targetFile), 0, null, $originFile);
}
stream_copy_to_stream($source, $target);
fclose($source);
fclose($target);

View File

@ -53,6 +53,27 @@ class FilesystemTest extends FilesystemTestCase
$this->filesystem->copy($sourceFilePath, $targetFilePath);
}
/**
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
*/
public function testCopyUnreadableFileFails()
{
// skip test on Windows; PHP can't easily set file as unreadable on Windows
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$this->markTestSkipped('This test cannot run on Windows.');;
}
$sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
$targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
file_put_contents($sourceFilePath, 'SOURCE FILE');
// make sure target cannot be read
$this->filesystem->chmod($sourceFilePath, 0222);
$this->filesystem->copy($sourceFilePath, $targetFilePath);
}
public function testCopyOverridesExistingFileIfModified()
{
$sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
@ -106,6 +127,33 @@ class FilesystemTest extends FilesystemTestCase
$this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
}
/**
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
*/
public function testCopyWithOverrideWithReadOnlyTargetFails()
{
// skip test on Windows; PHP can't easily set file as unwritable on Windows
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$this->markTestSkipped('This test cannot run on Windows.');;
}
$sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
$targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
file_put_contents($sourceFilePath, 'SOURCE FILE');
file_put_contents($targetFilePath, 'TARGET FILE');
// make sure both files have the same modification time
$modificationTime = time() - 1000;
touch($sourceFilePath, $modificationTime);
touch($targetFilePath, $modificationTime);
// make sure target is read-only
$this->filesystem->chmod($targetFilePath, 0444);
$this->filesystem->copy($sourceFilePath, $targetFilePath, true);
}
public function testCopyCreatesTargetDirectoryIfItDoesNotExist()
{
$sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';