Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Filesystem] Made sure Filesystem::dumpFile() overwrites an existing file #7820

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

Merged
merged 1 commit into from
Apr 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 6 additions & 5 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,17 @@ public function chgrp($files, $group, $recursive = false)
/**
* Renames a file.
*
* @param string $origin The origin filename
* @param string $target The new filename
* @param string $origin The origin filename
* @param string $target The new filename
* @param Boolean $overwrite Whether to overwrite the target if it already exists
*
* @throws IOException When target file already exists
* @throws IOException When origin cannot be renamed
*/
public function rename($origin, $target)
public function rename($origin, $target, $overwrite = false)
{
// we check that target does not exist
if (is_readable($target)) {
if (!$overwrite && is_readable($target)) {
throw new IOException(sprintf('Cannot rename because the target "%s" already exist.', $target));
}

Expand Down Expand Up @@ -452,7 +453,7 @@ public function dumpFile($filename, $content, $mode = 0666)
throw new IOException(sprintf('Failed to write file "%s".', $filename));
}

$this->rename($tmpFile, $filename);
$this->rename($tmpFile, $filename, true);
$this->chmod($filename, $mode);
}
}
25 changes: 25 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,20 @@ public function testRenameThrowsExceptionIfTargetAlreadyExists()
$this->filesystem->rename($file, $newPath);
}

public function testRenameOverwritesTheTargetIfItAlreadyExists()
{
$file = $this->workspace.DIRECTORY_SEPARATOR.'file';
$newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';

touch($file);
touch($newPath);

$this->filesystem->rename($file, $newPath, true);

$this->assertFileNotExists($file);
$this->assertFileExists($newPath);
}

/**
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
*/
Expand Down Expand Up @@ -894,6 +908,17 @@ public function testDumpFile()
$this->assertEquals(753, $this->getFilePermissions($filename));
}

public function testDumpFileOverwritesAnExistingFile()
{
$filename = $this->workspace.DIRECTORY_SEPARATOR.'foo.txt';
file_put_contents($filename, 'FOO BAR');

$this->filesystem->dumpFile($filename, 'bar');

$this->assertFileExists($filename);
$this->assertSame('bar', file_get_contents($filename));
}

/**
* Returns file permissions as three digits (i.e. 755)
*
Expand Down