streamich / memfs

JavaScript file system utilities
http://streamich.github.io/memfs/
Apache License 2.0
1.74k stars 130 forks source link

Directory modification time should be update when "contents of directory" of changes #869

Open williamstein opened 1 year ago

williamstein commented 1 year ago

This could related to #116.

The rules are possibly complicated (see [1] and [2]), but certain operations, e.g., "create a new file" in a directory should result in the modification timestamp of the directory itself being updated. Posix OS's rely on this behavior; as an example, Python won't scan a path again if its time isn't updated, so without this behavior you can't install new modules in a running Python session, which is causing trouble for python-wasm. Here's a simple example to illustrate the problem:

~/upstream/memfs-js$ node
Welcome to Node.js v19.0.0.
Type ".help" for more information.
> require('nemfs').vol.fromJSON({'/x/y': '...'}); vol=require('.').vol; 0;
0
> vol.statSync('/x').mtime
2022-10-26T01:53:18.917Z
> vol.mkdirSync('/x/z')
undefined
> vol.statSync('/x').mtime
2022-10-26T01:53:18.917Z

Note that the mtime did not change. Now try the same on your normal filesystem:

/tmp$ rm -rf x
/tmp$ mkdir x; touch x/y
/tmp$ node
Welcome to Node.js v19.0.0.
Type ".help" for more information.
> require('fs').statSync('/tmp/x').mtime
2022-10-26T01:54:22.172Z
> require('fs').mkdirSync('/tmp/x/z')
undefined
> require('fs').statSync('/tmp/x').mtime
2022-10-26T01:54:52.265Z

Notice that the timestamp changed this time.

I haven't even begun to think about how to implement this in memfs, but I do think it's important and not doing so leads to subtle bugs, so I'll try...

[1] https://stackoverflow.com/questions/3451863/when-does-a-unix-directory-change-its-timestamp [2] https://medium.com/@quoscient/mac-b-timestamps-across-posix-implementations-linux-openbsd-freebsd-1e2d5893e4f

williamstein commented 1 year ago

Here is how I fixed this is @cowasm/memfs: https://github.com/sagemathinc/memfs-js/commit/8454383a819ee4c99bc96aa2ef23a051a5fe577e

If you like this and agree with the semantics (?), let me know and I'll create a PR.

G-Rath commented 1 year ago

@williamstein let's open a PR and go from there :)