Module Name:    src
Committed By:   pgoyette
Date:           Thu May  4 17:07:57 UTC 2023

Modified Files:
        src/sbin/chown: chgrp.1 chown.8 chown.c

Log Message:
Add a -d flag to avoid changing a file's owner/group to the current
value.  This avoids some unnecessary operations on the file.

As discussed on tech-userlevel@


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sbin/chown/chgrp.1
cvs rdiff -u -r1.12 -r1.13 src/sbin/chown/chown.8
cvs rdiff -u -r1.9 -r1.10 src/sbin/chown/chown.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/chown/chgrp.1
diff -u src/sbin/chown/chgrp.1:1.8 src/sbin/chown/chgrp.1:1.9
--- src/sbin/chown/chgrp.1:1.8	Tue Jul  4 06:52:20 2017
+++ src/sbin/chown/chgrp.1	Thu May  4 17:07:56 2023
@@ -29,9 +29,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     from: @(#)chgrp.1	8.3 (Berkeley) 3/31/94
-.\"	$NetBSD: chgrp.1,v 1.8 2017/07/04 06:52:20 wiz Exp $
+.\"	$NetBSD: chgrp.1,v 1.9 2023/05/04 17:07:56 pgoyette Exp $
 .\"
-.Dd October 22, 2012
+.Dd May 1, 2023
 .Dt CHGRP 1
 .Os
 .Sh NAME
@@ -86,6 +86,10 @@ option is specified, no symbolic links a
 .It Fl R
 Change the group ID for the file hierarchies rooted
 in the files instead of just the files themselves.
+.It Fl d
+Do not attempt to update a file's group, nor update the file's
+set-user-id or set-group-id bits if they are already set to the
+desired values.
 .It Fl f
 The force option ignores errors, except for usage errors and doesn't
 query about strange modes (unless the user does not have proper permissions).
@@ -164,6 +168,8 @@ utility is expected to be POSIX 1003.2 c
 .Pp
 The
 .Fl v
-option and the use of ``#'' to force a numeric group ID
+and
+.Fl d
+options and the use of ``#'' to force a numeric group ID
 are extensions to
 .St -p1003.2 .

Index: src/sbin/chown/chown.8
diff -u src/sbin/chown/chown.8:1.12 src/sbin/chown/chown.8:1.13
--- src/sbin/chown/chown.8:1.12	Tue Jul  4 06:53:12 2017
+++ src/sbin/chown/chown.8	Thu May  4 17:07:56 2023
@@ -26,9 +26,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     from: @(#)chown.8	8.3 (Berkeley) 3/31/94
-.\"	$NetBSD: chown.8,v 1.12 2017/07/04 06:53:12 wiz Exp $
+.\"	$NetBSD: chown.8,v 1.13 2023/05/04 17:07:56 pgoyette Exp $
 .\"
-.Dd September 11, 2016
+.Dd May 1, 2023
 .Dt CHOWN 8
 .Os
 .Sh NAME
@@ -84,6 +84,9 @@ option is specified, no symbolic links a
 .It Fl R
 Change the user ID and/or the group ID for the file hierarchies rooted
 in the files instead of just the files themselves.
+.It Fl d
+Do not attempt to update a file's owner or group or its set-user-id
+and set-group-id bits if they are all already set to the desired values.
 .It Fl f
 Do not report any failure to change file owner or group, nor modify
 the exit status to reflect such failures.
@@ -174,7 +177,9 @@ command is expected to be POSIX 1003.2 c
 .Pp
 The
 .Fl v
-option and the use of ``#'' to force a numeric lookup
+and
+.Fl d
+options and the use of ``#'' to force a numeric lookup
 are extensions to
 .St -p1003.2 .
 .Sh HISTORY

Index: src/sbin/chown/chown.c
diff -u src/sbin/chown/chown.c:1.9 src/sbin/chown/chown.c:1.10
--- src/sbin/chown/chown.c:1.9	Fri Apr 28 09:56:45 2023
+++ src/sbin/chown/chown.c	Thu May  4 17:07:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: chown.c,v 1.9 2023/04/28 09:56:45 pgoyette Exp $	*/
+/*	$NetBSD: chown.c,v 1.10 2023/05/04 17:07:56 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 1988, 1993, 1994, 2003
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)chown.c	8.8 (Berkeley) 4/4/94";
 #else
-__RCSID("$NetBSD: chown.c,v 1.9 2023/04/28 09:56:45 pgoyette Exp $");
+__RCSID("$NetBSD: chown.c,v 1.10 2023/05/04 17:07:56 pgoyette Exp $");
 #endif
 #endif /* not lint */
 
@@ -82,7 +82,7 @@ main(int argc, char **argv)
 {
 	FTS *ftsp;
 	FTSENT *p;
-	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval, vflag;
+	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval, vflag, dflag;
 	char *cp, *reference;
 	int (*change_owner)(const char *, uid_t, gid_t);
 
@@ -94,13 +94,16 @@ main(int argc, char **argv)
 	ischown = (myname[2] == 'o');
 	reference = NULL;
 
-	Hflag = Lflag = Rflag = fflag = hflag = vflag = 0;
-	while ((ch = getopt_long(argc, argv, "HLPRfhv",
+	Hflag = Lflag = Rflag = fflag = hflag = vflag = dflag = 0;
+	while ((ch = getopt_long(argc, argv, "HLPRdfhv",
 	    chown_longopts, NULL)) != -1)
 		switch (ch) {
 		case 1:
 			reference = optarg;
 			break;
+		case 'd':
+			dflag = 1;
+			break;
 		case 'H':
 			Hflag = 1;
 			Lflag = 0;
@@ -232,6 +235,18 @@ main(int argc, char **argv)
 			break;
 		}
 
+		/*
+		 * If dflag was set, and the owner and group are already
+		 * set to the right values and the set-user-id and
+		 * set-group-id bits are both already clear, skip any
+		 * attempt to update.
+		 */
+		if (dflag &&
+		    ( -1 == uid || p->fts_statp->st_uid == uid ) &&
+		    ( -1 == gid || p->fts_statp->st_gid == gid ) &&
+		    ( p->fts_statp->st_mode & 07000 ) == 0))
+			continue;
+
 		if ((*change_owner)(p->fts_accpath, uid, gid) && !fflag) {
 			warn("%s", p->fts_path);
 			rval = EXIT_FAILURE;
@@ -294,8 +309,8 @@ usage(void)
 {
 
 	(void)fprintf(stderr,
-	    "Usage: %s [-R [-H | -L | -P]] [-fhv] %s file ...\n"
-	    "\t%s [-R [-H | -L | -P]] [-fhv] --reference=rfile file ...\n",
+	    "Usage: %s [-R [-H | -L | -P]] [-dfhv] %s file ...\n"
+	    "\t%s [-R [-H | -L | -P]] [-dfhv] --reference=rfile file ...\n",
 	    myname, ischown ? "owner:group|owner|:group" : "group",
 	    myname);
 	exit(EXIT_FAILURE);

Reply via email to