From 3e653e801ce9eb769e028da5af6da0373763fc1d Mon Sep 17 00:00:00 2001 From: Nikolay Borisov Date: Mon, 25 Jan 2021 12:43:58 +0200 Subject: [PATCH] btrfs-progs: remove duplicate checks from cmd_filesystem_resize btrfs_open_dir already has a check whether the passed path is a directory and if so it returns a specific error code (-3) when such an error occurs. Use this instead of open-coding the directory check. To avoid regression in cli/003 test also move directory checks before fs type in btrfs_open. Output before this check: ERROR: resize works on mounted filesystems and accepts only directories as argument. Passing file containing a btrfs image would resize the underlying filesystem instead of the image. After: ERROR: not a directory: /root/btrfs-progs/tests/test.img ERROR: resize works on mounted filesystems and accepts only directories as argument. Passing file containing a btrfs image would resize the underlying filesystem instead of the image. Signed-off-by: Nikolay Borisov Signed-off-by: David Sterba --- cmds/filesystem.c | 23 +++++++++-------------- common/utils.c | 20 ++++++++++---------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/cmds/filesystem.c b/cmds/filesystem.c index 05e46030..0d23daf4 100644 --- a/cmds/filesystem.c +++ b/cmds/filesystem.c @@ -1082,7 +1082,6 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd, char *amount, *path; DIR *dirstream = NULL; int ret; - struct stat st; bool enqueue = false; /* @@ -1115,21 +1114,17 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd, return 1; } - res = stat(path, &st); - if (res < 0) { - error("resize: cannot stat %s: %m", path); - return 1; - } - if (!S_ISDIR(st.st_mode)) { - error("resize works on mounted filesystems and accepts only\n" - "directories as argument. Passing file containing a btrfs image\n" - "would resize the underlying filesystem instead of the image.\n"); - return 1; - } - fd = btrfs_open_dir(path, &dirstream, 1); - if (fd < 0) + if (fd < 0) { + /* The path is a directory */ + if (fd == -3) { + error( + "resize works on mounted filesystems and accepts only\n" + "directories as argument. Passing file containing a btrfs image\n" + "would resize the underlying filesystem instead of the image.\n"); + } return 1; + } ret = check_running_fs_exclop(fd, BTRFS_EXCLOP_RESIZE, enqueue); if (ret != 0) { diff --git a/common/utils.c b/common/utils.c index a29fecdf..852fdd44 100644 --- a/common/utils.c +++ b/common/utils.c @@ -184,16 +184,6 @@ int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only) struct stat st; int ret; - if (statfs(path, &stfs) != 0) { - error_on(verbose, "cannot access '%s': %m", path); - return -1; - } - - if (stfs.f_type != BTRFS_SUPER_MAGIC) { - error_on(verbose, "not a btrfs filesystem: %s", path); - return -2; - } - if (stat(path, &st) != 0) { error_on(verbose, "cannot access '%s': %m", path); return -1; @@ -204,6 +194,16 @@ int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only) return -3; } + if (statfs(path, &stfs) != 0) { + error_on(verbose, "cannot access '%s': %m", path); + return -1; + } + + if (stfs.f_type != BTRFS_SUPER_MAGIC) { + error_on(verbose, "not a btrfs filesystem: %s", path); + return -2; + } + ret = open_file_or_dir(path, dirstream); if (ret < 0) { error_on(verbose, "cannot access '%s': %m", path);