btrfs-progs: utils: switch more error messages to common helpers

Functions relatd to device changes/status/open, mount checks.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2016-03-14 14:36:30 +01:00
parent 230bb91aa8
commit 3bb71d27ac
2 changed files with 24 additions and 39 deletions

View File

@ -76,10 +76,10 @@ int main(int argc, char **argv)
radix_tree_init(); radix_tree_init();
if((ret = check_mounted(argv[optind])) < 0) { if((ret = check_mounted(argv[optind])) < 0) {
fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret)); error("cannot check mount status: %s", strerror(-ret));
return ret; return ret;
} else if(ret) { } else if(ret) {
fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]); error("%s is currently mounted, aborting", argv[optind]);
return -EBUSY; return -EBUSY;
} }

59
utils.c
View File

@ -865,13 +865,13 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
ret = fstat(fd, &st); ret = fstat(fd, &st);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "unable to stat %s\n", file); error("unable to stat %s: %s", file, strerror(errno));
return 1; return 1;
} }
block_count = btrfs_device_size(fd, &st); block_count = btrfs_device_size(fd, &st);
if (block_count == 0) { if (block_count == 0) {
fprintf(stderr, "unable to find %s size\n", file); error("unable to determine size of %s", file);
return 1; return 1;
} }
if (max_block_count) if (max_block_count)
@ -899,15 +899,13 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
ZERO_DEV_BYTES, block_count); ZERO_DEV_BYTES, block_count);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: failed to zero device '%s' - %s\n", error("failed to zero device '%s': %s", file, strerror(-ret));
file, strerror(-ret));
return 1; return 1;
} }
ret = btrfs_wipe_existing_sb(fd); ret = btrfs_wipe_existing_sb(fd);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: cannot wipe superblocks on '%s'\n", error("cannot wipe superblocks on %s", file);
file);
return 1; return 1;
} }
@ -1054,11 +1052,10 @@ int get_btrfs_mount(const char *dev, char *mp, size_t mp_size)
ret = is_block_device(dev); ret = is_block_device(dev);
if (ret <= 0) { if (ret <= 0) {
if (!ret) { if (!ret) {
fprintf(stderr, "%s is not a block device\n", dev); error("not a block device: %s", dev);
ret = -EINVAL; ret = -EINVAL;
} else { } else {
fprintf(stderr, "Could not check %s: %s\n", error("cannot check %s: %s", dev, strerror(-ret));
dev, strerror(-ret));
} }
goto out; goto out;
} }
@ -1066,7 +1063,7 @@ int get_btrfs_mount(const char *dev, char *mp, size_t mp_size)
fd = open(dev, O_RDONLY); fd = open(dev, O_RDONLY);
if (fd < 0) { if (fd < 0) {
ret = -errno; ret = -errno;
fprintf(stderr, "Could not open %s: %s\n", dev, strerror(errno)); error("cannot open %s: %s", dev, strerror(errno));
goto out; goto out;
} }
@ -1125,43 +1122,31 @@ int btrfs_open_dir(const char *path, DIR **dirstream, int verbose)
int ret; int ret;
if (statfs(path, &stfs) != 0) { if (statfs(path, &stfs) != 0) {
if (verbose) error_on(verbose, "cannot access '%s': %s", path,
fprintf(stderr, strerror(errno));
"ERROR: can't access '%s': %s\n",
path, strerror(errno));
return -1; return -1;
} }
if (stfs.f_type != BTRFS_SUPER_MAGIC) { if (stfs.f_type != BTRFS_SUPER_MAGIC) {
if (verbose) error_on(verbose, "not a btrfs filesystem: %s", path);
fprintf(stderr,
"ERROR: not a btrfs filesystem: %s\n",
path);
return -2; return -2;
} }
if (stat(path, &st) != 0) { if (stat(path, &st) != 0) {
if (verbose) error_on(verbose, "cannot access '%s': %s", path,
fprintf(stderr, strerror(errno));
"ERROR: can't access '%s': %s\n",
path, strerror(errno));
return -1; return -1;
} }
if (!S_ISDIR(st.st_mode)) { if (!S_ISDIR(st.st_mode)) {
if (verbose) error_on(verbose, "not a directory: %s", path);
fprintf(stderr,
"ERROR: not a directory: %s\n",
path);
return -3; return -3;
} }
ret = open_file_or_dir(path, dirstream); ret = open_file_or_dir(path, dirstream);
if (ret < 0) { if (ret < 0) {
if (verbose) error_on(verbose, "cannot access '%s': %s", path,
fprintf(stderr, strerror(errno));
"ERROR: can't access '%s': %s\n",
path, strerror(errno));
} }
return ret; return ret;
@ -1437,7 +1422,8 @@ int check_mounted(const char* file)
fd = open(file, O_RDONLY); fd = open(file, O_RDONLY);
if (fd < 0) { if (fd < 0) {
fprintf (stderr, "check_mounted(): Could not open %s\n", file); error("mount check: cannot open %s: %s", file,
strerror(errno));
return -errno; return -errno;
} }
@ -1525,8 +1511,8 @@ int btrfs_register_one_device(const char *fname)
fd = open("/dev/btrfs-control", O_RDWR); fd = open("/dev/btrfs-control", O_RDWR);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "failed to open /dev/btrfs-control " warning(
"skipping device registration: %s\n", "failed to open /dev/btrfs-control, skipping device registration: %s",
strerror(errno)); strerror(errno));
return -errno; return -errno;
} }
@ -1534,8 +1520,8 @@ int btrfs_register_one_device(const char *fname)
strncpy_null(args.name, fname); strncpy_null(args.name, fname);
ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args); ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR: device scan failed '%s' - %s\n", error("device scan failed on '%s': %s", fname,
fname, strerror(errno)); strerror(errno));
ret = -errno; ret = -errno;
} }
close(fd); close(fd);
@ -2197,8 +2183,7 @@ int get_fs_info(char *path, struct btrfs_ioctl_fs_info_args *fi_args,
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (fd < 0) { if (fd < 0) {
ret = -errno; ret = -errno;
fprintf(stderr, "Couldn't open %s: %s\n", error("cannot open %s: %s", path, strerror(errno));
path, strerror(errno));
goto out; goto out;
} }
ret = check_mounted_where(fd, path, mp, sizeof(mp), ret = check_mounted_where(fd, path, mp, sizeof(mp),