From b83f72c4ebe6d0e92bbe50c8b701d1ac12544d98 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 26 Aug 2025 10:06:56 +0530 Subject: [PATCH 1/3] fix: deletePath not working properly in Local --- src/Storage/Device/Local.php | 3 ++- tests/Storage/Device/LocalTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index a587a7cf..9726940f 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -370,7 +370,8 @@ public function deletePath(string $path): bool foreach ($files as $file) { if (is_dir($file)) { - $this->deletePath(\substr_replace($file, '', 0, \strlen($this->getRoot().DIRECTORY_SEPARATOR))); + $relativePath = \str_replace($this->getRoot().DIRECTORY_SEPARATOR, '', $file); + $this->deletePath($relativePath); } else { $this->delete($file, true); } diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index c3d91b23..a4e9d609 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -376,6 +376,35 @@ public function testDeletePath() $this->assertEquals(false, $this->object->exists($path)); $this->assertEquals(false, $this->object->exists($path2)); $this->assertEquals(false, $this->object->exists($path3)); + + // Test Nested Directory Structure (prevents "Directory not empty" errors) + $nestedBucket = 'nested-bucket'; + + // Create nested structure: nested-bucket/sub1/sub2/ with files at each level + $this->assertTrue($this->object->createDirectory($nestedBucket)); + $this->assertTrue($this->object->createDirectory($nestedBucket.DIRECTORY_SEPARATOR.'sub1')); + $this->assertTrue($this->object->createDirectory($nestedBucket.DIRECTORY_SEPARATOR.'sub1'.DIRECTORY_SEPARATOR.'sub2')); + + // Add files in nested directories + $rootFile = $this->object->getPath('file1.txt'); + $rootFile = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.$nestedBucket, $rootFile); + $this->assertTrue($this->object->write($rootFile, 'Content 1', 'text/plain')); + + $nestedFile = $this->object->getPath('file2.txt'); + $nestedFile = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.$nestedBucket.DIRECTORY_SEPARATOR.'sub1'.DIRECTORY_SEPARATOR.'sub2', $nestedFile); + $this->assertTrue($this->object->write($nestedFile, 'Content 2', 'text/plain')); + + // Verify files exist + $this->assertTrue($this->object->exists($rootFile)); + $this->assertTrue($this->object->exists($nestedFile)); + + // Delete entire nested structure - should work without "Directory not empty" error + $this->assertTrue($this->object->deletePath($nestedBucket)); + + // Verify everything is deleted + $this->assertFalse($this->object->exists($rootFile)); + $this->assertFalse($this->object->exists($nestedFile)); + $this->assertFalse(file_exists($this->object->getRoot().DIRECTORY_SEPARATOR.$nestedBucket)); } public function testGetFiles() From c671cdcf7a1eff2f9f46d34bbbcbe2e5b8f4d202 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 26 Aug 2025 10:09:36 +0530 Subject: [PATCH 2/3] lint --- tests/Storage/Device/LocalTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index a4e9d609..09d14df9 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -379,28 +379,28 @@ public function testDeletePath() // Test Nested Directory Structure (prevents "Directory not empty" errors) $nestedBucket = 'nested-bucket'; - + // Create nested structure: nested-bucket/sub1/sub2/ with files at each level $this->assertTrue($this->object->createDirectory($nestedBucket)); $this->assertTrue($this->object->createDirectory($nestedBucket.DIRECTORY_SEPARATOR.'sub1')); $this->assertTrue($this->object->createDirectory($nestedBucket.DIRECTORY_SEPARATOR.'sub1'.DIRECTORY_SEPARATOR.'sub2')); - + // Add files in nested directories $rootFile = $this->object->getPath('file1.txt'); $rootFile = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.$nestedBucket, $rootFile); $this->assertTrue($this->object->write($rootFile, 'Content 1', 'text/plain')); - + $nestedFile = $this->object->getPath('file2.txt'); $nestedFile = str_ireplace($this->object->getRoot(), $this->object->getRoot().DIRECTORY_SEPARATOR.$nestedBucket.DIRECTORY_SEPARATOR.'sub1'.DIRECTORY_SEPARATOR.'sub2', $nestedFile); $this->assertTrue($this->object->write($nestedFile, 'Content 2', 'text/plain')); - + // Verify files exist $this->assertTrue($this->object->exists($rootFile)); $this->assertTrue($this->object->exists($nestedFile)); - + // Delete entire nested structure - should work without "Directory not empty" error $this->assertTrue($this->object->deletePath($nestedBucket)); - + // Verify everything is deleted $this->assertFalse($this->object->exists($rootFile)); $this->assertFalse($this->object->exists($nestedFile)); From 41bc8b0b171fa043217d46ef366a804c63051731 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Tue, 26 Aug 2025 10:10:02 +0530 Subject: [PATCH 3/3] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/Storage/Device/Local.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 9726940f..241d5459 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -370,7 +370,7 @@ public function deletePath(string $path): bool foreach ($files as $file) { if (is_dir($file)) { - $relativePath = \str_replace($this->getRoot().DIRECTORY_SEPARATOR, '', $file); + $relativePath = \substr($file, \strlen($this->getRoot().DIRECTORY_SEPARATOR)); $this->deletePath($relativePath); } else { $this->delete($file, true);