Skip to content

dirfd file operations (2/4)#150679

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Qelxiros:dirfd-files
Jul 16, 2026
Merged

dirfd file operations (2/4)#150679
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Qelxiros:dirfd-files

Conversation

@Qelxiros

@Qelxiros Qelxiros commented Jan 4, 2026

Copy link
Copy Markdown
Contributor

View all comments

Previous PR: #146341
Reference: #139514
Tracking issue: #120426

@rustbot rustbot added O-windows Operating system: Windows 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 Jan 4, 2026
@rustbot

rustbot commented Jan 4, 2026

Copy link
Copy Markdown
Collaborator

r? @tgross35

rustbot has assigned @tgross35.
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

@Qelxiros

Qelxiros commented Jan 4, 2026

Copy link
Copy Markdown
Contributor Author

This is going to need a try run for windows at least. @tgross35 I'll let you decide which jobs make the most sense here.

@tgross35

tgross35 commented Jan 4, 2026

Copy link
Copy Markdown
Contributor

@bors2 try jobs=x86_64-msvc-*

Cc @the8472 for dirfd, and @ChrisDenton for Windows

@rust-bors

This comment has been minimized.

rust-bors Bot added a commit that referenced this pull request Jan 4, 2026
dirfd file operations (2/4)

try-job: x86_64-msvc-*
@rust-bors

rust-bors Bot commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 796e04e (796e04e9c306cfa50aa215b1e93ebdb1cbdc51d8, parent: e29fcf45e4ae686d77b490bf07320f0d3a2cf35f)

@rust-bors

This comment has been minimized.

@tgross35

Copy link
Copy Markdown
Contributor

I haven't had a chance to look here yet, sorry

@rustbot reroll

@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot

This comment has been minimized.

@Qelxiros

Qelxiros commented Apr 1, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot reroll

@Mark-Simulacrum Mark-Simulacrum left a comment

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.

The Windows code looks like it could use a second set of eyes from someone with Windows knowledge, unless we manage to remove most of the new code through reusing the existing rename logic.

View changes since this review

opts.access_mode(c::DELETE);
opts.custom_flags(c::FILE_FLAG_OPEN_REPARSE_POINT | c::FILE_FLAG_BACKUP_SEMANTICS);
let handle = self.open_file_native(from, &opts, dir)?;
// Calculate the layout of the `FILE_RENAME_INFORMATION` we pass to `NtSetInformationFile`

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.

A bunch of the content here looks similar to

let err = api::get_last_error();
-- could we reuse some of that? Is there an opportunity to use MoveFileExW for this path at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

MoveFileExW operates exclusively on filenames. Dir::rename is intended to be TOCTOU-safe, so it needs to use directory Handles. I've factored out the NtSetInformationFile section of both functions into nt_rename. The implementations are a little different from one another, so I'm not 100% sure I got the function signature right. I'll wait for the CI to run and reevaluate.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 4, 2026
@rustbot

This comment has been minimized.

@rustbot rustbot added the O-windows Operating system: Windows label Apr 30, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 7, 2026
Comment thread library/std/src/sys/fs/windows/dir.rs Outdated
}

pub fn remove_file(&self, path: &Path) -> io::Result<()> {
run_path_with_cstr(path, &|path| self.remove_c(path, false))

@Mark-Simulacrum Mark-Simulacrum Jun 7, 2026

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.

Should this check somewhere that the passed path is relative? Otherwise we're not actually getting the promised semantics: "If path is absolute, then dirfd is ignored." (https://www.man7.org/linux/man-pages/man2/unlink.2.html)

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.

That seems like a question for all dirfd-relative operations that take a path: should they require the path to be relative, or should they allow absolute paths and do the obvious thing for them? IMO it makes sense to allow absolute paths. That basically lets you use dirfd as a "local working directory" without the downsides of set_current_dir.

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.

The argument against is that people would like to use dirfd in security relevant contexts. You can build a less secure variant on top of dirfd but not really the other way around (well technically you can but defaulting to the less secure option has historically been a footgun).

But in any case, I think this is a question for libs-api.

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.

It's unstable, so you can put it on the open questions in the tracking issue? I don't think this needs to be settled immediately.

On linux there's openat2 which adds RESOLVE_BENEATH and RESOLVE_IN_ROOT flags which give you certain security properties, depending on which you want.

@RalfJung RalfJung Jun 10, 2026

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.

The tracking issue explicitly says that this is NOT using O_BENEATH. We also have a fallback implementation that just stores the dir name as a string. So anything security critical seems to be already ruled out.

I have added this to #120426.

@rustbot rustbot 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 Jun 7, 2026
@rustbot

rustbot commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Comment thread library/std/src/fs/tests.rs Outdated
@RalfJung

Copy link
Copy Markdown
Member

@Qelxiros we haven't heard from you in a while here -- are you still interested in working on this?
If not that's okay, it would just be good for us to know this so someone else can take over. :)

@rustbot

This comment has been minimized.

@Qelxiros

Copy link
Copy Markdown
Contributor Author

I am still interested in working on this; I had changes for the previous review in my local repo, but I forgot to push them.

@rustbot

rustbot commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@RalfJung

Copy link
Copy Markdown
Member

Remember to @rustbot ready when this is ready for review again, i.e., after you incorporated all feedback.

@Qelxiros

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 15, 2026
@Mark-Simulacrum

Copy link
Copy Markdown
Member

@bors squash msg="add file operations to std dirfd"

@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

🔨 3 commits were squashed into b6849b3.

@Mark-Simulacrum

Copy link
Copy Markdown
Member

@bors r+ rollup=iffy

@rust-bors

rust-bors Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

📌 Commit b6849b3 has been approved by Mark-Simulacrum

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-review Status: Awaiting review from the assignee but also interested parties. labels Jul 16, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 16, 2026
…acrum

dirfd file operations (2/4)

Previous PR: rust-lang#146341
Reference: rust-lang#139514
Tracking issue: rust-lang#120426
rust-bors Bot pushed a commit that referenced this pull request Jul 16, 2026
…uwer

Rollup of 18 pull requests

Successful merges:

 - #150679 (dirfd file operations (2/4))
 - #158758 (Rename `lazy_type_alias` feature gate to `checked_type_aliases` )
 - #159177 (Explicitly materialize debuginfo tests for all debuggers)
 - #159392 (Update Rust crate rand to v0.9.3 [SECURITY])
 - #159393 (Update Rust crate rkyv to v0.8.16 [SECURITY])
 - #159394 (Update Rust crate tar to v0.4.46 [SECURITY])
 - #159395 (Update Rust crate tracing-subscriber [SECURITY])
 - #159193 (Port compiletest's CLI to clap)
 - #158992 (Shorten types more when only interesting part is lifetimes)
 - #159226 (Track extra_lifetime_params_map per-owner)
 - #159273 (tidy: document u64 limit for numeric sort keys)
 - #159322 (Fix safety doc in intrinsics::simd)
 - #159329 (Fix the stale metrics directory warning)
 - #159357 (Add regression tests for two fixed issues)
 - #159360 (Remove FIXMEs referencing #44232)
 - #159375 (make a couple codegen-llvm tests compatible with 2021 edition)
 - #159381 (emit `(lldb)` prefix to make commands more distinct)
 - #159396 (Update actions/checkout action to v7)
@rust-bors
rust-bors Bot merged commit 9133612 into rust-lang:main Jul 16, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-windows Operating system: Windows 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.

8 participants