1

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.

AetX
  • 193
  • 6
  • I'm not sure I understand the question, but perhaps you want to map with `MAP_PRIVATE`? This ensures that your changes are only made in memory, and never written back to the file itself, which stays unmodified. – Nate Eldredge Jun 01 '21 at 01:15

1 Answers1

-2

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.

chqrlie
  • 131,814
  • 10
  • 121
  • 189