Skip to content

Commit 7d17a26

Browse files
committed
added a problematic example
1 parent e7dc177 commit 7d17a26

File tree

1 file changed

+25
-3
lines changed
  • library/std/src/os/unix

1 file changed

+25
-3
lines changed

library/std/src/os/unix/fs.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,31 @@ pub trait FileExt {
150150
/// short write.
151151
///
152152
/// # Bug
153-
/// On some systems, due to a [bug] with [`pwrite64`] (the underlying
154-
/// syscall), files opened with the `O_APPEND` flag fail to respect the
155-
/// offset parameter, always appending to the end of the file instead.
153+
/// On some systems, `write_at` utilises [`pwrite64`] to write to files.
154+
/// However, this syscall has a [bug] where files opened with the `O_APPEND`
155+
/// flag fail to respect the offset parameter, always appending to the end
156+
/// of the file instead.
157+
///
158+
/// It is possible to inadvertantly set this flag, like in the example below.
159+
/// Therefore, it is important to be vigilant while changing options to mitigate
160+
/// unexpected behaviour.
161+
///
162+
/// ```no_run
163+
/// use std::fs::File;
164+
/// use std::io;
165+
/// use std::os::unix::prelude::FileExt;
166+
///
167+
/// fn main() -> io::Result<()> {
168+
/// // Open a file with the append option (sets the `O_APPEND` flag)
169+
/// let file = File::options().append(true).open("foo.txt")?;
170+
///
171+
/// // We attempt to write at offset 10; instead appended to EOF
172+
/// file.write_at(b"sushi", 10)?;
173+
///
174+
/// // foo.txt is 5 bytes long instead of 15
175+
/// Ok(())
176+
/// }
177+
/// ```
156178
///
157179
/// [`File::write`]: fs::File::write
158180
/// [`pwrite64`]: https://man7.org/linux/man-pages/man2/pwrite.2.html

0 commit comments

Comments
 (0)