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]
Sealed size: F_SEAL_SHRINK and F_SEAL_GROW independently restrict size changes on supporting Linux objects; F_SEAL_WRITE alone does not lock file length. [5]
Relations
- related
- File size, Memory-mapped file, fdatasync, Open file description, Sparse file, msync, Linux file sealing, Linux memfd_create
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
- Linux man-pages: F_GET_SEALS / F_ADD_SEALS
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.