From 88632355cad8eeeb044a43f51f5199b445dfa541 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Sun, 22 Dec 2019 22:29:21 +0100 Subject: [PATCH] Symbolic link handling --- src/Local/LocalFilesystem.php | 15 +++++++-------- src/Local/LocalFilesystemTest.php | 27 +++++++++++++++++++++++++++ src/SymbolicLinkEncountered.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 src/SymbolicLinkEncountered.php diff --git a/src/Local/LocalFilesystem.php b/src/Local/LocalFilesystem.php index f8a477f33..e0b53c48f 100644 --- a/src/Local/LocalFilesystem.php +++ b/src/Local/LocalFilesystem.php @@ -12,6 +12,7 @@ use League\Flysystem\FileAttributes; use League\Flysystem\FilesystemAdapter; use League\Flysystem\PathPrefixer; +use League\Flysystem\SymbolicLinkEncountered; use League\Flysystem\UnableToCreateDirectory; use League\Flysystem\UnableToDeleteDirectory; use League\Flysystem\UnableToDeleteFile; @@ -38,8 +39,6 @@ use function stream_copy_to_stream; use function unlink; -use function var_dump; - use const DIRECTORY_SEPARATOR; use const LOCK_EX; @@ -232,17 +231,17 @@ public function listContents(string $path, bool $recursive): Generator $iterator = $recursive ? $this->listDirectoryRecursively($location) : $this->listDirectory($location); foreach ($iterator as $fileInfo) { - if ($fileInfo->isLink() && $this->linkHandling & self::DISALLOW_LINKS) { - continue; + if ($fileInfo->isLink()) { + if ($this->linkHandling & self::DISALLOW_LINKS) { + continue; + } + throw SymbolicLinkEncountered::atLocation($fileInfo->getPathname()); } $path = $this->prefixer->stripPrefix($fileInfo->getPathname()); yield $fileInfo->isDir() ? new DirectoryAttributes($path) : new FileAttributes( - $path, - $fileInfo->getSize(), - null, - $fileInfo->getMTime() + $path, $fileInfo->getSize(), null, $fileInfo->getMTime() ); } } diff --git a/src/Local/LocalFilesystemTest.php b/src/Local/LocalFilesystemTest.php index 4aa8e2fa6..23a955f9a 100644 --- a/src/Local/LocalFilesystemTest.php +++ b/src/Local/LocalFilesystemTest.php @@ -5,6 +5,7 @@ namespace League\Flysystem\Local; use League\Flysystem\Config; +use League\Flysystem\StorageAttributes; use League\Flysystem\UnableToCreateDirectory; use League\Flysystem\UnableToDeleteFile; use League\Flysystem\UnableToSetVisibility; @@ -293,6 +294,32 @@ public function listing_contents() $contents = iterator_to_array($adapter->listContents('/', false)); $this->assertCount(2, $contents); + $this->assertContainsOnlyInstancesOf(StorageAttributes::class, $contents); + } + + /** + * @test + */ + public function listing_contents_recursively() + { + $adapter = new LocalFilesystem(static::ROOT); + $adapter->write('directory/filename.txt', 'content', new Config()); + $adapter->write('filename.txt', 'content' , new Config()); + $contents = iterator_to_array($adapter->listContents('/', true)); + + $this->assertCount(3, $contents); + $this->assertContainsOnlyInstancesOf(StorageAttributes::class, $contents); + } + + /** + * @test + */ + public function listing_a_non_existing_directory() + { + $adapter = new LocalFilesystem(static::ROOT); + $contents = iterator_to_array($adapter->listContents('/directory/', false)); + + $this->assertCount(0, $contents); } private function streamWithContents(string $contents) diff --git a/src/SymbolicLinkEncountered.php b/src/SymbolicLinkEncountered.php new file mode 100644 index 000000000..4d6a54381 --- /dev/null +++ b/src/SymbolicLinkEncountered.php @@ -0,0 +1,28 @@ +location; + } + + public static function atLocation(string $pathName) + { + $e = new static("Unsupported symbolic link encountered at location $pathName"); + $e->location = $pathName; + + return $e; + } +}