Skip to content

Commit

Permalink
Fix in Utilities::cleanup() to delete dirs that start with a dot.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Jan 4, 2021
1 parent 7cff7bc commit 667a218
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/ObjectStorage/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
20 changes: 16 additions & 4 deletions tests/CleanupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 667a218

Please sign in to comment.