Skip to content

Commit

Permalink
path normalization in case of different drives
Browse files Browse the repository at this point in the history
  • Loading branch information
crishoj committed Apr 26, 2020
1 parent ce6cb71 commit e75b87b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
30 changes: 13 additions & 17 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,34 +461,30 @@ public function makePathRelative($endPath, $startPath)
: [$path, null];
};

list($endPath, $endDriveLetter) = $stripDriveLetter($endPath);
list($startPath, $startDriveLetter) = $stripDriveLetter($startPath);

if ($endDriveLetter && $startDriveLetter && $endDriveLetter != $startDriveLetter) {
// Different drives
return $endDriveLetter.':'.$endPath;
}

// 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;
}
}

return $result;
};

$startPathArr = $normalizePathArray($startPathArr, static::isAbsolutePath($startPath));
$endPathArr = $normalizePathArray($endPathArr, static::isAbsolutePath($endPath));
list($endPath, $endDriveLetter) = $stripDriveLetter($endPath);
list($startPath, $startDriveLetter) = $stripDriveLetter($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;
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,9 @@ public function providePathsForMakePathRelative()
['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:/aa/bb', 'C:/aa', 'D:/aa/bb'],
['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) {
Expand Down

0 comments on commit e75b87b

Please sign in to comment.