Message ID | 75264e50d49f3c68cc14dc87510c8f3767390dcf.1603347462.git.anand.jain@oracle.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | readmirror feature (read_policy sysfs and in-memory only approach) | expand |
> + if ((strncmp(stripped, golden, len) == 0) && > + (strlen(skip_spaces(stripped + len)) == 0)) No need for the inner braces.
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
>> +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.
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.
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 --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),
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(+)