diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-02-01 23:48:27 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-02-01 23:48:27 +0100 |
| commit | 40e7d25aca1abbe080e00e2bed64b444a5ec7858 (patch) | |
| tree | 05cda08f66542aeb94d1ebb906a0f04b04a41d62 | |
| parent | 4875e7148b0512ee3c255526a484503da984935a (diff) | |
| download | busybox-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>
| -rw-r--r-- | include/libbb.h | 5 | ||||
| -rw-r--r-- | include/usage.h | 2 | ||||
| -rw-r--r-- | libbb/Kbuild | 1 | ||||
| -rw-r--r-- | libbb/get_volsize.c | 48 | ||||
| -rw-r--r-- | util-linux/mkfs_ext2.c | 14 | ||||
| -rw-r--r-- | util-linux/mkfs_reiser.c | 15 | ||||
| -rw-r--r-- | util-linux/mkfs_vfat.c | 14 | ||||
| -rw-r--r-- | util-linux/mkswap.c | 16 |
8 files changed, 66 insertions, 49 deletions
diff --git a/include/libbb.h b/include/libbb.h index a86d64400..09852d036 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -412,6 +412,11 @@ int rename_or_warn(const char *oldpath, const char *newpath) FAST_FUNC; | |||
| 412 | off_t xlseek(int fd, off_t offset, int whence) FAST_FUNC; | 412 | off_t xlseek(int fd, off_t offset, int whence) FAST_FUNC; |
| 413 | off_t fdlength(int fd) FAST_FUNC; | 413 | off_t fdlength(int fd) FAST_FUNC; |
| 414 | 414 | ||
| 415 | uoff_t FAST_FUNC get_volume_size_in_bytes(int fd, | ||
| 416 | const char *override, | ||
| 417 | unsigned override_units, | ||
| 418 | int extend); | ||
| 419 | |||
| 415 | void xpipe(int filedes[2]) FAST_FUNC; | 420 | void xpipe(int filedes[2]) FAST_FUNC; |
| 416 | /* In this form code with pipes is much more readable */ | 421 | /* In this form code with pipes is much more readable */ |
| 417 | struct fd_pair { int rd; int wr; }; | 422 | struct fd_pair { int rd; int wr; }; |
diff --git a/include/usage.h b/include/usage.h index 44cc83422..683d53fbd 100644 --- a/include/usage.h +++ b/include/usage.h | |||
| @@ -2844,7 +2844,7 @@ | |||
| 2844 | "$ mknod -m 644 /tmp/pipe p\n" | 2844 | "$ mknod -m 644 /tmp/pipe p\n" |
| 2845 | 2845 | ||
| 2846 | #define mkswap_trivial_usage \ | 2846 | #define mkswap_trivial_usage \ |
| 2847 | "[OPTIONS] BLOCKDEV" /* [SIZE_IN_KB] */ | 2847 | "[OPTIONS] BLOCKDEV [KBYTES]" |
| 2848 | #define mkswap_full_usage "\n\n" \ | 2848 | #define mkswap_full_usage "\n\n" \ |
| 2849 | "Prepare BLOCKDEV to be used as swap partition\n" \ | 2849 | "Prepare BLOCKDEV to be used as swap partition\n" \ |
| 2850 | "\nOptions:" \ | 2850 | "\nOptions:" \ |
diff --git a/libbb/Kbuild b/libbb/Kbuild index 7e793109e..c205ceb4c 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild | |||
| @@ -42,6 +42,7 @@ lib-y += get_last_path_component.o | |||
| 42 | lib-y += get_line_from_file.o | 42 | lib-y += get_line_from_file.o |
| 43 | lib-y += getopt32.o | 43 | lib-y += getopt32.o |
| 44 | lib-y += getpty.o | 44 | lib-y += getpty.o |
| 45 | lib-y += get_volsize.o | ||
| 45 | lib-y += herror_msg.o | 46 | lib-y += herror_msg.o |
| 46 | lib-y += herror_msg_and_die.o | 47 | lib-y += herror_msg_and_die.o |
| 47 | lib-y += human_readable.o | 48 | lib-y += human_readable.o |
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 | |||
| 11 | uoff_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 | } | ||
diff --git a/util-linux/mkfs_ext2.c b/util-linux/mkfs_ext2.c index 19c3c673e..44fb40250 100644 --- a/util-linux/mkfs_ext2.c +++ b/util-linux/mkfs_ext2.c | |||
| @@ -230,19 +230,7 @@ int mkfs_ext2_main(int argc UNUSED_PARAM, char **argv) | |||
| 230 | bb_error_msg_and_die("can't format mounted filesystem"); | 230 | bb_error_msg_and_die("can't format mounted filesystem"); |
| 231 | 231 | ||
| 232 | // open the device, get size in kbytes | 232 | // open the device, get size in kbytes |
| 233 | if (argv[1]) { | 233 | kilobytes = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ !(option_mask32 & OPT_n)) / 1024; |
| 234 | kilobytes = xatoull(argv[1]); | ||
| 235 | // seek past end fails on block devices but works on files | ||
| 236 | if (lseek(fd, kilobytes * 1024 - 1, SEEK_SET) != (off_t)-1) { | ||
| 237 | if (!(option_mask32 & OPT_n)) | ||
| 238 | xwrite(fd, "", 1); // file grows if needed | ||
| 239 | } | ||
| 240 | //else { | ||
| 241 | // bb_error_msg("warning, block device is smaller"); | ||
| 242 | //} | ||
| 243 | } else { | ||
| 244 | kilobytes = (uoff_t)xlseek(fd, 0, SEEK_END) / 1024; | ||
| 245 | } | ||
| 246 | 234 | ||
| 247 | bytes_per_inode = 16384; | 235 | bytes_per_inode = 16384; |
| 248 | if (kilobytes < 512*1024) | 236 | if (kilobytes < 512*1024) |
diff --git a/util-linux/mkfs_reiser.c b/util-linux/mkfs_reiser.c index 7f37eb845..eb2c94d02 100644 --- a/util-linux/mkfs_reiser.c +++ b/util-linux/mkfs_reiser.c | |||
| @@ -175,23 +175,12 @@ int mkfs_reiser_main(int argc UNUSED_PARAM, char **argv) | |||
| 175 | 175 | ||
| 176 | // check if it is mounted | 176 | // check if it is mounted |
| 177 | // N.B. what if we format a file? find_mount_point will return false negative since | 177 | // N.B. what if we format a file? find_mount_point will return false negative since |
| 178 | // it is loop block device which mounted! | 178 | // it is loop block device which is mounted! |
| 179 | if (find_mount_point(argv[0], 0)) | 179 | if (find_mount_point(argv[0], 0)) |
| 180 | bb_error_msg_and_die("can't format mounted filesystem"); | 180 | bb_error_msg_and_die("can't format mounted filesystem"); |
| 181 | 181 | ||
| 182 | // open the device, get size in blocks | 182 | // open the device, get size in blocks |
| 183 | if (argv[1]) { | 183 | blocks = get_volume_size_in_bytes(fd, argv[1], blocksize, /*extend:*/ 1) / blocksize; |
| 184 | blocks = xatoull(argv[1]); | ||
| 185 | // seek past end fails on block devices but works on files | ||
| 186 | if (lseek(fd, blocks * blocksize - 1, SEEK_SET) != (off_t)-1) { | ||
| 187 | xwrite(fd, "", 1); // file grows if needed | ||
| 188 | } | ||
| 189 | //else { | ||
| 190 | // bb_error_msg("warning, block device is smaller"); | ||
| 191 | //} | ||
| 192 | } else { | ||
| 193 | blocks = (uoff_t)xlseek(fd, 0, SEEK_END) / blocksize; | ||
| 194 | } | ||
| 195 | 184 | ||
| 196 | // block number sanity check | 185 | // block number sanity check |
| 197 | // we have a limit: skipped area, super block, journal and root block | 186 | // we have a limit: skipped area, super block, journal and root block |
diff --git a/util-linux/mkfs_vfat.c b/util-linux/mkfs_vfat.c index a9a65aa07..1363612f2 100644 --- a/util-linux/mkfs_vfat.c +++ b/util-linux/mkfs_vfat.c | |||
| @@ -244,7 +244,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) | |||
| 244 | // default volume ID = creation time | 244 | // default volume ID = creation time |
| 245 | volume_id = time(NULL); | 245 | volume_id = time(NULL); |
| 246 | 246 | ||
| 247 | dev = xopen(device_name, O_EXCL | O_RDWR); | 247 | dev = xopen(device_name, O_RDWR); |
| 248 | if (fstat(dev, &st) < 0) | 248 | if (fstat(dev, &st) < 0) |
| 249 | bb_simple_perror_msg_and_die(device_name); | 249 | bb_simple_perror_msg_and_die(device_name); |
| 250 | 250 | ||
| @@ -252,7 +252,6 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) | |||
| 252 | // Get image size and sector size | 252 | // Get image size and sector size |
| 253 | // | 253 | // |
| 254 | bytes_per_sect = SECTOR_SIZE; | 254 | bytes_per_sect = SECTOR_SIZE; |
| 255 | volume_size_bytes = st.st_size; | ||
| 256 | if (!S_ISBLK(st.st_mode)) { | 255 | if (!S_ISBLK(st.st_mode)) { |
| 257 | if (!S_ISREG(st.st_mode)) { | 256 | if (!S_ISREG(st.st_mode)) { |
| 258 | if (!argv[1]) | 257 | if (!argv[1]) |
| @@ -262,10 +261,6 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) | |||
| 262 | opts &= ~OPT_c; | 261 | opts &= ~OPT_c; |
| 263 | } else { | 262 | } else { |
| 264 | int min_bytes_per_sect; | 263 | int min_bytes_per_sect; |
| 265 | |||
| 266 | // more portable than BLKGETSIZE[64] | ||
| 267 | volume_size_bytes = xlseek(dev, 0, SEEK_END); | ||
| 268 | xlseek(dev, 0, SEEK_SET); | ||
| 269 | #if 0 | 264 | #if 0 |
| 270 | unsigned device_num; | 265 | unsigned device_num; |
| 271 | // for true block devices we do check sanity | 266 | // for true block devices we do check sanity |
| @@ -290,12 +285,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) | |||
| 290 | bb_error_msg("for this device sector size is %u", min_bytes_per_sect); | 285 | bb_error_msg("for this device sector size is %u", min_bytes_per_sect); |
| 291 | } | 286 | } |
| 292 | } | 287 | } |
| 293 | if (argv[1]) { | 288 | volume_size_bytes = get_volume_size_in_bytes(dev, argv[1], 1024, /*extend:*/ 1); |
| 294 | volume_size_bytes = XATOOFF(argv[1]); | ||
| 295 | if (volume_size_bytes >= MAXINT(off_t) / 1024) | ||
| 296 | bb_error_msg_and_die("image size is too big"); | ||
| 297 | volume_size_bytes *= 1024; | ||
| 298 | } | ||
| 299 | volume_size_sect = volume_size_bytes / bytes_per_sect; | 289 | volume_size_sect = volume_size_bytes / bytes_per_sect; |
| 300 | 290 | ||
| 301 | // | 291 | // |
diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c index 289692da3..949c71a78 100644 --- a/util-linux/mkswap.c +++ b/util-linux/mkswap.c | |||
| @@ -86,23 +86,19 @@ int mkswap_main(int argc UNUSED_PARAM, char **argv) | |||
| 86 | off_t len; | 86 | off_t len; |
| 87 | const char *label = ""; | 87 | const char *label = ""; |
| 88 | 88 | ||
| 89 | opt_complementary = "=1"; | 89 | opt_complementary = "-1"; /* at least one param */ |
| 90 | /* TODO: -p PAGESZ, -U UUID, | 90 | /* TODO: -p PAGESZ, -U UUID */ |
| 91 | * optional SIZE_IN_KB 2nd param | ||
| 92 | */ | ||
| 93 | getopt32(argv, "L:", &label); | 91 | getopt32(argv, "L:", &label); |
| 94 | argv += optind; | 92 | argv += optind; |
| 95 | 93 | ||
| 96 | fd = xopen(argv[0], O_WRONLY); | 94 | fd = xopen(argv[0], O_WRONLY); |
| 97 | 95 | ||
| 98 | /* Figure out how big the device is and announce our intentions */ | 96 | /* Figure out how big the device is */ |
| 99 | /* fdlength was reported to be unreliable - use seek */ | 97 | len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1); |
| 100 | len = xlseek(fd, 0, SEEK_END); | ||
| 101 | if (ENABLE_SELINUX) | ||
| 102 | xlseek(fd, 0, SEEK_SET); | ||
| 103 | |||
| 104 | pagesize = getpagesize(); | 98 | pagesize = getpagesize(); |
| 105 | len -= pagesize; | 99 | len -= pagesize; |
| 100 | |||
| 101 | /* Announce our intentions */ | ||
| 106 | printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len); | 102 | printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len); |
| 107 | mkswap_selinux_setcontext(fd, argv[0]); | 103 | mkswap_selinux_setcontext(fd, argv[0]); |
| 108 | 104 | ||
