[PATCH v10 1/3] btrfs: add btrfs_strmatch helper

Anand Jain
2020-10-29T02:45:43Z

Add a generic helper to match the golden-string in the given-string,
and ignore the leading and trailing whitespaces if any.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
Suggested-by: David Sterba <dsterba@suse.com>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
---
v10: return bool instead of int.
drop unnecessary local variable and ( ) with in if.
v9: use Josef suggested C coding style, using single if statement.
v5: born
fs/btrfs/sysfs.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)

diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index fcd6c7a9bbd1..5955379d3d9e 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -888,6 +888,24 @@ static ssize_t btrfs_generation_show(struct kobject *kobj,
}
BTRFS_ATTR(, generation, btrfs_generation_show);

+/*
+ * Match the %golden in the %given. Ignore the leading and trailing whitespaces
+ * if any.
+ */
+static bool btrfs_strmatch(const char *given, const char *golden)
+{
+ size_t len = strlen(golden);
+
+ /* skip leading whitespace */
+ given = skip_spaces(given);
+
+ if (strncmp(given, golden, len) == 0 &&
+ !strlen(skip_spaces(given + len)))
+ return true;
+
+ return false;
+}
+
static const struct attribute *btrfs_attrs[] = {
BTRFS_ATTR_PTR(, label),
BTRFS_ATTR_PTR(, nodesize),
--
2.25.1