Skip to content

Commit 25dea46

Browse files
authored
Correctly handle calls setting timestamp to 0 (emscripten-core#23310)
If someone calls `utimes("file", 0, 0)` the previous code I wrote would not adjust the time stamp.
1 parent a5f3599 commit 25dea46

23 files changed

+41
-25
lines changed

src/library_memfs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ addToLibrary({
172172
},
173173
setattr(node, attr) {
174174
for (const key of ["mode", "atime", "mtime", "ctime"]) {
175-
if (attr[key]) {
175+
if (attr[key] != null) {
176176
node[key] = attr[key];
177177
}
178178
}

src/library_nodefs.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,15 @@ addToLibrary({
166166
// update the common node structure mode as well
167167
node.mode = attr.mode;
168168
}
169-
if (attr.atime || attr.mtime) {
170-
var atime = attr.atime && new Date(attr.atime);
171-
var mtime = attr.mtime && new Date(attr.mtime);
169+
if (typeof (attr.atime ?? attr.mtime) === "number") {
170+
// Unfortunately, we have to stat the current value if we don't want
171+
// to change it. On top of that, since the times don't round trip
172+
// this will only keep the value nearly unchanged not exactly
173+
// unchanged. See:
174+
// https://github.com/nodejs/node/issues/56492
175+
var stat = () => fs.lstatSync(NODEFS.realPath(node));
176+
var atime = new Date(attr.atime ?? stat().atime);
177+
var mtime = new Date(attr.mtime ?? stat().mtime);
172178
fs.utimesSync(path, atime, mtime);
173179
}
174180
if (attr.size !== undefined) {

src/library_workerfs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ addToLibrary({
9191
},
9292
setattr(node, attr) {
9393
for (const key of ["mode", "atime", "mtime", "ctime"]) {
94-
if (attr[key]) {
94+
if (attr[key] != null) {
9595
node[key] = attr[key];
9696
}
9797
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8332
1+
8335
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20255
1+
20261
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8314
1+
8317
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20223
1+
20229
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9336
1+
9339
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24023
1+
24029
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8280
1+
8283

0 commit comments

Comments
 (0)