Skip to content

[SPL] Fix FilesystemIterator::FOLLOW_SYMLINKS bug #6695

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 2 additions & 2 deletions ext/spl/spl_directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ static inline spl_filesystem_object* spl_filesystem_iterator_to_object(spl_files

#define SPL_FILE_DIR_KEY_AS_PATHNAME 0x00000000 /* make RecursiveDirectoryTree::key() return getPathname() */
#define SPL_FILE_DIR_KEY_AS_FILENAME 0x00000100 /* make RecursiveDirectoryTree::key() return getFilename() */
#define SPL_FILE_DIR_FOLLOW_SYMLINKS 0x00000200 /* make RecursiveDirectoryTree::hasChildren() follow symlinks */
#define SPL_FILE_DIR_KEY_MODE_MASK 0x00000F00 /* mask RecursiveDirectoryTree::key() */
#define SPL_FILE_DIR_KEY(intern,mode) ((intern->flags&SPL_FILE_DIR_KEY_MODE_MASK)==mode)

#define SPL_FILE_DIR_SKIPDOTS 0x00001000 /* Tells whether it should skip dots or not */
#define SPL_FILE_DIR_UNIXPATHS 0x00002000 /* Whether to unixify path separators */
#define SPL_FILE_DIR_OTHERS_MASK 0x00003000 /* mask used for get/setFlags */
#define SPL_FILE_DIR_FOLLOW_SYMLINKS 0x00004000 /* make RecursiveDirectoryTree::hasChildren() follow symlinks */
#define SPL_FILE_DIR_OTHERS_MASK 0x00007000 /* mask used for get/setFlags */

#endif /* SPL_DIRECTORY_H */
35 changes: 35 additions & 0 deletions ext/spl/tests/RecursiveDirectoryIterator_hasChildren.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
SPL: RecursiveDirectoryIterator::hasChildren() follow symlinks test
--FILE--
<?php

$dir = __DIR__ . DIRECTORY_SEPARATOR . 'symlinktest';

if (!mkdir($dir)) {
die('Failed to create temporary directory for testing');
} elseif (!symlink(__DIR__, $dir . DIRECTORY_SEPARATOR . 'symlink')) {
die('Failed to create symbolic link');
}

$it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::KEY_AS_FILENAME);

var_dump($it->key());
var_dump($it->hasChildren());

$it->setFlags(FilesystemIterator::SKIP_DOTS | FilesystemIterator::KEY_AS_FILENAME);

var_dump($it->key());
var_dump($it->hasChildren());

?>
--EXPECT--
string(7) "symlink"
bool(true)
string(7) "symlink"
bool(false)
--CLEAN--
<?php
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'symlinktest';
unlink($dir . DIRECTORY_SEPARATOR . 'symlink');
rmdir($dir);
?>
46 changes: 46 additions & 0 deletions ext/spl/tests/bug80724.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
Bug #80724 (FOLLOW_SYMLINKS interfering with FilesystemIterator key flags)
--FILE--
<?php
$iterator = new FilesystemIterator(__DIR__, FilesystemIterator::KEY_AS_FILENAME);
foreach ($iterator as $key => $value) {
echo var_dump(hasSeparator($key));
break;
}
$iterator->rewind();
echo var_dump(hasSeparator($iterator->key()));

$iterator->setFlags(0);
echo var_dump(hasSeparator($iterator->key()));

$iterator->setFlags(FilesystemIterator::KEY_AS_FILENAME);
echo var_dump(hasSeparator($iterator->key()));

$iterator2 = new FilesystemIterator(__DIR__, FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::KEY_AS_FILENAME);
foreach ($iterator2 as $key => $value) {
echo var_dump(hasSeparator($key));
break;
}
$iterator2->rewind();
echo var_dump(hasSeparator($iterator2->key()));

$iterator2->setFlags(0);
echo var_dump(hasSeparator($iterator2->key()));

$iterator2->setFlags(FilesystemIterator::KEY_AS_FILENAME);
echo var_dump(hasSeparator($iterator2->key()));

function hasSeparator($key) {
return str_contains($key, __DIR__ . DIRECTORY_SEPARATOR);
}

?>
--EXPECT--
bool(false)
bool(false)
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)
4 changes: 2 additions & 2 deletions ext/spl/tests/filesystemiterator_flags.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function printflags($it) {
00000010
00000100
00003000
00003FF0
00007FF0
000000F0
00000F00
00003000
00007000