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 <dsterba@suse.cz>
This commit is contained in:
David Sterba 2015-06-11 00:46:30 +02:00
parent 4647ab887a
commit c848046eb9
5 changed files with 19 additions and 25 deletions

View File

@ -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;
}

View File

@ -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);

4
mkfs.c
View File

@ -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;

26
utils.c
View File

@ -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;
}

View File

@ -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);