Blame SOURCES/xfsprogs-5.7.0-xfs_quota-refactor-code-to-generate-id-from-name.patch

48cf7c
From 67a73d6139d0336eb7ced05bd78a26b57f408187 Mon Sep 17 00:00:00 2001
48cf7c
From: Eric Sandeen <sandeen@redhat.com>
48cf7c
Date: Tue, 26 May 2020 14:36:04 -0400
48cf7c
Subject: [PATCH] xfs_quota: refactor code to generate id from name
48cf7c
48cf7c
There's boilerplate for setting limits and warnings, where we have
48cf7c
a case statement for each of the 3 quota types, and from there call
48cf7c
3 different functions to configure each of the 3 types, each of which
48cf7c
calls its own version of id to string function...
48cf7c
48cf7c
Refactor this so that the main function can call a generic id to string
48cf7c
conversion routine, and then call a common action.  This save a lot of
48cf7c
LOC.
48cf7c
48cf7c
I was looking at allowing xfs to bump out individual grace periods like
48cf7c
setquota can do, and this refactoring allows us to add new actions like
48cf7c
that without copying all the boilerplate again.
48cf7c
48cf7c
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
48cf7c
Reviewed-by: Christoph Hellwig <hch@lst.de>
48cf7c
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
48cf7c
---
48cf7c
 quota/edit.c | 194 +++++++++++++--------------------------------------
48cf7c
 1 file changed, 49 insertions(+), 145 deletions(-)
48cf7c
48cf7c
Index: xfsprogs-5.0.0/quota/edit.c
48cf7c
===================================================================
48cf7c
--- xfsprogs-5.0.0.orig/quota/edit.c
48cf7c
+++ xfsprogs-5.0.0/quota/edit.c
48cf7c
@@ -101,6 +101,40 @@ warn_help(void)
48cf7c
 "\n"));
48cf7c
 }
48cf7c
 
48cf7c
+static uint32_t
48cf7c
+id_from_string(
48cf7c
+	char	*name,
48cf7c
+	int	type)
48cf7c
+{
48cf7c
+	uint32_t	id = -1;
48cf7c
+	const char	*type_name = "unknown type";
48cf7c
+
48cf7c
+	switch (type) {
48cf7c
+	case XFS_USER_QUOTA:
48cf7c
+		type_name = "user";
48cf7c
+		id = uid_from_string(name);
48cf7c
+		break;
48cf7c
+	case XFS_GROUP_QUOTA:
48cf7c
+		type_name = "group";
48cf7c
+		id = gid_from_string(name);
48cf7c
+		break;
48cf7c
+	case XFS_PROJ_QUOTA:
48cf7c
+		type_name = "project";
48cf7c
+		id = prid_from_string(name);
48cf7c
+		break;
48cf7c
+	default:
48cf7c
+		ASSERT(0);
48cf7c
+		break;
48cf7c
+	}
48cf7c
+
48cf7c
+	if (id == -1) {
48cf7c
+		fprintf(stderr, _("%s: invalid %s name: %s\n"),
48cf7c
+			type_name, progname, name);
48cf7c
+		exitcode = 1;
48cf7c
+	}
48cf7c
+	return id;
48cf7c
+}
48cf7c
+
48cf7c
 static void
48cf7c
 set_limits(
48cf7c
 	uint32_t	id,
48cf7c
@@ -135,75 +169,6 @@ set_limits(
48cf7c
 	}
48cf7c
 }
48cf7c
 
