Skip to content
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

Avoid some unneccesary path resolution #23172

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
50 changes: 33 additions & 17 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ FS.staticInit();
streams: [],
nextInode: 1,
nameTable: null,
currentPath: '/',
currentPath: null,
currentNode: null,
initialized: false,
// Whether we are currently ignoring permissions. Useful when preparing the
// filesystem and creating files inside read-only folders.
Expand Down Expand Up @@ -173,21 +174,22 @@ FS.staticInit();
//
lookupPath(path, opts = {}) {
if (!path) return { path: '', node: null };
opts.follow_mount ??= true
opts.follow_mount ??= true;

if (!PATH.isAbs(path)) {
path = FS.cwd() + '/' + path;
var current;
var current_path;
if (PATH.isAbs(path)) {
current = FS.root;
current_path = "/";
} else {
current = FS.currentNode;
current_path = FS.currentPath;
}
var pathParts = (x) => x.split('/').filter((p) => !!p && (p !== '.'));
var parts = pathParts(path);

// limit max consecutive symlinks to 40 (SYMLOOP_MAX).
linkloop: for (var nlinks = 0; nlinks < 40; nlinks++) {
// split the absolute path
var parts = path.split('/').filter((p) => !!p && (p !== '.'));

// start at the root
var current = FS.root;
var current_path = '/';

for (var i = 0; i < parts.length; i++) {
var islast = (i === parts.length-1);
if (islast && opts.parent) {
Expand Down Expand Up @@ -226,10 +228,14 @@ FS.staticInit();
throw new FS.ErrnoError({{{ cDefs.ENOSYS }}});
}
var link = current.node_ops.readlink(current);
if (!PATH.isAbs(link)) {
link = PATH.dirname(current_path) + '/' + link;
if (PATH.isAbs(link)) {
current = FS.root;
current_path = "/";
} else {
current = current.parent;
current_path = PATH.dirname(current_path);
}
path = link + '/' + parts.slice(i + 1).join('/');
parts = [].concat(pathParts(link), parts.slice(i + 1));
continue linkloop;
}
}
Expand Down Expand Up @@ -405,7 +411,7 @@ FS.staticInit();
if (!FS.isDir(node.mode)) {
return {{{ cDefs.ENOTDIR }}};
}
if (FS.isRoot(node) || FS.getPath(node) === FS.cwd()) {
if (FS.isRoot(node) || node === FS.currentNode) {
return {{{ cDefs.EBUSY }}};
}
} else {
Expand Down Expand Up @@ -879,15 +885,20 @@ FS.staticInit();
#endif
parent.node_ops.rmdir(parent, name);
FS.destroyNode(node);
node.parent = null;
#if FS_DEBUG
if (FS.trackingDelegate['onDeletePath']) {
FS.trackingDelegate['onDeletePath'](path);
}
#endif
},
readdir(path) {
var lookup = FS.lookupPath(path, { follow: true });
var node = lookup.node;
FS.readdirNode(FS.lookupPath(path, { follow: true }).node);
},
readdirNode(node) {
if (!node.parent) {
return [];
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any idea what these lines were supposed to be doing?

What does not having a parent mean? Is that only true for the root directory? Why would we want that to mean return the empty list?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, if we've unlinked a directory we want to return empty list or raise ENOENT. By contrast, if the directory exists but is empty we return [".", ".."]. So we need to label the directory node as unlinked somehow when we unlink them. I can add a comment here saying that.

if (!node.node_ops.readdir) {
throw new FS.ErrnoError({{{ cDefs.ENOTDIR }}});
}
Expand Down Expand Up @@ -944,6 +955,9 @@ FS.staticInit();
if (!node) {
throw new FS.ErrnoError({{{ cDefs.ENOENT }}});
}
return FS.statNode(node);
},
statNode(node) {
if (!node.node_ops.getattr) {
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
}
Expand Down Expand Up @@ -1366,6 +1380,7 @@ FS.staticInit();
throw new FS.ErrnoError(errCode);
}
FS.currentPath = lookup.path;
FS.currentNode = lookup.node;
},
createDefaultDirectories() {
FS.mkdir('/tmp');
Expand Down Expand Up @@ -1479,6 +1494,7 @@ FS.staticInit();
FS.nameTable = new Array(4096);

FS.mount(MEMFS, {}, '/');
FS.chdir('/');

FS.createDefaultDirectories();
FS.createDefaultDevices();
Expand Down
1 change: 1 addition & 0 deletions src/library_noderawfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ addToLibrary({
rename(...args) { fs.renameSync(...args); },
rmdir(...args) { fs.rmdirSync(...args); },
readdir(...args) { return ['.', '..'].concat(fs.readdirSync(...args)); },
readdirNode(node) { return ['.', '..'].concat(fs.readdirSync(node.path)); },
unlink(...args) { fs.unlinkSync(...args); },
readlink(...args) { return fs.readlinkSync(...args); },
stat(path, dontFollow) {
Expand Down
16 changes: 11 additions & 5 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,11 @@ var SyscallsLibrary = {
#endif // ~PROXY_POSIX_SOCKETS==0
__syscall_fchdir: (fd) => {
var stream = SYSCALLS.getStreamFromFD(fd);
FS.chdir(stream.path);
if (!FS.isDir(stream.node.mode)) {
return -{{{ cDefs.ENOTDIR }}};
}
FS.currentPath = stream.path;
FS.currentNode = stream.node;
return 0;
},
__syscall__newselect: (nfds, readfds, writefds, exceptfds, timeout) => {
Expand Down Expand Up @@ -688,8 +692,8 @@ var SyscallsLibrary = {
},
__syscall_getdents64__deps: ['$stringToUTF8'],
__syscall_getdents64: (fd, dirp, count) => {
var stream = SYSCALLS.getStreamFromFD(fd)
stream.getdents ||= FS.readdir(stream.path);
var stream = SYSCALLS.getStreamFromFD(fd);
stream.getdents ||= FS.readdirNode(stream.node);

var struct_size = {{{ C_STRUCTS.dirent.__size__ }}};
var pos = 0;
Expand All @@ -706,8 +710,10 @@ var SyscallsLibrary = {
type = 4; // DT_DIR
}
else if (name === '..') {
var lookup = FS.lookupPath(stream.path, { parent: true });
id = lookup.node.id;
// In Node rawfs, node.parent is null so we fall back to calling
// lookupPath.
var parent = stream.node.parent ?? FS.lookupPath(stream.path, { parent: true }).node;
id = parent.id;
type = 4; // DT_DIR
}
else {
Expand Down
Loading