Skip to content

Commit 51b99e1

Browse files
authored
WasmFS JS API: Implement utime (#19561)
1 parent ea13106 commit 51b99e1

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

emcc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,7 @@ def phase_linker_setup(options, state, newargs):
23462346
'_wasmfs_chmod',
23472347
'_wasmfs_fchmod',
23482348
'_wasmfs_lchmod',
2349+
'_wasmfs_utime',
23492350
'_wasmfs_llseek',
23502351
'_wasmfs_identify',
23512352
'_wasmfs_readlink',

src/library_wasmfs.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ FS.createPreloadedFile = FS_createPreloadedFile;
161161
var buffer = stringToUTF8OnStack(path);
162162
return __wasmfs_chdir(buffer);
163163
}),
164-
// TODO: read
165164
read: (stream, buffer, offset, length, position) => {
166165
var seeking = typeof position != 'undefined';
167166

@@ -251,7 +250,6 @@ FS.createPreloadedFile = FS_createPreloadedFile;
251250
ino: {{{ makeGetValue('statBuf', C_STRUCTS.stat.st_ino, "u53") }}}
252251
}
253252
},
254-
// TODO: stat
255253
stat: (path) => {
256254
var statBuf = _malloc({{{ C_STRUCTS.stat.__size__ }}});
257255
FS.handleError(withStackSave(() => {
@@ -262,7 +260,6 @@ FS.createPreloadedFile = FS_createPreloadedFile;
262260

263261
return stats;
264262
},
265-
// TODO: lstat
266263
lstat: (path) => {
267264
var statBuf = _malloc({{{ C_STRUCTS.stat.__size__ }}});
268265
FS.handleError(withStackSave(() => {
@@ -291,13 +288,17 @@ FS.createPreloadedFile = FS_createPreloadedFile;
291288
// TDOO: chown
292289
// TODO: lchown
293290
// TODO: fchown
291+
utime: (path, atime, mtime) => (
292+
FS.handleError(withStackSave(() => (
293+
__wasmfs_utime(stringToUTF8OnStack(path), atime, mtime)
294+
)))
295+
),
294296
truncate: (path, len) => {
295297
return FS.handleError(withStackSave(() => (__wasmfs_truncate(stringToUTF8OnStack(path), {{{ splitI64('len') }}}))));
296298
},
297299
ftruncate: (fd, len) => {
298300
return FS.handleError(__wasmfs_ftruncate(fd, {{{ splitI64('len') }}}));
299301
},
300-
// TODO: utime
301302
findObject: (path) => {
302303
var result = __wasmfs_identify(path);
303304
if (result == {{{ cDefs.ENOENT }}}) {

system/lib/wasmfs/js_api.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ int _wasmfs_close(int fd) {
246246
return __wasi_fd_close(fd);
247247
}
248248

249+
int _wasmfs_utime(char *path, long atime_ms, long mtime_ms) {
250+
struct timespec times[2];
251+
times[0].tv_sec = atime_ms / 1000;
252+
times[0].tv_nsec = (atime_ms % 1000) * 1000000;
253+
times[1].tv_sec = mtime_ms / 1000;
254+
times[1].tv_nsec = (mtime_ms % 1000) * 1000000;
255+
256+
return __syscall_utimensat(AT_FDCWD, (intptr_t)path, (intptr_t)times, 0);
257+
};
258+
249259
int _wasmfs_stat(char* path, struct stat* statBuf) {
250260
return __syscall_stat64((intptr_t)path, (intptr_t)statBuf);
251261
}

test/fs/test_fs_js_api.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,32 @@ void test_fs_truncate() {
326326
remove("truncatetest");
327327
}
328328

329+
void test_fs_utime() {
330+
EM_ASM(
331+
FS.writeFile('utimetest', 'a=1\nb=2\n');
332+
);
333+
334+
EM_ASM(
335+
FS.utime('utimetest', 10500, 8000);
336+
);
337+
struct stat utimeStats;
338+
stat("utimetest", &utimeStats);
339+
340+
assert(utimeStats.st_atime == 10);
341+
assert(utimeStats.st_atim.tv_sec == 10);
342+
343+
// WasmFS correctly sets both times, but the legacy API sets both times to the max of atime and mtime.
344+
#if WASMFS
345+
assert(utimeStats.st_mtime == 8);
346+
assert(utimeStats.st_mtim.tv_sec == 8);
347+
#else
348+
assert(utimeStats.st_mtime == 10);
349+
assert(utimeStats.st_mtim.tv_sec == 10);
350+
#endif
351+
352+
remove("utimetest");
353+
}
354+
329355
void cleanup() {
330356
remove("testfile");
331357
remove("renametestfile");
@@ -336,12 +362,14 @@ void cleanup() {
336362
int main() {
337363
test_fs_open();
338364
test_fs_rename();
365+
test_fs_readlink();
339366
test_fs_read();
340367
test_fs_rmdir();
341368
test_fs_close();
342369
test_fs_mknod();
343370
test_fs_allocate();
344371
test_fs_truncate();
372+
test_fs_utime();
345373

346374
cleanup();
347375

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
163996
1+
164034

0 commit comments

Comments
 (0)