From 667a21839cb95fcc2c5955edef9254d53962508d Mon Sep 17 00:00:00 2001 From: Ivo Petkov Date: Mon, 4 Jan 2021 17:28:52 +0200 Subject: [PATCH] Fix in Utilities::cleanup() to delete dirs that start with a dot. --- src/ObjectStorage/Utilities.php | 14 ++++++++++++-- tests/CleanupTest.php | 20 ++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/ObjectStorage/Utilities.php b/src/ObjectStorage/Utilities.php index 92f7d0d..a816dcb 100644 --- a/src/ObjectStorage/Utilities.php +++ b/src/ObjectStorage/Utilities.php @@ -27,11 +27,21 @@ static function cleanup(string $dir): void return; } + $hasGlobBrace = defined('GLOB_BRACE'); + $dir = rtrim($dir, '\\/'); - $removeEmptyDirs = function ($dir) use (&$removeEmptyDirs) { - $subDirs = glob($dir . '/*', GLOB_ONLYDIR); + $removeEmptyDirs = function ($dir) use (&$removeEmptyDirs, $hasGlobBrace) { + if ($hasGlobBrace) { + $subDirs = glob($dir . '/{,.}*', GLOB_ONLYDIR | GLOB_NOSORT | GLOB_BRACE); + } else { + $subDirs = array_unique(array_merge(glob($dir . '/*', GLOB_ONLYDIR | GLOB_NOSORT), glob($dir . '/.*', GLOB_ONLYDIR | GLOB_NOSORT))); + } + $allSubDirsAreRemoved = true; foreach ($subDirs as $subDir) { + if (substr($subDir, -3) === DIRECTORY_SEPARATOR . '..' || substr($subDir, -2) === DIRECTORY_SEPARATOR . '.') { + continue; + } if (!$removeEmptyDirs($subDir)) { $allSubDirsAreRemoved = false; } diff --git a/tests/CleanupTest.php b/tests/CleanupTest.php index 35c63f7..ef1bad0 100644 --- a/tests/CleanupTest.php +++ b/tests/CleanupTest.php @@ -38,18 +38,30 @@ public function testCleanup1() $objectStorage->delete([ 'key' => 'books/category1/book1' ]); + $objectStorage->set( + [ + 'key' => '.temp/user1', + 'body' => 'user 1', + 'metadata.title' => 'User 1' + ] + ); + $objectStorage->delete([ + 'key' => '.temp/user1' + ]); $this->assertEquals(scandir($dataDir . 'objects'), array( 0 => '.', 1 => '..', - 2 => 'books', - 3 => 'services', + 2 => '.temp', + 3 => 'books', + 4 => 'services', )); $this->assertEquals(scandir($dataDir . 'metadata/'), array( 0 => '.', 1 => '..', - 2 => 'books', - 3 => 'services', + 2 => '.temp', + 3 => 'books', + 4 => 'services', )); IvoPetkov\ObjectStorage\Utilities::cleanup($dataDir . 'objects');