Skip to content

Commit

Permalink
refactor(fs): reduce the repetition in exists.ts (#5088)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored Jun 19, 2024
1 parent ec27c49 commit c52c6ee
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions fs/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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?
}

0 comments on commit c52c6ee

Please sign in to comment.