aboutsummaryrefslogtreecommitdiff
path: root/libbb/get_volsize.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-02-01 23:48:27 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2010-02-01 23:48:27 +0100
commit40e7d25aca1abbe080e00e2bed64b444a5ec7858 (patch)
tree05cda08f66542aeb94d1ebb906a0f04b04a41d62 /libbb/get_volsize.c
parent4875e7148b0512ee3c255526a484503da984935a (diff)
downloadbusybox-w32-40e7d25aca1abbe080e00e2bed64b444a5ec7858.tar.gz
busybox-w32-40e7d25aca1abbe080e00e2bed64b444a5ec7858.tar.bz2
busybox-w32-40e7d25aca1abbe080e00e2bed64b444a5ec7858.zip
mkXXXX: unify [KBYTES] parameter handling (added it to mkswap)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/get_volsize.c')
-rw-r--r--libbb/get_volsize.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/libbb/get_volsize.c b/libbb/get_volsize.c
new file mode 100644
index 000000000..5b0270968
--- /dev/null
+++ b/libbb/get_volsize.c
@@ -0,0 +1,48 @@
1/* 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}