Skip to content

Conversation

@tbu-
Copy link
Contributor

@tbu- tbu- commented Dec 10, 2025

Adds a (private to std) File::close method and uses it in std::fs::write and std::fs::copy.

This is a lighter version of rust-lang/libs-team#705 that can be added even if that ACP isn't accepted, as this is purely a quality of implementation thing that could be reverted at any time. External libraries and applications can use external crates like https://crates.io/crates/close-err to achieve the same with the files they own.

CC rust-lang/libs-team#705.

@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 Dec 10, 2025
@rustbot
Copy link
Collaborator

rustbot commented Dec 10, 2025

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

@tbu- tbu- force-pushed the pr_private_file_close branch from ab5eab8 to 38e2c5b Compare December 10, 2025 06:40
@rustbot
Copy link
Collaborator

rustbot commented Dec 10, 2025

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.


io::copy(&mut reader, &mut writer)
let ret = io::copy(&mut reader, &mut writer)?;
writer.close()?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Even though this can't fail, I copied it for symmetry with the other implementations.

@rust-log-analyzer

This comment has been minimized.

@tbu- tbu- force-pushed the pr_private_file_close branch from 38e2c5b to cac4eb5 Compare December 10, 2025 08:59
@rust-log-analyzer

This comment has been minimized.

Adds a (private to `std`) `File::close` method and uses it in
`std::fs::write` and `std::fs::copy`.

This is a lighter version of
rust-lang/libs-team#705 that can be added even
if that ACP isn't accepted, as this is purely a quality of
implementation thing that could be reverted at any time. External
libraries and applications can use external crates like
https://crates.io/crates/close-err to achieve the same with the files
they own.

CC rust-lang/libs-team#705.
@tbu- tbu- force-pushed the pr_private_file_close branch from cac4eb5 to f7e0a04 Compare December 10, 2025 11:00
@the8472
Copy link
Member

the8472 commented Dec 10, 2025

std::fs::{copy, write} are convenience methods, that do nothing about durability. They'll also happily truncate existing files and then leaving you stranded in the middle of the operation if something goes wrong. They don't do the rename dance, they don't use tempfiles, they don't fsync. Basically, if one is using them then one is signalling not caring much.

So I don't see why these practically least-effort methods are the ones that would warrant extra care.

Such a PR might be more appropriate to atomic write crates... except that they already do fsync, which subsumes this.

@tbu-
Copy link
Contributor Author

tbu- commented Dec 10, 2025

We're just returning the error from close(2) that we're calling anyway. I'm just changing the code from ignoring the error to returning it. Why is that such a problem? The OS is telling us something, and you'd prefer to throw the error away over returning it to the user?

@the8472
Copy link
Member

the8472 commented Dec 10, 2025

Well, I both do not like the precedent, and I also think that if anything really needs to do this then these are the methods where it's the least necessary, which makes the first point worse "look, even these simple, least-defensive std methods do it!"

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-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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants