From 2bc11505f4c2a932101748bfbc582993c91274fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Auswo=CC=88ger?= Date: Thu, 6 Apr 2017 20:04:32 +0200 Subject: [PATCH] [Filesystem] Fixed makePathRelative --- .../Component/Filesystem/Filesystem.php | 42 +++++++++---------- .../Filesystem/Tests/FilesystemTest.php | 12 ++++++ 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index edfc1b9d46..ac2a1c7857 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -358,25 +358,28 @@ class Filesystem $startPath = str_replace('\\', '/', $startPath); } + $stripDriveLetter = function ($path) { + if (strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) { + return substr($path, 2); + } + + return $path; + }; + + $endPath = $stripDriveLetter($endPath); + $startPath = $stripDriveLetter($startPath); + // Split the paths into arrays $startPathArr = explode('/', trim($startPath, '/')); $endPathArr = explode('/', trim($endPath, '/')); - if ('/' !== $startPath[0]) { - array_shift($startPathArr); - } - - if ('/' !== $endPath[0]) { - array_shift($endPathArr); - } - - $normalizePathArray = function ($pathSegments) { + $normalizePathArray = function ($pathSegments, $absolute) { $result = array(); foreach ($pathSegments as $segment) { - if ('..' === $segment) { + if ('..' === $segment && ($absolute || count($result))) { array_pop($result); - } else { + } elseif ('.' !== $segment) { $result[] = $segment; } } @@ -384,8 +387,8 @@ class Filesystem return $result; }; - $startPathArr = $normalizePathArray($startPathArr); - $endPathArr = $normalizePathArray($endPathArr); + $startPathArr = $normalizePathArray($startPathArr, static::isAbsolutePath($startPath)); + $endPathArr = $normalizePathArray($endPathArr, static::isAbsolutePath($endPath)); // Find for which directory the common path stops $index = 0; @@ -394,19 +397,14 @@ class Filesystem } // Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels) - if (count($startPathArr) === 1 && $startPathArr[0] === '') { + if (1 === count($startPathArr) && '' === $startPathArr[0]) { $depth = 0; } else { $depth = count($startPathArr) - $index; } - // When we need to traverse from the start, and we are starting from a root path, don't add '../' - if ('/' === $startPath[0] && 0 === $index && 0 === $depth) { - $traverser = ''; - } else { - // Repeated "../" for each level need to reach the common path - $traverser = str_repeat('../', $depth); - } + // Repeated "../" for each level need to reach the common path + $traverser = str_repeat('../', $depth); $endPathRemainder = implode('/', array_slice($endPathArr, $index)); @@ -500,7 +498,7 @@ class Filesystem { return strspn($file, '/\\', 0, 1) || (strlen($file) > 3 && ctype_alpha($file[0]) - && substr($file, 1, 1) === ':' + && ':' === substr($file, 1, 1) && strspn($file, '/\\', 2, 1) ) || null !== parse_url($file, PHP_URL_SCHEME) diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index ab2395cd00..d2058d0a01 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -847,6 +847,7 @@ class FilesystemTest extends FilesystemTestCase 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('usr/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'), array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', 'src/Symfony/'), array('/aa/bb', '/aa/bb', './'), array('/aa/bb', '/aa/bb/', './'), @@ -878,6 +879,17 @@ class FilesystemTest extends FilesystemTestCase array('C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'), array('C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'), array('C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'), + array('aa/bb', 'aa/cc', '../bb/'), + array('aa/cc', 'bb/cc', '../../aa/cc/'), + array('aa/bb', 'aa/./cc', '../bb/'), + array('aa/./bb', 'aa/cc', '../bb/'), + array('aa/./bb', 'aa/./cc', '../bb/'), + array('../../', '../../', './'), + array('../aa/bb/', 'aa/bb/', '../../../aa/bb/'), + array('../../../', '../../', '../'), + array('', '', './'), + array('', 'aa/', '../'), + array('aa/', '', 'aa/'), ); if ('\\' === DIRECTORY_SEPARATOR) {