Skip to content

Commit 60f1367

Browse files
authored
Merge pull request #5493 from kenjis/fix-directory_mirror
fix: directory_mirror() throws an error if destination directory exists
2 parents 160adc2 + 39ce053 commit 60f1367

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

system/Helpers/filesystem_helper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ function directory_mirror(string $originDir, string $targetDir, bool $overwrite
9191
$target = $targetDir . substr($origin, $dirLen);
9292

9393
if ($file->isDir()) {
94-
mkdir($target, 0755);
94+
if (! is_dir($target)) {
95+
mkdir($target, 0755);
96+
}
9597
} elseif (! is_file($target) || ($overwrite && is_file($target))) {
9698
copy($origin, $target);
9799
}

tests/system/Helpers/FilesystemHelperTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Test\CIUnitTestCase;
1515
use InvalidArgumentException;
1616
use org\bovigo\vfs\vfsStream;
17+
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
1718

1819
/**
1920
* @internal
@@ -161,6 +162,34 @@ public function testDirectoryMirrorNotOverwrites()
161162
$this->assertSame($this->structure['boo']['faz'], $result);
162163
}
163164

165+
public function testDirectoryMirrorSkipExistingFolder()
166+
{
167+
$this->assertTrue(function_exists('directory_mirror'));
168+
169+
$this->structure = [
170+
'src' => [
171+
'AnEmptyFolder' => [],
172+
],
173+
'dest' => [
174+
'AnEmptyFolder' => [],
175+
],
176+
];
177+
vfsStream::setup('root', null, $this->structure);
178+
$root = rtrim(vfsStream::url('root') . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
179+
180+
// skips the existing folder
181+
directory_mirror($root . 'src', $root . 'dest');
182+
183+
$structure = vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure();
184+
$this->assertSame([], $structure['root']['dest']['AnEmptyFolder']);
185+
186+
// skips the existing folder (the same as overwrite = true)
187+
directory_mirror($root . 'src', $root . 'dest', false);
188+
189+
$structure = vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure();
190+
$this->assertSame([], $structure['root']['dest']['AnEmptyFolder']);
191+
}
192+
164193
public function testWriteFileSuccess()
165194
{
166195
$vfs = vfsStream::setup('root');

0 commit comments

Comments
 (0)