Skip to content

Commit

Permalink
Windows symlinkg fix attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed May 27, 2024
1 parent c6b90b1 commit 6585f68
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
36 changes: 18 additions & 18 deletions src/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,24 @@ public function cleanup(array $sourceFiles): void
// Fix for Windows paths.
$absolutePath = str_replace(['\\','/'], DIRECTORY_SEPARATOR, $absolutePath);

$file = new \SplFileInfo($absolutePath);

if ($file->isLink()) {
unlink($absolutePath);
}

if (false !== strpos('WIN', PHP_OS)
&& (stat($absolutePath)['nlink'] !== lstat($absolutePath)['nlink']
|| $file->isLink())
) {
/**
* `unlink()` will not work on Windows. `rmdir()` will not work if there are files in the directory.
* "On windows, take care that `is_link()` returns false for Junctions."
*
* @see https://www.php.net/manual/en/function.is-link.php#113263
* @see https://stackoverflow.com/a/18262809/336146
*/
rmdir($absolutePath);
$filesystem = new \Symfony\Component\Filesystem\Filesystem();
$isSymlink = function ($path) use ($filesystem): bool {
return ! is_null($filesystem->readlink($path));
};

if ($isSymlink($absolutePath)) {
if (false !== strpos('WIN', PHP_OS)) {
/**
* `unlink()` will not work on Windows. `rmdir()` will not work if there are files in the directory.
* "On windows, take care that `is_link()` returns false for Junctions."
*
* @see https://www.php.net/manual/en/function.is-link.php#113263
* @see https://stackoverflow.com/a/18262809/336146
*/
rmdir($absolutePath);
} else {
unlink($absolutePath);
}
}

if ($absolutePath !== realpath($absolutePath)) {
Expand Down
34 changes: 19 additions & 15 deletions tests/Integration/Util/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,28 +87,32 @@ protected function deleteDir($dir)

$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);

$filesystem = new \Symfony\Component\Filesystem\Filesystem();
$isSymlink = function ($path) use ($filesystem): bool {
return ! is_null($filesystem->readlink($path));
};

/** @var \SplFileInfo $file */
foreach ($files as $file) {
if ($file->isLink()) {
unlink($file);
if ($isSymlink($file->getRealPath())) {
if (false !== strpos('WIN', PHP_OS)) {
/**
* `unlink()` will not work on Windows. `rmdir()` will not work if there are files in the directory.
* "On windows, take care that `is_link()` returns false for Junctions."
*
* @see https://www.php.net/manual/en/function.is-link.php#113263
* @see https://stackoverflow.com/a/18262809/336146
*/
rmdir($file);
} else {
unlink($file);
}
} elseif ($file->isDir()) {
rmdir($file->getRealPath());
} elseif (is_readable($file->getRealPath())) {
unlink($file->getRealPath());
}

if (false !== strpos('WIN', PHP_OS)
&& stat($file)['nlink'] !== lstat($file)['nlink']
) {
/**
* `unlink()` will not work on Windows. `rmdir()` will not work if there are files in the directory.
* "On windows, take care that `is_link()` returns false for Junctions."
*
* @see https://www.php.net/manual/en/function.is-link.php#113263
* @see https://stackoverflow.com/a/18262809/336146
*/
rmdir($file);
}
}
rmdir($dir);
}
Expand Down

0 comments on commit 6585f68

Please sign in to comment.