From keramida@ceid.upatras.gr  Fri Nov  9 16:46:14 2007
Return-Path: <keramida@ceid.upatras.gr>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id E9D4116A417;
	Fri,  9 Nov 2007 16:46:13 +0000 (UTC)
	(envelope-from keramida@ceid.upatras.gr)
Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36])
	by mx1.freebsd.org (Postfix) with ESMTP id 5701213C480;
	Fri,  9 Nov 2007 16:46:13 +0000 (UTC)
	(envelope-from keramida@ceid.upatras.gr)
Received: from kobe.laptop (vader.bytemobile-rio.ondsl.gr [83.235.57.37])
	(authenticated bits=128)
	by igloo.linux.gr (8.14.1/8.14.1/Debian-9) with ESMTP id lA9GQs0u022988
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT);
	Fri, 9 Nov 2007 18:27:01 +0200
Received: from kobe.laptop (kobe.laptop [127.0.0.1])
	by kobe.laptop (8.14.1/8.14.1) with ESMTP id lA9GQnYG015455;
	Fri, 9 Nov 2007 18:26:49 +0200 (EET)
	(envelope-from keramida@kobe.laptop)
Received: (from keramida@localhost)
	by kobe.laptop (8.14.1/8.14.1/Submit) id lA9GQn6S015454;
	Fri, 9 Nov 2007 18:26:49 +0200 (EET)
	(envelope-from keramida)
Message-Id: <200711091626.lA9GQn6S015454@kobe.laptop>
Date: Fri, 9 Nov 2007 18:26:49 +0200 (EET)
From: Giorgos Keramidas <keramida@freebsd.org>
Reply-To: Giorgos Keramidas <keramida@freebsd.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc: ru@freebsd.org
Subject: [PATCH] add a du(1) -l option to count hardlinks multiple times
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         117944
>Category:       bin
>Synopsis:       [patch] [request] add a du(1) -l option to count hardlinks multiple times
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    keramida
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Nov 09 16:50:01 UTC 2007
>Closed-Date:    Wed Jan 21 17:46:47 UTC 2009
>Last-Modified:  Wed Jan 21 17:46:47 UTC 2009
>Originator:     Giorgos Keramidas
>Release:        FreeBSD 8.0-CURRENT i386
>Organization:
>Environment:
System: FreeBSD kobe 8.0-CURRENT FreeBSD 8.0-CURRENT #0:
Sun Nov 4 06:20:54 EET 2007 build@kobe:/home/build/obj/home/build/src/sys/KOBE i386

>Description:

While testing an Ubuntu installation at home, I discovered the -l
option of du(1), which allows one to count hardlinked files multiple
times.

This was useful when trying to count the disk size of two Mercurial
repository clones, which by default use hard-links for their metadata
directories.  With our current du(1) utility, one sees:

  keramida@kobe:/home/keramida/hg/doc$ du -sk bsd/.hg el/.hg
  8312    bsd/.hg
  698     el/.hg
  keramida@kobe:/home/keramida/hg/doc$ du -sk el/.hg
  8288    el/.hg
  keramida@kobe:/home/keramida/hg/doc$

Now it's obvious that hard links in el/.hg would require that we either
run du(1) many times, or patch our du(1) utility to make the hard link
checks optional.

The Linux version of du(1) uses the -l option to turn off the hard link
checks, so I wrote from scratch a short patch to do the same.

The patch attached below is also available at:
http://people.freebsd.org/~keramida/diff/du-hardlinks-00.patch

>How-To-Repeat:
>Fix:

--- du-hardlinks-00.patch begins here ---
diff -r 2d7179484ba4 du.1
--- a/du.1	Fri Nov 09 18:17:29 2007 +0200
+++ b/du.1	Fri Nov 09 18:25:17 2007 +0200
@@ -32,7 +32,7 @@
 .\"	@(#)du.1	8.2 (Berkeley) 4/1/94
 .\" $FreeBSD: src/usr.bin/du/du.1,v 1.32 2006/09/29 15:20:44 ru Exp $
 .\"
-.Dd May 6, 2006
+.Dd November 9, 2007
 .Dt DU 1
 .Os
 .Sh NAME
@@ -43,6 +43,7 @@
 .Op Fl H | L | P
 .Op Fl a | s | d Ar depth
 .Op Fl c
+.Op Fl l
 .Op Fl h | k | m
 .Op Fl n
 .Op Fl x
@@ -94,6 +95,15 @@ Display a grand total.
 Display a grand total.
 .It Fl k
 Display block counts in 1024-byte (1-Kbyte) blocks.
