Skip to contentSkip to conversation

xfs_quota: enable id->name lookups with lower/upper bounds

PATCH New Eric Sandeen 2 participants 3 messages Dec 16, 2015

lore
Share & tools
lore
#0 lore raw

cover xfs_quota: enable id->name lookups with lower/upper bounds

Today, xfs_quota -x -c "report -L 0 -U 100" will report on any ids with quotas within the range, but the output only contains the ids, not the names of the user, group, or project.

This patchset adds an "-l" flag to tell xfs_quota to do the id->name lookup for all found quotas.

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#1 lore raw

PATCH 1/2 xfs_quota: push id/name printing down into report_mount()

Push the printing of the id number and/or name during quota reporting down into report_mount(), rather than doing it in the caller.

This lets the next patch do a lookup of any ID found with a quota, when upper/lower ID bounds are specified. This patch, however, changes no behavior. Both the ID and the name are passed in, and which one gets printed depends on the presence of the NO_LOOKUP_FLAG.

If "NULL" is passed in (as in the upper/lower ID search case), then as before, only the ID is printed. Next patch changes this.

Drops a few lines of code as well.

---

quota/report.c +11 -26
index 3d84447..55e44c5 100644
@@ -322,7 +322,11 @@ report_mount(
 	if (!(flags & NO_HEADER_FLAG))
 		report_header(fp, form, type, mount, flags);
 
-	fprintf(fp, "%-10s", name);
+	if ((name == NULL) || (flags & NO_LOOKUP_FLAG))
+		fprintf(fp, "#%-10u", id);
+	else
+		fprintf(fp, "%-10s", name);
+
 	if (form & XFS_BLOCK_QUOTA) {
 		qflags = (flags & HUMAN_FLAG);
 		if (d.d_blk_hardlimit && d.d_bcount > d.d_blk_hardlimit)
@@ -400,24 +404,18 @@ report_user_mount(
 	uint		flags)
 {
 	struct passwd	*u;
-	char		n[NMAX];
 	uint		id;
 
 	if (upper) {	/* identifier range specified */
 		for (id = lower; id <= upper; id++) {
-			snprintf(n, sizeof(n)-1, "#%u", id);
-			if (report_mount(fp, id, n,
+			if (report_mount(fp, id, NULL,
 					form, XFS_USER_QUOTA, mount, flags))
 				flags |= NO_HEADER_FLAG;
 		}
 	} else {
 		setpwent();
 		while ((u = getpwent()) != NULL) {
-			if (flags & NO_LOOKUP_FLAG)
-				snprintf(n, sizeof(n)-1, "#%u", u->pw_uid);
-			else
-				strncpy(n, u->pw_name, sizeof(n)-1);
-			if (report_mount(fp, u->pw_uid, n,
+			if (report_mount(fp, u->pw_uid, u->pw_name,
 					form, XFS_USER_QUOTA, mount, flags))
 				flags |= NO_HEADER_FLAG;
 		}
@@ -438,24 +436,18 @@ report_group_mount(
 	uint		flags)
 {
 	struct group	*g;
-	char		n[NMAX];
 	uint		id;
 
 	if (upper) {	/* identifier range specified */
 		for (id = lower; id <= upper; id++) {
-			snprintf(n, sizeof(n)-1, "#%u", id);
-			if (report_mount(fp, id, n,
+			if (report_mount(fp, id, NULL,
 					form, XFS_GROUP_QUOTA, mount, flags))
 				flags |= NO_HEADER_FLAG;
 		}
 	} else {
 		setgrent();
 		while ((g = getgrent()) != NULL) {
-			if (flags & NO_LOOKUP_FLAG)
-				snprintf(n, sizeof(n)-1, "#%u", g->gr_gid);
-		else
-			strncpy(n, g->gr_name, sizeof(n)-1);
-			if (report_mount(fp, g->gr_gid, n,
+			if (report_mount(fp, g->gr_gid, g->gr_name,
 					form, XFS_GROUP_QUOTA, mount, flags))
 				flags |= NO_HEADER_FLAG;
 		}
@@ -475,25 +467,18 @@ report_project_mount(
 	uint		flags)
 {
 	fs_project_t	*p;
-	char		n[NMAX];
 	uint		id;
 
 	if (upper) {	/* identifier range specified */
 		for (id = lower; id <= upper; id++) {
-			snprintf(n, sizeof(n)-1, "#%u", id);
-			if (report_mount(fp, id, n,
+			if (report_mount(fp, id, NULL,
 					form, XFS_PROJ_QUOTA, mount, flags))
 				flags |= NO_HEADER_FLAG;
 		}
 	} else {
 		setprent();
 		while ((p = getprent()) != NULL) {
-			if (flags & NO_LOOKUP_FLAG)
-				snprintf(n, sizeof(n)-1, "#%u",
-					 (unsigned int)p->pr_prid);
-			else
-				strncpy(n, p->pr_name, sizeof(n)-1);
-			if (report_mount(fp, p->pr_prid, n,
+			if (report_mount(fp, p->pr_prid, p->pr_name,
 					form, XFS_PROJ_QUOTA, mount, flags))
 				flags |= NO_HEADER_FLAG;
 		}

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

#2 lore raw

PATCH 2/2 xfs_quota: allow name lookup when reporting from ID range

In reply to Eric Sandeen

Add a new "-l" (lookup) argument to the "report" command, to enable id->name lookups when doing a report across a lower/upper ID range as specified by the -L / -U report options.

---

man/man8/xfs_quota.8 +6 -2
index 9b555e9..3bee145 100644
@@ -335,7 +335,7 @@ file.
 ] [
 .B \-bir
 ] [
-.B \-ahntLNU
+.B \-ahntlLNU
 ] [
 .B \-f
 .I file
@@ -363,7 +363,11 @@ option outputs the numeric ID instead of the name. The
 .B \-L
 and
 .B \-U
-options specify lower and upper ID bounds to report on. The
+options specify lower and upper ID bounds to report on.  If upper/lower
+bounds are specified, then by default only the IDs will be displayed
+in output; with the
+.B \-l
+option, a lookup will be performed to translate these IDs to names. The
 .B \-N
 option reports information without the header line. The
 .B \-t
quota/report.c +30 -4
index 55e44c5..c77b24f 100644
@@ -45,7 +45,7 @@ dump_help(void)
 static void
 report_help(void)
 {
-	report_cmd.args = _("[-bir] [-gpu] [-ahntLNU] [-f file]");
+	report_cmd.args = _("[-bir] [-gpu] [-ahntlLNU] [-f file]");
 	report_cmd.oneline = _("report filesystem quota information");
 	printf(_(
 "\n"
@@ -63,6 +63,7 @@ report_help(void)
 " -t -- terse output format, hides rows which are all zero\n"
 " -L -- lower ID bound to report on\n"
 " -U -- upper ID bound to report on\n"
+" -l -- look up names for IDs in lower-upper range\n"
 " -g -- report group usage and quota information\n"
 " -p -- report project usage and quota information\n"
 " -u -- report user usage and quota information\n"
@@ -322,10 +323,26 @@ report_mount(
 	if (!(flags & NO_HEADER_FLAG))
 		report_header(fp, form, type, mount, flags);
 
-	if ((name == NULL) || (flags & NO_LOOKUP_FLAG))
+	if (flags & NO_LOOKUP_FLAG) {
 		fprintf(fp, "#%-10u", id);
-	else
+	} else {
+		if (name == NULL) {
+			if (type == XFS_USER_QUOTA) {
+				struct passwd	*u = getpwuid(id);
+				if (u)
+					name = u->pw_name;
+			} else if (type == XFS_GROUP_QUOTA) {
+				struct group	*g = getgrgid(id);
+				if (g)
+					name = g->gr_name;
+			} else if (type == XFS_PROJ_QUOTA) {
+				fs_project_t	*p = getprprid(id);
+				if (p)
+					name = p->pr_name;
+			}
+		}
 		fprintf(fp, "%-10s", name);
+	}
 
 	if (form & XFS_BLOCK_QUOTA) {
 		qflags = (flags & HUMAN_FLAG);
@@ -545,9 +562,10 @@ report_f(
 	FILE		*fp = NULL;
 	char		*fname = NULL;
 	uint		lower = 0, upper = 0;
+	bool		lookup = false;
 	int		c, flags = 0, type = 0, form = 0;
 
-	while ((c = getopt(argc, argv, "abf:ghiL:NnprtuU:")) != EOF) {
+	while ((c = getopt(argc, argv, "abdf:ghilL:NnprtuU:")) != EOF) {
 		switch (c) {
 		case 'f':
 			fname = optarg;
@@ -587,9 +605,14 @@ report_f(
 			break;
 		case 'L':
 			lower = (uint)atoi(optarg);
+			flags |= NO_LOOKUP_FLAG;
 			break;
 		case 'U':
 			upper = (uint)atoi(optarg);
+			flags |= NO_LOOKUP_FLAG;
+			break;
+		case 'l':
+			lookup = true;
 			break;
 		default:
 			return command_usage(&report_cmd);
@@ -602,6 +625,9 @@ report_f(
 	if (!type)
 		type = XFS_USER_QUOTA | XFS_GROUP_QUOTA | XFS_PROJ_QUOTA;
 
+	if (lookup)
+		flags &= ~NO_LOOKUP_FLAG;
+
 	if ((fp = fopen_write_secure(fname)) == NULL)
 		return 0;
 

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs