blob: 5b0270968ef94102eea9e3f39b36e544e12aa877 [file] [log] [blame]
Denys Vlasenko40e7d252010-02-01 23:48:27 +01001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 2010 Denys Vlasenko
6 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 */
9#include "libbb.h"
10
11uoff_t FAST_FUNC get_volume_size_in_bytes(int fd,
12 const char *override,
13 unsigned override_units,
14 int extend)
15{
16 uoff_t result;
17
18 if (override) {
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) {
25 if (extend)
26 xwrite(fd, "", 1); /* file grows if needed */
27 }
28 //else {
29 // bb_error_msg("warning, block device is smaller");
30 //}
31 } else {
32 /* more portable than BLKGETSIZE[64] */
33 result = xlseek(fd, 0, SEEK_END);
34 }
35
36 xlseek(fd, 0, SEEK_SET);
37
38 /* Prevent things like this:
39 * $ dd if=/dev/zero of=foo count=1 bs=1024
40 * $ mkswap foo
41 * Setting up swapspace version 1, size = 18446744073709548544 bytes
42 *
43 * Picked 16k arbitrarily: */
44 if (result < 16*1024)
45 bb_error_msg_and_die("image is too small");
46
47 return result;
48}