Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions src/main/php/io/Path.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -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));
}

/**
Expand Down
51 changes: 47 additions & 4 deletions src/test/php/io/unittest/PathTest.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace io\unittest;

use io\{File, Folder, Path};
use io\{File, Folder, Path, OperationFailed};
use lang\{Environment, IllegalArgumentException, IllegalStateException, Runtime};
use test\verify\Runtime as VerifyRuntime;
use test\{Assert, Expect, Test, Values};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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'],
Expand Down
Loading