modifiy MEMFS timestamp handling to support better caching (#893)

This commit is contained in:
Nicolas Ollinger 2020-12-24 09:34:02 +01:00 committed by GitHub
parent bcc3996a0d
commit 04ca90747f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,38 @@
Modify MEMFS to update timestamp of parent directory on file creation and removal.
More POSIXish compliant for a one timestamp fs, behaves like a modification date.
--- a/emsdk/fastcomp/emscripten/src/library_memfs.js 2019-08-19 18:11:40.000000000 +0200
+++ b/emsdk/fastcomp/emscripten/src/library_memfs.js 2020-12-23 12:22:38.000000000 +0100
@@ -88,6 +88,7 @@
// add the new node to the parent
if (parent) {
parent.contents[name] = node;
+ parent.timestamp = node.timestamp;
}
return node;
},
@@ -213,12 +214,16 @@
}
// do the internal rewiring
delete old_node.parent.contents[old_node.name];
+ var now = Date.now();
+ old_node.parent.timestamp = now;
old_node.name = new_name;
new_dir.contents[new_name] = old_node;
+ new_dir.timestamp = now;
old_node.parent = new_dir;
},
unlink: function(parent, name) {
delete parent.contents[name];
+ parent.timestamp = Date.now();
},
rmdir: function(parent, name) {
var node = FS.lookupNode(parent, name);
@@ -226,6 +231,7 @@
throw new FS.ErrnoError({{{ cDefine('ENOTEMPTY') }}});
}
delete parent.contents[name];
+ parent.timestamp = Date.now();
},
readdir: function(node) {
var entries = ['.', '..'];

View File

@ -0,0 +1,78 @@
import subprocess
from . import common
def test_memfsmtime(tmpdir):
with tmpdir.as_cwd():
with open("main.c", "w") as f:
f.write(
r"""\
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
void mysleep(int delta)
{
time_t end = time(NULL) + delta;
while (time(NULL)<end);
}
time_t getmtime(char *fname)
{
struct stat buf;
stat(fname, &buf);
return buf.st_mtime;
}
void writefile(char *fname, char *content)
{
int fd = open(fname, O_CREAT | O_TRUNC | O_WRONLY, 0777);
write(fd, content, strlen(content));
close(fd);
}
int main(int argc, char *argv[])
{
char tmpdir[64] = "/tmp/tmpXXXXXXX";
char fname[64];
time_t t0, t1, t2, t3;
mkdtemp(tmpdir);
strcpy(fname, tmpdir);
strcat(fname, "/foo.py");
t0 = getmtime(tmpdir);
mysleep(1);
writefile(fname, "bar = 54\n");
t1 = getmtime(tmpdir);
t2 = getmtime(fname);
assert(t1 > t0);
assert(t1 == t2);
mysleep(1);
unlink(fname);
t3 = getmtime(tmpdir);
assert(t3 > t1);
}
"""
)
subprocess.run(
[
"emcc",
"-s",
"MAIN_MODULE=1",
"main.c",
],
check=True,
env=common.env,
)
out = subprocess.run(
["node", "a.out.js"], capture_output=True, check=False, env=common.env
)
assert out.returncode == 0