48cf7c
-static void
48cf7c
-set_user_limits(
48cf7c
-	char		*name,
48cf7c
-	uint		type,
48cf7c
-	uint		mask,
48cf7c
-	uint64_t	*bsoft,
48cf7c
-	uint64_t	*bhard,
48cf7c
-	uint64_t	*isoft,
48cf7c
-	uint64_t	*ihard,
48cf7c
-	uint64_t	*rtbsoft,
48cf7c
-	uint64_t	*rtbhard)
48cf7c
-{
48cf7c
-	uid_t		uid = uid_from_string(name);
48cf7c
-
48cf7c
-	if (uid == -1) {
48cf7c
-		exitcode = 1;
48cf7c
-		fprintf(stderr, _("%s: invalid user name: %s\n"),
48cf7c
-				progname, name);
48cf7c
-	} else
48cf7c
-		set_limits(uid, type, mask, fs_path->fs_name,
48cf7c
-				bsoft, bhard, isoft, ihard, rtbsoft, rtbhard);
48cf7c
-}
48cf7c
-
48cf7c
-static void
48cf7c
-set_group_limits(
48cf7c
-	char		*name,
48cf7c
-	uint		type,
48cf7c
-	uint		mask,
48cf7c
-	uint64_t	*bsoft,
48cf7c
-	uint64_t	*bhard,
48cf7c
-	uint64_t	*isoft,
48cf7c
-	uint64_t	*ihard,
48cf7c
-	uint64_t	*rtbsoft,
48cf7c
-	uint64_t	*rtbhard)
48cf7c
-{
48cf7c
-	gid_t		gid = gid_from_string(name);
48cf7c
-
48cf7c
-	if (gid == -1) {
48cf7c
-		exitcode = 1;
48cf7c
-		fprintf(stderr, _("%s: invalid group name: %s\n"),
48cf7c
-				progname, name);
48cf7c
-	} else
48cf7c
-		set_limits(gid, type, mask, fs_path->fs_name,
48cf7c
-				bsoft, bhard, isoft, ihard, rtbsoft, rtbhard);
48cf7c
-}
48cf7c
-
48cf7c
-static void
48cf7c
-set_project_limits(
48cf7c
-	char		*name,
48cf7c
-	uint		type,
48cf7c
-	uint		mask,
48cf7c
-	uint64_t	*bsoft,
48cf7c
-	uint64_t	*bhard,
48cf7c
-	uint64_t	*isoft,
48cf7c
-	uint64_t	*ihard,
48cf7c
-	uint64_t	*rtbsoft,
48cf7c
-	uint64_t	*rtbhard)
48cf7c
-{
48cf7c
-	prid_t		prid = prid_from_string(name);
48cf7c
-
48cf7c
-	if (prid == -1) {
48cf7c
-		exitcode = 1;
48cf7c
-		fprintf(stderr, _("%s: invalid project name: %s\n"),
48cf7c
-				progname, name);
48cf7c
-	} else
48cf7c
-		set_limits(prid, type, mask, fs_path->fs_name,
48cf7c
-				bsoft, bhard, isoft, ihard, rtbsoft, rtbhard);
48cf7c
-}
48cf7c
-
48cf7c
 /* extract number of blocks from an ascii string */
48cf7c
 static int
48cf7c
 extractb(
48cf7c
@@ -258,6 +223,7 @@ limit_f(
48cf7c
 	char		**argv)
48cf7c
 {
48cf7c
 	char		*name;
48cf7c
+	uint32_t	id;
48cf7c
 	uint64_t	bsoft, bhard, isoft, ihard, rtbsoft, rtbhard;
48cf7c
 	int		c, type = 0, mask = 0, flags = 0;
48cf7c
 	uint		bsize, ssize, endoptions;
48cf7c
@@ -339,20 +305,13 @@ limit_f(
48cf7c
 		return command_usage(&limit_cmd);
48cf7c
 	}
48cf7c
 
48cf7c
-	switch (type) {
48cf7c
-	case XFS_USER_QUOTA:
48cf7c
-		set_user_limits(name, type, mask,
48cf7c
-			&bsoft, &bhard, &isoft, &ihard, &rtbsoft, &rtbhard);
48cf7c
-		break;
48cf7c
-	case XFS_GROUP_QUOTA:
48cf7c
-		set_group_limits(name, type, mask,
48cf7c
-			&bsoft, &bhard, &isoft, &ihard, &rtbsoft, &rtbhard);
48cf7c
-		break;
48cf7c
-	case XFS_PROJ_QUOTA:
48cf7c
-		set_project_limits(name, type, mask,
48cf7c
-			&bsoft, &bhard, &isoft, &ihard, &rtbsoft, &rtbhard);
48cf7c
-		break;
48cf7c
-	}
48cf7c
+
48cf7c
+	id = id_from_string(name, type);
48cf7c
+	if (id >= 0)
48cf7c
+		set_limits(id, type, mask, fs_path->fs_name,
48cf7c
+			   &bsoft, &bhard, &isoft, &ihard, &rtbsoft, &rtbhard);
48cf7c
+	else
48cf7c
+		exitcode = -1;
48cf7c
 	return 0;
48cf7c
 }
48cf7c
 
48cf7c
@@ -561,63 +520,13 @@ set_warnings(
48cf7c
 	}
48cf7c
 }
