diff mbox series

[v8,1/3] btrfs: add btrfs_strmatch helper

Message ID 7418ae34e5a4cc7d110eed756d9e37c5630ec568.1602756068.git.anand.jain@oracle.com (mailing list archive)
State New, archived
Headers show
Series readmirror feature (read_policy sysfs and | expand

Commit Message

Anand Jain Oct. 20, 2020, 2:02 p.m. UTC
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>
---
v5: born

 fs/btrfs/sysfs.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

Comments

Josef Bacik Oct. 21, 2020, 2:44 p.m. UTC | #1
On 10/20/20 10:02 AM, Anand Jain wrote:
> 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>
> ---
> v5: born
> 
>   fs/btrfs/sysfs.c | 23 +++++++++++++++++++++++
>   1 file changed, 23 insertions(+)
> 
> diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
> index 8424f5d0e5ed..eb0b2bfcce67 100644
> --- a/fs/btrfs/sysfs.c
> +++ b/fs/btrfs/sysfs.c
> @@ -863,6 +863,29 @@ 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 int btrfs_strmatch(const char *given, const char *golden)
> +{
> +	size_t len = strlen(golden);
> +	char *stripped;
> +
> +	/* strip leading whitespace */
> +	stripped = skip_spaces(given);
> +
> +	if (strncmp(stripped, golden, len) == 0) {
> +		/* strip trailing whitespace */
> +		if (strlen(skip_spaces(stripped + len)))
> +			return -EINVAL;

if ((strncmp(stripped, golden, len) == 0) &&
     (strlen(skip_spaces(stripped + len)) == 0))
	return 0;

Thanks,

Josef
Anand Jain Oct. 22, 2020, 7:40 a.m. UTC | #2
On 21/10/20 10:44 pm, Josef Bacik wrote:
> On 10/20/20 10:02 AM, Anand Jain wrote:
>> 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>
>> ---
>> v5: born
>>
>>   fs/btrfs/sysfs.c | 23 +++++++++++++++++++++++
>>   1 file changed, 23 insertions(+)
>>
>> diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
>> index 8424f5d0e5ed..eb0b2bfcce67 100644
>> --- a/fs/btrfs/sysfs.c
>> +++ b/fs/btrfs/sysfs.c
>> @@ -863,6 +863,29 @@ 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 int btrfs_strmatch(const char *given, const char *golden)
>> +{
>> +    size_t len = strlen(golden);
>> +    char *stripped;
>> +
>> +    /* strip leading whitespace */
>> +    stripped = skip_spaces(given);
>> +
>> +    if (strncmp(stripped, golden, len) == 0) {
>> +        /* strip trailing whitespace */
>> +        if (strlen(skip_spaces(stripped + len)))
>> +            return -EINVAL;
> 
> if ((strncmp(stripped, golden, len) == 0) &&
>      (strlen(skip_spaces(stripped + len)) == 0))
>      return 0;

fixed in v9 as well.

Thanks, Anand

> 
> Thanks,
> 
> Josef
diff mbox series

Patch

diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index 8424f5d0e5ed..eb0b2bfcce67 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -863,6 +863,29 @@  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 int btrfs_strmatch(const char *given, const char *golden)
+{
+	size_t len = strlen(golden);
+	char *stripped;
+
+	/* strip leading whitespace */
+	stripped = skip_spaces(given);
+
+	if (strncmp(stripped, golden, len) == 0) {
+		/* strip trailing whitespace */
+		if (strlen(skip_spaces(stripped + len)))
+			return -EINVAL;
+
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
 static const struct attribute *btrfs_attrs[] = {
 	BTRFS_ATTR_PTR(, label),
 	BTRFS_ATTR_PTR(, nodesize),