dev_path = argv[optind];
- fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
-
- if (fdmnt < 0) {
- if (errno == EINVAL)
- fprintf(stderr,
- "ERROR: '%s' is not a mounted btrfs device\n",
- dev_path);
- else
- fprintf(stderr, "ERROR: can't access '%s': %s\n",
- dev_path, strerror(errno));
+ fdmnt = open_path_or_dev_mnt(dev_path, &dirstream, 1);
+ if (fdmnt < 0)
return 1;
- }
ret = get_fs_info(dev_path, &fi_args, &di_args);
if (ret) {
usage(cmd_replace_start_usage);
path = argv[optind + 2];
- fdmnt = open_path_or_dev_mnt(path, &dirstream);
-
- if (fdmnt < 0) {
- if (errno == EINVAL)
- fprintf(stderr,
- "ERROR: '%s' is not a mounted btrfs device\n",
- path);
- else
- fprintf(stderr, "ERROR: can't access '%s': %s\n",
- path, strerror(errno));
+ fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
+ if (fdmnt < 0)
goto leave_with_error;
- }
/* check for possible errors before backgrounding */
status_args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS;
path = argv[optind];
- fdmnt = open_path_or_dev_mnt(path, &dirstream);
-
- if (fdmnt < 0) {
- if (errno == EINVAL)
- error_on(!do_quiet, "'%s' is not a mounted btrfs device",
- path);
- else
- error_on(!do_quiet, "can't access '%s': %s",
- path, strerror(errno));
+ fdmnt = open_path_or_dev_mnt(path, &dirstream, !do_quiet);
+ if (fdmnt < 0)
return 1;
- }
ret = get_fs_info(path, &fi_args, &di_args);
if (ret) {
path = argv[1];
- fdmnt = open_path_or_dev_mnt(path, &dirstream);
+ fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
if (fdmnt < 0) {
- if (errno == EINVAL)
- error("'%s' is not a mounted btrfs device", path);
- else
- error("can't access '%s': %s", path, strerror(errno));
ret = 1;
goto out;
}
path = argv[optind];
- fdmnt = open_path_or_dev_mnt(path, &dirstream);
-
- if (fdmnt < 0) {
- if (errno == EINVAL)
- error("'%s' is not a mounted btrfs device", path);
- else
- error("can't access '%s': %s", path, strerror(errno));
+ fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
+ if (fdmnt < 0)
return 1;
- }
ret = get_fs_info(path, &fi_args, &di_args);
if (ret) {
*
* On error, return -1, errno should be set.
*/
-int open_path_or_dev_mnt(const char *path, DIR **dirstream)
+int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose)
{
char mp[PATH_MAX];
- int fdmnt;
-
- fdmnt = is_block_device(path);
- if (fdmnt == 1) {
- int ret;
+ int ret;
+ if (is_block_device(path)) {
ret = get_btrfs_mount(path, mp, sizeof(mp));
if (ret < 0) {
/* not a mounted btrfs dev */
+ error_on(verbose, "'%s' is not a mounted btrfs device",
+ path);
errno = EINVAL;
return -1;
}
- fdmnt = open_file_or_dir(mp, dirstream);
- } else if (fdmnt == 0) {
- fdmnt = open_file_or_dir(path, dirstream);
+ ret = open_file_or_dir(mp, dirstream);
+ error_on(verbose && ret < 0, "can't access '%s': %s",
+ path, strerror(errno));
+ } else {
+ ret = btrfs_open_dir(path, dirstream, 1);
}
- return fdmnt;
+ return ret;
}
/*
int is_block_device(const char *file);
int is_mount_point(const char *file);
int check_arg_type(const char *input);
-int open_path_or_dev_mnt(const char *path, DIR **dirstream);
+int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose);
int btrfs_open_dir(const char *path, DIR **dirstream, int verbose);
u64 btrfs_device_size(int fd, struct stat *st);
/* Helper to always get proper size of the destination string */