Skip to content

Commit

Permalink
[Filesystem] Junstion detect an remove #1105
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed May 9, 2024
1 parent eeea757 commit 844d5e3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/FileObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ public function delete(): bool
// In case of restricted permissions we zap it one way or the other
// as long as the owner is either the webserver or the ftp
try {
if ($this->isJunction()) {
return $this->removeJunction();
}

if ($this->isDir()) {
return rmdir($path);
}
Expand Down Expand Up @@ -794,6 +798,49 @@ public function isLink(): bool
return $isLink;
}

/**
* @see https://github.com/composer/composer/blob/4e5be9ee7d924d8efc58d676439b0c7bd18a9ce4/src/Composer/Util/Filesystem.php#L828
*
* @return bool
*/
public function isJunction(): bool
{
if (!static::isWindows()) {
return false;
}

$junction = $this->getPathname();

// Important to clear all caches first
clearstatcache(true, $junction);

if (!is_dir($junction) || is_link($junction)) {
return false;
}

$stat = lstat($junction);

// S_ISDIR test (S_IFDIR is 0x4000, S_IFMT is 0xF000 bitmask)
return is_array($stat) && 0x4000 !== ($stat['mode'] & 0xF000);
}

public function removeJunction(): bool
{
if (!static::isWindows()) {
return false;
}

return rmdir($this->getPathname());
}

/**
* @return bool
*/
protected static function isWindows(): bool
{
return defined('PHP_WINDOWS_VERSION_BUILD');
}

/**
* @param string $name
* @param array $args
Expand Down
31 changes: 28 additions & 3 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
use Windwalker\Filesystem\Iterator\FilesIterator;
use Windwalker\Promise\Promise;
use Windwalker\Scalars\StringObject;
use Windwalker\Stream\Stream;
use Windwalker\Utilities\Iterator\UniqueIterator;
use Windwalker\Utilities\Str;

use function Windwalker\fs;
use function Windwalker\uid;

use const Windwalker\Stream\READ_ONLY_FROM_BEGIN;
Expand Down Expand Up @@ -68,7 +68,7 @@ class Filesystem
/**
* Get a path as FileObject.
*
* @param string $path
* @param string $path
* @param string|null $root
*
* @return FileObject
Expand Down Expand Up @@ -261,7 +261,7 @@ public static function createTempFolder(
*/
public static function symlink(string $target, string $link): bool
{
$windows = defined('PHP_WINDOWS_VERSION_BUILD');
$windows = static::isWindows();

$target = Path::normalize($target);
$link = Path::normalize($link);
Expand All @@ -281,6 +281,31 @@ public static function symlink(string $target, string $link): bool
return symlink($target, $link);
}

/**
* @return bool
*/
protected static function isWindows(): bool
{
return defined('PHP_WINDOWS_VERSION_BUILD');
}

public function isJunction(string $junction): bool
{
return fs($junction)->isJunction();
}

/**
* Removes a Windows NTFS junction.
*
* @param string $junction
*
* @return bool
*/
public function removeJunction(string $junction): bool
{
return fs($junction)->removeJunction();
}

/**
* __callStatic
*
Expand Down

0 comments on commit 844d5e3

Please sign in to comment.