Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: directory_mirror() throws an error if destination directory exists #5493

Merged
merged 1 commit into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion system/Helpers/filesystem_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ function directory_mirror(string $originDir, string $targetDir, bool $overwrite
$target = $targetDir . substr($origin, $dirLen);

if ($file->isDir()) {
mkdir($target, 0755);
if (! is_dir($target)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this not nested if? I mean you can combine this with the outer if by a &&

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/system/Helpers/filesystem_helper.php
+++ b/system/Helpers/filesystem_helper.php
@@ -90,10 +90,8 @@ if (! function_exists('directory_mirror')) {
             $origin = $file->getPathname();
             $target = $targetDir . substr($origin, $dirLen);
 
-            if ($file->isDir()) {
-                if (! is_dir($target)) {
-                    mkdir($target, 0755);
-                }
+            if ($file->isDir() && ! is_dir($target)) {
+                mkdir($target, 0755);
             } elseif (! is_file($target) || ($overwrite && is_file($target))) {
                 copy($origin, $target);
             }
$ ./phpunit tests/system/Helpers/FilesystemHelperTest.php
PHPUnit 9.5.10 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.0.14
Configuration: /Users/kenji/work/codeigniter/CodeIgniter4/phpunit.xml

.......E.............................                             37 / 37 (100%)

Time: 00:00.256, Memory: 12.00 MB

There was 1 error:

1) CodeIgniter\Helpers\FilesystemHelperTest::testDirectoryMirrorSkipExistingFolder
ErrorException: copy(): The first argument to copy() function cannot be a directory

/Users/kenji/work/codeigniter/CodeIgniter4/system/Helpers/filesystem_helper.php:96
/Users/kenji/work/codeigniter/CodeIgniter4/tests/system/Helpers/FilesystemHelperTest.php:181

ERRORS!
Tests: 37, Assertions: 87, Errors: 1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulbalandan If you want to remove if in if, this is the code.
But I'm not sure it is correct.

--- a/system/Helpers/filesystem_helper.php
+++ b/system/Helpers/filesystem_helper.php
@@ -90,10 +90,12 @@ if (! function_exists('directory_mirror')) {
             $origin = $file->getPathname();
             $target = $targetDir . substr($origin, $dirLen);
 
-            if ($file->isDir()) {
-                if (! is_dir($target)) {
-                    mkdir($target, 0755);
-                }
+            if ($file->isDir() && ! is_dir($target)) {
+                mkdir($target, 0755);
+            } elseif ($file->isDir() && is_dir($target)) {
+                continue;
+            } elseif (is_dir($target)) {
+                continue;
             } elseif (! is_file($target) || ($overwrite && is_file($target))) {
                 copy($origin, $target);
             }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this:

diff --git a/system/Helpers/filesystem_helper.php b/system/Helpers/filesystem_helper.php
index 89d4d507f..104b739a3 100644
--- a/system/Helpers/filesystem_helper.php
+++ b/system/Helpers/filesystem_helper.php
@@ -90,11 +90,11 @@ if (! function_exists('directory_mirror')) {
             $origin = $file->getPathname();
             $target = $targetDir . substr($origin, $dirLen);

-            if ($file->isDir()) {
-                if (! is_dir($target)) {
-                    mkdir($target, 0755);
-                }
-            } elseif (! is_file($target) || ($overwrite && is_file($target))) {
+            if ($file->isDir() && ! is_dir($target)) {
+                mkdir($target, 0755);
+            }
+
+            if ($file->isFile() && (! is_file($target) || $overwrite)) {
                 copy($origin, $target);
             }
         }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the conditions of the if statements does not seem to make it easier to understand.

mkdir($target, 0755);
}
} elseif (! is_file($target) || ($overwrite && is_file($target))) {
copy($origin, $target);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/system/Helpers/FilesystemHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use InvalidArgumentException;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;

/**
* @internal
Expand Down Expand Up @@ -161,6 +162,34 @@ public function testDirectoryMirrorNotOverwrites()
$this->assertSame($this->structure['boo']['faz'], $result);
}

public function testDirectoryMirrorSkipExistingFolder()
{
$this->assertTrue(function_exists('directory_mirror'));

$this->structure = [
'src' => [
'AnEmptyFolder' => [],
],
'dest' => [
'AnEmptyFolder' => [],
],
];
vfsStream::setup('root', null, $this->structure);
$root = rtrim(vfsStream::url('root') . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;

// skips the existing folder
directory_mirror($root . 'src', $root . 'dest');

$structure = vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure();
$this->assertSame([], $structure['root']['dest']['AnEmptyFolder']);

// skips the existing folder (the same as overwrite = true)
directory_mirror($root . 'src', $root . 'dest', false);

$structure = vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure();
$this->assertSame([], $structure['root']['dest']['AnEmptyFolder']);
}

public function testWriteFileSuccess()
{
$vfs = vfsStream::setup('root');
Expand Down