Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Finder] Fix initial directory is opened twice #50884

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

Merged
merged 1 commit into from
Jul 6, 2023
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
[Finder] Fix initial directory is opened twice
  • Loading branch information
mvorisek authored and nicolas-grekas committed Jul 6, 2023
commit d3f03bee5ccd783f5405d67dfc2ec0adf5dbaadc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RecursiveDirectoryIterator extends \RecursiveDirectoryIterator
/**
* @var bool
*/
private $rewindable;
private $ignoreFirstRewind = true;

// these 3 properties take part of the performance optimization to avoid redoing the same work in all iterations
private $rootPath;
Expand Down Expand Up @@ -118,7 +118,6 @@ public function getChildren()
$children->ignoreUnreadableDirs = $this->ignoreUnreadableDirs;

// performance optimization to avoid redoing the same work in all children
$children->rewindable = &$this->rewindable;
$children->rootPath = $this->rootPath;
}

Expand All @@ -129,40 +128,30 @@ public function getChildren()
}

/**
* Do nothing for non rewindable stream.
*
* @return void
*/
#[\ReturnTypeWillChange]
public function rewind()
public function next()
{
if (false === $this->isRewindable()) {
return;
}
$this->ignoreFirstRewind = false;

parent::rewind();
parent::next();
}

/**
* Checks if the stream is rewindable.
*
* @return bool
* @return void
*/
public function isRewindable()
#[\ReturnTypeWillChange]
public function rewind()
{
if (null !== $this->rewindable) {
return $this->rewindable;
}

if (false !== $stream = @opendir($this->getPath())) {
$infos = stream_get_meta_data($stream);
closedir($stream);
// some streams like FTP are not rewindable, ignore the first rewind after creation,
// as newly created DirectoryIterator does not need to be rewound
if ($this->ignoreFirstRewind) {
$this->ignoreFirstRewind = false;

if ($infos['seekable']) {
return $this->rewindable = true;
}
return;
}

return $this->rewindable = false;
parent::rewind();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@

class RecursiveDirectoryIteratorTest extends IteratorTestCase
{
protected function setUp(): void
{
if (!\in_array('ftp', stream_get_wrappers(), true) || !\ini_get('allow_url_fopen')) {
$this->markTestSkipped('Unsupported stream "ftp".');
}
}

/**
* @group network
*/
public function testRewindOnFtp()
{
try {
$i = new RecursiveDirectoryIterator('ftp://speedtest:[email protected]/', \RecursiveDirectoryIterator::SKIP_DOTS);
} catch (\UnexpectedValueException $e) {
$this->markTestSkipped('Unsupported stream "ftp".');
}
$i = new RecursiveDirectoryIterator('ftp://speedtest:[email protected]/', \RecursiveDirectoryIterator::SKIP_DOTS);

$i->rewind();

Expand All @@ -36,11 +39,7 @@ public function testRewindOnFtp()
*/
public function testSeekOnFtp()
{
try {
$i = new RecursiveDirectoryIterator('ftp://speedtest:[email protected]/', \RecursiveDirectoryIterator::SKIP_DOTS);
} catch (\UnexpectedValueException $e) {
$this->markTestSkipped('Unsupported stream "ftp".');
}
$i = new RecursiveDirectoryIterator('ftp://speedtest:[email protected]/', \RecursiveDirectoryIterator::SKIP_DOTS);

$contains = [
'ftp://speedtest:[email protected]'.\DIRECTORY_SEPARATOR.'test100Mb.db',
Expand Down