Skip to content
41 changes: 29 additions & 12 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,18 @@ FS.staticInit();
break;
}

current = FS.lookupNode(current, parts[i]);
current_path = PATH.join2(current_path, parts[i]);
try {
current = FS.lookupNode(current, parts[i]);
} catch (e) {
// if noent_okay is true, suppress a ENOENT in the last component
// and return an object with an undefined node. This is needed for
// resolving symlinks in the path when creating a file.
if ((e?.errno === {{{ cDefs.ENOENT }}}) && islast && opts.noent_okay) {
return { path: current_path };
}
throw e;
}

// jump to the mount's root node if this is a mountpoint
if (FS.isMountpoint(current)) {
Expand All @@ -213,11 +223,17 @@ FS.staticInit();
// setting opts.follow = true will override this behavior.
if (!islast || opts.follow) {
var count = 0;
while (FS.isLink(current.mode)) {
var link = FS.readlink(current_path);
while (current && FS.isLink(current.mode)) {
if (!current.node_ops.readlink) {
throw new FS.ErrnoError({{{ cDefs.ENOSYS }}});
}
var link = current.node_ops.readlink(current);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Before this change, FS.readlink calls lookupPath again which repeats exactly the same work we've done up to this point in this function except with opts.follow false. Rather than doing that, we already have the node here so I figure it's better just to call node_ops.readlink.

current_path = PATH_FS.resolve(PATH.dirname(current_path), link);

var lookup = FS.lookupPath(current_path, { recurse_count: opts.recurse_count + 1 });
var lookup = FS.lookupPath(current_path, {
recurse_count: opts.recurse_count + 1,
noent_okay: islast && opts.noent_okay
});
current = lookup.node;

if (count++ > 40) { // limit max consecutive symlinks to 40 (SYMLOOP_MAX).
Expand Down Expand Up @@ -1025,14 +1041,15 @@ FS.staticInit();
node = path;
} else {
path = PATH.normalize(path);
try {
var lookup = FS.lookupPath(path, {
follow: !(flags & {{{ cDefs.O_NOFOLLOW }}})
});
node = lookup.node;
} catch (e) {
// ignore
}
// noent_okay makes it so that if the final component of the path
// doesn't exist, lookupPath returns `node: undefined`. `path` will be
// updated to point to the target of all symlinks.
var lookup = FS.lookupPath(path, {
follow: !(flags & {{{ cDefs.O_NOFOLLOW }}}),
noent_okay: true
});
node = lookup.node;
path = lookup.path;
}
// perhaps we need to create the node
var created = false;
Expand Down
3 changes: 3 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6059,6 +6059,9 @@ def test_unistd_links(self, args, nodefs):

self.do_run_in_out_file_test('unistd/links.c', emcc_args=args)

def test_unistd_write_broken_link(self):
self.do_run_in_out_file_test('unistd/write_broken_link.c')

@no_windows('Skipping NODEFS test, since it would require administrative privileges.')
@requires_node
def test_unistd_symlink_on_nodefs(self):
Expand Down
34 changes: 34 additions & 0 deletions test/unistd/write_broken_link.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>


int main() {
char* p1 = "link_source";
char* p2 = "link_target";

int res = symlink(p2, p1);
printf("link result: %d\n", res);
int src_fd = open(p1, O_CREAT | O_WRONLY, 0777);
printf("source_fd: %d, errno: %d %s\n", src_fd, errno, strerror(errno));
write(src_fd, "abc", 3);
close(src_fd);
{
int target_fd = open(p2, O_RDONLY);
printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno));
char buf[10];
read(target_fd, buf, 10);
printf("buf: '%s'\n", buf);
close(target_fd);
}
{
int target_fd = open(p1, O_RDONLY);
printf("target_fd: %d, errno: %d %s\n", target_fd, errno, strerror(errno));
char buf[10];
read(target_fd, buf, 10);
printf("buf: '%s'\n", buf);
close(target_fd);
}
}
6 changes: 6 additions & 0 deletions test/unistd/write_broken_link.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
link result: 0
source_fd: 3, errno: 0 No error information
target_fd: 3, errno: 0 No error information
buf: 'abc'
target_fd: 3, errno: 0 No error information
buf: 'abc'