8000 Follow symlinks when dumping files · symfony/symfony@41df08a · GitHub
[go: up one dir, main page]

Skip to content

Commit 41df08a

Browse files
committed
Follow symlinks when dumping files
1 parent 8c0bc94 commit 41df08a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,10 @@ public function dumpFile(string $filename, $content)
667667
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
668668
}
669669

670+
if (is_link($filename) && $linkTarget = $this->readlink($filename, true)) {
671+
$filename = $linkTarget;
672+
}
673+
670674
$dir = \dirname($filename);
671675

672676
if (!is_dir($dir)) {

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,21 @@ public function testDumpFileOverwritesAnExistingFile()
16051605
$this->assertStringEqualsFile($filename, 'bar');
16061606
}
16071607

1608+
public function testDumpFileFollowsSymlink()
1609+
{
1610+
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo.txt';
1611+
file_put_contents($filename, 'FOO BAR');
1612+
$linkname = $this->workspace.\DIRECTORY_SEPARATOR.'bar.txt';
1613+
$this->filesystem->symlink($filename, $linkname);
1614+
1615+
$this->filesystem->dumpFile($linkname, 'bar');
1616+
1617+
$this->assertFileExists($filename);
1618+
$this->assertFileExists($linkname);
1619+
$this->assertStringEqualsFile($filename, 'bar');
1620+
$this->assertStringEqualsFile($linkname, 'bar');
1621+
}
1622+
16081623
public function testDumpFileWithFileScheme()
16091624
{
16101625
$scheme = 'file://';
@@ -1678,6 +1693,21 @@ public function testAppendToFileWithResource()
16781693
}
16791694
}
16801695

1696+
public function testAppendToFileFollowsSymlink()
1697+
{
1698+
$filename = $this->workspace.\DIRECTORY_SEPARATOR.'foo.txt';
1699+
file_put_contents($filename, 'foo');
1700+
$linkname = $this->workspace.\DIRECTORY_SEPARATOR.'bar.txt';
1701+
$this->filesystem->symlink($filename, $linkname);
1702+
1703+
$this->filesystem->appendToFile($linkname, 'bar');
1704+
1705+
$this->assertFileExists($filename);
1706+
$this->assertFileExists($linkname);
1707+
$this->assertStringEqualsFile($filename, 'foobar');
1708+
$this->assertStringEqualsFile($linkname, 'foobar');
1709+
}
1710+
16811711
public function testAppendToFileWithScheme()
16821712
{
16831713
$scheme = 'file://';

0 commit comments

Comments
 (0)
0