+.It Fl l
+If a file has multiple hard links, count its size many times.
+The default behavior of
+.Nm
+is to count files with multiple hard links only once.
+When the
+.Fl l
+option is specified, the hard link checks are disabled, and these files
+are counted (and displayed) as many times as they are found.
 .It Fl m
 Display block counts in 1048576-byte (1-Mbyte) blocks.
 .It Fl n
@@ -120,11 +130,6 @@ or
 .Fl L
 options are specified, storage used by any symbolic links which are
 followed is not counted or displayed.
-.Pp
-Files having multiple hard links are counted (and displayed) a single
-time per
-.Nm
-execution.
 .Sh ENVIRONMENT
 .Bl -tag -width BLOCKSIZE
 .It Ev BLOCKSIZE
diff -r 2d7179484ba4 du.c
--- a/du.c	Fri Nov 09 18:17:29 2007 +0200
+++ b/du.c	Fri Nov 09 18:25:17 2007 +0200
@@ -90,20 +90,22 @@ main(int argc, char *argv[])
 	int		ftsoptions;
 	int		listall;
 	int		depth;
-	int		Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
+	int		Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag,
+			hflag, lflag, ch, notused, rval;
 	char 		**save;
 	static char	dot[] = ".";
 
 	setlocale(LC_ALL, "");
 
-	Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
+	Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag =
+	    lflag = 0;
 
 	save = argv;
 	ftsoptions = 0;
 	depth = INT_MAX;
 	SLIST_INIT(&ignores);
 
-	while ((ch = getopt(argc, argv, "HI:LPasd:chkmnrx")) != -1)
+	while ((ch = getopt(argc, argv, "HI:LPasd:chklmnrx")) != -1)
 		switch (ch) {
 			case 'H':
 				Hflag = 1;
@@ -149,6 +151,9 @@ main(int argc, char *argv[])
 				hflag = 0;
 				if (setenv("BLOCKSIZE", "1024", 1) == -1)
 					warnx("setenv: cannot set BLOCKSIZE=1024");
+				break;
+			case 'l':
+				lflag = 1;
 				break;
 			case 'm':
 				hflag = 0;
@@ -261,7 +266,7 @@ main(int argc, char *argv[])
 				if (ignorep(p))
 					break;
 
-				if (p->fts_statp->st_nlink > 1 && linkchk(p))
+				if (lflag == 0 && p->fts_statp->st_nlink > 1 && linkchk(p))
 					break;
 
 				if (listall || p->fts_level == 0) {
@@ -447,7 +452,8 @@ usage(void)
 usage(void)
 {
 	(void)fprintf(stderr,
-		"usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k | -m] [-n] [-x] [-I mask] [file ...]\n");
+		"usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] "
+		"[-l] [-h | -k | -m] [-n] [-x] [-I mask] [file ...]\n");
 	exit(EX_USAGE);
 }
 
--- du-hardlinks-00.patch ends here ---
>Release-Note:
>Audit-Trail:

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/117944: commit references a PR
Date: Mon, 25 Feb 2008 19:06:52 +0000 (UTC)

 keramida    2008-02-25 19:06:43 UTC
 
   FreeBSD src repository (doc committer)
 
   Modified files:
     usr.bin/du           du.1 du.c 
   Log:
   Implement from scratch a -l option for du(1), to match the same option
   of the GNU utility.  The default behavior of our original `du' is to
   count hardlinked files only once for each invocation of the utility.
   With the new -l option they count towards the final size every time
   they are found.
   
   PR:             bin/117944
   Submitted by:   keramida
   Reviewed by:    des, obrien
   MFC after:      2 weeks
   
   Revision  Changes    Path
   1.33      +11 -6     src/usr.bin/du/du.1
   1.45      +12 -5     src/usr.bin/du/du.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: keramida 
State-Changed-When: Mon Feb 25 19:10:32 UTC 2008 
State-Changed-Why:  
Mark as `patched'.  After a bit of help from des and obrien, 
I've committed a slightly edited version to HEAD, as: 

Revision  Changes    Path 
1.33      +11 -6     src/usr.bin/du/du.1 
1.45      +12 -5     src/usr.bin/du/du.c 



Responsible-Changed-From-To: freebsd-bugs->keramida 
Responsible-Changed-By: keramida 
Responsible-Changed-When: Mon Feb 25 19:10:32 UTC 2008 
Responsible-Changed-Why:  
I'll handle the MFC of this to RELENG_X branches. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117944 
State-Changed-From-To: patched->closed 
State-Changed-By: keramida 
State-Changed-When: Wed Jan 21 17:46:26 UTC 2009 
State-Changed-Why:  
This has been merged both to 7-STABLE and 6-STABLE a few 
days ago. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=117944 
>Unformatted:
