8000 feature #12653 [Filesystem] Keep executable permission when a file is… · symfony/symfony@8e6b0bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 8e6b0bb

Browse files
committed
feature #12653 [Filesystem] Keep executable permission when a file is copied (joelwurtz)
This PR was merged into the 2.7 branch. Discussion ---------- [Filesystem] Keep executable permission when a file is copied | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | / | License | MIT | Doc PR | / I made this PR in order to have the same behavior as the default `cp` unix command: permissions are not keep when copying a file at the exception of the execution flag (unless `-p` is passed to the command). Commits ------- b8f8234 [Filesystem] Keep executable permission when a file is copied
2 parents 5f8d71b + b8f8234 commit 8e6b0bb

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
@@ -995,4 +995,30 @@ public function testDumpFileOverwritesAnExistingFile()
995995
$this->assertFileExists($filename);
996996
$this->assertSame('bar', file_get_contents($filename));
997997
}
998+
999+
public function testCopyShouldKeepExecutionPermission()
1000+
{
1001+
$sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
1002+
$targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
1003+
1004+
file_put_contents($sourceFilePath, 'SOURCE FILE');
1005+
chmod($sourceFilePath, 0755);
1006+
1007+
$this->filesystem->copy($sourceFilePath, $targetFilePath);
1008+
1009+
$this->assertFilePermissions(755, $targetFilePath);
1010+
}
1011+
1012+
public function testCopyShouldNotKeepWritePermissionOtherThanCurrentUser()
1013+
{
1014+
$sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file';
1015+
$targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file';
1016+
1017+
file_put_contents($sourceFilePath, 'SOURCE FILE');
1018+
chmod($sourceFilePath, 0777);
1019+
1020+
$this->filesystem->copy($sourceFilePath, $targetFilePath);
1021+
1022+
$this->assertFilePermissions(755, $targetFilePath);
1023+
}
9981024
}

0 commit comments

Comments
 (0)
0