-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
No cross-platform way to close an fs::File and check for errors #32255
Comments
|
Even when invoking |
I think should have a policy to provide these methods whenever fallible close / fallible drop can occur. Harder question is maybe if there should be an I/O trait for close. |
Note: This issue was linked here. |
|
I have a practical need for this |
@dpc Some platforms support unbuffered writes, which can be faster than buffered writes + flushing. On Windows this can be done with the |
@dpc if we were to add this to the standard library, this is the implementation that I would prefer. Which is also to say that this is not impossible to do today, it's just not as ergonomic as it could be. (you can also imagine a similar implementation for |
On #42619 I was doing some investigation of what the Linux kernel does when closing files depending on the file system.
Many moons ago in GNOME we were having a related discussion, around fsync() and saving files reliably. There are various ways in which user-facing programs have tried to save files:
(3) is the preferred way for File/Save for "precious" data: if the user has been typing for hours, you don't want to:
(Then, there is debate on whether one needs to fsync() the directory after the rename()...) Conclusion: maybe we could have a recommendation to File.sync_all() for important data. We could have an extra crate for doing the rename() dance for user-initiated saves (we had something like that in GNOME at some point in libgnome-office; there was clearly a need for it). I don't know if we should expose |
That not sufficient. You need to write, fsync, close, rename, fsync dir to ensure the rename went through. The rust stdlib currently provides no way to fsync a directory. |
There are several issues that often get mixed up in the discussion on proper closing of files:
The filesystem operations necessary for case 2 cause significant performance hits, and furthermore they don't necessarily guarantee data persistency; that also depends on the storage hardware. Case 3, on Unix, requires creation of temporary files, which may be left in the filesystem in case the replacement sequence is aborted. I don't think |
Yes; this is why I suggested having a different crate for high-level, application-type atomic saves with rename(), creation of backup files, and everything. |
I don't believe we are going to reach consensus here on how to implement this, so I would prefer to see this addressed in an RFC. The RFC will need to summarize the tradeoffs in rust-lang/api-guidelines#61 about how checked teardown should work in general, identify any important considerations that are specific to closing files, address any cross-platform differences in how closing files works, and make a concrete recommendation for how the standard library should allow checking for errors when closing a |
There should be an:
fn close(self) -> io::Result<()>
that does the same as Drop but does not swallow any errors. Currently the only way to do this is withinto_raw_fd
andlibc::close
, which is both unergonomic and non-cross-platform.The text was updated successfully, but these errors were encountered: