[core] redo deltree to also delete dotfiles, fixes #972
This commit is contained in:
parent
af309601f5
commit
d4b86b0e95
2 changed files with 23 additions and 33 deletions
|
@ -54,42 +54,16 @@ function ip_in_range(string $IP, string $CIDR): bool
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete an entire file heirachy
|
* Delete an entire file heirachy
|
||||||
*
|
|
||||||
* from a patch by Christian Walde; only intended for use in the
|
|
||||||
* "extension manager" extension, but it seems to fit better here
|
|
||||||
*/
|
*/
|
||||||
function deltree(string $f): void
|
function deltree(string $dir): void
|
||||||
{
|
{
|
||||||
//Because Windows (I know, bad excuse)
|
$di = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::KEY_AS_PATHNAME);
|
||||||
if (PHP_OS === 'WINNT') {
|
$ri = new \RecursiveIteratorIterator($di, \RecursiveIteratorIterator::CHILD_FIRST);
|
||||||
$real = realpath($f);
|
/** @var \SplFileInfo $file */
|
||||||
$path = realpath('./').'\\'.str_replace('/', '\\', $f);
|
foreach ($ri as $filename => $file) {
|
||||||
if ($path != $real) {
|
$file->isDir() ? rmdir($filename) : unlink($filename);
|
||||||
rmdir($path);
|
|
||||||
} else {
|
|
||||||
foreach (glob($f.'/*') as $sf) {
|
|
||||||
if (is_dir($sf) && !is_link($sf)) {
|
|
||||||
deltree($sf);
|
|
||||||
} else {
|
|
||||||
unlink($sf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rmdir($f);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (is_link($f)) {
|
|
||||||
unlink($f);
|
|
||||||
} elseif (is_dir($f)) {
|
|
||||||
foreach (glob($f.'/*') as $sf) {
|
|
||||||
if (is_dir($sf) && !is_link($sf)) {
|
|
||||||
deltree($sf);
|
|
||||||
} else {
|
|
||||||
unlink($sf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rmdir($f);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
rmdir($dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -235,4 +235,20 @@ class PolyfillsTest extends TestCase
|
||||||
// A single IP should be interpreted as a /32
|
// A single IP should be interpreted as a /32
|
||||||
$this->assertTrue(ip_in_range("1.2.3.4", "1.2.3.4"));
|
$this->assertTrue(ip_in_range("1.2.3.4", "1.2.3.4"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_deltree()
|
||||||
|
{
|
||||||
|
$tmp = sys_get_temp_dir();
|
||||||
|
$dir = "$tmp/test_deltree";
|
||||||
|
mkdir($dir);
|
||||||
|
file_put_contents("$dir/foo", "bar");
|
||||||
|
mkdir("$dir/baz");
|
||||||
|
file_put_contents("$dir/baz/.qux", "quux");
|
||||||
|
$this->assertTrue(file_exists($dir));
|
||||||
|
$this->assertTrue(file_exists("$dir/foo"));
|
||||||
|
$this->assertTrue(file_exists("$dir/baz"));
|
||||||
|
$this->assertTrue(file_exists("$dir/baz/.qux"));
|
||||||
|
deltree($dir);
|
||||||
|
$this->assertFalse(file_exists($dir));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue