From 78a93e5606a19d0fb490afc3c4a9b7ecd86e1515 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 12 Apr 2020 18:54:01 +0200 Subject: [PATCH 1/2] Fix test --- Tests/LockHandlerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/LockHandlerTest.php b/Tests/LockHandlerTest.php index d130803d44..b63e0c224f 100644 --- a/Tests/LockHandlerTest.php +++ b/Tests/LockHandlerTest.php @@ -24,7 +24,7 @@ class LockHandlerTest extends TestCase public function testConstructWhenRepositoryDoesNotExist() { $this->expectException('Symfony\Component\Filesystem\Exception\IOException'); - $this->expectExceptionMessage('Failed to create "/a/b/c/d/e": mkdir(): Permission denied.'); + $this->expectExceptionMessage('Failed to create "/a/b/c/d/e": mkdir(): Permission denied'); if (!getenv('USER') || 'root' === getenv('USER')) { $this->markTestSkipped('This test will fail if run under superuser'); } From 4934b0f1e79096d941733024e1fb45b2b48e6aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Rish=C3=B8j?= Date: Wed, 22 Apr 2020 19:34:57 +0200 Subject: [PATCH 2/2] [Filesystem] Handle paths on different drives --- Filesystem.php | 35 +++++++++++++++++------------------ Tests/FilesystemTest.php | 4 ++++ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Filesystem.php b/Filesystem.php index a8701533cb..e2812f8e22 100644 --- a/Filesystem.php +++ b/Filesystem.php @@ -455,28 +455,19 @@ public function makePathRelative($endPath, $startPath) $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; + $splitDriveLetter = function ($path) { + return (\strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) + ? [substr($path, 2), strtoupper($path[0])] + : [$path, null]; }; - $endPath = $stripDriveLetter($endPath); - $startPath = $stripDriveLetter($startPath); - - // Split the paths into arrays - $startPathArr = explode('/', trim($startPath, '/')); - $endPathArr = explode('/', trim($endPath, '/')); - - $normalizePathArray = function ($pathSegments, $absolute) { + $splitPath = function ($path, $absolute) { $result = []; - foreach ($pathSegments as $segment) { + foreach (explode('/', trim($path, '/')) as $segment) { if ('..' === $segment && ($absolute || \count($result))) { array_pop($result); - } elseif ('.' !== $segment) { + } elseif ('.' !== $segment && '' !== $segment) { $result[] = $segment; } } @@ -484,8 +475,16 @@ public function makePathRelative($endPath, $startPath) return $result; }; - $startPathArr = $normalizePathArray($startPathArr, static::isAbsolutePath($startPath)); - $endPathArr = $normalizePathArray($endPathArr, static::isAbsolutePath($endPath)); + list($endPath, $endDriveLetter) = $splitDriveLetter($endPath); + list($startPath, $startDriveLetter) = $splitDriveLetter($startPath); + + $startPathArr = $splitPath($startPath, static::isAbsolutePath($startPath)); + $endPathArr = $splitPath($endPath, static::isAbsolutePath($endPath)); + + if ($endDriveLetter && $startDriveLetter && $endDriveLetter != $startDriveLetter) { + // End path is on another drive, so no relative path exists + return $endDriveLetter.':/'.($endPathArr ? implode('/', $endPathArr).'/' : ''); + } // Find for which directory the common path stops $index = 0; diff --git a/Tests/FilesystemTest.php b/Tests/FilesystemTest.php index e9e7784a3a..8ac80437fe 100644 --- a/Tests/FilesystemTest.php +++ b/Tests/FilesystemTest.php @@ -1111,10 +1111,14 @@ public function providePathsForMakePathRelative() ['/../aa/bb/cc', '/aa/dd/..', 'bb/cc/'], ['/../../aa/../bb/cc', '/aa/dd/..', '../bb/cc/'], ['C:/aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'], + ['C:/aa/bb/cc', 'c:/aa/dd/..', 'bb/cc/'], ['c:/aa/../bb/cc', 'c:/aa/dd/..', '../bb/cc/'], ['C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'], ['C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'], ['C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'], + ['D:/', 'C:/aa/../bb/cc', 'D:/'], + ['D:/aa/bb', 'C:/aa', 'D:/aa/bb/'], + ['D:/../../aa/../bb/cc', 'C:/aa/dd/..', 'D:/bb/cc/'], ]; if ('\\' === \DIRECTORY_SEPARATOR) {