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 18, 2022
1 parent a41b1a3 commit 0f8903c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
13 changes: 7 additions & 6 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ 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_nsec, '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', 'i64') }}};
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i64') }}};
return 0;
},
Expand Down Expand Up @@ -955,6 +955,7 @@ var SyscallsLibrary = {
return 0;
},
__syscall_utimensat__sig: 'iippi',
__syscall_utimensat__deps: ['$readI53FromU64'],
__syscall_utimensat: function(dirfd, path, times, flags) {
path = SYSCALLS.getStr(path);
#if ASSERTIONS
Expand All @@ -965,11 +966,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
4 changes: 4 additions & 0 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,11 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned, ignore, align) {
}
}


const offset = calcFastOffset(ptr, pos, noNeedFirst);
if (type == 'i53') {
return 'readI53FromU64(' + offset + ')';
}

const slab = getHeapForType(type);
let ret = slab + '[' + getHeapOffset(offset, type) + ']';
Expand Down
2 changes: 1 addition & 1 deletion system/lib/libc/musl/arch/emscripten/bits/alltypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ struct timeval { time_t tv_sec; suseconds_t tv_usec; };
#endif

#if defined(__NEED_struct_timespec) && !defined(__DEFINED_struct_timespec)
struct timespec { time_t tv_sec; long tv_nsec; };
struct timespec { time_t tv_sec; int tv_nsec; };
#define __DEFINED_struct_timespec
#endif

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 0f8903c

Please sign in to comment.