1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2010 Denys Vlasenko
7 * Licensed under GPLv2, see file LICENSE in this source tree.
11 uoff_t FAST_FUNC get_volume_size_in_bytes(int fd,
13 unsigned override_units,
19 result = XATOOFF(override);
20 if (result >= (uoff_t)(MAXINT(off_t)) / override_units)
21 bb_error_msg_and_die("image size is too big");
22 result *= override_units;
23 /* seek past end fails on block devices but works on files */
24 if (lseek(fd, result - 1, SEEK_SET) != (off_t)-1) {
26 xwrite(fd, "", 1); /* file grows if needed */
29 // bb_error_msg("warning, block device is smaller");
32 /* more portable than BLKGETSIZE[64] */
33 result = xlseek(fd, 0, SEEK_END);
36 xlseek(fd, 0, SEEK_SET);
38 /* Prevent things like this:
39 * $ dd if=/dev/zero of=foo count=1 bs=1024
41 * Setting up swapspace version 1, size = 18446744073709548544 bytes
43 * Picked 16k arbitrarily: */
45 bb_error_msg_and_die("image is too small");