Skip to content

Add Dir equivalents of fs::metadata & fs::symlink_metadata - #163024

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
RalfJung:dir-metadata-at
Sep 29, 2026
Merged

rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
RalfJung:dir-metadata-at

Conversation

@RalfJung

@RalfJung RalfJung commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

View all comments

This adds methods to Dir that allow querying the metadata of files/directories relative to a Dir. Miri would really like to be able to do this so I figured I'd give it a shot. :)

The first commit refactors the Unix DirEntry methods a bit with cfg_select to avoid repeating the cfg condition.

Tracking issue: #120426

try-jobs: test-x86_64-msvc-1

@rustbot

rustbot commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

Any special-casing of Miri in the standard library requires review.

cc @rust-lang/miri

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 19, 2026
@rustbot

rustbot commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 13 candidates
  • Random selection from 6 candidates

@RalfJung RalfJung mentioned this pull request Sep 19, 2026
4 of 13 tasks
Comment thread library/std/src/sys/fs/windows/dir.rs Outdated
Comment on lines +250 to +268
fn metadata_at_native(&self, path: &[u16], reparse: ReparsePoint) -> io::Result<FileAttr> {
let mut opts = OpenOptions::new();
// No read or write permissions are necessary
opts.access_mode(0);
opts.custom_flags(c::FILE_FLAG_BACKUP_SEMANTICS | reparse.as_flag());

// Attempt to open the file normally.
// FIXME: `fs::metadata` has a fallback path when that fails. That has not (yet) been ported
// to directory handles.
let name = UnicodeStrRef::from_slice(path);
let object_attributes = c::OBJECT_ATTRIBUTES {
RootDirectory: self.handle.as_raw_handle(),
ObjectName: name.as_ptr().cast_mut(),
..c::OBJECT_ATTRIBUTES::with_length()
};
let create_opt = 0; // We don't want to create anything, only open existing things.
let handle = unsafe { nt_create_file(&opts, &object_attributes, create_opt)? };
File { handle }.file_attr()
}

@RalfJung RalfJung Sep 19, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChrisDenton I hope this makes sense, I am mostly guessing here.^^ Especially about passing 0 for create_opt; the only other caller passes if dir { c::FILE_DIRECTORY_FILE } else { c::FILE_NON_DIRECTORY_FILE } but we cannot know in advance whether this is a directory or a file...

I also didn't copy the complicated fallback stuff from fs::metadata as I had no idea what that would look like with directory handles. I hope this first step is still useful and we can always add more fallback code later?

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left some notes but essentially NT APIs are a bit different to the higher level win32 APIs. I'm ok with a partial implementation since I think I'm likely going to be rewriting (or at least refactoring) a lot of this anyway.

@RalfJung

This comment was marked as resolved.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 19, 2026
Add `Dir::metadata_at` & `Dir::symlink_metadata_at`


try-job: test*msvc*
@rust-log-analyzer

This comment has been minimized.

@RalfJung
RalfJung force-pushed the dir-metadata-at branch 2 times, most recently from 23f96b7 to 473f0c0 Compare September 19, 2026 16:44
@rust-log-analyzer

This comment has been minimized.

@RalfJung

RalfJung commented Sep 19, 2026 •

Copy link
Copy Markdown
Member Author

Hm...

thread 'fs::tests::test_dir_metadata_at' (6552) panicked at library\std\src\fs\tests.rs:2812:5:
dir.open_file_with("subdir/bar.txt",
&OpenOptions::new().create(true).write(true)) failed with: The filename, directory name, or volume label syntax is incorrect. (os error 123)

Does this not support /? That would then likely affect the other Dir methods as well and would be inconsistent with the normal fs methods AFAIK.

This isn't even in my new method, the failure is from

dir.open_file_with("subdir/bar.txt", &OpenOptions::new().create(true).write(true))

I should file an issue... EDIT: #163032

@RalfJung

This comment was marked as outdated.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 19, 2026
Add `Dir::metadata_at` & `Dir::symlink_metadata_at`


