From: Christoph Hellwig <hch@infradead.org>
To: Eric Sandeen <sandeen@redhat.com>
Cc: linux-xfs <linux-xfs@vger.kernel.org>
Subject: Re: [PATCH] xfs_quota: refactor code to generate id from name
Date: Mon, 11 May 2020 08:32:49 -0700 [thread overview]
Message-ID: <20200511153249.GA11320@infradead.org> (raw)
In-Reply-To: <8b4b7edb-94b2-3bb1-9ede-73674db82330@redhat.com>
On Sat, May 09, 2020 at 12:18:42PM -0500, Eric Sandeen wrote:
> There's boilerplate for setting limits and warnings, where we have
> a case statement for each of the 3 quota types, and from there call
> 3 different functions to configure each of the 3 types, each of which
> calls its own version of id to string function...
>
> Refactor this so that the main function can call a generic id to string
> conversion routine, and then call a common action. This save a lot of
> LOC.
>
> I was looking at allowing xfs to bump out individual grace periods like
> setquota can do, and this refactoring allows us to add new actions like
> that without copyingall the boilerplate again.
>
> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> ---
>
> edit.c | 196 ++++++++++++++++-----------------------------------------
> 1 file changed, 51 insertions(+), 145 deletions(-)
>
> diff --git a/quota/edit.c b/quota/edit.c
> index f9938b8a..70c0969f 100644
> --- a/quota/edit.c
> +++ b/quota/edit.c
> @@ -101,6 +101,42 @@ warn_help(void)
> "\n"));
> }
>
> +static uint32_t
> +id_from_string(
> + char *name,
> + int type)
> +{
> + uint32_t id = -1;
> +
> + switch (type) {
> + case XFS_USER_QUOTA:
> + id = uid_from_string(name);
> + if (id == -1)
> + fprintf(stderr, _("%s: invalid user name: %s\n"),
> + progname, name);
> + break;
> + case XFS_GROUP_QUOTA:
> + id = gid_from_string(name);
> + if (id == -1)
> + fprintf(stderr, _("%s: invalid group name: %s\n"),
> + progname, name);
> + break;
> + case XFS_PROJ_QUOTA:
> + id = prid_from_string(name);
> + if (id == -1)
> + fprintf(stderr, _("%s: invalid project name: %s\n"),
> + progname, name);
> + break;
> + default:
> + ASSERT(0);
> + break;
> + }
> +
> + if (id == -1)
> + exitcode = 1;
> + return id;
What about de-duplicating the error printk as well?
static uint32_t
id_from_string(
char *name,
int type)
{
uint32_t id = -1;
const char *type = "invalid";
switch (type) {
case XFS_USER_QUOTA:
type = "user";
id = uid_from_string(name);
break;
case XFS_GROUP_QUOTA:
type = "group";
id = gid_from_string(name);
break;
case XFS_PROJ_QUOTA:
type = "project";
id = prid_from_string(name);
break;
default:
ASSERT(0);
break;
}
if (id == -1) {
fprintf(stderr, _("%s: invalid %s name: %s\n"),
type, progname, name);
exitcode = 1;
}
return id;
}
next prev parent reply other threads:[~2020-05-11 15:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-09 17:18 [PATCH] xfs_quota: refactor code to generate id from name Eric Sandeen
2020-05-11 15:32 ` Christoph Hellwig [this message]
2020-05-11 16:00 ` Eric Sandeen
2020-05-11 19:08 ` [PATCH V2] " Eric Sandeen
2020-05-12 8:09 ` Christoph Hellwig
2020-05-12 15:20 ` Eric Sandeen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200511153249.GA11320@infradead.org \
--to=hch@infradead.org \
--cc=linux-xfs@vger.kernel.org \
--cc=sandeen@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox