Antoine Pitrou <pit...@free.fr> added the comment:

Ok, after experimenting, I now understand what the truncate() call is for.

However, your heuristic for detecting sparse files is wrong. The unit for 
st_blocks is undefined as per the POSIX standard, although it gives 
recommendations:

“The unit for the st_blocks member of the stat structure is not defined within 
IEEE Std 1003.1-2001. In some implementations it is 512 bytes. It may differ on 
a file system basis. There is no correlation between values of the st_blocks 
and st_blksize, and the f_bsize (from <sys/statvfs.h>) structure members.

Traditionally, some implementations defined the multiplier for st_blocks in 
<sys/param.h> as the symbol DEV_BSIZE.”

(http://www.opengroup.org/onlinepubs/000095399/basedefs/sys/stat.h.html)

Under Linux, 512 turns out to be the right multiplier (and not st_blksize):

>>> f = open("foo", "wb")
>>> f.write(b"x" * 4096)
4096
>>> f.truncate(16384)
16384
>>> f.close()
>>> st = os.stat("foo")
>>> st.st_size
16384
>>> st.st_blocks
8
>>> st.st_blocks * st.st_blksize
32768
>>> st.st_blocks * 512
4096

Also, GNU `cp` uses S_BLKSIZE rather than DEV_BSIZE when trying to detect the 
st_blocks unit size (both are 512 under Linux).

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue10016>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to