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

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

    hdiutil can handle sparse images and files but unfortunately the framework it links against is private.

    You could try defining external symbols as defined by the DiskImages framework below but this is most likely not acceptable for production code, plus since the framework is private you'd have to reverse engineer its use cases.

    cristi:~ diciu$ otool -L /usr/bin/hdiutil

    /usr/bin/hdiutil: /System/Library/PrivateFrameworks/DiskImages.framework/Versions/A/DiskImages (compatibility version 1.0.8, current version 194.0.0) [..]

    cristi:~ diciu$ nm /System/Library/PrivateFrameworks/DiskImages.framework/Versions/A/DiskImages | awk -F' ' '{print $3}' | c++filt | grep -i sparse

    [..]

    CSparseFile::sector2Band(long long)

    CSparseFile::addIndexNode()

    CSparseFile::readIndexNode(long long, SparseFileIndexNode*)

    CSparseFile::readHeaderNode(CBackingStore*, SparseFileHeaderNode*, unsigned long)

    [... cut for brevity]

    Later Edit

    You could use hdiutil as an external process and have it create an sparse disk image for you. From the C process you would then create a file in the (mounted) sparse disk image.

提交回复
热议问题