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.
/Johan Holmberg
···
On Wed, Jan 5, 2011 at 5:37 PM, Daniel Berger <djberg96@gmail.com> 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"
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
···
On Jan 5, 12:39 pm, Johan Holmberg <johan...@gmail.com> wrote:
On Wed, Jan 5, 2011 at 5:37 PM, Daniel Berger <djber...@gmail.com> 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"
> 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.
I think you'll want to read up on the stat() system call. The POSIX
standard leaves a bit of wiggle room though since while it does specify
that st_blocks must be returned it doesn't specify the size of the blocks.
I'm not sure I understand your concern about 'actual' vs. 'preferred'.
I'm guessing they would be the same in almost any rational implementation
but the main reason for having the information is to perform I/O in
efficiently sized chunks. In that case, the 'preferred' block size would
seem to be what you want even if the 'actual' block size was different.
Gary Wright
···
On Jan 6, 2011, at 3:44 PM, Daniel Berger wrote:
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?
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?
I think the two fields "st_blocks" and "st_blksize" just happens to
use the same word ("block") in two slightly different meanings. To
count the "st_blocks" in 512-byte units seem to be an arbitrary
convention, unrelated to the "physical block size" used for files.
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?
I looked in "Advanced UNIX Programming, 2nd ed" by Rochkind, and there
the "st_blocks" field is described as the number of 512-byte blocks
allocated for a file. So I guess this is a universal thing for UN*X
(Linux, Mac OS X, Solaris, etc.).
The Rochkind book also mentions that "st_blksize is in the stat
structure so that an implementation can vary it by file if it chooses
to do so".
Regards,
/Johan Holmberg
···
On Thu, Jan 6, 2011 at 9:44 PM, Daniel Berger <djberg96@gmail.com> wrote: