Skip to content

Commit

Permalink
Fix support for UNC paths in normalizePath, refs composer#9993
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Jul 12, 2021
1 parent 078aaa6 commit cc81f5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Composer/Util/Filesystem.php
Expand Up @@ -544,7 +544,13 @@ public function normalizePath($path)
$parts = array();
$path = strtr($path, '\\', '/');
$prefix = '';
$absolute = false;
$absolute = '';

// extract windows UNC paths e.g. \\foo\bar
if (strpos($path, '//') === 0 && \strlen($path) > 2) {
$absolute = '//';
$path = substr($path, 2);
}

// extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive:
if (preg_match('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) {
Expand All @@ -553,13 +559,13 @@ public function normalizePath($path)
}

if (strpos($path, '/') === 0) {
$absolute = true;
$absolute = '/';
$path = substr($path, 1);
}

$up = false;
foreach (explode('/', $path) as $chunk) {
if ('..' === $chunk && ($absolute || $up)) {
if ('..' === $chunk && ($absolute !== '' || $up)) {
array_pop($parts);
$up = !(empty($parts) || '..' === end($parts));
} elseif ('.' !== $chunk && '' !== $chunk) {
Expand All @@ -568,7 +574,7 @@ public function normalizePath($path)
}
}

return $prefix.($absolute ? '/' : '').implode('/', $parts);
return $prefix.((string) $absolute).implode('/', $parts);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Composer/Test/Util/FilesystemTest.php
Expand Up @@ -220,6 +220,7 @@ public function provideNormalizedPaths()
array('../src', 'Foo/Bar/../../../src'),
array('c:../b', 'c:.\\..\\a\\..\\b'),
array('phar://c:../Foo', 'phar://c:../Foo'),
array('//foo/bar', '\\\\foo\\bar'),
);
}

Expand Down

0 comments on commit cc81f5b

Please sign in to comment.