diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-07-22 11:48:54 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-22 11:48:54 +0200 |
| commit | 746d507c726dbbae0b3067925215c0aa57a93975 (patch) | |
| tree | f6ea2cadfa6090ea375783778a5ea9a71ff87fed | |
| parent | 0877d11e8dee12839804909171e61ad77ae0656a (diff) | |
| parent | 7d17a263d1a6e46700f65d378a9ae7280a2b3371 (diff) | |
| download | rust-746d507c726dbbae0b3067925215c0aa57a93975.tar.gz rust-746d507c726dbbae0b3067925215c0aa57a93975.zip | |
Rollup merge of #113876 - darklyspaced:master, r=cuviper
fix docs & example for `std::os::unix::prelude::FileExt::write_at` Changelog: * used `File::create` instead of `File::read` to get a writeable file * explicity mentioned the bug with `pwrite64` in docs Unfortunately, I don't think that there is really much we can do about this since the feature has already been stabilised. We could potentially add a clippy lint warning people on Linux that using `write_at` with the `O_APPEND` flag does not exhibit the behaviour that they would have assumed. fixes #113627
| -rw-r--r-- | library/std/src/os/unix/fs.rs | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/library/std/src/os/unix/fs.rs b/library/std/src/os/unix/fs.rs index 7464ac6069c..88326aaf295 100644 --- a/library/std/src/os/unix/fs.rs +++ b/library/std/src/os/unix/fs.rs @@ -149,7 +149,36 @@ pub trait FileExt { /// Note that similar to [`File::write`], it is not an error to return a /// short write. /// + /// # Bug + /// On some systems, `write_at` utilises [`pwrite64`] to write to files. + /// However, this syscall has a [bug] where files opened with the `O_APPEND` + /// flag fail to respect the offset parameter, always appending to the end + /// of the file instead. + /// + /// It is possible to inadvertantly set this flag, like in the example below. + /// Therefore, it is important to be vigilant while changing options to mitigate + /// unexpected behaviour. + /// + /// ```no_run + /// use std::fs::File; + /// use std::io; + /// use std::os::unix::prelude::FileExt; + /// + /// fn main() -> io::Result<()> { + /// // Open a file with the append option (sets the `O_APPEND` flag) + /// let file = File::options().append(true).open("foo.txt")?; + /// + /// // We attempt to write at offset 10; instead appended to EOF + /// file.write_at(b"sushi", 10)?; + /// + /// // foo.txt is 5 bytes long instead of 15 + /// Ok(()) + /// } + /// ``` + /// /// [`File::write`]: fs::File::write + /// [`pwrite64`]: https://man7.org/linux/man-pages/man2/pwrite.2.html + /// [bug]: https://man7.org/linux/man-pages/man2/pwrite.2.html#BUGS /// /// # Examples /// @@ -159,7 +188,7 @@ pub trait FileExt { /// use std::os::unix::prelude::FileExt; /// /// fn main() -> io::Result<()> { - /// let file = File::open("foo.txt")?; + /// let file = File::create("foo.txt")?; /// /// // We now write at the offset 10. /// file.write_at(b"sushi", 10)?; |
