tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Issues with lseek(2) on a block device



>>> lseek(fd, 0, SEEK_END);
[on a disk device]

>> [...]
> [...]
> This is such a buggy behaviour that [...]

I wouldn't call it buggy, not unless there is a spec that it's supposed
to conform to that says otherwise (even if the "spec" is just an
author's description of intent), which is something I so far haven't
seen reason to think exists.  It looks to me like "we didn't bother
making it do anything in particular, so you get whatever it happens to
give you".

> I stopped using stat for finding out block device size a long time
> ago, and not just on NetBSD.

That sounds sensible to me.  I think your first mail on the subject was
the first time I'd even _considered_ that stat() applied to a disk
device might return drive/partition size.

My own method of finding disk device size is to lseek to offset N and
try to read one sector, for various N: initially N = sector size, then
double N until I get EOF-or-error, then do binary search between the
last working value and the first failing value.  I think that's worked
on everything I've tried it on; admittedly, that's only a few OSes, but
I'd expect it to be highly portable.  The only cases I'd expect it to
fail in are disks over 4G (or perhaps 2G) on systems with only 32 bits
for off_t.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index