From c848046eb984fcbeeb78d681bf483aefbb35a013 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 11 Jun 2015 00:46:30 +0200 Subject: [PATCH] btrfs-progs: print error within test_dev_for_mkfs The error string buffer passed as an argument is of a fixed size, though we could print up to PATH_MAX + something bytes. Print the error message directly. Signed-off-by: David Sterba --- cmds-device.c | 4 +--- cmds-replace.c | 8 +++----- mkfs.c | 4 +--- utils.c | 26 +++++++++++++------------- utils.h | 2 +- 5 files changed, 19 insertions(+), 25 deletions(-) diff --git a/cmds-device.c b/cmds-device.c index 4fa6b4a..3a10438 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -52,7 +52,6 @@ static int cmd_add_dev(int argc, char **argv) DIR *dirstream = NULL; int discard = 1; int force = 0; - char estr[100]; while (1) { int c; @@ -97,9 +96,8 @@ static int cmd_add_dev(int argc, char **argv) int mixed = 0; char *path; - res = test_dev_for_mkfs(argv[i], force, estr); + res = test_dev_for_mkfs(argv[i], force); if (res) { - fprintf(stderr, "%s", estr); ret++; continue; } diff --git a/cmds-replace.c b/cmds-replace.c index 75b131b..85365e3 100644 --- a/cmds-replace.c +++ b/cmds-replace.c @@ -142,7 +142,6 @@ static int cmd_start_replace(int argc, char **argv) int do_not_background = 0; int mixed = 0; DIR *dirstream = NULL; - char estr[100]; /* check test_dev_for_mkfs() for error string size*/ while ((c = getopt(argc, argv, "Brf")) != -1) { switch (c) { @@ -256,11 +255,10 @@ static int cmd_start_replace(int argc, char **argv) start_args.start.srcdevid = 0; } - ret = test_dev_for_mkfs(dstdev, force_using_targetdev, estr); - if (ret) { - fprintf(stderr, "%s", estr); + ret = test_dev_for_mkfs(dstdev, force_using_targetdev); + if (ret) goto leave_with_error; - } + fddstdev = open(dstdev, O_RDWR); if (fddstdev < 0) { fprintf(stderr, "Unable to open %s\n", dstdev); diff --git a/mkfs.c b/mkfs.c index 14b75bc..8bc7783 100644 --- a/mkfs.c +++ b/mkfs.c @@ -1335,10 +1335,8 @@ int main(int ac, char **av) while (dev_cnt-- > 0) { file = av[optind++]; if (is_block_device(file)) - if (test_dev_for_mkfs(file, force_overwrite, estr)) { - fprintf(stderr, "Error: %s", estr); + if (test_dev_for_mkfs(file, force_overwrite)) exit(1); - } } optind = saved_optind; diff --git a/utils.c b/utils.c index 41a04bd..36aba39 100644 --- a/utils.c +++ b/utils.c @@ -2397,58 +2397,58 @@ int group_profile_max_safe_loss(u64 flags) } } -/* Check if disk is suitable for btrfs +/* + * Check if a device is suitable for btrfs * returns: - * 1: something is wrong, estr provides the error + * 1: something is wrong, an error is printed * 0: all is fine */ -int test_dev_for_mkfs(char *file, int force_overwrite, char *estr) +int test_dev_for_mkfs(char *file, int force_overwrite) { int ret, fd; - size_t sz = 100; struct stat st; ret = is_swap_device(file); if (ret < 0) { - snprintf(estr, sz, "error checking %s status: %s\n", file, + fprintf(stderr, "ERROR: checking status of %s: %s\n", file, strerror(-ret)); return 1; } if (ret == 1) { - snprintf(estr, sz, "%s is a swap device\n", file); + fprintf(stderr, "ERROR: %s is a swap device\n", file); return 1; } if (!force_overwrite) { if (check_overwrite(file)) { - snprintf(estr, sz, "Use the -f option to force overwrite.\n"); + fprintf(stderr, "Use the -f option to force overwrite.\n"); return 1; } } ret = check_mounted(file); if (ret < 0) { - snprintf(estr, sz, "error checking %s mount status\n", - file); + fprintf(stderr, "ERROR: checking mount status of %s: %s\n", + file, strerror(-ret)); return 1; } if (ret == 1) { - snprintf(estr, sz, "%s is mounted\n", file); + fprintf(stderr, "ERROR: %s is mounted\n", file); return 1; } /* check if the device is busy */ fd = open(file, O_RDWR|O_EXCL); if (fd < 0) { - snprintf(estr, sz, "unable to open %s: %s\n", file, + fprintf(stderr, "ERROR: unable to open %s: %s\n", file, strerror(errno)); return 1; } if (fstat(fd, &st)) { - snprintf(estr, sz, "unable to stat %s: %s\n", file, + fprintf(stderr, "ERROR: unable to stat %s: %s\n", file, strerror(errno)); close(fd); return 1; } if (!S_ISBLK(st.st_mode)) { - fprintf(stderr, "'%s' is not a block device\n", file); + fprintf(stderr, "ERROR: %s is not a block device\n", file); close(fd); return 1; } diff --git a/utils.h b/utils.h index 50d1853..08518c5 100644 --- a/utils.h +++ b/utils.h @@ -156,7 +156,7 @@ int open_path_or_dev_mnt(const char *path, DIR **dirstream); u64 btrfs_device_size(int fd, struct stat *st); /* Helper to always get proper size of the destination string */ #define strncpy_null(dest, src) __strncpy__null(dest, src, sizeof(dest)) -int test_dev_for_mkfs(char *file, int force_overwrite, char *estr); +int test_dev_for_mkfs(char *file, int force_overwrite); int get_label_mounted(const char *mount_path, char *labelp); int test_num_disk_vs_raid(u64 metadata_profile, u64 data_profile, u64 dev_cnt, int mixed, char *estr); -- 2.7.4