Search code examples
clinuxfilesystemslarge-filesfallocate

How to quickly create large files in C in Linux?


Following this question: How to quickly create large files in C?

I remember 4-5 years ago, I was able to use fallocate shell utility in Linux to create files without holes/gaps.

Disk filesystem was either ext3, ext4 or xfs.

At that time, fallocate shell utility created files of 8 GB in less one second. I do not remember if file was full of zeroes or random.

I try to replicate this nowadays, but it seems to create file with holes/gaps.

I am wondering how this was done in C language?

  • Some kernel call that is Linux only?
  • Some kernel call with error in specification that was later fixed?
  • Some kernel call that is no longer available?

Solution

  • The fallocate system call on Linux has an option to zero the space.

    #define _GNU_SOURCE
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int fd = open("testfile", O_RDWR | O_TRUNC | O_CREAT, 0755);
        off_t size = 1024 * 1024 * 1024;
    
        if (fd == -1) {
            perror("open");
            exit(1);
        }
    
        if (fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, size) == -1) {
            perror("fallocate");
            exit(1);
        }
    }
    

    Note that FALLOC_FL_ZERO_RANGE may not be supported by all filesystems. ext4 supports it.

    Otherwise, you could write zeros yourself if you are looking for a more portable solution (which isn't that efficient, of course).