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.
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.