diff mbox series

[4/4] btrfs-progs: 'btrfs fi label' using a dev instead of a path

Message ID d8029a3418079187b092a0542deb1003e24a9355.1701891165.git.kreijack@inwind.it (mailing list archive)
State New, archived
Headers show
Series RFC: add the btrfs_info helper | expand

Commit Message

Goffredo Baroncelli Dec. 6, 2023, 7:32 p.m. UTC
From: Goffredo Baroncelli <kreijack@inwind.it>

'btrfs fi label' has a strange behaviour: when the filesystem
is unmounted, it has to be invoked against a device.
When the filesystem is mounted, it has to be invoked against
a filesystem path.

Even tough it has a technical explanation, this may confuses
the users.

This patch allow to pass a device even in the latter cases.
Alternatevely, it is possible to pass a UUID=xxx or a
LABEL=zzz identification.

Signed-off-by: Goffredo Baroncelli <kreijack@libero.it>
---
 common/filesystem-utils.c | 51 ++++++++++++++++++++++++++++++---------
 1 file changed, 39 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/common/filesystem-utils.c b/common/filesystem-utils.c
index 8c159365..655f4c72 100644
--- a/common/filesystem-utils.c
+++ b/common/filesystem-utils.c
@@ -29,6 +29,7 @@ 
 #include "common/messages.h"
 #include "common/open-utils.h"
 #include "common/path-utils.h"
+#include "common/btrfs-info.h"
 
 /*
  * For a given:
@@ -189,31 +190,57 @@  int get_label_mounted(const char *mount_path, char *labelp)
 	return 0;
 }
 
-int get_label(const char *btrfs_dev, char *label)
+int get_label(const char *path, char *label)
 {
 	int ret;
+	const struct btrfs_info *bi;
 
-	ret = path_is_reg_or_block_device(btrfs_dev);
-	if (!ret)
-		ret = get_label_mounted(btrfs_dev, label);
-	else if (ret > 0)
-		ret = get_label_unmounted(btrfs_dev, label);
+	ret = btrfs_info_find(path, &bi);
+	if (ret == -ENOENT) {
+		error("'%s' is not a btrfs filesystem", path);
+		return -ENOENT;
+	}
+	if (ret < 0) {
+		error("unable to access btrfs filesystem info");
+		return -EINVAL;
+	}
+
+	if (btrfs_info_is_mounted(bi)) {
+		path = btrfs_info_find_valid_path(bi);
+		ret = get_label_mounted(path, label);
+	} else {
+		path = btrfs_info_find_valid_dev(bi);
+		ret = get_label_unmounted(path, label);
+	}
 
 	return ret;
 }
 
-int set_label(const char *btrfs_dev, const char *label)
+int set_label(const char *path, const char *label)
 {
 	int ret;
+	const struct btrfs_info *bi;
 
 	if (check_label(label))
 		return -1;
 
-	ret = path_is_reg_or_block_device(btrfs_dev);
-	if (!ret)
-		ret = set_label_mounted(btrfs_dev, label);
-	else if (ret > 0)
-		ret = set_label_unmounted(btrfs_dev, label);
+	ret = btrfs_info_find(path, &bi);
+	if (ret == -ENOENT) {
+		error("'%s' is not a btrfs filesystem", path);
+		return -ENOENT;
+	}
+	if (ret < 0) {
+		error("unable to access btrfs filesystem info");
+		return -EINVAL;
+	}
+
+	if (btrfs_info_is_mounted(bi)) {
+		path = btrfs_info_find_valid_path(bi);
+		ret = set_label_mounted(path, label);
+	} else {
+		path = btrfs_info_find_valid_dev(bi);
+		ret = set_label_unmounted(path, label);
+	}
 
 	return ret;
 }