Use posix_fallocate(3) in your code. The difference with ftruncate(2) is that (on file systems supporting it, e.g. Ext4) disk space is indeed reserved by posix_fallocate
but ftruncate
extends the file by adding holes (and without reserving disk space).
E.g. if you disk is 100Gbytes, and you posix_fallocate
a 5Gb file, you'll see (with df
) that available space has reduced (since before the call); if you did only an ftruncate
you won't see available space reducing.
See also this answer to a related question.