From d3e08ed830d671e66597d8df4ab6487615e7f5c0 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Tue, 24 Dec 2019 22:54:13 +0100 Subject: [PATCH] Make directory creation idempotent --- src/Local/LocalFilesystem.php | 23 +++++++++++++++++------ src/Local/LocalFilesystemTest.php | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/Local/LocalFilesystem.php b/src/Local/LocalFilesystem.php index 389e54204..a45543756 100644 --- a/src/Local/LocalFilesystem.php +++ b/src/Local/LocalFilesystem.php @@ -289,8 +289,14 @@ public function fileExists(string $location): bool public function createDirectory(string $path, Config $config): void { $location = $this->prefixer->prefixPath($path); - $visibility = $config->get('visibility', $config->get('directory_visibilty')); + $visibility = $config->get('visibility', $config->get('directory_visibility')); $permissions = $this->resolveDirectoryVisibility($visibility); + + if (is_dir($location)) { + $this->setPermissions($location, $permissions); + return; + } + error_clear_last(); if ( ! @mkdir($location, $permissions, true)) { @@ -305,11 +311,7 @@ public function setVisibility(string $location, $visibility): void $visibility ); - error_clear_last(); - if ( ! @chmod($location, $visibility)) { - $extraMessage = error_get_last()['message'] ?? ''; - throw UnableToSetVisibility::atLocation($this->prefixer->stripPrefix($location), $extraMessage); - } + $this->setPermissions($location, $visibility); } public function visibility(string $location): string @@ -347,4 +349,13 @@ private function listDirectory(string $location): Generator yield $item; } } + + private function setPermissions(string $location, int $visibility): void + { + error_clear_last(); + if ( ! @chmod($location, $visibility)) { + $extraMessage = error_get_last()['message'] ?? ''; + throw UnableToSetVisibility::atLocation($this->prefixer->stripPrefix($location), $extraMessage); + } + } } diff --git a/src/Local/LocalFilesystemTest.php b/src/Local/LocalFilesystemTest.php index f2ce89bb9..c58757cbd 100644 --- a/src/Local/LocalFilesystemTest.php +++ b/src/Local/LocalFilesystemTest.php @@ -429,6 +429,28 @@ public function creating_a_directory() $this->assertFileHasPermissions(static::ROOT . '/also_private', 0700); } + /** + * @test + */ + public function not_being_able_to_create_a_directory() + { + $this->expectException(UnableToCreateDirectory::class); + $adapter = new LocalFilesystem('/'); + $adapter->createDirectory('/something/', new Config()); + } + + /** + * @test + */ + public function creating_a_directory_is_idempotent() + { + $adapter = new LocalFilesystem(static::ROOT); + $adapter->createDirectory('/something/', new Config(['visibility' => 'private'])); + $this->assertFileHasPermissions(static::ROOT . '/something', 0700); + $adapter->createDirectory('/something/', new Config(['visibility' => 'public'])); + $this->assertFileHasPermissions(static::ROOT . '/something', 0755); + } + private function streamWithContents(string $contents) { $stream = fopen('php://temp', 'w+b');