File truncation
File truncation changes a regular file to a specified byte length, discarding data beyond a shorter end or extending it when supported.
Length and position: Linux ftruncate operates through a writable descriptor without changing the file offset. An extended region reads as zero bytes; this does not imply physical allocation of every block. [1]
Mapped readers: POSIX.1-2017 specifies that shrinking a mapped file discards whole pages beyond the new end, and accessing those pages raises SIGBUS. This rule concerns whole pages, not the unused tail of a partial final page. [2]
Storage boundary: changing file size and synchronizing it are separate operations. File-size metadata is retrieval-critical for fdatasync. [3]
Relations
- related
- memory-mapped-file, fdatasync, open-file-description, sparse-file, msync
Advantages
- A descriptor selects the already-open file without repeating pathname lookup.
Limitations
- A successful size change does not safely resize every reader's mapping or provide secure erasure.
Citations
- Linux man-pages: truncate(2), regular-file length and offset
- POSIX.1-2017: ftruncate(), discarded mapped pages
- Linux man-pages: fsync(2), fdatasync and file-size metadata
Further reading
Sources & caveats
- The mapped-shrink rule is cited to POSIX.1-2017; no claim is made about arbitrary file-growth remapping.
- The companion test accesses a discarded whole page in an isolated child process.