feat: add IsReadable and IsWritable - #7
Merged
Conversation
Two predicates for the question the package could not answer: not what a path *is*, but what you may do with it. The motivating case is a tool that validates a path it will later rewrite — a configuration file, a lock file — and would rather refuse at validation time than fail halfway through a write. `Exists` and `IsFile` both pass a read-only file happily. Both inspect the file mode rather than the calling process's effective access, and the documentation says so plainly. That is the portable answer the standard library can give: os.Stat exposes mode bits on every platform, while asking "can I open this?" requires access(2) or an attempted open — neither available portably without cgo or a build tag. The doc comments point a caller who needs certainty at opening the file instead, and note that the answer can change between any check and the open anyway. Consistent with the rest of the package: paths are expanded first, and an empty path, a missing path and a non-regular file all report false. Tests cover both predicates across empty/readable/write-only/read-only/directory/missing, plus HOME expansion. One example and one benchmark, matching the existing style.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two predicates for the question
fsxcannot currently answer: not what a path is, but what you may do with it.Why
The motivating case is a tool that validates a path it will later rewrite — a configuration file, a lock file — and would rather refuse at validation time than fail halfway through a write.
ExistsandIsFileboth pass a read-only file happily, so a validator built from them reports success and the write fails later, in a worse place.Found while adopting
fsxinaizon-shared/machete, which needed exactly this for its--config.filecheck and had to keep a local helper for it. Everything else it needed,fsxalready had — includingHasExtension, which is better than the hardcoded suffix check it replaced.Mode bits, not effective access — and the docs say so
Both inspect the file mode, not the calling process's effective access. That is the portable answer the standard library can give:
os.Statexposes mode bits on every platform, while asking "can I open this?" requiresaccess(2)or an attempted open, neither available portably without cgo or a build tag.Rather than paper over that, the doc comments state it and point a caller who needs certainty at opening the file — noting that the answer can change between any check and the open regardless. A predicate cannot promise more than the mode bits it read, and a name like
IsWritableis a footgun if the limitation isn't written down.Consistency
Follows the existing conventions:
ExpandPathfirst, like every other functionfalsehasOwnerBitshelper, so the two cannot driftdoc.gogains a "Permission Predicates" sectionTests
Table-driven in the existing style, covering empty / readable / write-only / read-only / directory / missing for both, plus
HOMEand~expansion. OneExampleand oneBenchmark.gofmt,go vetandgo test ./...clean.