diff --git a/utils/src/dev.c b/utils/src/dev.c index 2a0c9eb6..8eae2d40 100644 --- a/utils/src/dev.c +++ b/utils/src/dev.c @@ -12,13 +12,10 @@ #include "sparse.h" #include "dev.h" -int device_size(char *path, int fd, - u64 min_size, u64 max_size, bool allow_small_size, - char *use_type, u64 *size_ret) +int get_device_size(char *path, int fd, u64 *size_ret) { struct stat st; u64 size; - char *target_type; int ret; if (fstat(fd, &st)) { @@ -30,7 +27,6 @@ int device_size(char *path, int fd, if (S_ISREG(st.st_mode)) { size = st.st_size; - target_type = "file"; } else if (S_ISBLK(st.st_mode)) { if (ioctl(fd, BLKGETSIZE64, &size)) { ret = -errno; @@ -38,13 +34,26 @@ int device_size(char *path, int fd, path, strerror(errno), errno); return ret; } - target_type = "device"; } else { fprintf(stderr, "path isn't regular or device file '%s'\n", path); return -EINVAL; } + *size_ret = size; + return 0; +} + +int limit_device_size(char *path, int fd, u64 min_size, u64 max_size, bool allow_small_size, + char *use_type, u64 *size_ret) +{ + u64 size; + int ret; + + ret = get_device_size(path, fd, &size); + if (ret < 0) + return ret; + if (max_size) { if (size > max_size) { printf("Limiting use of "BASE_SIZE_FMT @@ -64,9 +73,9 @@ int device_size(char *path, int fd, if (size < min_size) { fprintf(stderr, - BASE_SIZE_FMT" %s too small for min " + BASE_SIZE_FMT" too small for min " BASE_SIZE_FMT" %s device%s\n", - BASE_SIZE_ARGS(size), target_type, + BASE_SIZE_ARGS(size), BASE_SIZE_ARGS(min_size), use_type, allow_small_size ? ", allowing with -A" : ""); diff --git a/utils/src/dev.h b/utils/src/dev.h index d483f70b..3d14c22d 100644 --- a/utils/src/dev.h +++ b/utils/src/dev.h @@ -9,9 +9,9 @@ #define SIZE_FMT "%llu (%.2f %s)" #define SIZE_ARGS(nr, sz) (nr), size_flt(nr, sz), size_str(nr, sz) -int device_size(char *path, int fd, - u64 min_size, u64 max_size, bool allow_small_size, - char *use_type, u64 *size_ret); +int get_device_size(char *path, int fd, u64 *size_ret); +int limit_device_size(char *path, int fd, u64 min_size, u64 max_size, bool allow_small_size, + char *use_type, u64 *size_ret); float size_flt(u64 nr, unsigned size); char *size_str(u64 nr, unsigned size); int flush_device(int fd); diff --git a/utils/src/mkfs.c b/utils/src/mkfs.c index 962927c9..f27a5674 100644 --- a/utils/src/mkfs.c +++ b/utils/src/mkfs.c @@ -206,14 +206,14 @@ static int do_mkfs(struct mkfs_args *args) } /* minumum meta device size to make reserved blocks reasonably large */ - ret = device_size(args->meta_device, meta_fd, 64ULL * (1024 * 1024 * 1024), - args->max_meta_size, args->allow_small_size, "meta", &meta_size); + ret = limit_device_size(args->meta_device, meta_fd, 64ULL * (1024 * 1024 * 1024), + args->max_meta_size, args->allow_small_size, "meta", &meta_size); if (ret) goto out; /* .. then arbitrarily the same minimum data device size */ - ret = device_size(args->data_device, data_fd, 64ULL * (1024 * 1024 * 1024), - args->max_data_size, args->allow_small_size, "data", &data_size); + ret = limit_device_size(args->data_device, data_fd, 64ULL * (1024 * 1024 * 1024), + args->max_data_size, args->allow_small_size, "data", &data_size); if (ret) goto out;