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

Detecting symbolic links inside of directories. #254

Closed
TheCymaera opened this issue Dec 7, 2020 · 2 comments
Closed

Detecting symbolic links inside of directories. #254

TheCymaera opened this issue Dec 7, 2020 · 2 comments

Comments

@TheCymaera
Copy link

TheCymaera commented Dec 7, 2020

The ability to detect symbolic links will help guard against circular references when recursively reading a directory. For example, the following function breaks when encountering a symlink that refers to its own parent:

/**
 * Print file tree to the console
 * @param {FileSystemHandle} handle
 */
async function printFileTree(handle) {
	switch (handle.kind) {
		case "file": {
			console.log(handle.name);
			return;
		}
		case "directory": {
			console.group(handle.name);
			for await (const childHandle of handle.values()) {
				await printFileTree(childHandle);
			}
			console.groupEnd();
			return;
		}
	}
}

A potential solution is to add a boolean property to indicate that a handle was created from a symlink. For example:

for await (const [name,handle] of dirHandle) {
	if (handle.fromSymlink) {
		// if visited, continue
	}
	switch (handle.kind) {
		case "file":
			// do stuff with file...
		case "directory":
			// do stuff with directory...
	}
}

Alternatively, directory iterators can return objects similar to Dirents from Node.js, which we can use to create handles. For example:

for await (const entry of dirHandle) {
	if (entry.isFile()) {
		const handle = entry.handle();
		// do stuff with file handle...
	}
	if (entry.isDirectory()) {
		const handle = entry.handle();
		// do stuff with directory handle...
	}
	if (entry.isSymlink()) {
		const linkedHandle = entry.handle();
		// do stuff with linked file/directory, skip if visited
	}
}
@rektide
Copy link

rektide commented Aug 12, 2021

Agreed. It's not just Node.JS: supporting seeing a POSIX dirent link type would be a major boon.

It would be great to consider what other types we might be able to support here too. Sockets, character devices, block devices, FIFO ought all seem near-at-hand, like something that could be supported by the API in general & identified in directory listing.

@dslee414
Copy link
Collaborator

Thanks for filing this! I noticed that there is another issue: whatwg/fs#54 you filed, so closing this one as duplicate.

@dslee414 dslee414 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants