Skip to content

Commit f50d327

Browse files
committed
Correct usage note on OpenOptions::append()
1 parent 0809f78 commit f50d327

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

library/std/src/fs.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -971,15 +971,18 @@ impl OpenOptions {
971971
/// Note that setting `.write(true).append(true)` has the same effect as
972972
/// setting only `.append(true)`.
973973
///
974-
/// For most filesystems, the operating system guarantees that all writes are
975-
/// atomic: no writes get mangled because another process writes at the same
976-
/// time.
977-
///
978-
/// One maybe obvious note when using append-mode: make sure that all data
979-
/// that belongs together is written to the file in one operation. This
980-
/// can be done by concatenating strings before passing them to [`write()`],
981-
/// or using a buffered writer (with a buffer of adequate size),
982-
/// and calling [`flush()`] when the message is complete.
974+
/// Append mode guarantees that writes will be positioned at the current end of file,
975+
/// even when there are other processes or threads appending to the same file. This is
976+
/// unlike <code>[seek]\([SeekFrom]::[End]\(0))</code>` followed by `write()`, which
977+
/// has a race between seeking and writing during which another writer can write, with
978+
/// our `write()` overwriting their data.
979+
///
980+
/// Keep in mind that atomicity of `write()` in append mode is less useful than it
981+
/// appears at first. A successful `write()` is allowed to write only part of the
982+
/// given data, so even if you're careful to provide the whole message in a single
983+
/// call to `write()`, there is no guarantee that it will written out in full. Unless
984+
/// the data to append consists of a single byte, you can't append atomically without
985+
/// external locking.
983986
///
984987
/// If a file is opened with both read and append access, beware that after
985988
/// opening, and after every write, the position for reading may be set at the
@@ -995,6 +998,7 @@ impl OpenOptions {
995998
/// [`flush()`]: Write::flush "io::Write::flush"
996999
/// [seek]: Seek::seek "io::Seek::seek"
9971000
/// [Current]: SeekFrom::Current "io::SeekFrom::Current"
1001+
/// [End]: SeekFrom::End "io::SeekFrom::End"
9981002
///
9991003
/// # Examples
10001004
///

0 commit comments

Comments
 (0)