diff --git a/fs/exists.ts b/fs/exists.ts index d8a52503e..14d8ccd1f 100644 --- a/fs/exists.ts +++ b/fs/exists.ts @@ -133,15 +133,7 @@ export async function exists( return false; } if (options.isReadable) { - if (stat.mode === null) { - return true; // Exclusive on Non-POSIX systems - } - if (Deno.uid() === stat.uid) { - return (stat.mode & 0o400) === 0o400; // User is owner and can read? - } else if (Deno.gid() === stat.gid) { - return (stat.mode & 0o040) === 0o040; // User group is owner and can read? - } - return (stat.mode & 0o004) === 0o004; // Others can read? + return fileIsReadable(stat); } } return true; @@ -270,15 +262,7 @@ export function existsSync( return false; } if (options.isReadable) { - if (stat.mode === null) { - return true; // Exclusive on Non-POSIX systems - } - if (Deno.uid() === stat.uid) { - return (stat.mode & 0o400) === 0o400; // User is owner and can read? - } else if (Deno.gid() === stat.gid) { - return (stat.mode & 0o040) === 0o040; // User group is owner and can read? - } - return (stat.mode & 0o004) === 0o004; // Others can read? + return fileIsReadable(stat); } } return true; @@ -297,3 +281,14 @@ export function existsSync( throw error; } } + +function fileIsReadable(stat: Deno.FileInfo) { + if (stat.mode === null) { + return true; // Exclusive on Non-POSIX systems + } else if (Deno.uid() === stat.uid) { + return (stat.mode & 0o400) === 0o400; // User is owner and can read? + } else if (Deno.gid() === stat.gid) { + return (stat.mode & 0o040) === 0o040; // User group is owner and can read? + } + return (stat.mode & 0o004) === 0o004; // Others can read? +}