Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 25 Feb 2009 17:24:40 -0900
From:      Mel <fbsd.hackers@rachie.is-a-geek.net>
To:        freebsd-hackers@freebsd.org
Subject:   [PATCH] Support for thresholds in du(1)
Message-ID:  <200902251724.40212.fbsd.hackers@rachie.is-a-geek.net>

next in thread | raw e-mail | index | archive | help
--Boundary-00=_o1fpJvCx4PioHXE
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hi,

attached is a small patch to add threshold support to du(1). I've been using 
it on 7-STABLE machines for a while, cause I got tired of the noise I get 
when sorting and then reformatting to human-readable. Especially since 
sorting isn't part of the equasion "I'd like to see all dirs exceeding a 
given size".
I've not updated the manpage on -STABLE yet, should be the same as HEAD.

Example usage:
# du -xht 20m .
 29M    ./contrib/binutils
 52M    ./contrib/gcc
237M    ./contrib
 35M    ./crypto
 28M    ./lib
 20M    ./share
 55M    ./sys/dev
139M    ./sys
545M    .

I'll file a PR for it, if there's no objections to this feature / 
implementation, the style(9) or the usage of -t.
-- 
Mel

--Boundary-00=_o1fpJvCx4PioHXE
Content-Type: text/x-diff;
  charset="iso 8859-15";
  name="du.HEAD.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="du.HEAD.diff"

Index: usr.bin/du/du.1
===================================================================
RCS file: /home/ncvs/src/usr.bin/du/du.1,v
retrieving revision 1.34
diff -u -r1.34 du.1
--- usr.bin/du/du.1	6 Nov 2008 16:30:38 -0000	1.34
+++ usr.bin/du/du.1	26 Feb 2009 01:58:25 -0000
@@ -42,7 +42,7 @@
 .Nm
 .Op Fl A
 .Op Fl H | L | P
-.Op Fl a | s | d Ar depth
+.Op Fl a | s | d Ar depth | t Ar threshold
 .Op Fl c
 .Op Fl l
 .Op Fl h | k | m | B Ar blocksize
@@ -106,6 +106,13 @@
 .It Fl s
 Display an entry for each specified file.
 (Equivalent to
+.It Fl t Ar threshold
+Display only entries for which the size exceeds
+.Ar threshold
+, which can be given in bytes (default), kB, MB or GB, by appending k, m,
+or g respectively.
+.Ar threshold
+cannot exceeds the platform's OFF_MAX.
 .Fl d Li 0 )
 .It Fl d Ar depth
 Display an entry for all files and directories
