Subject: du -n - ignore "nodump" files
To: None <tech-userlevel@netbsd.org>
From: Simon Burge <simonb@wasabisystems.com>
List: tech-userlevel
Date: 02/26/2003 19:03:25
--WIyZ46R2i8wDzkSu
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Folks,

I'd like at commit the following change to du(1) so that -n will ignore
files and directories marked with "chflags nodump ...". I've found this
useful for working out how big a backup will be when used in conjunction
with "dump -h".

The "#ifdef HN_AUTOSCALE" check allows this to be compiled on the 1.6
branch as well.  I'd remove that before I would commit it.

Any objections?

Simon.
--
Simon Burge                                   <simonb@wasabisystems.com>
NetBSD Development, Support and Service:   http://www.wasabisystems.com/

--WIyZ46R2i8wDzkSu
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="du.diff"

Index: du.1
===================================================================
RCS file: /cvsroot/src/usr.bin/du/du.1,v
retrieving revision 1.14
diff -d -p -u -r1.14 du.1
--- du.1	2002/09/27 07:18:16	1.14
+++ du.1	2003/02/26 08:00:06
@@ -90,6 +90,8 @@ If the
 .Fl m
 flag is specified, the number displayed is the number of megabyte
 (1024*1024 bytes) blocks.
+.It Fl n
+Ignore files and directories with user "nodump" flag (UF_NODUMP) set.
 .It Fl r
 Generate warning messages about directories that cannot be read.
 This is the default behaviour.
@@ -139,6 +141,7 @@ size block.
 .El
 .Sh SEE ALSO
 .Xr df 1 ,
+.Xr stat 2 ,
 .Xr fts 3 ,
 .Xr getbsize 3 ,
 .Xr symlink 7 ,
Index: du.c
===================================================================
RCS file: /cvsroot/src/usr.bin/du/du.c,v
retrieving revision 1.19
diff -d -p -u -r1.19 du.c
--- du.c	2002/09/28 21:14:03	1.19
+++ du.c	2003/02/26 08:00:06
@@ -80,14 +80,15 @@ main(argc, argv)
 	FTSENT *p;
 	int64_t totalblocks;
 	int ftsoptions, listdirs, listfiles;
-	int Hflag, Lflag, Pflag, aflag, ch, cflag, kmflag, notused, rval, sflag;
+	int Hflag, Lflag, Pflag, aflag, cflag, kmflag, nflag, sflag;
+	int ch, notused, rval;
 	char **save;
 
 	save = argv;
-	Hflag = Lflag = Pflag = aflag = cflag = kmflag = sflag = 0;
+	Hflag = Lflag = Pflag = aflag = cflag = kmflag = nflag = sflag = 0;
 	totalblocks = 0;
 	ftsoptions = FTS_PHYSICAL;
-	while ((ch = getopt(argc, argv, "HLPachkmrsx")) != -1)
+	while ((ch = getopt(argc, argv, "HLPachkmnrsx")) != -1)
 		switch (ch) {
 		case 'H':
 			Hflag = 1;
@@ -118,6 +119,9 @@ main(argc, argv)
 			blocksize = 1024 * 1024;
 			kmflag = 1;
 			break; 
+		case 'n':
+			nflag = 1;
+			break;
 		case 'r':
 			break;
 		case 's':
@@ -176,7 +180,20 @@ main(argc, argv)
 	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
 		err(1, "fts_open `%s'", *argv);
 
-	for (rval = 0; (p = fts_read(fts)) != NULL;)
+	for (rval = 0; (p = fts_read(fts)) != NULL;) {
+		if (nflag) {
+			switch (p->fts_info) {
+			case FTS_NS:
+			case FTS_SLNONE:
+				/* nothing */
+				break;
+			default:
+				if (p->fts_statp->st_flags & UF_NODUMP) {
+					fts_set(fts, p, FTS_SKIP);
+					continue;
+				}
+			}
+		}
 		switch (p->fts_info) {
 		case FTS_D:			/* Ignore. */
 			break;
@@ -214,6 +231,7 @@ main(argc, argv)
 			if (cflag)
 				totalblocks += p->fts_statp->st_blocks;
 		}
+	}
 	if (errno)
 		err(1, "fts_read");
 	if (cflag)
@@ -224,6 +242,7 @@ main(argc, argv)
 void
 prstat(const char *fname, int64_t blocks)
 {
+#ifdef HN_AUTOSCALE
 	if (hflag) {
 		char buf[5];
 		int64_t sz = blocks * 512;
@@ -233,6 +252,7 @@ prstat(const char *fname, int64_t blocks
 
 		(void)printf("%s\t%s\n", buf, fname);
 	} else
+#endif
 		(void)printf("%lld\t%s\n",
 		    (long long)howmany(blocks, (int64_t)blocksize),
 		    fname);

--WIyZ46R2i8wDzkSu--