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

后端 未结 7 1497
谎友^
谎友^ 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:41

    If you seek (fseek, ftruncate, ...) to past the end, the file size will be increased without allocating blocks until you write to the holes. But there's no way to create a magic file that automatically converts blocks of zeroes to holes. You have to do it yourself.

    This may be helpful to look at (the OpenBSD cp command inserts holes instead of writing zeroes). patch

提交回复
热议问题