refactor(fs): reduce the repetition in exists.ts (#5088)

This commit is contained in:
Yoshiya Hinosawa 2024-06-19 19:15:15 +09:00 committed by GitHub
parent ec27c49cbf
commit c52c6ee057
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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?
}