From b8f8234fdd245873050a746f5029d488b4206339 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Sat, 29 Nov 2014 11:51:28 +0100 Subject: [PATCH] [Filesystem] Keep executable permission when a file is copied --- .../Component/Filesystem/Filesystem.php | 5 ++++ .../Filesystem/Tests/FilesystemTest.php | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index fc626db63f..e17d889631 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -69,6 +69,11 @@ class Filesystem throw new IOException(sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile); } + if (is_executable($originFile)) { + // User Executable | Group Executable | Other Executable + chmod($targetFile, fileperms($targetFile) | 0x0040 | 0x0008 | 0x0001); + } + if (stream_is_local($originFile) && $bytesCopied !== filesize($originFile)) { throw new IOException(sprintf('Failed to copy the whole content of "%s" to "%s %g bytes copied".', $originFile, $targetFile, $bytesCopied), 0, null, $originFile); } diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 2790e5e4e3..a55d635905 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -967,4 +967,30 @@ class FilesystemTest extends FilesystemTestCase $this->assertFileExists($filename); $this->assertSame('bar', file_get_contents($filename)); } + + public function testCopyShouldKeepExecutionPermission() + { + $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file'; + $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file'; + + file_put_contents($sourceFilePath, 'SOURCE FILE'); + chmod($sourceFilePath, 0755); + + $this->filesystem->copy($sourceFilePath, $targetFilePath); + + $this->assertFilePermissions(755, $targetFilePath); + } + + public function testCopyShouldNotKeepWritePermissionOtherThanCurrentUser() + { + $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file'; + $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file'; + + file_put_contents($sourceFilePath, 'SOURCE FILE'); + chmod($sourceFilePath, 0777); + + $this->filesystem->copy($sourceFilePath, $targetFilePath); + + $this->assertFilePermissions(755, $targetFilePath); + } }