{
  "slug": "file-truncation",
  "term": "File truncation",
  "aliases": [
    "ftruncate",
    "ftruncate()",
    "truncate()"
  ],
  "definition": "File truncation changes a regular file to a specified byte length, discarding data beyond a shorter end or extending it when supported.",
  "note": "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. [truncate-api]\n\nMapped 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. [posix-truncate]\n\nStorage boundary: changing file size and synchronizing it are separate operations. File-size metadata is retrieval-critical for fdatasync. [fsync-api]",
  "status": "shipping",
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "updated": "2026-10-17",
  "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."
  ],
  "related": [
    "memory-mapped-file",
    "fdatasync",
    "open-file-description",
    "sparse-file",
    "msync"
  ],
  "citations": [
    {
      "id": "truncate-api",
      "title": "Linux man-pages: truncate(2), regular-file length and offset",
      "url": "https://man7.org/linux/man-pages/man2/truncate.2.html",
      "accessed": "2026-10-17"
    },
    {
      "id": "posix-truncate",
      "title": "POSIX.1-2017: ftruncate(), discarded mapped pages",
      "url": "https://man7.org/linux/man-pages/man3/ftruncate.3p.html",
      "accessed": "2026-10-17"
    },
    {
      "id": "fsync-api",
      "title": "Linux man-pages: fsync(2), fdatasync and file-size metadata",
      "url": "https://man7.org/linux/man-pages/man2/fsync.2.html",
      "accessed": "2026-10-17"
    },
    {
      "id": "hesela-mmap",
      "title": "Hesela: Memory-mapped files: visibility, size and synchronization",
      "url": "https://hesela.com/analyses/mmap-visibility-size-durability/",
      "accessed": "2026-10-17"
    }
  ],
  "provenance": [
    "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."
  ]
}
