WICG / file-system-access

Expose the file system on the user’s device, so Web apps can interoperate with the user’s native applications.
https://wicg.github.io/file-system-access/
Other
669 stars 66 forks source link

Detecting symbolic links inside of directories. #254

Closed TheCymaera closed 1 year ago

TheCymaera commented 3 years ago

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 commented 3 years ago

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 commented 1 year ago

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