Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Filesystem] Throw Exception on copying from an unreadable file or to…
… an unwritable file
  • Loading branch information
djotto authored and fabpot committed Jul 9, 2014
1 parent 85e0827 commit cd5da9b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Symfony/Component/Filesystem/Filesystem.php
Expand Up @@ -51,8 +51,12 @@ public function copy($originFile, $targetFile, $override = false)

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);
Expand Down
48 changes: 48 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Expand Up @@ -53,6 +53,27 @@ public function testCopyFails()
$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';
Expand Down Expand Up @@ -106,6 +127,33 @@ public function testCopyOverridesExistingFileIfForced()
$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';
Expand Down

0 comments on commit cd5da9b

Please sign in to comment.