This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author pitrou
Recipients karaken12, pitrou, r.david.murray, tarek
Date 2010-10-04.11:57:42
SpamBayes Score 2.823695e-06
Marked as misclassified No
Message-id <1286193464.95.0.149547410668.issue10016@psf.upfronthosting.co.za>
In-reply-to
Content
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).
History
Date User Action Args
2010-10-04 11:57:45pitrousetrecipients: + pitrou, tarek, r.david.murray, karaken12
2010-10-04 11:57:44pitrousetmessageid: <1286193464.95.0.149547410668.issue10016@psf.upfronthosting.co.za>
2010-10-04 11:57:43pitroulinkissue10016 messages
2010-10-04 11:57:42pitroucreate