diff mbox series

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

Message ID 75264e50d49f3c68cc14dc87510c8f3767390dcf.1603347462.git.anand.jain@oracle.com
State New, archived
Headers show
Series readmirror feature (read_policy sysfs and in-memory only approach) | expand

Commit Message

Anand Jain Oct. 22, 2020, 7:43 a.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>
---
v9: use Josef suggested C coding style, using single if statement.
v5: born

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

Comments

Christoph Hellwig Oct. 23, 2020, 11:12 a.m. UTC | #1
> +	if ((strncmp(stripped, golden, len) == 0) &&
> +	    (strlen(skip_spaces(stripped + len)) == 0))

No need for the inner braces.
David Sterba Oct. 26, 2020, 5:52 p.m. UTC | #2
On Thu, Oct 22, 2020 at 03:43:35PM +0800, 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>
> ---
> v9: use Josef suggested C coding style, using single if statement.
> v5: born
> 
>  fs/btrfs/sysfs.c | 20 ++++++++++++++++++++
>  1 file changed, 20 insertions(+)
> 
> diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
> index 8424f5d0e5ed..5ea262d289c6 100644
> --- a/fs/btrfs/sysfs.c
> +++ b/fs/btrfs/sysfs.c
> @@ -863,6 +863,26 @@ 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 */

This is confusing as it's not stripping the space but merely skipping
it.  The arguments are not changed so you also don't need the separate
variable and just update 'given'.

> +	stripped = skip_spaces(given);
> +
> +	/* strip trailing whitespace */
> +	if ((strncmp(stripped, golden, len) == 0) &&
> +	    (strlen(skip_spaces(stripped + len)) == 0))
> +		return 0;

This a bit hard to read but ok, essentially we can do the string
comparison in a loop or use the library functions.

> +
> +	return -EINVAL;

This does not make sense as it's an error code while the function is a
predicate, without error states.

> +}
> +
>  static const struct attribute *btrfs_attrs[] = {
>  	BTRFS_ATTR_PTR(, label),
>  	BTRFS_ATTR_PTR(, nodesize),
> -- 
> 2.25.1
Anand Jain Oct. 27, 2020, 1:19 p.m. UTC | #3
>> +static int btrfs_strmatch(const char *given, const char *golden)
>> +{
>> +	size_t len = strlen(golden);
>> +	char *stripped;
>> +
>> +	/* strip leading whitespace */
> 
> This is confusing as it's not stripping the space but merely skipping
> it.  The arguments are not changed so you also don't need the separate
> variable and just update 'given'.

  ok let me update it.

> 
>> +	stripped = skip_spaces(given);
>> +
>> +	/* strip trailing whitespace */
>> +	if ((strncmp(stripped, golden, len) == 0) &&
>> +	    (strlen(skip_spaces(stripped + len)) == 0))
>> +		return 0;
> 
> This a bit hard to read but ok, essentially we can do the string
> comparison in a loop or use the library functions.
>

>> +
>> +	return -EINVAL;
> 
> This does not make sense as it's an error code while the function is a
> predicate, without error states.
> 

  A non zero value return will be some arbitrary number, is that OK?
  Or the return arg can be bool instead of int.
Anand Jain Oct. 27, 2020, 1:25 p.m. UTC | #4
On 23/10/20 7:12 pm, Christoph Hellwig wrote:
>> +	if ((strncmp(stripped, golden, len) == 0) &&
>> +	    (strlen(skip_spaces(stripped + len)) == 0))
> 
> No need for the inner braces.
> 

fixed in next version. Thanks.
David Sterba Oct. 27, 2020, 6 p.m. UTC | #5
On Tue, Oct 27, 2020 at 09:19:33PM +0800, Anand Jain wrote:
> >> +	stripped = skip_spaces(given);
> >> +
> >> +	/* strip trailing whitespace */
> >> +	if ((strncmp(stripped, golden, len) == 0) &&
> >> +	    (strlen(skip_spaces(stripped + len)) == 0))
> >> +		return 0;
> > 
> > This a bit hard to read but ok, essentially we can do the string
> > comparison in a loop or use the library functions.
> >
> 
> >> +
> >> +	return -EINVAL;
> > 
> > This does not make sense as it's an error code while the function is a
> > predicate, without error states.
> > 
> 
>   A non zero value return will be some arbitrary number, is that OK?
>   Or the return arg can be bool instead of int.

Bool is fine in this case.
diff mbox series

Patch

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