Skip to content

[WasmFS] Track open files in the Node backend #16432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 79 additions & 27 deletions src/library_wasmfs_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,38 @@ mergeInto(LibraryManager.library, {
return ERRNO_CODES[code];
},

$wasmfsNodeLstat__deps: ['$wasmfsNodeIsWindows'],
$wasmfsNodeFixStat__deps: ['$wasmfsNodeIsWindows'],
$wasmfsNodeFixStat: function(stat) {
if (wasmfsNodeIsWindows) {
// Node.js on Windows never represents permission bit 'x', so
// propagate read bits to execute bits
stat.mode = stat.mode | ((stat.mode & 292) >> 2);
}
return stat;
},

$wasmfsNodeLstat__deps: ['$wasmfsNodeFixStat'],
$wasmfsNodeLstat: function(path) {
let stat;
try {
stat = fs.lstatSync(path);
if (wasmfsNodeIsWindows) {
// Node.js on Windows never represents permission bit 'x', so
// propagate read bits to execute bits
stat.mode = stat.mode | ((stat.mode & 292) >> 2);
}
return stat;
} catch (e) {
if (!e.code) throw e;
return undefined;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the return and wasmfsNodeFixStat call after the try/catch?

return wasmfsNodeFixStat(stat);
},

$wasmfsNodeFstat__deps: ['$wasmfsNodeFixStat'],
$wasmfsNodeFstat: function(fd) {
let stat;
try {
stat = fs.fstatSync(fd);
} catch (e) {
if (!e.code) throw e;
return undefined;
}
return wasmfsNodeFixStat(stat);
},

_wasmfs_node_readdir__deps: ['$wasmfsNodeConvertNodeCode'],
Expand Down Expand Up @@ -73,6 +90,26 @@ mergeInto(LibraryManager.library, {
// implicitly return 0
},

_wasmfs_node_stat_size__deps: ['$wasmfsNodeLstat'],
_wasmfs_node_stat_size: function(path_p, size_p) {
let stat = wasmfsNodeLstat(UTF8ToString(path_p));
if (stat === undefined) {
return 1;
}
{{{ makeSetValue('size_p', 0, 'stat.size', 'i32') }}};
// implicitly return 0
},

_wasmfs_node_fstat_size__deps: ['$wasmfsNodeFstat'],
_wasmfs_node_fstat_size: function(fd, size_p) {
let stat = wasmfsNodeFstat(fd);
if (stat === undefined) {
return 1;
}
{{{ makeSetValue('size_p', 0, 'stat.size', 'i32') }}};
// implicitly return 0
},

_wasmfs_node_insert_file__deps: ['$wasmfsNodeConvertNodeCode'],
_wasmfs_node_insert_file: function(path_p, mode) {
try {
Expand All @@ -95,52 +132,67 @@ mergeInto(LibraryManager.library, {
// implicitly return 0
},

_wasmfs_node_get_file_size__deps: ['$wasmfsNodeLstat'],
_wasmfs_node_get_file_size: function(path_p, size_p) {
let stat = wasmfsNodeLstat(UTF8ToString(path_p));
if (stat === undefined) {
return 1;
_wasmfs_node_unlink__deps: ['$wasmfsNodeConvertNodeCode'],
_wasmfs_node_unlink: function(path_p) {
try {
fs.unlinkSync(UTF8ToString(path_p));
} catch (e) {
if (!e.code) throw e;
return wasmfsNodeConvertNodeCode(e);
}
{{{ makeSetValue('size_p', 0, 'stat.size', 'i32') }}};
// implicitly return 0
},

_wasmfs_node_rmdir__deps: ['$wasmfsNodeConvertNodeCode'],
_wasmfs_node_rmdir: function(path_p) {
try {
fs.rmdirSync(UTF8ToString(path_p));
} catch (e) {
if (!e.code) throw e;
return wasmfsNodeConvertNodeCode(e);
}
// implicitly return 0
},

_wasmfs_node_open__deps: ['$wasmfsNodeConvertNodeCode'],
_wasmfs_node_open: function(path_p, mode_p) {
try {
return fs.openSync(UTF8ToString(path_p), UTF8ToString(mode_p));
} catch (e) {
if (!e.code) throw e;
return wasmfsNodeConvertNodeCode(e);
}
},

_wasmfs_node_close__deps: [],
_wasmfs_node_close: function(fd) {
fs.closeSync(fd);
},

_wasmfs_node_read__deps: ['$wasmfsNodeConvertNodeCode'],
_wasmfs_node_read: function(path_p, buf_p, len, pos, nread_p) {
let fd;
_wasmfs_node_read: function(fd, buf_p, len, pos, nread_p) {
try {
// TODO: Cache open file descriptors to guarantee that opened files will
// still exist when we try to access them.
fd = fs.openSync(UTF8ToString(path_p));
let nread = fs.readSync(fd, HEAPU8, buf_p, len, pos);
{{{ makeSetValue('nread_p', 0, 'nread', 'i32') }}};
} catch (e) {
if (!e.code) throw e;
return wasmfsNodeConvertNodeCode(e);
} finally {
if (fd !== undefined) {
fs.closeSync(fd);
}
}
// implicitly return 0
},

_wasmfs_node_write__deps : ['$wasmfsNodeConvertNodeCode'],
_wasmfs_node_write : function(path_p, buf_p, len, pos, nwritten_p) {
let fd;
_wasmfs_node_write : function(fd, buf_p, len, pos, nwritten_p) {
try {
// TODO: Cache open file descriptors to guarantee that opened files will
// still exist when we try to access them.
fd = fs.openSync(UTF8ToString(path_p), 'w');
let nwritten = fs.writeSync(fd, HEAPU8, buf_p, len, pos);
{{{ makeSetValue('nwritten_p', 0, 'nwritten', 'i32') }}};
} catch (e) {
if (!e.code) throw e;
return wasmfsNodeConvertNodeCode(e);
} finally {
if (fd !== undefined) {
fs.closeSync(fd);
}
}
// implicitly return 0
},
Expand Down
6 changes: 3 additions & 3 deletions system/include/emscripten/wasmfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ backend_t wasmfs_get_backend_by_fd(int fd);
// Returns the file descriptor for the new file like `open`. Returns a negative
// value on error. TODO: It might be worth returning a more specialized type
// like __wasi_fd_t here.
int wasmfs_create_file(char* pathname, mode_t mode, backend_t backend);
int wasmfs_create_file(const char* pathname, mode_t mode, backend_t backend);

// Creates a new directory in the new file system under a specific backend.
// Returns 0 on success like `mkdir`, or a negative value on error.
int wasmfs_create_directory(char* path, long mode, backend_t backend);
int wasmfs_create_directory(const char* path, long mode, backend_t backend);

// Backend creation

Expand All @@ -44,7 +44,7 @@ typedef backend_t (*backend_constructor_t)(void*);
backend_t wasmfs_create_proxied_backend(backend_constructor_t create_backend,
void* arg);

backend_t wasmfs_create_fetch_backend(char* base_url);
backend_t wasmfs_create_fetch_backend(const char* base_url);

backend_t wasmfs_create_node_backend(const char* root);

Expand Down
Loading