I was debugging a fuse filesystem that was reporting wrong sizes for du
. It turned out that it was putting st_size / st_blksize
[*] into st_blocks
of the stat
structure. The Linux manual page for stat(2)
says:
struct stat {
…
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
…
};
What is st_blksize
for if st_blocks
is in 512B blocks anyway?
[*] which looks wrong anyway, as integer division doesn't account for the fractional part…
st_blocks = (st_size + 511) / 512
-- maybe needlessly obfuscating it through an obtuse macro likeROUNDING_UP_DIV_BY_SECTORS()
so the 1st midwit that comes along is impressed by the professional looking code instead of start carping about magic numbers and things he doesn't understand;-)In [4]: os.stat('/vmlinuz').st_blksize Out[4]: 4096
MAGIC!