diff --git a/src/main/php/io/Path.class.php b/src/main/php/io/Path.class.php index 06cd5df45..6f2017003 100755 --- a/src/main/php/io/Path.class.php +++ b/src/main/php/io/Path.class.php @@ -69,10 +69,7 @@ public static function compose(array $args): self { * @return self */ public static function real($arg, $wd= null): self { - return new self(self::real0( - self::pathFor(is_array($arg) ? $arg : [$arg]), - $wd ?? getcwd() - )); + return new self(self::real0(self::pathFor(is_array($arg) ? $arg : [$arg]), $wd)); } /** Returns the name of this path */ @@ -181,7 +178,7 @@ private static function real0($path, $wd) { $normalized= strtoupper($drive).':'; $components= explode(DIRECTORY_SEPARATOR, substr($path, 3)); } else if (null === $wd) { - throw new IllegalStateException('Cannot resolve '.$path); + return self::real0(getcwd().DIRECTORY_SEPARATOR.$path, null); } else { return self::real0(self::pathFor([$wd]).DIRECTORY_SEPARATOR.$path, null); } @@ -193,22 +190,37 @@ private static function real0($path, $wd) { } else if ('..' === $component) { $normalized= substr($normalized, 0, strrpos($normalized, DIRECTORY_SEPARATOR)); $check= true; + } else if ($check) { + $node= $normalized.DIRECTORY_SEPARATOR.$component; + if (false === ($stat= lstat($node))) { + \xp::gc(__FILE__); + $check= false; + } else if (0120000 === ($stat[2] & 0120000)) { + $node= self::real0(readlink($node), $normalized); + } + $normalized= $node; } else { $normalized.= DIRECTORY_SEPARATOR.$component; - if ($check) { - $stat= lstat($normalized); - if (false === $stat) { - \xp::gc(__FILE__); - $check= false; - } else if (0120000 === ($stat[2] & 0120000)) { - $normalized= readlink($normalized); - } - } } } return $normalized; } + /** + * Checks whether this path contains another path, resolving filesystem links. + * + * @see php://getcwd + * @param string|self|io.File|io.Folder|io.collections.IOElement $arg + * @return bool + */ + public function contains($arg, $wd= null) { + $compare= $arg instanceof self ? $arg->path : self::pathFor([$arg]); + return strlen($compare) && str_starts_with( + rtrim(self::real0($compare, $wd), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, + rtrim(self::real0($this->path, $wd), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, + ); + } + /** * Returns the real URI for this path, resolving links if necessary. * If no working directory is given, the current working directory is @@ -219,7 +231,7 @@ private static function real0($path, $wd) { * @return string */ public function asURI($wd= null): string { - return self::real0($this->path, $wd ?? getcwd()); + return self::real0($this->path, $wd); } /** @@ -232,7 +244,7 @@ public function asURI($wd= null): string { * @return self */ public function asRealpath($wd= null): self { - return new self(self::real0($this->path, $wd ?? getcwd())); + return new self(self::real0($this->path, $wd)); } /** diff --git a/src/test/php/io/unittest/PathTest.class.php b/src/test/php/io/unittest/PathTest.class.php index db794f116..e8eccc69c 100755 --- a/src/test/php/io/unittest/PathTest.class.php +++ b/src/test/php/io/unittest/PathTest.class.php @@ -1,6 +1,6 @@ path, Path::real('..', $folder)->toString()); } + #[Test] + public function real_with_relative_working_directory() { + Assert::equals((new Path('.', 'README.md'))->asRealpath(), Path::real('README.md', '.')); + } + #[Test] public function real_with_array() { $folder= $this->existingFolder(); @@ -187,18 +192,36 @@ public function relative_as_realpath_with_non_existant_components($components) { Assert::equals($current, Path::compose($components)->asRealpath($current)->toString()); } - #[Test, VerifyRuntime(os: "!^Win")] + #[Test, VerifyRuntime(os: '^(?!WIN)')] public function links_resolved_in_realpath() { $temp= Environment::tempDir(); $link= new Path($temp, 'link-to-temp'); if (false === symlink($temp, $link)) { - $this->skip('Cannot create '.$link.' -> '.$temp); + throw new OperationFailed('Cannot create '.$link.' -> '.$temp); } $resolved= (new Path($link))->asRealpath()->toString(); unlink($link); - Assert::equals($temp, $resolved); + Assert::equals(rtrim($temp, DIRECTORY_SEPARATOR), rtrim($resolved, DIRECTORY_SEPARATOR)); + } + + #[Test, VerifyRuntime(os: '^(?!WIN)')] + public function relative_links_resolved_in_realpath() { + $temp= Environment::tempDir(); + + $child= new Folder($temp, 'child-of-temp'); + $child->exists() || $child->create(); + + $link= new Path($child, 'link-to-temp'); + if (false === symlink($child->getURI().'..', $link)) { + throw new OperationFailed('Cannot create '.$link.' -> ..'); + } + + $resolved= (new Path($link))->asRealpath()->toString(); + $child->unlink(); + + Assert::equals(rtrim($temp, DIRECTORY_SEPARATOR), rtrim($resolved, DIRECTORY_SEPARATOR)); } #[Test] @@ -366,6 +389,26 @@ public function normalize_uppercases_drive_letter() { Assert::equals('C:/test', (new Path('c:/test'))->normalize()->toString('/')); } + #[Test, Values([ + ['/usr', true], + ['/usr/', true], + ['/usr/.', true], + ['/usr/bin', true], + ['/usr/bin/xp', true], + ['/usr/bin/..', true], + ['/opt/../usr', true], + ['', false], + ['/', false], + ['/usr/..', false], + ['/opt', false], + ['/opt/bin', false], + ['/usrs', false], + ['/usr.txt', false], + ])] + public function usr_path_contains($path, $expected) { + Assert::equals($expected, (new Path('/usr'))->contains($path)); + } + #[Test, Values([ ['a', 'b', 'a/b'], ['.', 'b', './b'],