From da39777e71da2583f9f99e14ec631aa1110387b6 Mon Sep 17 00:00:00 2001 From: kre Date: Fri, 5 May 2023 04:14:02 +0000 Subject: [PATCH] If chown and chgrp can grow -d flags to suppress performing the operation when it will have no effect (other than changing the inode's ctime value) then chmod and chflags should also have -d flags for the same purpose. Make it so. --- bin/chmod/chmod.1 | 16 +++++++++++----- bin/chmod/chmod.c | 26 +++++++++++++++----------- usr.bin/chflags/chflags.1 | 8 +++++++- usr.bin/chflags/chflags.c | 17 +++++++++++------ 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/bin/chmod/chmod.1 b/bin/chmod/chmod.1 index c9c11504a348..7e854ce90c9c 100644 --- a/bin/chmod/chmod.1 +++ b/bin/chmod/chmod.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: chmod.1,v 1.28 2017/07/04 06:47:27 wiz Exp $ +.\" $NetBSD: chmod.1,v 1.29 2023/05/05 04:14:02 kre Exp $ .\" .\" Copyright (c) 1989, 1990, 1993, 1994 .\" The Regents of the University of California. All rights reserved. @@ -44,7 +44,7 @@ .Fl R .Op Fl H | Fl L | Fl P .Oc -.Op Fl fh +.Op Fl dfh .Ar mode .Ar .Nm @@ -52,7 +52,7 @@ .Fl R .Op Fl H | Fl L | Fl P .Oc -.Op Fl fh +.Op Fl dfh .Fl Fl reference=rfile .Ar .Sh DESCRIPTION @@ -84,12 +84,18 @@ If the .Fl R option is specified, no symbolic links are followed. .It Fl R -Change the modes of the file hierarchies rooted in the files +Change the modes of the file hierarchies rooted in the +.Ar file Ns s instead of just the files themselves. +.It Fl d +Do not attempt to change the mode of a +.Ar file +if its mode is already as requested. .It Fl f Do not display a diagnostic message or modify the exit status if .Nm -fails to change the mode of a file. +fails to change the mode of a +.Ar file . .It Fl h If .Ar file diff --git a/bin/chmod/chmod.c b/bin/chmod/chmod.c index c10101e54701..d5869233a241 100644 --- a/bin/chmod/chmod.c +++ b/bin/chmod/chmod.c @@ -1,4 +1,4 @@ -/* $NetBSD: chmod.c,v 1.38 2012/10/22 18:00:46 christos Exp $ */ +/* $NetBSD: chmod.c,v 1.39 2023/05/05 04:14:02 kre Exp $ */ /* * Copyright (c) 1989, 1993, 1994 @@ -40,7 +40,7 @@ __COPYRIGHT( #if 0 static char sccsid[] = "@(#)chmod.c 8.8 (Berkeley) 4/1/94"; #else -__RCSID("$NetBSD: chmod.c,v 1.38 2012/10/22 18:00:46 christos Exp $"); +__RCSID("$NetBSD: chmod.c,v 1.39 2023/05/05 04:14:02 kre Exp $"); #endif #endif /* not lint */ @@ -74,17 +74,17 @@ main(int argc, char *argv[]) FTS *ftsp; FTSENT *p; void *set; - mode_t mval; - int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval; + mode_t mval, nval; + int Hflag, Lflag, Rflag, ch, dflag, fflag, fts_options, hflag, rval; char *mode, *reference; int (*change_mode)(const char *, mode_t); setprogname(argv[0]); (void)setlocale(LC_ALL, ""); - Hflag = Lflag = Rflag = fflag = hflag = 0; + Hflag = Lflag = Rflag = dflag = fflag = hflag = 0; reference = NULL; - while ((ch = getopt_long(argc, argv, "HLPRXfghorstuwx", + while ((ch = getopt_long(argc, argv, "HLPRXdfghorstuwx", chmod_longopts, NULL)) != -1) switch (ch) { case 1: @@ -104,6 +104,9 @@ main(int argc, char *argv[]) case 'R': Rflag = 1; break; + case 'd': + dflag = 1; + break; case 'f': fflag = 1; break; @@ -212,9 +215,10 @@ done: argv += optind; default: break; } - if ((*change_mode)(p->fts_accpath, - set ? getmode(set, p->fts_statp->st_mode) : mval) - && !fflag) { + nval = set ? getmode(set, p->fts_statp->st_mode) : mval; + if (dflag && nval == p->fts_statp->st_mode) + continue; + if ((*change_mode)(p->fts_accpath, nval) && !fflag) { warn("%s", p->fts_path); rval = 1; } @@ -231,8 +235,8 @@ static void usage(void) { (void)fprintf(stderr, - "Usage: %s [-R [-H | -L | -P]] [-fh] mode file ...\n" - "\t%s [-R [-H | -L | -P]] [-fh] --reference=rfile file ...\n", + "Usage: %s [-R [-H | -L | -P]] [-dfh] mode file ...\n" + "\t%s [-R [-H | -L | -P]] [-dfh] --reference=rfile file ...\n", getprogname(), getprogname()); exit(1); /* NOTREACHED */ diff --git a/usr.bin/chflags/chflags.1 b/usr.bin/chflags/chflags.1 index c04b92a758aa..9e1fdcc97a44 100644 --- a/usr.bin/chflags/chflags.1 +++ b/usr.bin/chflags/chflags.1 @@ -1,4 +1,4 @@ -.\" $NetBSD: chflags.1,v 1.24 2013/12/17 09:54:08 apb Exp $ +.\" $NetBSD: chflags.1,v 1.25 2023/05/05 04:14:02 kre Exp $ .\" .\" Copyright (c) 1989, 1990, 1993, 1994 .\" The Regents of the University of California. All rights reserved. @@ -44,6 +44,7 @@ .Fl R .Op Fl H | Fl L | Fl P .Oc +.Op Fl d .Op Fl h .Ar flags .Ar @@ -57,6 +58,11 @@ operand. .Pp The options are as follows: .Bl -tag -width Ds +.It Fl d +If the change requested would not alter the +flags currently set for +.Ar file +then attempt no change operation. .It Fl H If the .Fl R diff --git a/usr.bin/chflags/chflags.c b/usr.bin/chflags/chflags.c index e4150e4d21f5..eaec87152b67 100644 --- a/usr.bin/chflags/chflags.c +++ b/usr.bin/chflags/chflags.c @@ -1,4 +1,4 @@ -/* $NetBSD: chflags.c,v 1.17 2023/04/28 22:23:45 andvar Exp $ */ +/* $NetBSD: chflags.c,v 1.18 2023/05/05 04:14:02 kre Exp $ */ /* * Copyright (c) 1992, 1993, 1994 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\ #if 0 static char sccsid[] = "from: @(#)chflags.c 8.5 (Berkeley) 4/1/94"; #else -__RCSID("$NetBSD: chflags.c,v 1.17 2023/04/28 22:23:45 andvar Exp $"); +__RCSID("$NetBSD: chflags.c,v 1.18 2023/05/05 04:14:02 kre Exp $"); #endif #endif /* not lint */ @@ -64,12 +64,12 @@ main(int argc, char *argv[]) FTSENT *p; u_long clear, set, newflags; long val; - int Hflag, Lflag, Rflag, ch, fts_options, hflag, oct, rval; + int Hflag, Lflag, Rflag, ch, fts_options, dflag, hflag, oct, rval; char *flags, *ep; int (*change_flags)(const char *, u_long); - Hflag = Lflag = Rflag = hflag = 0; - while ((ch = getopt(argc, argv, "HLPRh")) != -1) + Hflag = Lflag = Rflag = dflag = hflag = 0; + while ((ch = getopt(argc, argv, "HLPRdh")) != -1) switch (ch) { case 'H': Hflag = 1; @@ -85,6 +85,9 @@ main(int argc, char *argv[]) case 'R': Rflag = 1; break; + case 'd': + dflag = 1; + break; case 'h': hflag = 1; break; @@ -181,6 +184,8 @@ main(int argc, char *argv[]) newflags |= set; newflags &= clear; } + if (dflag && newflags == p->fts_statp->st_flags) + continue; if ((*change_flags)(p->fts_accpath, newflags)) { warn("%s", p->fts_path); rval = 1; @@ -196,6 +201,6 @@ usage(void) { (void)fprintf(stderr, - "usage: chflags [-R [-H | -L | -P]] [-h] flags file ...\n"); + "usage: chflags [-R [-H | -L | -P]] [-dh] flags file ...\n"); exit(1); }