48cf7c
 
48cf7c
-static void
48cf7c
-set_user_warnings(
48cf7c
-	char		*name,
48cf7c
-	uint		type,
48cf7c
-	uint		mask,
48cf7c
-	uint		value)
48cf7c
-{
48cf7c
-	uid_t		uid = uid_from_string(name);
48cf7c
-
48cf7c
-	if (uid == -1) {
48cf7c
-		exitcode = 1;
48cf7c
-		fprintf(stderr, _("%s: invalid user name: %s\n"),
48cf7c
-				progname, name);
48cf7c
-	} else
48cf7c
-		set_warnings(uid, type, mask, fs_path->fs_name, value);
48cf7c
-}
48cf7c
-
48cf7c
-static void
48cf7c
-set_group_warnings(
48cf7c
-	char		*name,
48cf7c
-	uint		type,
48cf7c
-	uint		mask,
48cf7c
-	uint		value)
48cf7c
-{
48cf7c
-	gid_t		gid = gid_from_string(name);
48cf7c
-
48cf7c
-	if (gid == -1) {
48cf7c
-		exitcode = 1;
48cf7c
-		fprintf(stderr, _("%s: invalid group name: %s\n"),
48cf7c
-				progname, name);
48cf7c
-	} else
48cf7c
-		set_warnings(gid, type, mask, fs_path->fs_name, value);
48cf7c
-}
48cf7c
-
48cf7c
-static void
48cf7c
-set_project_warnings(
48cf7c
-	char		*name,
48cf7c
-	uint		type,
48cf7c
-	uint		mask,
48cf7c
-	uint		value)
48cf7c
-{
48cf7c
-	prid_t		prid = prid_from_string(name);
48cf7c
-
48cf7c
-	if (prid == -1) {
48cf7c
-		exitcode = 1;
48cf7c
-		fprintf(stderr, _("%s: invalid project name: %s\n"),
48cf7c
-				progname, name);
48cf7c
-	} else
48cf7c
-		set_warnings(prid, type, mask, fs_path->fs_name, value);
48cf7c
-}
48cf7c
-
48cf7c
 static int
48cf7c
 warn_f(
48cf7c
 	int		argc,
48cf7c
 	char		**argv)
48cf7c
 {
48cf7c
 	char		*name;
48cf7c
+	uint32_t	id;
48cf7c
 	uint		value;
48cf7c
 	int		c, flags = 0, type = 0, mask = 0;
48cf7c
 
48cf7c
@@ -675,17 +584,12 @@ warn_f(
48cf7c
 		return command_usage(&warn_cmd);
48cf7c
 	}
48cf7c
 
48cf7c
-	switch (type) {
48cf7c
-	case XFS_USER_QUOTA:
48cf7c
-		set_user_warnings(name, type, mask, value);
48cf7c
-		break;
48cf7c
-	case XFS_GROUP_QUOTA:
48cf7c
-		set_group_warnings(name, type, mask, value);
48cf7c
-		break;
48cf7c
-	case XFS_PROJ_QUOTA:
48cf7c
-		set_project_warnings(name, type, mask, value);
48cf7c
-		break;
48cf7c
-	}
48cf7c
+	id = id_from_string(name, type);
48cf7c
+	if (id >= 0)
48cf7c
+		set_warnings(id, type, mask, fs_path->fs_name, value);
48cf7c
+	else
48cf7c
+		exitcode = -1;
48cf7c
+
48cf7c
 	return 0;
48cf7c
 }
48cf7c