From bc93787a0d771e82bfbc1ba3b262fc68a32a5753 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Sat, 7 Apr 2012 21:19:12 +0100 Subject: [PATCH 1/2] [Filesystem] Fixed relative path calculation for paths with various combinations of trailing directory separators. --- src/Symfony/Component/Filesystem/Filesystem.php | 3 ++- src/Symfony/Component/Filesystem/Tests/FilesystemTest.php | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index b99b65162e..c0fe4b48d7 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -185,7 +185,8 @@ class Filesystem } // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels) - $depth = substr_count(substr($startPath, $offset), DIRECTORY_SEPARATOR) + 1; + $diffPath = trim(substr($startPath, $offset), DIRECTORY_SEPARATOR); + $depth = substr_count($diffPath, DIRECTORY_SEPARATOR) + 1; // Repeated "../" for each level need to reach the common path $traverser = str_repeat('../', $depth); diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index b6c48fab6f..faa189c971 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -443,6 +443,9 @@ class FilesystemTest extends \PHPUnit_Framework_TestCase { $paths = array( array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component', '../'), + array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/src/Symfony/Component/', '../'), + array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component', '../'), + array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component/', '../'), array('var/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../'), array('/usr/lib/symfony/', '/var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'), array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', '../src/Symfony/'), From 22e2ad80c90f4bacbc2ebf4c8ea0d089ac57bc58 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Sat, 7 Apr 2012 21:52:26 +0100 Subject: [PATCH 2/2] [Filesystem] Fixed relative path calculation for end path which is a subdirectory of the start path. --- src/Symfony/Component/Filesystem/Filesystem.php | 2 +- src/Symfony/Component/Filesystem/Tests/FilesystemTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index c0fe4b48d7..a63c5a313f 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -186,7 +186,7 @@ class Filesystem // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels) $diffPath = trim(substr($startPath, $offset), DIRECTORY_SEPARATOR); - $depth = substr_count($diffPath, DIRECTORY_SEPARATOR) + 1; + $depth = strlen($diffPath) > 0 ? substr_count($diffPath, DIRECTORY_SEPARATOR) + 1 : 0; // Repeated "../" for each level need to reach the common path $traverser = str_repeat('../', $depth); diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index faa189c971..80b5167231 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -448,7 +448,7 @@ class FilesystemTest extends \PHPUnit_Framework_TestCase array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component/', '../'), array('var/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../'), array('/usr/lib/symfony/', '/var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'), - array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', '../src/Symfony/'), + array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', 'src/Symfony/'), ); // fix directory separator