Ensuring sufficient disk space

Linux provides the posix_fallocate(3) API; its job is to guarantee that sufficient disk space is available for a given range specific to a given file. What that actually means is that whenever the app writes to that file within that range, the write is guaranteed not to fail due to lack of disk space (if it does fail, errno will be set to ENOSPC; that won't happen). It's signature is as follows:

#include <fcntl.h>int posix_fallocate(int fd, off_t offset, off_t len);

Here are some quick points to note regarding this API:

  • The file is the one referred to by the descriptor fd.
  • The range is from offset for len bytes; in effect, this is the disk space that will be reserved for the file.
  • If the current file size is ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.