Skip to content

Commit f323baa

Browse files
committed
Reduce number of stat() calls
1 parent c2fc25f commit f323baa

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Diff for: ext/spl/spl_directory.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -1474,13 +1474,15 @@ PHP_METHOD(RecursiveDirectoryIterator, hasChildren)
14741474
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
14751475
RETURN_THROWS();
14761476
}
1477-
if (!allow_links && !(intern->flags & SPL_FILE_DIR_FOLLOW_SYMLINKS)) {
1478-
php_stat(intern->file_name, FS_IS_LINK, return_value);
1479-
if (zend_is_true(return_value)) {
1477+
php_stat(intern->file_name, FS_LPERMS, return_value);
1478+
if (S_ISLNK(Z_LVAL_P(return_value))) {
1479+
if (!allow_links
1480+
&& !(intern->flags & SPL_FILE_DIR_FOLLOW_SYMLINKS)) {
14801481
RETURN_FALSE;
14811482
}
1483+
php_stat(intern->file_name, FS_PERMS, return_value);
14821484
}
1483-
php_stat(intern->file_name, FS_IS_DIR, return_value);
1485+
RETURN_BOOL(S_ISDIR(Z_LVAL_P(return_value)));
14841486
}
14851487
}
14861488
/* }}} */

Diff for: ext/standard/filestat.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ PHP_FUNCTION(clearstatcache)
714714
}
715715
/* }}} */
716716

717-
#define IS_LINK_OPERATION(__t) ((__t) == FS_TYPE || (__t) == FS_IS_LINK || (__t) == FS_LSTAT)
717+
#define IS_LINK_OPERATION(__t) ((__t) == FS_TYPE || (__t) == FS_IS_LINK || (__t) == FS_LSTAT || (__t) == FS_LPERMS)
718718
#define IS_EXISTS_CHECK(__t) ((__t) == FS_EXISTS || (__t) == FS_IS_W || (__t) == FS_IS_R || (__t) == FS_IS_X || (__t) == FS_IS_FILE || (__t) == FS_IS_DIR || (__t) == FS_IS_LINK)
719719
#define IS_ABLE_CHECK(__t) ((__t) == FS_IS_R || (__t) == FS_IS_W || (__t) == FS_IS_X)
720720
#define IS_ACCESS_CHECK(__t) (IS_ABLE_CHECK(type) || (__t) == FS_EXISTS)
@@ -824,7 +824,9 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value)
824824
}
825825
BG(CurrentLStatFile) = zend_string_copy(filename);
826826
memcpy(&BG(lssb), &ssb, sizeof(php_stream_statbuf));
827-
} else {
827+
}
828+
if (!(flags & PHP_STREAM_URL_STAT_LINK)
829+
|| !S_ISLNK(ssb.sb.st_mode)) {
828830
if (BG(CurrentStatFile)) {
829831
zend_string_release(BG(CurrentStatFile));
830832
}
@@ -878,6 +880,7 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value)
878880

879881
switch (type) {
880882
case FS_PERMS:
883+
case FS_LPERMS:
881884
RETURN_LONG((zend_long)ssb.sb.st_mode);
882885
case FS_INODE:
883886
RETURN_LONG((zend_long)ssb.sb.st_ino);

Diff for: ext/standard/php_filestat.h

+1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value);
6262
#define FS_EXISTS 15
6363
#define FS_LSTAT 16
6464
#define FS_STAT 17
65+
#define FS_LPERMS 18
6566

6667
#endif /* PHP_FILESTAT_H */

0 commit comments

Comments
 (0)