How to delete content in sparse files?

268 Views Asked by At

I'm writing a program, which write some contents in a sparse file. Say, I create the file by

fd = open(filepath, O_RDWR | O_CREAT | O_EXCL, 0644);
assert(fd != -1);
int s = ftruncate(fd, len);

len is very large, but the actual content may only takes several kbs.

Then I do memory mapped IO on this file. How do I ensure contents are deleted from this file, so that temporary stuff is not stored, and the actual storage is still "sparse"?

Thanks for any suggestions.

1

There are 1 best solutions below

0
On

This question is beyond the scope of the C language. The C Standard does not have a notion of sparse files, nor even a notion of POSIX file handles.

Answers are OS specific. For linux, you may find a solution here: What are functions to manipulate sparse files under Linux? and Detailed sparse file information on Linux.