8000 [Filesystem] Keep executable permission when a file is copied · symfony/symfony@b8f8234 · GitHub
[go: up one dir, main page]

Skip to content

Commit b8f8234

Browse files
committed
[Filesystem] Keep executable permission when a file is copied
1 parent d277c16 commit b8f8234

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public function copy($originFile, $targetFile, $override = false)
6969
throw new IOException(sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile);
7070
}
7171

72+
if (is_executable($originFile)) {
73+
// User Executable | Group Executable | Other Executable
74+
chmod($targetFile, fileperms($targetFile) | 0x0040 | 0x0008 | 0x0001);
75+
}
76+
7277
if (stream_is_local($originFile) && $bytesCopied !== filesize($originFile)) {
7378
throw new IOException(sprintf('Failed to copy the whole content of "%s" to "%s %g bytes copied".', $originFile, $targetFile, $bytesCopied), 0, null, $originFile);
7479
}

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,4 +967,30 @@ public function testDumpFileOverwritesAnExistingFile()
967967
$this->assertFileExists($filename);
968968
$this->assertSame('bar', file_get_contents($filename));
969969
}
970+
971+
public function testCopyShouldKeepExecutionPermission()
972+
{
973+
$sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
974+
$targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
975+
976+
file_put_contents($sourceFilePath, 'SOURCE FILE');
977+
chmod($sourceFilePath, 0755);
978+
979+
$this->filesystem->copy($sourceFilePath, $targetFilePath);
980+
981+
$this->assertFilePermissions(755, $targetFilePath);
982+
}
983+
984+
public function testCopyShouldNotKeepWritePermissionOtherThanCurrentUser()
985+
{
986+
$sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
987+
$targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
988+
989+
file_put_contents($sourceFilePath, 'SOURCE FILE');
990+
chmod($sourceFilePath, 0777);
991+
992+
$this->filesystem->copy($sourceFilePath, $targetFilePath);
993+
994+
$this->assertFilePermissions(755, $targetFilePath);
995+
}
970996
}

0 commit comments

Comments
 (0)
0