Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
throw error;
}
}

function fileIsReadable(stat: Deno.FileInfo) {
if (stat.mode === null) {
return true; // Exclusive on Non-POSIX systems
} else if (Deno.uid() === stat.uid) {

Check warning on line 288 in fs/exists.ts

View check run for this annotation

Codecov / codecov/patch

fs/exists.ts#L287-L288

Added lines #L287 - L288 were not covered by tests
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?

Check warning on line 293 in fs/exists.ts

View check run for this annotation

Codecov / codecov/patch

fs/exists.ts#L291-L293

Added lines #L291 - L293 were not covered by tests
}