Skip to content

Determine the size of a block device off Linux - #413

Merged
folbricht merged 1 commit into
masterfrom
device-size-nonlinux
Sep 2, 2026
Merged

folbricht merged 1 commit into
masterfrom
device-size-nonlinux

Conversation

@folbricht

Copy link
Copy Markdown
Owner

GetFileSize had a TODO where the device case should be, so on every platform but Linux it fell through to info.Size(), which is zero for a device. desync make against a block device produced an index for an empty blob and reported no problem.

Linux reads the size with BLKGETSIZE64. Seeking to the end is the portable equivalent across the BSDs and macOS, and it reports the same value the ioctl does. A device with no size to report — a character device like /dev/zero — now returns an error rather than the zero, which is the same outcome Linux already produces there.

Verification. The shipped function was compiled standalone and run against a real block device, since Linux builds the ioctl file instead:

/dev/zram0     size=8589934592     err=<nil>
/dev/zero      size=0              err=unable to determine the size of device /dev/zero
/etc/hostname  size=3              err=<nil>
/nonexistent   size=0              err=stat /nonexistent: no such file or directory

8589934592 is exactly what BLKGETSIZE64 returns for that device, against Stat().Size() of 0.

The tests assert behaviour both implementations share, so they cover the ioctl path on Linux and the new path on the four BSDs in CI, including that /dev/zero is an error on each. What no test covers is a real disk on a BSD or macOS, which needs a device the runner can open; the equivalence above and lseek being POSIX are what that rests on.

Stat reports zero for a device, so every non-Linux platform returned
zero for one. `desync make` on a block device produced an index for an
empty blob rather than failing.

Linux reads the size with an ioctl. Seeking to the end is what's
portable across the BSDs and macOS, and reports the same value the ioctl
does where both are available. A device with no size to report, a
character device such as /dev/zero, is now an error rather than a silent
zero.
@folbricht
folbricht merged commit 6e96e94 into master Sep 2, 2026
9 checks passed
@folbricht
folbricht deleted the device-size-nonlinux branch September 2, 2026 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ 换网址