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

Fix stat when the last path component is a symlink to ... #105

Merged
merged 6 commits into from
Aug 21, 2020

Conversation

sunfishcode
Copy link
Member

stat_by_parent doesn't handle this case because it drops the dirs stack before resolving the symlink. Fixing this required a significant amount of refactoring. Fortunately, splitting up open_manually into smaller pieces is useful to do anyway, since it was a large and complex function.

Replace `stat_via_parent` with `stat_manually` which similarly to
`open_manually`. Factor the `open_manually` implemenation to allow
parts to be reused.
Call `remove_dir_all` on the outermost directory, rather than the
innermost one, so that it removes the whole directory.
And make more things `pub(super)` instead of `pub(crate)`.
@sunfishcode sunfishcode force-pushed the sunfishcode/stat-manually branch 10 times, most recently from d8d7cde to ae7a077 Compare August 21, 2020 16:55
@sunfishcode sunfishcode merged commit f3f124f into main Aug 21, 2020
@sunfishcode sunfishcode deleted the sunfishcode/stat-manually branch August 21, 2020 17:09
ongardie added a commit to ongardie/cap-std that referenced this pull request Jun 3, 2022
Rust's standard library returns file creation times on Linux (at least with
glibc), and now rust-std will too.

File creation times are available on Linux via the `statx` syscall (not
`fstatat`), introduced in kernel 4.11, which was released in April 2017. The
Rust standard library already uses `statx` on Linux with glibc.

Making cap-std support file creation times on Linux required two changes:

1. `File::metadata()` uses the standard library (which uses `statx`), but this
   wouldn't ever copy over the created field on Linux. Now, any time the
   created field is set in the std Metadata struct, it's also set in the
   cap-primitives Metadata, regardless of platform.

2. `stat_unchecked` is used in several places, including fetching DirEntry
   metadata. Before, it called `fstatat` directly. Now, it calls `statx` on
   Linux when available, and it falls back to `fstatat` otherwise. Fortunately,
   Dan Gohman (@sunfishcode) had already added a method to convert `statx`
   results in commit d1fa735 (PR bytecodealliance#105) in 2020.

This commit also adds a new test to make sure file creation times are set in
cap-std Metadata if they are set in std Metadata.
sunfishcode added a commit that referenced this pull request Jun 4, 2022
* Return file creation times on Linux (using statx)

Rust's standard library returns file creation times on Linux (at least with
glibc), and now rust-std will too.

File creation times are available on Linux via the `statx` syscall (not
`fstatat`), introduced in kernel 4.11, which was released in April 2017. The
Rust standard library already uses `statx` on Linux with glibc.

Making cap-std support file creation times on Linux required two changes:

1. `File::metadata()` uses the standard library (which uses `statx`), but this
   wouldn't ever copy over the created field on Linux. Now, any time the
   created field is set in the std Metadata struct, it's also set in the
   cap-primitives Metadata, regardless of platform.

2. `stat_unchecked` is used in several places, including fetching DirEntry
   metadata. Before, it called `fstatat` directly. Now, it calls `statx` on
   Linux when available, and it falls back to `fstatat` otherwise. Fortunately,
   Dan Gohman (@sunfishcode) had already added a method to convert `statx`
   results in commit d1fa735 (PR #105) in 2020.

This commit also adds a new test to make sure file creation times are set in
cap-std Metadata if they are set in std Metadata.

* stat_unchecked: Add backticks to comments from PR suggestions

Co-authored-by: Dan Gohman <dev@sunfishcode.online>

* stat_unchecked: Reflow comment

* stat_unchecked: Rewrite match statement for clarity

* Rewrite file created times test to do exact comparisons against std

* Move file created times test into fs_additional

* Expand file created times test to check entire Metadata

* stat_unchecked: Handle EPERM errors from statx

* fs_additional: Fix non-Unix test build in metadata test

I had used `cfg!(unix)` instead of `#[cfg(unix)]` in 2eca023, which doesn't
conditionally compile out the Unix-specific code.

Co-authored-by: Dan Gohman <dev@sunfishcode.online>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant