Skip to content

Commit

Permalink
Fix utime/stat for time_t > 2^32
Browse files Browse the repository at this point in the history
See: #17393
  • Loading branch information
sbc100 committed Jul 19, 2022
1 parent a3d8979 commit 223f52a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
11 changes: 6 additions & 5 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ var SyscallsLibrary = {
{{{ makeSetValue('buf', C_STRUCTS.stat.st_size, 'stat.size', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blksize, '4096', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blocks, 'stat.blocks', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, '(stat.atime.getTime() / 1000)|0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, 'Math.floor(stat.atime.getTime() / 1000)', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_nsec, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, '(stat.mtime.getTime() / 1000)|0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, 'Math.floor(stat.mtime.getTime() / 1000)', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_nsec, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, '(stat.ctime.getTime() / 1000)|0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, 'Math.floor(stat.ctime.getTime() / 1000)', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '0', 'i32') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i64') }}};
return 0;
Expand Down Expand Up @@ -954,6 +954,7 @@ var SyscallsLibrary = {
return 0;
},
__syscall_utimensat__sig: 'iippi',
__syscall_utimensat__deps: ['$readI53FromI64'],
__syscall_utimensat: function(dirfd, path, times, flags) {
path = SYSCALLS.getStr(path);
#if ASSERTIONS
Expand All @@ -964,11 +965,11 @@ var SyscallsLibrary = {
var atime = Date.now();
var mtime = atime;
} else {
var seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i32') }}};
var seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i53') }}};
var nanoseconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_nsec, 'i32') }}};
atime = (seconds*1000) + (nanoseconds/(1000*1000));
times += {{{ C_STRUCTS.timespec.__size__ }}};
seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i32') }}};
seconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_sec, 'i53') }}};
nanoseconds = {{{ makeGetValue('times', C_STRUCTS.timespec.tv_nsec, 'i32') }}};
mtime = (seconds*1000) + (nanoseconds/(1000*1000));
}
Expand Down
32 changes: 18 additions & 14 deletions tests/stat/test_stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ void create_file(const char *path, const char *buffer, int mode) {
close(fd);
}

#define TEST_TIME (1ll << 34)
void setup() {
struct utimbuf t = {1200000000, 1200000000};
struct utimbuf t = {TEST_TIME, TEST_TIME};

mkdir("folder", 0777);
create_file("folder/file", "abcdef", 0777);
Expand All @@ -51,7 +52,7 @@ void cleanup() {
void test() {
int err;
struct stat s;
struct utimbuf t = {1200000000, 1200000000};
struct utimbuf t = {TEST_TIME, TEST_TIME};

// non-existent
err = stat("does_not_exist", &s);
Expand All @@ -70,8 +71,11 @@ void test() {
assert(s.st_rdev == 0);
#endif
assert(s.st_size);
assert(s.st_atime == 1200000000);
assert(s.st_mtime == 1200000000);
printf("TEST_TIME: %llx\n", TEST_TIME);
printf("s.st_atime: %llx\n", s.st_atime);
printf("s.st_mtime: %llx\n", s.st_mtime);
assert(s.st_atime == TEST_TIME);
assert(s.st_mtime == TEST_TIME);
assert(s.st_ctime);
#ifdef __EMSCRIPTEN__
assert(s.st_blksize == 4096);
Expand All @@ -92,8 +96,8 @@ void test() {
assert(s.st_nlink);
assert(s.st_rdev == 0);
assert(s.st_size == 6);
assert(s.st_atime == 1200000000);
assert(s.st_mtime == 1200000000);
assert(s.st_atime == TEST_TIME);
assert(s.st_mtime == TEST_TIME);
assert(s.st_ctime);
#ifdef __EMSCRIPTEN__
assert(s.st_blksize == 4096);
Expand All @@ -110,8 +114,8 @@ void test() {
assert(s.st_nlink);
assert(s.st_rdev == 0);
assert(s.st_size == 6);
assert(s.st_atime == 1200000000);
assert(s.st_mtime == 1200000000);
assert(s.st_atime == TEST_TIME);
assert(s.st_mtime == TEST_TIME);
assert(s.st_ctime);
#ifdef __EMSCRIPTEN__
assert(s.st_blksize == 4096);
Expand Down Expand Up @@ -152,8 +156,8 @@ void test() {
assert(s.st_nlink);
assert(s.st_rdev == 0);
assert(s.st_size == 6);
assert(s.st_atime == 1200000000);
assert(s.st_mtime == 1200000000);
assert(s.st_atime == TEST_TIME);
assert(s.st_mtime == TEST_TIME);
assert(s.st_ctime);
#ifdef __EMSCRIPTEN__
assert(s.st_blksize == 4096);
Expand All @@ -170,8 +174,8 @@ void test() {
assert(s.st_nlink);
assert(s.st_rdev == 0);
assert(s.st_size == 4); // strlen("file")
assert(s.st_atime != 1200000000); // should NOT match the utime call we did for dir/file
assert(s.st_mtime != 1200000000);
assert(s.st_atime != TEST_TIME); // should NOT match the utime call we did for dir/file
assert(s.st_mtime != TEST_TIME);
assert(s.st_ctime);
#ifdef __EMSCRIPTEN__
assert(s.st_blksize == 4096);
Expand All @@ -183,11 +187,11 @@ void test() {
utime("folder/subdir", &t);
create_file("folder/subdir/file", "abcdef", 0777);
err = stat("folder/subdir", &s);
assert(s.st_mtime != 1200000000);
assert(s.st_mtime != TEST_TIME);
utime("folder/subdir", &t);
unlink("folder/subdir/file");
err = stat("folder/subdir", &s);
assert(s.st_mtime != 1200000000);
assert(s.st_mtime != TEST_TIME);

puts("success");
}
Expand Down

0 comments on commit 223f52a

Please sign in to comment.