Skip to content

Commit

Permalink
Correctly handle calls setting timestamp to 0 (#23310)
Browse files Browse the repository at this point in the history
If someone calls `utimes("file", 0, 0)` the previous code I wrote would
not adjust the time stamp.
  • Loading branch information
hoodmane authored Jan 9, 2025
1 parent a5f3599 commit 25dea46
Show file tree
Hide file tree
Showing 23 changed files with 41 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/library_memfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ addToLibrary({
},
setattr(node, attr) {
for (const key of ["mode", "atime", "mtime", "ctime"]) {
if (attr[key]) {
if (attr[key] != null) {
node[key] = attr[key];
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/library_nodefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,15 @@ addToLibrary({
// update the common node structure mode as well
node.mode = attr.mode;
}
if (attr.atime || attr.mtime) {
var atime = attr.atime && new Date(attr.atime);
var mtime = attr.mtime && new Date(attr.mtime);
if (typeof (attr.atime ?? attr.mtime) === "number") {
// Unfortunately, we have to stat the current value if we don't want
// to change it. On top of that, since the times don't round trip
// this will only keep the value nearly unchanged not exactly
// unchanged. See:
// https://github.com/nodejs/node/issues/56492
var stat = () => fs.lstatSync(NODEFS.realPath(node));
var atime = new Date(attr.atime ?? stat().atime);
var mtime = new Date(attr.mtime ?? stat().mtime);
fs.utimesSync(path, atime, mtime);
}
if (attr.size !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion src/library_workerfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ addToLibrary({
},
setattr(node, attr) {
for (const key of ["mode", "atime", "mtime", "ctime"]) {
if (attr[key]) {
if (attr[key] != null) {
node[key] = attr[key];
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8332
8335
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20255
20261
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8314
8317
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20223
20229
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9336
9339
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24023
24029
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8280
8283
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20148
20154
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8280
8283
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20148
20154
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8346
8350
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20331
20337
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9341
9343
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24023
24029
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8332
8335
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20255
20261
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7640
7643
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18801
18807
2 changes: 1 addition & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5572,7 +5572,7 @@ def test_libgen(self):
def test_utime(self):
self.do_runf('utime/test_utime.c', 'success')

@also_with_noderawfs
@also_with_nodefs_both
def test_futimens(self):
self.do_runf('utime/test_futimens.c', 'success')

Expand Down
12 changes: 11 additions & 1 deletion test/utime/test_futimens.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <utime.h>


void create_file(const char *path, const char *buffer, int mode) {
Expand Down Expand Up @@ -67,7 +68,7 @@ void test() {
assert(s.st_rdev == 0);
assert(s.st_size == 8);
assert(s.st_ctime);
#if defined(__EMSCRIPTEN__) && !defined(NODERAWFS)
#if defined(__EMSCRIPTEN__) && !defined(NODERAWFS) && !defined(NODEFS)
assert(s.st_blksize == 4096);
assert(s.st_blocks == 1);
#endif
Expand Down Expand Up @@ -128,6 +129,15 @@ void test() {
times[1].tv_nsec = now.tv_nsec;
check_times(fd, times, 1);

printf("check setting time to 0...\n");
struct utimbuf tb = {0};
utime("folder/file", &tb);
times[0].tv_sec = 0;
times[0].tv_nsec = 0;
times[1].tv_sec = 0;
times[1].tv_nsec = 0;
check_times(fd, times, 0);

close(fd);

puts("success");
Expand Down

0 comments on commit 25dea46

Please sign in to comment.