I have a sparse file, in which only some blocks are allocated:
~% du -h --apparent-size example
100K example
~% du -h example
52K example
I would like to know which blocks of the file are actually allocated. Is there a system call or kernel interface that could be used to get a list of either the allocations, or the holes of file?
Simply checking for a long enough string of zeros (the approach used by GNU cp, rsync, etc) does not work correctly:
~% cp example example1
~% du -h example1
32K example1
It detected other sequences of zeros that were actually allocated.
Best Answer
There is a similar question on SO. The currently accepted answer by @ephemient suggests using an
ioctl
calledfiemap
which is documented inlinux/Documentation/filesystems/fiemap.txt
. Quoting from that file:Sounds like this is the kind of information you're looking for. Support by filesystems is again optional:
Support for the
SEEK_DATA
andSEEK_HOLE
arguments tolseek
you mentioned from Solaris was added in Linux 3.1 according to the man page, so you might use that as well. Thefiemap ioctl
appears to be older, so it might be more portable across different Linux versions for now, whereaslseek
might be more portable across operating systems if Solaris has the same.