Module sys/fallocate
File space pre-allocation (fallocate) — the raw syscall boundary.
One wrapper, three genuinely different implementations underneath. No
public std module reserves space yet; tests/sys/fallocate.test.yo is
its only caller in this tree.
Stability
unstable — the mode word is a Linux flag set that only Linux can honour.
macOS reaches fcntl(F_PREALLOCATE) and Windows reaches
SetFileInformationByHandle, and neither has an equivalent of
PUNCH_HOLE or ZERO_RANGE, so those bits are silently ignored rather
than rejected. Freezing needs that resolved one way or the other: either
narrow the API to the one mode every platform can implement (reserve, with
or without growing the file) and give hole-punching its own Linux-only
entry point, or report -ENOTSUP for a mode the platform cannot serve
instead of returning success.
Functions
Reserve disk space for [offset, offset + length) so that later writes
there cannot fail with -ENOSPC. Returns 0 on success, a negative errno
on failure.
The implementations are not the same call, and the difference is observable:
- Linux —
fallocate(2), withmodepassed through unchanged. - macOS —
fcntl(F_PREALLOCATE), first asking for a contiguous run (F_ALLOCATECONTIG) and retrying withF_ALLOCATEALL; if the filesystem refuses outright (ENOTSUP/EOPNOTSUPP/ENOSYS/EINVAL) it degrades toftruncate, which reserves nothing — so success here is weaker than on Linux. Rejects a negativeoffsetorlengthwith-EINVAL. - Windows —
SetFileInformationByHandle(FileAllocationInfo)plus_chsize_swhen the size must grow. - wasm —
ftruncateonly, and only formode == 0; any other mode returns 0 without doing anything.
Parameters
| Name | Type | Notes |
|---|---|---|
fd | i32 | |
mode | i32 | |
offset | i64 | |
length | i64 |
Returns: i32
Constants
FALLOC_FL_KEEP_SIZE from Linux <linux/falloc.h> — reserve the blocks
but leave the file's reported length alone. This is the ONE mode bit every
platform's implementation inspects: with it clear, macOS and Windows also
grow the file to offset + length.
Value: 1
FALLOC_FL_PUNCH_HOLE — deallocate the range, leaving a sparse hole that
reads as zeroes. Linux-only, and Linux requires it OR-ed with
FALLOC_FL_KEEP_SIZE. Ignored (not rejected) on macOS, Windows and wasm.
Value: 2
FALLOC_FL_ZERO_RANGE — zero the range, converting it to unwritten
extents where the filesystem can. Linux-only; ignored elsewhere.
Value: 16