linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] btrfs-progs: tests: Extend cli/003
@ 2021-01-25 10:43 Nikolay Borisov
  2021-01-25 10:43 ` [PATCH 2/2] btrfs-progs: Remove duplicate checks from cmd_filesystem_resize Nikolay Borisov
  2021-02-19 12:06 ` [PATCH 1/2] btrfs-progs: tests: Extend cli/003 David Sterba
  0 siblings, 2 replies; 3+ messages in thread
From: Nikolay Borisov @ 2021-01-25 10:43 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Nikolay Borisov

Add a test which ensures that when resize is tried on an image instead
of a directory appropriate warning is produced and the command fails.

Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---
 tests/cli-tests/003-fi-resize-args/test.sh | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tests/cli-tests/003-fi-resize-args/test.sh b/tests/cli-tests/003-fi-resize-args/test.sh
index 2e03725bb91a..0d2263f4b97f 100755
--- a/tests/cli-tests/003-fi-resize-args/test.sh
+++ b/tests/cli-tests/003-fi-resize-args/test.sh
@@ -54,4 +54,11 @@ for sep in '' '--'; do
 	run_check $SUDO_HELPER "$TOP/btrfs" filesystem resize $sep 1:max "$TEST_MNT"
 done

+
+# test passing a file instead of a directory
+run_mustfail_stdout "should fail for image" "$TOP/btrfs" filesystem resize 1:-128M "$TEST_DEV" |
+       grep -q "ERROR: resize works on mounted filesystems and accepts only" ||
+       _fail "no expected error message in the output 2"
+
+
 run_check_umount_test_dev
--
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] btrfs-progs: Remove duplicate checks from cmd_filesystem_resize
  2021-01-25 10:43 [PATCH 1/2] btrfs-progs: tests: Extend cli/003 Nikolay Borisov
@ 2021-01-25 10:43 ` Nikolay Borisov
  2021-02-19 12:06 ` [PATCH 1/2] btrfs-progs: tests: Extend cli/003 David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: Nikolay Borisov @ 2021-01-25 10:43 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Nikolay Borisov

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 <nborisov@suse.com>
---
 cmds/filesystem.c | 21 +++++++--------------
 common/utils.c    | 16 ++++++++--------
 2 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/cmds/filesystem.c b/cmds/filesystem.c
index ba2e5928cc02..8379fd7a8151 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,15 @@ 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) {
+		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 f7dc320c8915..15fda84ed291 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -185,24 +185,24 @@ int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only)
 	struct stat st;
 	int ret;

-	if (statfs(path, &stfs) != 0) {
+	if (stat(path, &st) != 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 (dir_only && !S_ISDIR(st.st_mode)) {
+		error_on(verbose, "not a directory: %s", path);
+		return -3;
 	}

-	if (stat(path, &st) != 0) {
+	if (statfs(path, &stfs) != 0) {
 		error_on(verbose, "cannot access '%s': %m", path);
 		return -1;
 	}

-	if (dir_only && !S_ISDIR(st.st_mode)) {
-		error_on(verbose, "not a directory: %s", path);
-		return -3;
+	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);
--
2.25.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] btrfs-progs: tests: Extend cli/003
  2021-01-25 10:43 [PATCH 1/2] btrfs-progs: tests: Extend cli/003 Nikolay Borisov
  2021-01-25 10:43 ` [PATCH 2/2] btrfs-progs: Remove duplicate checks from cmd_filesystem_resize Nikolay Borisov
@ 2021-02-19 12:06 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2021-02-19 12:06 UTC (permalink / raw)
  To: Nikolay Borisov; +Cc: linux-btrfs

On Mon, Jan 25, 2021 at 12:43:57PM +0200, Nikolay Borisov wrote:
> Add a test which ensures that when resize is tried on an image instead
> of a directory appropriate warning is produced and the command fails.
> 
> Signed-off-by: Nikolay Borisov <nborisov@suse.com>

Added to devel with some fixups, thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-02-19 12:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-25 10:43 [PATCH 1/2] btrfs-progs: tests: Extend cli/003 Nikolay Borisov
2021-01-25 10:43 ` [PATCH 2/2] btrfs-progs: Remove duplicate checks from cmd_filesystem_resize Nikolay Borisov
2021-02-19 12:06 ` [PATCH 1/2] btrfs-progs: tests: Extend cli/003 David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).