diff --git a/NEWS b/NEWS index aabe78ad2a7c..4d4f485c6d86 100644 --- a/NEWS +++ b/NEWS @@ -187,6 +187,9 @@ PHP NEWS rmdir(), touch(), chmod() or stat(). (Ilia Alshanetsky) - Standard: + . Added fileatime_ns(), filemtime_ns(), and filectime_ns() to return file + timestamps as nanoseconds since the Unix epoch. (Alessio Attilio) + . Made extension names in ini_get_all() case-insensitive. (Weilin Du) . Fixed bug GH-25387 (Uploaded files leak in request_parse_body()). (Kévin Dunglas) diff --git a/configure.ac b/configure.ac index 40675011a2c0..4b32c4b9df24 100644 --- a/configure.ac +++ b/configure.ac @@ -500,6 +500,15 @@ AC_CHECK_MEMBERS(m4_normalize([ struct stat.st_blocks, struct stat.st_rdev ])) +AC_CHECK_MEMBERS(m4_normalize([ + struct stat.st_atim.tv_nsec, + struct stat.st_mtim.tv_nsec, + struct stat.st_ctim.tv_nsec, + struct stat.st_atimespec.tv_nsec, + struct stat.st_mtimespec.tv_nsec, + struct stat.st_ctimespec.tv_nsec +]),,,[#include +#include ]) dnl Checks for types. AC_TYPE_UID_T diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index ec0645720b8c..8bc465a882f6 100644 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -2920,14 +2920,20 @@ function sys_get_temp_dir(): string {} function fileatime(string $filename): int|false {} +function fileatime_ns(string $filename): int|float|false {} + function filectime(string $filename): int|false {} +function filectime_ns(string $filename): int|float|false {} + function filegroup(string $filename): int|false {} function fileinode(string $filename): int|false {} function filemtime(string $filename): int|false {} +function filemtime_ns(string $filename): int|float|false {} + function fileowner(string $filename): int|false {} function fileperms(string $filename): int|false {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 62fe3f768665..6441333dbcfd 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit basic_functions.stub.php instead. - * Stub hash: 53d47cfb46b55f62340f435b4637de62c4d0b531 + * Stub hash: 514f1812914afe4e84777239b10ad8e47b5cb710 * Has decl header: yes */ #include "zend_attributes.h" @@ -1374,14 +1374,22 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_fileatime, 0, 1, MAY_BE_LONG|MAY ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_fileatime_ns, 0, 1, MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_FALSE) + ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) +ZEND_END_ARG_INFO() + #define arginfo_filectime arginfo_fileatime +#define arginfo_filectime_ns arginfo_fileatime_ns + #define arginfo_filegroup arginfo_fileatime #define arginfo_fileinode arginfo_fileatime #define arginfo_filemtime arginfo_fileatime +#define arginfo_filemtime_ns arginfo_fileatime_ns + #define arginfo_fileowner arginfo_fileatime #define arginfo_fileperms arginfo_fileatime @@ -2713,10 +2721,13 @@ ZEND_FUNCTION(fnmatch); #endif ZEND_FUNCTION(sys_get_temp_dir); ZEND_FUNCTION(fileatime); +ZEND_FUNCTION(fileatime_ns); ZEND_FUNCTION(filectime); +ZEND_FUNCTION(filectime_ns); ZEND_FUNCTION(filegroup); ZEND_FUNCTION(fileinode); ZEND_FUNCTION(filemtime); +ZEND_FUNCTION(filemtime_ns); ZEND_FUNCTION(fileowner); ZEND_FUNCTION(fileperms); ZEND_FUNCTION(filesize); @@ -3327,10 +3338,13 @@ static const zend_function_entry ext_functions[] = { #endif ZEND_FE(sys_get_temp_dir, arginfo_sys_get_temp_dir) ZEND_FE(fileatime, arginfo_fileatime) + ZEND_FE(fileatime_ns, arginfo_fileatime_ns) ZEND_FE(filectime, arginfo_filectime) + ZEND_FE(filectime_ns, arginfo_filectime_ns) ZEND_FE(filegroup, arginfo_filegroup) ZEND_FE(fileinode, arginfo_fileinode) ZEND_FE(filemtime, arginfo_filemtime) + ZEND_FE(filemtime_ns, arginfo_filemtime_ns) ZEND_FE(fileowner, arginfo_fileowner) ZEND_FE(fileperms, arginfo_fileperms) ZEND_FE(filesize, arginfo_filesize) diff --git a/ext/standard/basic_functions_decl.h b/ext/standard/basic_functions_decl.h index a7614da39591..483e1328002f 100644 --- a/ext/standard/basic_functions_decl.h +++ b/ext/standard/basic_functions_decl.h @@ -1,8 +1,8 @@ /* This is a generated file, edit basic_functions.stub.php instead. - * Stub hash: 53d47cfb46b55f62340f435b4637de62c4d0b531 */ + * Stub hash: 514f1812914afe4e84777239b10ad8e47b5cb710 */ -#ifndef ZEND_BASIC_FUNCTIONS_DECL_53d47cfb46b55f62340f435b4637de62c4d0b531_H -#define ZEND_BASIC_FUNCTIONS_DECL_53d47cfb46b55f62340f435b4637de62c4d0b531_H +#ifndef ZEND_BASIC_FUNCTIONS_DECL_514f1812914afe4e84777239b10ad8e47b5cb710_H +#define ZEND_BASIC_FUNCTIONS_DECL_514f1812914afe4e84777239b10ad8e47b5cb710_H typedef enum zend_enum_SortDirection { ZEND_ENUM_SortDirection_Ascending = 1, @@ -20,4 +20,4 @@ typedef enum zend_enum_RoundingMode { ZEND_ENUM_RoundingMode_PositiveInfinity = 8, } zend_enum_RoundingMode; -#endif /* ZEND_BASIC_FUNCTIONS_DECL_53d47cfb46b55f62340f435b4637de62c4d0b531_H */ +#endif /* ZEND_BASIC_FUNCTIONS_DECL_514f1812914afe4e84777239b10ad8e47b5cb710_H */ diff --git a/ext/standard/filestat.c b/ext/standard/filestat.c index ebf7f5f28549..332edb138f42 100644 --- a/ext/standard/filestat.c +++ b/ext/standard/filestat.c @@ -98,6 +98,62 @@ PHP_RSHUTDOWN_FUNCTION(filestat) /* {{{ */ } /* }}} */ +static long php_stat_timestamp_nanoseconds(const zend_stat_t *stat_sb, int type) +{ + switch (type) { + case FS_ATIME_NS: +#ifdef PHP_WIN32 + return stat_sb->st_atime_nsec; +#else +#if defined(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC) + return stat_sb->st_atim.tv_nsec; +#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC) + return stat_sb->st_atimespec.tv_nsec; +#endif + return 0; +#endif + case FS_MTIME_NS: +#ifdef PHP_WIN32 + return stat_sb->st_mtime_nsec; +#else +#if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) + return stat_sb->st_mtim.tv_nsec; +#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC) + return stat_sb->st_mtimespec.tv_nsec; +#endif + return 0; +#endif + case FS_CTIME_NS: +#ifdef PHP_WIN32 + return stat_sb->st_ctime_nsec; +#else +#if defined(HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC) + return stat_sb->st_ctim.tv_nsec; +#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC_TV_NSEC) + return stat_sb->st_ctimespec.tv_nsec; +#endif + return 0; +#endif + } + + return 0; +} + +static void php_stat_return_timestamp_ns(zval *return_value, time_t seconds, long nanoseconds) +{ +#if SIZEOF_ZEND_LONG == 8 + if (seconds >= ZEND_LONG_MIN / 1000000000 && seconds <= ZEND_LONG_MAX / 1000000000) { + zend_long timestamp = (zend_long) seconds * 1000000000; + if (timestamp <= ZEND_LONG_MAX - nanoseconds) { + ZVAL_LONG(return_value, timestamp + nanoseconds); + return; + } + } +#endif + + ZVAL_DOUBLE(return_value, (double) seconds * 1000000000 + nanoseconds); +} + static zend_result php_disk_total_space(char *path, double *space) /* {{{ */ #if defined(PHP_WIN32) /* {{{ */ { @@ -890,6 +946,15 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value) RETURN_LONG((zend_long)stat_sb->st_mtime); case FS_CTIME: RETURN_LONG((zend_long)stat_sb->st_ctime); + case FS_ATIME_NS: + php_stat_return_timestamp_ns(return_value, stat_sb->st_atime, php_stat_timestamp_nanoseconds(stat_sb, type)); + return; + case FS_MTIME_NS: + php_stat_return_timestamp_ns(return_value, stat_sb->st_mtime, php_stat_timestamp_nanoseconds(stat_sb, type)); + return; + case FS_CTIME_NS: + php_stat_return_timestamp_ns(return_value, stat_sb->st_ctime, php_stat_timestamp_nanoseconds(stat_sb, type)); + return; case FS_TYPE: if (S_ISLNK(stat_sb->st_mode)) { RETURN_STRING("link"); @@ -1019,14 +1084,20 @@ FileFunction(PHP_FN(filegroup), FS_GROUP) FileFunction(PHP_FN(fileatime), FS_ATIME) /* }}} */ +FileFunction(PHP_FN(fileatime_ns), FS_ATIME_NS) + /* {{{ Get last modification time of file */ FileFunction(PHP_FN(filemtime), FS_MTIME) /* }}} */ +FileFunction(PHP_FN(filemtime_ns), FS_MTIME_NS) + /* {{{ Get inode modification time of file */ FileFunction(PHP_FN(filectime), FS_CTIME) /* }}} */ +FileFunction(PHP_FN(filectime_ns), FS_CTIME_NS) + /* {{{ Get file type */ FileFunction(PHP_FN(filetype), FS_TYPE) /* }}} */ diff --git a/ext/standard/php_filestat.h b/ext/standard/php_filestat.h index c2ea6eed66e2..7d7dc58a575f 100644 --- a/ext/standard/php_filestat.h +++ b/ext/standard/php_filestat.h @@ -61,5 +61,8 @@ PHPAPI void php_stat(zend_string *filename, int type, zval *return_value); #define FS_LSTAT 16 #define FS_STAT 17 #define FS_LPERMS 18 +#define FS_ATIME_NS 19 +#define FS_MTIME_NS 20 +#define FS_CTIME_NS 21 #endif /* PHP_FILESTAT_H */ diff --git a/ext/standard/tests/file/gh25506_filetime_ns.phpt b/ext/standard/tests/file/gh25506_filetime_ns.phpt new file mode 100644 index 000000000000..68f2b30e2d48 --- /dev/null +++ b/ext/standard/tests/file/gh25506_filetime_ns.phpt @@ -0,0 +1,87 @@ +--TEST-- +fileatime_ns(), filemtime_ns(), and filectime_ns() return nanosecond timestamps +--SKIPIF-- + ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); +if (!is_resource($process)) { + die('skip GNU touch is unavailable'); +} +fclose($pipes[0]); +$output = stream_get_contents($pipes[1]); +$error = stream_get_contents($pipes[2]); +fclose($pipes[1]); +fclose($pipes[2]); +if (proc_close($process) !== 0) { + die('skip GNU touch is unavailable'); +} +$filename = tempnam(sys_get_temp_dir(), 'filetime_ns'); +$process = proc_open(['touch', '-m', '-d', '@1234567890.123456789', $filename], [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); +fclose($pipes[0]); +fclose($pipes[1]); +fclose($pipes[2]); +if (proc_close($process) !== 0) { + unlink($filename); + die('skip GNU touch cannot set nanosecond timestamps'); +} +$process = proc_open(['stat', '--format=%y', $filename], [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); +fclose($pipes[0]); +$output = stream_get_contents($pipes[1]); +$error = stream_get_contents($pipes[2]); +fclose($pipes[1]); +fclose($pipes[2]); +$status = proc_close($process); +unlink($filename); +if ($status !== 0 || !str_contains($output, '.123456789')) { + die('skip filesystem does not retain nanosecond timestamps'); +} +?> +--FILE-- + ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); +fclose($pipes[0]); +fclose($pipes[1]); +fclose($pipes[2]); +proc_close($process); +var_dump(fileatime_ns($filename) === 1234567890123456789); + +$process = proc_open(['touch', '-m', '-d', '@1234567890.123456789', $filename], [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); +fclose($pipes[0]); +fclose($pipes[1]); +fclose($pipes[2]); +proc_close($process); +clearstatcache(); +var_dump(filemtime_ns($filename) === 1234567890123456789); + +$process = proc_open(['touch', '-a', '-d', '@-1.123456789', $filename], [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); +fclose($pipes[0]); +fclose($pipes[1]); +fclose($pipes[2]); +proc_close($process); +clearstatcache(); +var_dump(fileatime_ns($filename) == -1123456789); + +foreach (['atime', 'mtime', 'ctime'] as $time) { + $seconds = call_user_func('file' . $time, $filename); + $nanoseconds = call_user_func('file' . $time . '_ns', $filename); + var_dump(is_int($nanoseconds) || is_float($nanoseconds)); + var_dump((int) floor($nanoseconds / 1_000_000_000) === $seconds); +} + +unlink($filename); +var_dump(@filemtime_ns($filename)); +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(false) diff --git a/win32/ioutil.c b/win32/ioutil.c index 8ac4d39d67cf..91d47cb738ee 100644 --- a/win32/ioutil.c +++ b/win32/ioutil.c @@ -1021,6 +1021,9 @@ PW32IO int php_win32_ioutil_link_w(const wchar_t *target, const wchar_t *link) #define FILETIME_TO_TIME_T(filetime) \ (time_t)(FILETIME_TO_UINT(filetime) / 10000000ULL) +#define FILETIME_TO_NSEC(filetime) \ + (long)((FILETIME_TO_UINT(filetime) % 10000000ULL) * 100) + static int php_win32_ioutil_fstat_int(HANDLE h, php_win32_ioutil_stat_t *buf, const wchar_t *pathw, size_t pathw_len, PBY_HANDLE_FILE_INFORMATION dp) {/*{{{*/ BY_HANDLE_FILE_INFORMATION d; @@ -1028,6 +1031,9 @@ static int php_win32_ioutil_fstat_int(HANDLE h, php_win32_ioutil_stat_t *buf, co LARGE_INTEGER t; uint8_t is_dir; + buf->st_atime_nsec = 0; + buf->st_mtime_nsec = 0; + buf->st_ctime_nsec = 0; data = !dp ? &d : dp; if(!GetFileInformationByHandle(h, data)) { @@ -1046,8 +1052,11 @@ static int php_win32_ioutil_fstat_int(HANDLE h, php_win32_ioutil_stat_t *buf, co buf->st_rdev = _buf.st_rdev; buf->st_size = _buf.st_size; buf->st_atime = _buf.st_atime; + buf->st_atime_nsec = 0; buf->st_mtime = _buf.st_mtime; + buf->st_mtime_nsec = 0; buf->st_ctime = _buf.st_ctime; + buf->st_ctime_nsec = 0; return 0; } else if(h == INVALID_HANDLE_VALUE && pathw_len > 0) { /* An abnormal situation it is. For example, the user is the file @@ -1129,8 +1138,11 @@ static int php_win32_ioutil_fstat_int(HANDLE h, php_win32_ioutil_stat_t *buf, co as zend_long is 32 bit. */ buf->st_size = (zend_long)t.QuadPart; buf->st_atime = FILETIME_TO_TIME_T(data->ftLastAccessTime); + buf->st_atime_nsec = FILETIME_TO_NSEC(data->ftLastAccessTime); buf->st_ctime = FILETIME_TO_TIME_T(data->ftCreationTime); + buf->st_ctime_nsec = FILETIME_TO_NSEC(data->ftCreationTime); buf->st_mtime = FILETIME_TO_TIME_T(data->ftLastWriteTime); + buf->st_mtime_nsec = FILETIME_TO_NSEC(data->ftLastWriteTime); return 0; }/*}}}*/ diff --git a/win32/ioutil.h b/win32/ioutil.h index 81dd3e6f44ef..9b025214adb2 100644 --- a/win32/ioutil.h +++ b/win32/ioutil.h @@ -734,8 +734,11 @@ typedef struct { __int32 st_blocks; #endif php_win32_ioutil_time_t st_atime; + long st_atime_nsec; php_win32_ioutil_time_t st_mtime; + long st_mtime_nsec; php_win32_ioutil_time_t st_ctime; + long st_ctime_nsec; } php_win32_ioutil_stat_t; typedef struct {