Skip to main content

Module writethrough

Module writethrough 

Source
Expand description

Non-atomic write-through path for symlink and reparse-point targets (FR-010).

moreutils sponge uses fopen(path, "w") which follows symbolic links and truncates the underlying file. POSIX rename(2) would replace the symlink itself with a fresh regular file — diverging from moreutils. We therefore detect symlink targets up the call chain (in crate::Sponge::run) and dispatch here, where we use OpenOptions with truncate(true) (or append(true) for -a mode) so the linked file is updated and the link itself stays in place.

§Atomic-safety scope

Per FR-006: the atomic-safety guarantee does NOT apply on this path. OpenOptions::truncate(true).open(...) zeroes the linked file BEFORE we write the buffer; a mid-write failure can leave the linked file partial. This matches moreutils behavior and is documented in the compatibility statement. Use crate::atomic::write_atomic for the regular-file path when you need the atomic-safety guarantee.

Functions§

requires_write_through
Decide whether target requires the write-through path. Returns true for POSIX symlinks (is_symlink()) and for any non-regular existing file (FIFOs, devices, Windows reparse points). Missing targets return false — the caller dispatches to the atomic-rename path, which creates them.
write_through
Write buffer to the target by following any symlink and truncating (or appending to) the linked file. Non-atomic — see module docs.