1 Star 0 Fork 19

庞庆/xfsprogs

forked from src-anolis-os/xfsprogs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
xfsprogs-5.7.0-xfs_quota-refactor-code-to-generate-id-from-name.patch 5.86 KB
一键复制 编辑 原始数据 按行查看 历史
geliwei 提交于 2022-04-13 15:46 . update to xfsprogs-5.0.0-10.el8.src.rpm
From 67a73d6139d0336eb7ced05bd78a26b57f408187 Mon Sep 17 00:00:00 2001
From: Eric Sandeen <sandeen@redhat.com>
Date: Tue, 26 May 2020 14:36:04 -0400
Subject: [PATCH] xfs_quota: refactor code to generate id from name
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 copying all the boilerplate again.
Signed-off-by: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
---
quota/edit.c | 194 +++++++++++++--------------------------------------
1 file changed, 49 insertions(+), 145 deletions(-)
Index: xfsprogs-5.0.0/quota/edit.c
===================================================================
--- xfsprogs-5.0.0.orig/quota/edit.c
+++ xfsprogs-5.0.0/quota/edit.c
@@ -101,6 +101,40 @@ warn_help(void)
"\n"));
}
+static uint32_t
+id_from_string(
+ char *name,
+ int type)
+{
+ uint32_t id = -1;
+ const char *type_name = "unknown type";
+
+ switch (type) {
+ case XFS_USER_QUOTA:
+ type_name = "user";
+ id = uid_from_string(name);
+ break;
+ case XFS_GROUP_QUOTA:
+ type_name = "group";
+ id = gid_from_string(name);
+ break;
+ case XFS_PROJ_QUOTA:
+ type_name = "project";
+ id = prid_from_string(name);
+ break;
+ default:
+ ASSERT(0);
+ break;
+ }
+
+ if (id == -1) {
+ fprintf(stderr, _("%s: invalid %s name: %s\n"),
+ type_name, progname, name);
+ exitcode = 1;
+ }
+ return id;
+}
+
static void
set_limits(
uint32_t id,
@@ -135,75 +169,6 @@ set_limits(
}
}
-static void
-set_user_limits(
- char *name,
- uint type,
- uint mask,
- uint64_t *bsoft,
- uint64_t *bhard,
- uint64_t *isoft,
- uint64_t *ihard,
- uint64_t *rtbsoft,
- uint64_t *rtbhard)
-{
- uid_t uid = uid_from_string(name);
-
- if (uid == -1) {
- exitcode = 1;
- fprintf(stderr, _("%s: invalid user name: %s\n"),
- progname, name);
- } else
- set_limits(uid, type, mask, fs_path->fs_name,
- bsoft, bhard, isoft, ihard, rtbsoft, rtbhard);
-}
-
-static void
-set_group_limits(
- char *name,
- uint type,
- uint mask,
- uint64_t *bsoft,
- uint64_t *bhard,
- uint64_t *isoft,
- uint64_t *ihard,
- uint64_t *rtbsoft,
- uint64_t *rtbhard)
-{
- gid_t gid = gid_from_string(name);
-
- if (gid == -1) {
- exitcode = 1;
- fprintf(stderr, _("%s: invalid group name: %s\n"),
- progname, name);
- } else
- set_limits(gid, type, mask, fs_path->fs_name,
- bsoft, bhard, isoft, ihard, rtbsoft, rtbhard);
-}
-
-static void
-set_project_limits(
- char *name,
- uint type,
- uint mask,
- uint64_t *bsoft,
- uint64_t *bhard,
- uint64_t *isoft,
- uint64_t *ihard,
- uint64_t *rtbsoft,
- uint64_t *rtbhard)
-{
- prid_t prid = prid_from_string(name);
-
- if (prid == -1) {
- exitcode = 1;
- fprintf(stderr, _("%s: invalid project name: %s\n"),
- progname, name);
- } else
- set_limits(prid, type, mask, fs_path->fs_name,
- bsoft, bhard, isoft, ihard, rtbsoft, rtbhard);
-}
-
/* extract number of blocks from an ascii string */
static int
extractb(
@@ -258,6 +223,7 @@ limit_f(
char **argv)
{
char *name;
+ uint32_t id;
uint64_t bsoft, bhard, isoft, ihard, rtbsoft, rtbhard;
int c, type = 0, mask = 0, flags = 0;
uint bsize, ssize, endoptions;
@@ -339,20 +305,13 @@ limit_f(
return command_usage(&limit_cmd);
}
- switch (type) {
- case XFS_USER_QUOTA:
- set_user_limits(name, type, mask,
- &bsoft, &bhard, &isoft, &ihard, &rtbsoft, &rtbhard);
- break;
- case XFS_GROUP_QUOTA:
- set_group_limits(name, type, mask,
- &bsoft, &bhard, &isoft, &ihard, &rtbsoft, &rtbhard);
- break;
- case XFS_PROJ_QUOTA:
- set_project_limits(name, type, mask,
- &bsoft, &bhard, &isoft, &ihard, &rtbsoft, &rtbhard);
- break;
- }
+
+ id = id_from_string(name, type);
+ if (id >= 0)
+ set_limits(id, type, mask, fs_path->fs_name,
+ &bsoft, &bhard, &isoft, &ihard, &rtbsoft, &rtbhard);
+ else
+ exitcode = -1;
return 0;
}
@@ -561,63 +520,13 @@ set_warnings(
}
}
-static void
-set_user_warnings(
- char *name,
- uint type,
- uint mask,
- uint value)
-{
- uid_t uid = uid_from_string(name);
-
- if (uid == -1) {
- exitcode = 1;
- fprintf(stderr, _("%s: invalid user name: %s\n"),
- progname, name);
- } else
- set_warnings(uid, type, mask, fs_path->fs_name, value);
-}
-
-static void
-set_group_warnings(
- char *name,
- uint type,
- uint mask,
- uint value)
-{
- gid_t gid = gid_from_string(name);
-
- if (gid == -1) {
- exitcode = 1;
- fprintf(stderr, _("%s: invalid group name: %s\n"),
- progname, name);
- } else
- set_warnings(gid, type, mask, fs_path->fs_name, value);
-}
-
-static void
-set_project_warnings(
- char *name,
- uint type,
- uint mask,
- uint value)
-{
- prid_t prid = prid_from_string(name);
-
- if (prid == -1) {
- exitcode = 1;
- fprintf(stderr, _("%s: invalid project name: %s\n"),
- progname, name);
- } else
- set_warnings(prid, type, mask, fs_path->fs_name, value);
-}
-
static int
warn_f(
int argc,
char **argv)
{
char *name;
+ uint32_t id;
uint value;
int c, flags = 0, type = 0, mask = 0;
@@ -675,17 +584,12 @@ warn_f(
return command_usage(&warn_cmd);
}
- switch (type) {
- case XFS_USER_QUOTA:
- set_user_warnings(name, type, mask, value);
- break;
- case XFS_GROUP_QUOTA:
- set_group_warnings(name, type, mask, value);
- break;
- case XFS_PROJ_QUOTA:
- set_project_warnings(name, type, mask, value);
- break;
- }
+ id = id_from_string(name, type);
+ if (id >= 0)
+ set_warnings(id, type, mask, fs_path->fs_name, value);
+ else
+ exitcode = -1;
+
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/pang-qing/xfsprogs.git
git@gitee.com:pang-qing/xfsprogs.git
pang-qing
xfsprogs
xfsprogs
a8

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385