From 206c7ca890b31d9e5e99bb779fcd2627fdc7f58a Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 22 Nov 2026 14:25:33 +0100 Subject: [PATCH 1/6] Add `contains()` to check whether a given path exists in a base path --- src/main/php/io/Path.class.php | 16 ++++++++++++++++ src/test/php/io/unittest/PathTest.class.php | 17 +++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/main/php/io/Path.class.php b/src/main/php/io/Path.class.php index 06cd5df45..0f31033fd 100755 --- a/src/main/php/io/Path.class.php +++ b/src/main/php/io/Path.class.php @@ -209,6 +209,22 @@ private static function real0($path, $wd) { 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]); + $wd??= getcwd(); + 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 diff --git a/src/test/php/io/unittest/PathTest.class.php b/src/test/php/io/unittest/PathTest.class.php index db794f116..bd8616c68 100755 --- a/src/test/php/io/unittest/PathTest.class.php +++ b/src/test/php/io/unittest/PathTest.class.php @@ -366,6 +366,23 @@ public function normalize_uppercases_drive_letter() { Assert::equals('C:/test', (new Path('c:/test'))->normalize()->toString('/')); } + #[Test, Values([ + ['/usr', true], + ['/usr/', true], + ['/usr/bin', true], + ['/usr/bin/ls', true], + ['/opt/../usr', true], + ['', false], + ['/', 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'], From 10d94fc59aa39eab269aa41b382e0699225bbfdb Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 22 Nov 2026 14:34:34 +0100 Subject: [PATCH 2/6] Show test details (CI runtime debugging) --- .github/workflows/ci.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.sh b/.github/workflows/ci.sh index c9b8517e2..1519dd7f6 100755 --- a/.github/workflows/ci.sh +++ b/.github/workflows/ci.sh @@ -18,6 +18,6 @@ case $1 in ;; run-tests) - sh xp-run xp.test.Runner -r Dots src/test/php + sh xp-run xp.test.Runner src/test/php ;; esac \ No newline at end of file From 844ec120b0cc088b2baed6d2334baa63df620bf5 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 22 Nov 2026 14:39:54 +0100 Subject: [PATCH 3/6] The file /usr/bin/ls may be a symlink --- .github/workflows/ci.sh | 2 +- src/test/php/io/unittest/PathTest.class.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.sh b/.github/workflows/ci.sh index 1519dd7f6..c9b8517e2 100755 --- a/.github/workflows/ci.sh +++ b/.github/workflows/ci.sh @@ -18,6 +18,6 @@ case $1 in ;; run-tests) - sh xp-run xp.test.Runner src/test/php + sh xp-run xp.test.Runner -r Dots src/test/php ;; esac \ No newline at end of file diff --git a/src/test/php/io/unittest/PathTest.class.php b/src/test/php/io/unittest/PathTest.class.php index bd8616c68..2d0e37afd 100755 --- a/src/test/php/io/unittest/PathTest.class.php +++ b/src/test/php/io/unittest/PathTest.class.php @@ -370,7 +370,7 @@ public function normalize_uppercases_drive_letter() { ['/usr', true], ['/usr/', true], ['/usr/bin', true], - ['/usr/bin/ls', true], + ['/usr/bin/xp', true], ['/opt/../usr', true], ['', false], ['/', false], From cb06ba4117782f8066f087617022c09a9653f72e Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 22 Nov 2026 15:21:27 +0100 Subject: [PATCH 4/6] Resolve relative symlinks --- src/main/php/io/Path.class.php | 18 +++++++------- src/test/php/io/unittest/PathTest.class.php | 26 +++++++++++++++++---- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main/php/io/Path.class.php b/src/main/php/io/Path.class.php index 0f31033fd..c35f967df 100755 --- a/src/main/php/io/Path.class.php +++ b/src/main/php/io/Path.class.php @@ -193,17 +193,17 @@ 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; diff --git a/src/test/php/io/unittest/PathTest.class.php b/src/test/php/io/unittest/PathTest.class.php index 2d0e37afd..12c153945 100755 --- a/src/test/php/io/unittest/PathTest.class.php +++ b/src/test/php/io/unittest/PathTest.class.php @@ -1,6 +1,6 @@ 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] From da034af5edfa24c037774bb1fe90767ec68cb142 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 22 Nov 2026 15:26:13 +0100 Subject: [PATCH 5/6] Add more "." and ".." cases to usr_path_contains() test --- src/test/php/io/unittest/PathTest.class.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/php/io/unittest/PathTest.class.php b/src/test/php/io/unittest/PathTest.class.php index 12c153945..22477b875 100755 --- a/src/test/php/io/unittest/PathTest.class.php +++ b/src/test/php/io/unittest/PathTest.class.php @@ -387,11 +387,14 @@ public function normalize_uppercases_drive_letter() { #[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], From 91e2ef94c7a8afe59e877fc8a5c2e64b612bebe4 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 22 Nov 2026 15:42:18 +0100 Subject: [PATCH 6/6] Support relative working directories --- src/main/php/io/Path.class.php | 12 ++++-------- src/test/php/io/unittest/PathTest.class.php | 5 +++++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/php/io/Path.class.php b/src/main/php/io/Path.class.php index c35f967df..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); } @@ -218,7 +215,6 @@ private static function real0($path, $wd) { */ public function contains($arg, $wd= null) { $compare= $arg instanceof self ? $arg->path : self::pathFor([$arg]); - $wd??= getcwd(); 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, @@ -235,7 +231,7 @@ public function contains($arg, $wd= null) { * @return string */ public function asURI($wd= null): string { - return self::real0($this->path, $wd ?? getcwd()); + return self::real0($this->path, $wd); } /** @@ -248,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 22477b875..e8eccc69c 100755 --- a/src/test/php/io/unittest/PathTest.class.php +++ b/src/test/php/io/unittest/PathTest.class.php @@ -157,6 +157,11 @@ public function real_with_working_directory() { Assert::equals($folder->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();