How do I create a sparse file programmatically, in C, on Mac OS X?

后端 未结 7 1449
谎友^
谎友^ 2020-12-19 03:00

I\'d like to create a sparse file such that all-zero blocks don\'t take up actual disk space until I write data to them. Is it possible?

7条回答
  •  囚心锁ツ
    2020-12-19 03:44

    There seems to be some confusion as to whether the default Mac OS X filesystem (HFS+) supports holes in files. The following program demonstrates that this is not the case.

    #include 
    #include 
    #include 
    #include 
    
    void create_file_with_hole(void)
    {
        int fd = open("file.hole", O_WRONLY|O_TRUNC|O_CREAT, 0600);
        write(fd, "Hello", 5);
        lseek(fd, 99988, SEEK_CUR); // Make a hole
        write(fd, "Goodbye", 7);
        close(fd);
    }
    
    void create_file_without_hole(void)
    {
        int fd = open("file.nohole", O_WRONLY|O_TRUNC|O_CREAT, 0600);
        write(fd, "Hello", 5);
        char buf[99988];
        memset(buf, 'a', 99988);
        write(fd, buf, 99988); // Write lots of bytes
        write(fd, "Goodbye", 7);
        close(fd);
    }
    
    int main()
    {
        create_file_with_hole();
        create_file_without_hole();
        return 0;
    }
    

    The program creates two files, each 100,000 bytes in length, one of which has a hole of 99,988 bytes.

    On Mac OS X 10.5 on an HFS+ partition, both files take up the same number of disk blocks (200):

    $ ls -ls
    total 400
    200 -rw-------  1 user  staff  100000 Oct 10 13:48 file.hole
    200 -rw-------  1 user  staff  100000 Oct 10 13:48 file.nohole

    Whereas on CentOS 5, the file without holes consumes 88 more disk blocks than the other:

    $ ls -ls
    total 136
     24 -rw-------  1 user   nobody 100000 Oct 10 13:46 file.hole
    112 -rw-------  1 user   nobody 100000 Oct 10 13:46 file.nohole

提交回复
热议问题