Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle calls setting timestamp to 0 #23310

Merged
merged 9 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
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 (typeof attr[key] === 'number') {
node[key] = attr[key];
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/library_nodefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,14 @@ 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 atime = new Date(attr.atime ?? fs.lstatSync(NODEFS.realPath(node)).atime);
var mtime = new Date(attr.mtime ?? fs.lstatSync(NODEFS.realPath(node)).mtime);
hoodmane marked this conversation as resolved.
Show resolved Hide resolved
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 (typeof attr[key] === 'number') {
hoodmane marked this conversation as resolved.
Show resolved Hide resolved
node[key] = attr[key];
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5571,7 +5571,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
Loading