emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.92k stars 3.32k forks source link

JS file system: distinguish between atime, mtime, and ctime #22998

Open hoodmane opened 1 week ago

hoodmane commented 1 week ago

This improves posix compliance of the file system.

Consider the following file:

#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>

static void create_file(const char *path, const char *buffer, int mode) {
  printf("creating: %s\n", path);
  int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
  assert(fd >= 0);

  int err = write(fd, buffer, sizeof(char) * strlen(buffer));
  assert(err ==  (sizeof(char) * strlen(buffer)));

  close(fd);
}

int main() {
    create_file("file", "test", 0555);
    struct stat buf1;
    stat("file", &buf1);
    sleep(1);
    chmod("file", 0777);
    struct stat buf2;
    stat("file", &buf2);
    printf("delta mtime: %ld\n", buf2.st_atim.tv_sec - buf1.st_atim.tv_sec);
    unlink("file");
}

Compiling and running with gcc vs emcc gives different output because the chmod updated the mtime of the file in Emscripten but not in linux:

$ gcc a.c
$ ./a.c
creating: file
delta mtime: 0
$ emcc a.c
$ node a.out.js
creating: file
delta mtime: 1
hoodmane commented 1 week ago

@sbc100 the tests assert that ctime changed. But mtime and atime should not change. These are all a single property in the memfs, so without recording distinct times for them we can't satisfy both requirements at once...

hoodmane commented 1 week ago

I guess I'll just remove the assertions that say the time did change then.

hoodmane commented 1 week ago

Probably this merits a changelog entry yes.