Get and set mode using std, still on open file descriptor #1811
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.
This replaces explicit
fstatandfchmodcalls (viarustix) withFile::metadataandFile::set_permissions, respectively.The change here is confined to
gix-worktree-stateand, more specifically, to the operation of checking the mode of an open file and setting a new mode based on it with some executable bits added.Background
In practice, currently, on Unix-like systems:
File::metadataeither:fstat, orstatxin a way that causes it to operate similarly tofstat(this is used on Linux, in versions withstatx).This is not explicitly documented, though calling
statorlstat, or callingstatxin a way that would cause it to behave likestatorlstatrather thanfstat, would not preserve the documented behavior ofFile::metadata, which operates on aFileand does not take a path.File::set_permissionscallsfchmod.This is explicitly documented and
fchmodis listed as an alias for documentation purposes. But it is noted as an implementation detail that may change without warning. However, callingchmodorlchmodwould not preserve the documented behavior ofFile::set_permissions, which (likeFile::metadata) operates on aFileand does not take a path.Rationale
While the above details can, in principle, change without warning, per Platform-specific behavior, it seems that, in preserving the documented semantics of operating on the file referenced by the file descriptor (rather than a path), the behavior would still have to be correct for our use here.
This pull request intends to maintain the effect of #1803 with two minor improvements for maintainability and clarity:
No longer require
gix-worktree-stateto depend directly onrustix. (This is minor because it still depends transitively on it throughgix-fs, though some uses ofrustix::fsingix-fsmight likewise be possible to replace in the future.)Use the standard library's slightly higher level interface where modes are treated as
u32even on operating systems where they are different (e.g.u16on macOS). This removes operations that do not correspond to what the code is conceptually doing. It also lets the function that computes the new mode from the old mode no longer depend on a type that differs across Unix-like targets.Importantly, this continues to differ from the pre-#1803 behavior of using functions that operated on paths.
Relationship between functions that get and set mode bits
For reading metadata on a file on Unix-like systems, the current general correspondence between Rust
std, POSIX functions, andstatx, where the rightmost three columns pertain to howstatxis called, is:For writing metadata on a file on Unix-like systems, the current correspondence between Rust
stdand POSIX functions is:Future directions
It may be that some uses of
rustix::fsfacilities can be similarly replaced ingix_fs, but this PR does not include any such changes.