mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(fs): reduce the repetition in exists.ts
(#5088)
This commit is contained in:
parent
ec27c49cbf
commit
c52c6ee057
31
fs/exists.ts
31
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?
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user