file-truncation · shipping · also ftruncate, ftruncate(), truncate()

File truncation

File truncation changes a regular file to a specified byte length, discarding data beyond a shorter end or extending it when supported.

Related Observatory article: Read on hesela.com

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

  1. Linux man-pages: truncate(2), regular-file length and offset (accessed 2026-10-17)
  2. POSIX.1-2017: ftruncate(), discarded mapped pages (accessed 2026-10-17)
  3. Linux man-pages: fsync(2), fdatasync and file-size metadata (accessed 2026-10-17)
  4. Linux man-pages: F_GET_SEALS / F_ADD_SEALS (accessed 2026-10-17)

Further reading

  1. Hesela: Memory-mapped files: visibility, size and synchronization (accessed 2026-10-17)
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.

JSON: /data/terms/file-truncation.json

Published by Hesela team · Updated

Report a correction · Editorial standards · Reuse guidance

Cite this page
Hesela team. File truncation. Hesela. 2026-11-15. https://hesela.dev/terms/file-truncation/