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

Skip to content

Commit 1bbcff0

Browse files
bug #18054 [Filesystem] Fix false positive in ->remove() (nicolas-grekas)
This PR was merged into the 2.3 branch. Discussion ---------- [Filesystem] Fix false positive in ->remove() | Q | A | ------------- | --- | Branch | 2.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 3efd900 [Filesystem] Fix false positive in ->remove()
2 parents 35666f0 + 3efd900 commit 1bbcff0

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,23 @@ public function remove($files)
143143
$files = iterator_to_array($this->toIterator($files));
144144
$files = array_reverse($files);
145145
foreach ($files as $file) {
146+
if (@(unlink($file) || rmdir($file))) {
147+
continue;
148+
}
146149
if (is_link($file)) {
147-
// Workaround https://bugs.php.net/52176
148-
if (!@unlink($file) && !@rmdir($file)) {
149-
$error = error_get_last();
150-
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
151-
}
150+
// See https://bugs.php.net/52176
151+
$error = error_get_last();
152+
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
152153
} elseif (is_dir($file)) {
153154
$this->remove(new \FilesystemIterator($file));
154155

155156
if (!@rmdir($file)) {
156157
$error = error_get_last();
157158
throw new IOException(sprintf('Failed to remove directory "%s": %s.', $file, $error['message']));
158159
}
159-
} elseif ($this->exists($file)) {
160-
if (!@unlink($file)) {
161-
$error = error_get_last();
162-
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
163-
}
160+
} elseif (file_exists($file)) {
161+
$error = error_get_last();
162+
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
164163
}
165164
}
166165
}
@@ -403,7 +402,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
403402
}
404403

405404
$copyOnWindows = false;
406-
if (isset($options['copy_on_windows']) && !function_exists('symlink')) {
405+
if (isset($options['copy_on_windows'])) {
407406
$copyOnWindows = $options['copy_on_windows'];
408407
}
409408

@@ -420,7 +419,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
420419
$target = str_replace($originDir, $targetDir, $file->getPathname());
421420

422421
if ($copyOnWindows) {
423-
if (is_link($file) || is_file($file)) {
422+
if (is_file($file)) {
424423
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
425424
} elseif (is_dir($file)) {
426425
$this->mkdir($target);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ public static function setUpBeforeClass()
3838
if ('\\' === DIRECTORY_SEPARATOR && null === self::$symlinkOnWindows) {
3939
$target = tempnam(sys_get_temp_dir(), 'sl');
4040
$link = sys_get_temp_dir().'/sl'.microtime(true).mt_rand();
41-
if (@symlink($target, $link)) {
42-
self::$symlinkOnWindows = @is_link($link);
43-
unlink($link);
44-
}
41+
self::$symlinkOnWindows = @symlink($target, $link) && is_link($link);
42+
@unlink($link);
4543
unlink($target);
4644
}
4745
}
@@ -61,6 +59,7 @@ protected function tearDown()
6159
foreach ($this->longPathNamesWindows as $path) {
6260
exec('DEL '.$path);
6361
}
62+
$this->longPathNamesWindows = array();
6463
}
6564

6665
$this->filesystem->remove($this->workspace);
@@ -350,7 +349,7 @@ public function testRemoveCleansInvalidLinks()
350349

351350
// create symlink to nonexistent dir
352351
rmdir($basePath.'dir');
353-
$this->assertFalse(is_dir($basePath.'dir-link'));
352+
$this->assertFalse('\\' === DIRECTORY_SEPARATOR ? @readlink($basePath.'dir-link') : is_dir($basePath.'dir-link'));
354353

355354
$this->filesystem->remove($basePath);
356355

@@ -742,6 +741,8 @@ public function testRemoveSymlink()
742741
$this->filesystem->remove($link);
743742

744743
$this->assertTrue(!is_link($link));
744+
$this->assertTrue(!is_file($link));
745+
$this->assertTrue(!is_dir($link));
745746
}
746747

747748
public function testSymlinkIsOverwrittenIfPointsToDifferentTarget()
@@ -915,7 +916,7 @@ public function testMirrorCopiesLinks()
915916
$this->filesystem->mirror($sourcePath, $targetPath);
916917

917918
$this->assertTrue(is_dir($targetPath));
918-
$this->assertFileEquals($sourcePath.'file1', $targetPath.DIRECTORY_SEPARATOR.'link1');
919+
$this->assertFileEquals($sourcePath.'file1', $targetPath.'link1');
919920
$this->assertTrue(is_link($targetPath.DIRECTORY_SEPARATOR.'link1'));
920921
}
921922

@@ -935,7 +936,7 @@ public function testMirrorCopiesLinkedDirectoryContents()
935936
$this->filesystem->mirror($sourcePath, $targetPath);
936937

937938
$this->assertTrue(is_dir($targetPath));
938-
$this->assertFileEquals($sourcePath.'/nested/file1.txt', $targetPath.DIRECTORY_SEPARATOR.'link1/file1.txt');
939+
$this->assertFileEquals($sourcePath.'/nested/file1.txt', $targetPath.'link1/file1.txt');
939940
$this->assertTrue(is_link($targetPath.DIRECTORY_SEPARATOR.'link1'));
940941
}
941942

@@ -959,7 +960,7 @@ public function testMirrorCopiesRelativeLinkedContents()
959960
$this->filesystem->mirror($sourcePath, $targetPath);
960961

961962
$this->assertTrue(is_dir($targetPath));
962-
$this->assertFileEquals($sourcePath.'/nested/file1.txt', $targetPath.DIRECTORY_SEPARATOR.'link1/file1.txt');
963+
$this->assertFileEquals($sourcePath.'/nested/file1.txt', $targetPath.'link1/file1.txt');
963964
$this->assertTrue(is_link($targetPath.DIRECTORY_SEPARATOR.'link1'));
964965
$this->assertEquals('nested', readlink($targetPath.DIRECTORY_SEPARATOR.'link1'));
965966
}
@@ -1089,7 +1090,7 @@ private function markAsSkippedIfChmodIsMissing()
10891090

10901091
private function markAsSkippedIfPosixIsMissing()
10911092
{
1092-
if ('\\' === DIRECTORY_SEPARATOR || !function_exists('posix_isatty')) {
1093+
if (!function_exists('posix_isatty')) {
10931094
$this->markTestSkipped('POSIX is not supported');
10941095
}
10951096
}

0 commit comments

Comments
 (0)