Skip to content

Commit

Permalink
Symbolic link handling
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Dec 22, 2019
1 parent 9f6ce4c commit 8863235
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Local/LocalFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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()
);
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/Local/LocalFilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions src/SymbolicLinkEncountered.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace League\Flysystem;

use RuntimeException;

class SymbolicLinkEncountered extends RuntimeException implements FilesystemError
{
/**
* @var string
*/
private $location;

public function location(): string
{
return $this->location;
}

public static function atLocation(string $pathName)
{
$e = new static("Unsupported symbolic link encountered at location $pathName");
$e->location = $pathName;

return $e;
}
}

0 comments on commit 8863235

Please sign in to comment.