Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/chown Add a -d flag to avoid changing a file's owner/gr...



details:   https://anonhg.NetBSD.org/src/rev/b91f9ee85a36
branches:  trunk
changeset: 374569:b91f9ee85a36
user:      pgoyette <pgoyette%NetBSD.org@localhost>
date:      Thu May 04 17:07:56 2023 +0000

description:
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@

diffstat:

 sbin/chown/chgrp.1 |  12 +++++++++---
 sbin/chown/chown.8 |  11 ++++++++---
 sbin/chown/chown.c |  29 ++++++++++++++++++++++-------
 3 files changed, 39 insertions(+), 13 deletions(-)

diffs (148 lines):

diff -r 3b4c48af05d0 -r b91f9ee85a36 sbin/chown/chgrp.1
--- a/sbin/chown/chgrp.1        Thu May 04 13:41:46 2023 +0000
+++ b/sbin/chown/chgrp.1        Thu May 04 17:07:56 2023 +0000
@@ -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 .
diff -r 3b4c48af05d0 -r b91f9ee85a36 sbin/chown/chown.8
--- a/sbin/chown/chown.8        Thu May 04 13:41:46 2023 +0000
+++ b/sbin/chown/chown.8        Thu May 04 17:07:56 2023 +0000
@@ -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
diff -r 3b4c48af05d0 -r b91f9ee85a36 sbin/chown/chown.c
--- a/sbin/chown/chown.c        Thu May 04 13:41:46 2023 +0000
+++ b/sbin/chown/chown.c        Thu May 04 17:07:56 2023 +0000
@@ -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 @@
 #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);



Home | Main Index | Thread Index | Old Index