Skip to content
Open
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
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/types.h>
#include <sys/stat.h>])

dnl Checks for types.
AC_TYPE_UID_T
Expand Down
6 changes: 6 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
16 changes: 15 additions & 1 deletion ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions ext/standard/basic_functions_decl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions ext/standard/filestat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) /* {{{ */
{
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)
/* }}} */
Expand Down
3 changes: 3 additions & 0 deletions ext/standard/php_filestat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
87 changes: 87 additions & 0 deletions ext/standard/tests/file/gh25506_filetime_ns.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
--TEST--
fileatime_ns(), filemtime_ns(), and filectime_ns() return nanosecond timestamps
--SKIPIF--
<?php
if (PHP_OS_FAMILY !== 'Linux' || !function_exists('proc_open')) {
die('skip requires proc_open() and GNU touch');
}
$process = proc_open(['touch', '--version'], [0 => ['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--
<?php
$filename = tempnam(sys_get_temp_dir(), 'filetime_ns');
$process = proc_open(['touch', '-a', '-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);
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)
12 changes: 12 additions & 0 deletions win32/ioutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -1021,13 +1021,19 @@ 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;
PBY_HANDLE_FILE_INFORMATION data;
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)) {
Expand All @@ -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
Expand Down Expand Up @@ -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;
}/*}}}*/
Expand Down
3 changes: 3 additions & 0 deletions win32/ioutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading