From: Daniel Berger Date: 2011-01-07T05:44:18+09:00 Subject: Re: Getting the actual size of a sparse file On Jan 5, 12:39 pm, Johan Holmberg wrote: > On Wed, Jan 5, 2011 at 5:37 PM, Daniel Berger wrote: > > Hi, > > > How do you get the true size of a sparse file? Using /var/log/lastlog > > on Ubuntu as an example I see this with "ls -lh" > > > 287K lastlog > > > With "ls -sh" I see this: > > > 40K lastlog > > > A File.stat call reveals this: > > > # >  dev=0x801, > >  ino=5249695, > >  mode=0100664 (file rw-rw-r--), > >  nlink=1, > >  uid=0 (root), > >  gid=43 (utmp), > >  rdev=0x0 (0, 0), > >  size=292876, > >  blksize=4096, > >  blocks=80, > >  atime=Mon Jan 03 16:03:24 -0700 2011 (1294095804), > >  mtime=Thu Oct 21 11:34:51 -0600 2010 (1287682491), > >  ctime=Thu Oct 21 11:34:51 -0600 2010 (1287682491)> > > > Multiplying blocks * blksize doesn't seem to match up, either. > > See stat(2): > >        The st_blocks field indicates the number of  blocks  allocated  to  the >        file,  512-byte  units.  (This may be smaller than st_size/512 when the >        file has holes.) > >        The st_blksize field gives the "preferred" blocksize for efficient file >        system  I/O.  (Writing to a file in smaller chunks may cause an ineffi- >        cient read-modify-rewrite.) > > So "blksize" has nothing to do with the size of the "blocks". They are > always counted in 512-byte units. Oh, wow, I don't think I knew that. It's strikes me as particularly bizarre that they would return some notion of a "preferred block size" instead of the actual block size. Seriously, what's the use of that? Now I need to check other platforms (Solaris, HP-UX) to see if they use 512 byte convention. Is this something that's universal? Or is it something I can get via a C call somewhere? Regards, Dan