Index: usr.bin/du/du.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/du/du.c,v
retrieving revision 1.49
diff -u -r1.49 du.c
--- usr.bin/du/du.c	6 Nov 2008 23:55:28 -0000	1.49
+++ usr.bin/du/du.c	26 Feb 2009 02:03:48 -0000
@@ -87,7 +87,7 @@
 {
 	FTS		*fts;
 	FTSENT		*p;
-	off_t		savednumber, curblocks;
+	off_t		savednumber, curblocks, threshold;
 	int		ftsoptions;
 	int		listall;
 	int		depth;
@@ -95,6 +95,8 @@
 	int		hflag, lflag, ch, notused, rval;
 	char 		**save;
 	static char	dot[] = ".";
+	char last;
+	int multi = 1;
 
 	setlocale(LC_ALL, "");
 
@@ -103,13 +105,13 @@
 
 	save = argv;
 	ftsoptions = 0;
-	savednumber = 0;
+	savednumber = threshold = 0;
 	cblocksize = DEV_BSIZE;
 	blocksize = 0;
 	depth = INT_MAX;
 	SLIST_INIT(&ignores);
 
-	while ((ch = getopt(argc, argv, "AB:HI:LPasd:chklmnrx")) != -1)
+	while ((ch = getopt(argc, argv, "AB:HI:LPasd:chklmnrt:x")) != -1)
 		switch (ch) {
 		case 'A':
 			Aflag = 1;
@@ -177,6 +179,40 @@
 			break;
 		case 'r':		 /* Compatibility. */
 			break;
+		case 't' :
+			last = optarg[strlen(optarg)-1];
+			switch(last)
+			{
+				case 'k' :
+				case 'K' :
+					multi = 1024;
+					break;
+				case 'm' :
+				case 'M' :
+					multi = 1024*1024;
+					break;
+				case 'G' :
+				case 'g' :
+					multi = 1024*1024*1024;
+					break;
+				default :
+					break;
+			}
+			if( multi > 1 )
+				optarg[strlen(optarg)-1] = '\0';
+			threshold = (off_t)strtoll(optarg, NULL, 10);
+			if( errno == ERANGE || threshold < 0 )
+			{
+				warn("Invalid threshold: %s", optarg);
+				usage();
+			}
+			if( threshold > OFF_MAX / multi )
+			{
+				warnx("Threshold too large");
+				usage();
+			}
+			threshold *= multi;
+			break;
 		case 'x':
 			ftsoptions |= FTS_XDEV;
 			break;
@@ -267,7 +303,8 @@
 			p->fts_parent->fts_bignum += p->fts_bignum +=
 			    curblocks;
 
-			if (p->fts_level <= depth) {
+			if (p->fts_level <= depth && threshold < howmany(p->fts_bignum *
+						cblocksize, blocksize)) {
 				if (hflag) {
 					prthumanval(p->fts_bignum);
 					(void)printf("\t%s\n", p->fts_path);

--Boundary-00=_o1fpJvCx4PioHXE
Content-Type: text/x-diff;
  charset="iso 8859-15";
  name="du.RELENG_7.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="du.RELENG_7.diff"

Index: usr.bin/du/du.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/du/du.c,v
retrieving revision 1.42.2.1
diff -u -r1.42.2.1 du.c
--- usr.bin/du/du.c	15 Jan 2009 03:48:49 -0000	1.42.2.1
+++ usr.bin/du/du.c	26 Feb 2009 02:02:49 -0000
@@ -86,6 +86,7 @@
 	FTS		*fts;
 	FTSENT		*p;
 	off_t		savednumber = 0;
+	off_t		threshold = 0;
 	long		blocksize;
 	int		ftsoptions;
 	int		listall;
@@ -94,6 +95,8 @@
 	int		hflag, lflag, ch, notused, rval;
 	char 		**save;
 	static char	dot[] = ".";
+	char last;
+	int multi = 1;
 
 	setlocale(LC_ALL, "");
 
@@ -105,7 +108,7 @@
 	depth = INT_MAX;
 	SLIST_INIT(&ignores);
 
-	while ((ch = getopt(argc, argv, "HI:LPasd:chklmnrx")) != -1)
+	while ((ch = getopt(argc, argv, "HI:LPasd:chklmnrt:x")) != -1)
 		switch (ch) {
 			case 'H':
 				Hflag = 1;
@@ -161,6 +164,40 @@
 				break;
 			case 'r':		 /* Compatibility. */
 				break;
+			case 't' :
+				last = optarg[strlen(optarg)-1];
+				switch(last)
+				{
+					case 'k' :
+					case 'K' :
+						multi = 1024;
+						break;
+					case 'm' :
+					case 'M' :
+						multi = 1024*1024;
+						break;
+					case 'G' :
+					case 'g' :
+						multi = 1024*1024*1024;
+						break;
+					default :
+						break;
+				}
+				if( multi > 1 )
+					optarg[strlen(optarg)-1] = '\0';
+				threshold = (off_t)strtoll(optarg, NULL, 10);
+				if( errno == ERANGE || threshold < 0 )
+				{
+					warn("Invalid threshold: %s", optarg);
+					usage();
+				}
+				if( threshold > OFF_MAX / multi )
+				{
+					warnx("Threshold too large");
+					usage();
+				}
+				threshold *= multi;
+				break;
 			case 'x':
 				ftsoptions |= FTS_XDEV;
 				break;
@@ -239,7 +276,7 @@
 				p->fts_parent->fts_bignum +=
 				    p->fts_bignum += p->fts_statp->st_blocks;
 
-				if (p->fts_level <= depth) {
+				if (p->fts_level <= depth && threshold < howmany(p->fts_bignum * DEV_BSIZE, blocksize)) {
 					if (hflag) {
 						(void) prthumanval(howmany(p->fts_bignum, blocksize));
 						(void) printf("\t%s\n", p->fts_path);

--Boundary-00=_o1fpJvCx4PioHXE--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200902251724.40212.fbsd.hackers>