Skip to content

Commit 24e7299

Browse files
camporternikic
authored andcommitted
Fixed bug #80724
FilesystemIterator::FOLLOW_SYMLINKS is currently treated as a directory key mode flag, even though it does not change the way that the key during iteration is set. To address this, FOLLOW_SYMLINKS has been converted into an OTHER flag. Closes GH-6695.
1 parent da011a3 commit 24e7299

5 files changed

+89
-4
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ PHP NEWS
4444
. Convert resource<pspell> to object \PSpell. (Sara)
4545
. Convert resource<pspell config> to object \PSpellConfig. (Sara)
4646

47+
- SPL:
48+
. Fixed bug #80724 (FilesystemIterator::FOLLOW_SYMLINKS remove KEY_AS_FILE
49+
from bitmask). (Cameron Porter)
50+
4751
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

ext/spl/spl_directory.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ static inline spl_filesystem_object* spl_filesystem_iterator_to_object(spl_files
136136

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

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

147147
#endif /* SPL_DIRECTORY_H */
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
SPL: RecursiveDirectoryIterator::hasChildren() follow symlinks test
3+
--FILE--
4+
<?php
5+
6+
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'symlinktest';
7+
8+
if (!mkdir($dir)) {
9+
die('Failed to create temporary directory for testing');
10+
} elseif (!symlink(__DIR__, $dir . DIRECTORY_SEPARATOR . 'symlink')) {
11+
die('Failed to create symbolic link');
12+
}
13+
14+
$it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::KEY_AS_FILENAME);
15+
16+
var_dump($it->key());
17+
var_dump($it->hasChildren());
18+
19+
$it->setFlags(FilesystemIterator::SKIP_DOTS | FilesystemIterator::KEY_AS_FILENAME);
20+
21+
var_dump($it->key());
22+
var_dump($it->hasChildren());
23+
24+
?>
25+
--EXPECT--
26+
string(7) "symlink"
27+
bool(true)
28+
string(7) "symlink"
29+
bool(false)
30+
--CLEAN--
31+
<?php
32+
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'symlinktest';
33+
unlink($dir . DIRECTORY_SEPARATOR . 'symlink');
34+
rmdir($dir);
35+
?>

ext/spl/tests/bug80724.phpt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
Bug #80724 (FOLLOW_SYMLINKS interfering with FilesystemIterator key flags)
3+
--FILE--
4+
<?php
5+
$iterator = new FilesystemIterator(__DIR__, FilesystemIterator::KEY_AS_FILENAME);
6+
foreach ($iterator as $key => $value) {
7+
echo var_dump(hasSeparator($key));
8+
break;
9+
}
10+
$iterator->rewind();
11+
echo var_dump(hasSeparator($iterator->key()));
12+
13+
$iterator->setFlags(0);
14+
echo var_dump(hasSeparator($iterator->key()));
15+
16+
$iterator->setFlags(FilesystemIterator::KEY_AS_FILENAME);
17+
echo var_dump(hasSeparator($iterator->key()));
18+
19+
$iterator2 = new FilesystemIterator(__DIR__, FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::KEY_AS_FILENAME);
20+
foreach ($iterator2 as $key => $value) {
21+
echo var_dump(hasSeparator($key));
22+
break;
23+
}
24+
$iterator2->rewind();
25+
echo var_dump(hasSeparator($iterator2->key()));
26+
27+
$iterator2->setFlags(0);
28+
echo var_dump(hasSeparator($iterator2->key()));
29+
30+
$iterator2->setFlags(FilesystemIterator::KEY_AS_FILENAME);
31+
echo var_dump(hasSeparator($iterator2->key()));
32+
33+
function hasSeparator($key) {
34+
return str_contains($key, __DIR__ . DIRECTORY_SEPARATOR);
35+
}
36+
37+
?>
38+
--EXPECT--
39+
bool(false)
40+
bool(false)
41+
bool(true)
42+
bool(false)
43+
bool(false)
44+
bool(false)
45+
bool(true)
46+
bool(false)

ext/spl/tests/filesystemiterator_flags.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function printflags($it) {
3434
00000010
3535
00000100
3636
00003000
37-
00003FF0
37+
00007FF0
3838
000000F0
3939
00000F00
40-
00003000
40+
00007000

0 commit comments

Comments
 (0)