try-job: test-*-msvc-1
Comment thread library/std/src/fs.rs Outdated
/// }
/// ```
#[unstable(feature = "dirfd", issue = "120426")]
pub fn metadata_at<P: AsRef<Path>>(&self, path: P) -> io::Result<Metadata> {

@RalfJung RalfJung Sep 19, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name does match the operation in fs that it corresponds to -- but Dir::metadata is already taken. So I went for a new name, inspired by the Linux naming scheme with the *at calls working on directory handles.

Alternatives that have been suggested or that I can think of:

  • Use just metadata for this, and ask people to write dir.metadata(".") to get the metadata of the directory itself. But that seems silly in terms of the extra syscalls it causes.
  • Use just metadata for this, and use self_metadata/metadata_self or so for the metadata of the directory handle itself.

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be honest, I don't love the name and it would be especially weird if this would be our only use of the _at suffix (imho).

Another alternative I can think of is just making people open the fd and call fstat but I think that would be more controversial.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. I got rid of the _at suffix and renamed the old method to self_metadata.

@rust-log-analyzer

This comment has been minimized.

@rust-bors rust-bors Bot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 19, 2026
@rust-bors

This comment was marked as outdated.

@RalfJung

This comment was marked as resolved.

@RalfJung

This comment was marked as outdated.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Sep 19, 2026
Add `Dir::metadata_at` & `Dir::symlink_metadata_at`


try-job: test-*-msvc-1
@RalfJung

Copy link
Copy Markdown
Member Author

@bors try jobs=various,android

@rust-bors

rust-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

❌ Encountered an error while executing command

@RalfJung

Copy link
Copy Markdown
Member Author

@bors try jobs=various,android

@rust-bors

rust-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

❌ Encountered an error while executing command

@RalfJung

Copy link
Copy Markdown
Member Author

@bors try jobs=various,android

@rust-bors

rust-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

❌ Encountered an error while executing command

@RalfJung

Copy link
Copy Markdown
Member Author

@bors try cancel

@rust-bors

rust-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

❌ Encountered an error while executing command

@Kobzol

Kobzol commented Sep 29, 2026

Copy link
Copy Markdown
Member

@bors r-

@rust-bors

rust-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

❌ Encountered an error while executing command

@Kobzol

Kobzol commented Sep 29, 2026

Copy link
Copy Markdown
Member

@bors try jobs=various,android

rust-bors Bot pushed a commit that referenced this pull request Sep 29, 2026
Add `Dir` equivalents of `fs::metadata` & `fs::symlink_metadata`


try-job: *various*
try-job: *android*
@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: a624b40 (a624b4007ca55a0d20290262ae550d452880c639)
Base parent: 80a4f6c (80a4f6cdbdc30afa1f27c830386361d22b161286)

@RalfJung

Copy link
Copy Markdown
Member Author

@bors r=ChrisDenton

@rust-bors

rust-bors Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 5b172d9 has been approved by ChrisDenton

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 29, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 29, 2026
…enton

Add `Dir` equivalents of `fs::metadata` & `fs::symlink_metadata`

This adds methods to `Dir` that allow querying the metadata of files/directories relative to a `Dir`. Miri would [really like](rust-lang/miri#5327) to be able to do this so I figured I'd give it a shot. :)

The first commit refactors the Unix `DirEntry` methods a bit with `cfg_select` to avoid repeating the `cfg` condition.

Tracking issue: rust-lang#120426

try-jobs: test-x86_64-msvc-1
rust-bors Bot pushed a commit that referenced this pull request Sep 29, 2026
…uwer

Rollup of 3 pull requests

Successful merges:

 - #163024 (Add `Dir` equivalents of `fs::metadata` & `fs::symlink_metadata`)
 - #163279 (const and NonZero impl for clamp_magnitude())
 - #163361 (Specialize `advance_back_by` like the forward version)
rust-bors Bot pushed a commit that referenced this pull request Sep 29, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - #163024 (Add `Dir` equivalents of `fs::metadata` & `fs::symlink_metadata`)
 - #162839 (Bump min Emscripten version to 4.0, drop deprecated -sWASM_BIGINT for wasm32-unknown-emscripten)
 - #163370 (Fix incorrect typo suggestion for `struct field` shorthands)
 - #163471 (do not suggest capturing `'_` twice in `use<...>` for E0700)
@rust-bors
rust-bors Bot merged commit 78f7517 into rust-lang:main Sep 29, 2026
14 checks passed
rust-bors Bot pushed a commit that referenced this pull request Sep 29, 2026
Rollup merge of #163024 - RalfJung:dir-metadata-at, r=ChrisDenton

Add `Dir` equivalents of `fs::metadata` & `fs::symlink_metadata`

This adds methods to `Dir` that allow querying the metadata of files/directories relative to a `Dir`. Miri would [really like](rust-lang/miri#5327) to be able to do this so I figured I'd give it a shot. :)

The first commit refactors the Unix `DirEntry` methods a bit with `cfg_select` to avoid repeating the `cfg` condition.

Tracking issue: #120426

try-jobs: test-x86_64-msvc-1
@rustbot rustbot added this to the 1.101.0 milestone Sep 29, 2026
pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Sep 30, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - rust-lang/rust#163024 (Add `Dir` equivalents of `fs::metadata` & `fs::symlink_metadata`)
 - rust-lang/rust#162839 (Bump min Emscripten version to 4.0, drop deprecated -sWASM_BIGINT for wasm32-unknown-emscripten)
 - rust-lang/rust#163370 (Fix incorrect typo suggestion for `struct field` shorthands)
 - rust-lang/rust#163471 (do not suggest capturing `'_` twice in `use<...>` for E0700)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants