Skip to content

Commit d8745f9

Browse files
committed
Update usage note on OpenOptions::append()
Avoid implying that concatenating data before passing it to `write()` (with or without `BufWriter`) ensures atomicity.
1 parent ad18fe0 commit d8745f9

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

library/std/src/fs.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -980,15 +980,21 @@ impl OpenOptions {
980980
/// Note that setting `.write(true).append(true)` has the same effect as
981981
/// setting only `.append(true)`.
982982
///
983-
/// For most filesystems, the operating system guarantees that all writes are
984-
/// atomic: no writes get mangled because another process writes at the same
985-
/// time.
986-
///
987-
/// One maybe obvious note when using append-mode: make sure that all data
988-
/// that belongs together is written to the file in one operation. This
989-
/// can be done by concatenating strings before passing them to [`write()`],
990-
/// or using a buffered writer (with a buffer of adequate size),
991-
/// and calling [`flush()`] when the message is complete.
983+
/// Append mode guarantees that writes will be positioned at the current end of file,
984+
/// even when there are other processes or threads appending to the same file. This is
985+
/// unlike <code>[seek]\([SeekFrom]::[End]\(0))</code> followed by `write()`, which
986+
/// has a race between seeking and writing during which another writer can write, with
987+
/// our `write()` overwriting their data.
988+
///
989+
/// Keep in mind that this does not necessarily guarantee that data appended by
990+
/// different processes or threads does not interleave. The amount of data accepted a
991+
/// single `write()` call depends on the operating system and file system. A
992+
/// successful `write()` is allowed to write only part of the given data, so even if
993+
/// you're careful to provide the whole message in a single call to `write()`, there
994+
/// is no guarantee that it will be written out in full. If you rely on the filesystem
995+
/// accepting the message in a single write, make sure that all data that belongs
996+
/// together is written in one operation. This can be done by concatenating strings
997+
/// before passing them to [`write()`].
992998
///
993999
/// If a file is opened with both read and append access, beware that after
9941000
/// opening, and after every write, the position for reading may be set at the
@@ -1003,6 +1009,9 @@ impl OpenOptions {
10031009
/// [`write()`]: Write::write "io::Write::write"
10041010
/// [`flush()`]: Write::flush "io::Write::flush"
10051011
/// [stream_position]: Seek::stream_position "io::Seek::stream_position"
1012+
/// [seek]: Seek::seek "io::Seek::seek"
1013+
/// [Current]: SeekFrom::Current "io::SeekFrom::Current"
1014+
/// [End]: SeekFrom::End "io::SeekFrom::End"
10061015
///
10071016
/// # Examples
10081017
///

0 commit comments

Comments
 (0)