From jaakko@saunalahti.fi  Wed May 13 17:03:04 2009
Return-Path: <jaakko@saunalahti.fi>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id B98CE10656B5
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 13 May 2009 17:03:04 +0000 (UTC)
	(envelope-from jaakko@saunalahti.fi)
Received: from gw01.mail.saunalahti.fi (gw01.mail.saunalahti.fi [195.197.172.115])
	by mx1.freebsd.org (Postfix) with ESMTP id 4B4398FC2B
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 13 May 2009 17:03:03 +0000 (UTC)
	(envelope-from jaakko@saunalahti.fi)
Received: from ws64.jh.dy.fi (a91-153-125-115.elisa-laajakaista.fi [91.153.125.115])
	by gw01.mail.saunalahti.fi (Postfix) with ESMTP id D3D00151A8F
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 13 May 2009 20:03:01 +0300 (EEST)
Received: from ws64.jh.dy.fi (localhost [127.0.0.1])
	by ws64.jh.dy.fi (8.14.3/8.14.3) with ESMTP id n4DH31XI015333
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 13 May 2009 20:03:01 +0300 (EEST)
	(envelope-from jaakko@ws64.jh.dy.fi)
Received: (from jaakko@localhost)
	by ws64.jh.dy.fi (8.14.3/8.14.3/Submit) id n4DH31KN015332;
	Wed, 13 May 2009 20:03:01 +0300 (EEST)
	(envelope-from jaakko)
Message-Id: <200905131703.n4DH31KN015332@ws64.jh.dy.fi>
Date: Wed, 13 May 2009 20:03:01 +0300 (EEST)
From: Jaakko Heinonen <jh@saunalahti.fi>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [patch] fts(3) FTS_NOCHDIR misbehavior with empty directory
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         134513
>Category:       kern
>Synopsis:       [libc] [patch] fts(3) FTS_NOCHDIR misbehavior with empty directory
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jh
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed May 13 17:10:01 UTC 2009
>Closed-Date:    Sun Mar 28 14:25:21 UTC 2010
>Last-Modified:  Sun Mar 28 14:25:21 UTC 2010
>Originator:     Jaakko Heinonen
>Release:        FreeBSD 7.2-RELEASE / 8.0-CURRENT
>Organization:
>Environment:
FreeBSD 8.0-CURRENT r191987

>Description:
If you give an initial path name for fts_open() with trailing slash fts(3)
will strip the slash in post-order visit although the fts(3) manual page
states that the FTSENT structure will be unchanged from pre-order visit.
This happens only when the directory is empty and FTS_NOCHDIR option is used.

See also PR bin/133907.

>How-To-Repeat:
$ cc -Wall -Wextra -O ftstest.c -o ftstest
(Compile the test program below.)
$ mkdir x
$ ./ftstest x/
level: 0 path: x/ info: 1
level: 0 path: x info: 6
(Notice missing trailing slash in post-order.)

%%%
#include <sys/stat.h>
#include <sys/types.h>

#include <fts.h>
#include <stdio.h>

int
main(int argc, char **argv)
{
	FTS *fts;
	FTSENT *ftsent;
	char *fts_argv[] = {".", NULL};

	if (argc == 2)
		fts_argv[0] = argv[1];

	fts = fts_open(fts_argv, FTS_NOCHDIR | FTS_PHYSICAL, NULL);

	while ((ftsent = fts_read(fts)) != NULL) {
		printf("level: %ld path: %s info: %d\n",
		    (long)ftsent->fts_level,
		    ftsent->fts_path,
		    ftsent->fts_info);
	}

	fts_close(fts);

	return (0);
}
%%%

>Fix:
--- fts-NOCHDIR-trailing-slash.diff begins here ---
Index: lib/libc/gen/fts.3
===================================================================
--- lib/libc/gen/fts.3	(revision 191911)
+++ lib/libc/gen/fts.3	(working copy)
@@ -195,7 +195,7 @@ which was not specified as a file name t
 .Dv FTS_SEEDOT ) .
 .It Dv FTS_DP
 A directory being visited in post-order.
-The contents of the
+The other fields of the
 .Vt FTSENT
 structure will be unchanged from when
 it was returned in pre-order, i.e., with the
Index: lib/libc/gen/fts.c
===================================================================
--- lib/libc/gen/fts.c	(revision 191911)
+++ lib/libc/gen/fts.c	(working copy)
@@ -836,11 +836,8 @@ mem1:				saved_errno = errno;
 	 * If not changing directories, reset the path back to original
 	 * state.
 	 */
-	if (ISSET(FTS_NOCHDIR)) {
-		if (len == sp->fts_pathlen || nitems == 0)
-			--cp;
-		*cp = '\0';
-	}
+	if (ISSET(FTS_NOCHDIR))
+		sp->fts_path[cur->fts_pathlen] = '\0';
 
 	/*
 	 * If descended after called from fts_children or after called from
--- fts-NOCHDIR-trailing-slash.diff ends here ---

The patch also clarifies the manual page. fts_info info field has always
different value in post-order. Thus it's not exactly true that the structure
will be unchanged.


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->jh 
Responsible-Changed-By: jh 
Responsible-Changed-When: Mon Nov 23 13:47:00 UTC 2009 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=134513 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/134513: commit references a PR
Date: Thu, 26 Nov 2009 19:11:56 +0000 (UTC)

 Author: jh
 Date: Thu Nov 26 19:11:44 2009
 New Revision: 199844
 URL: http://svn.freebsd.org/changeset/base/199844
 
 Log:
   Reset path name back to original correctly in fts_build() when
   FTS_NOCHDIR option is used. fts_build() could strip a trailing slash
   from path name in post-order visit if a path pointing to an empty
   directory was given for fts_open().
   
   PR:		bin/133907, kern/134513
   Reviewed by:	das
   Approved by:	trasz (mentor)
   MFC after:	1 month
 
 Modified:
   head/lib/libc/gen/fts.c
 
 Modified: head/lib/libc/gen/fts.c
 ==============================================================================
 --- head/lib/libc/gen/fts.c	Thu Nov 26 19:09:10 2009	(r199843)
 +++ head/lib/libc/gen/fts.c	Thu Nov 26 19:11:44 2009	(r199844)
 @@ -842,11 +842,8 @@ mem1:				saved_errno = errno;
  	 * If not changing directories, reset the path back to original
  	 * state.
  	 */
 -	if (ISSET(FTS_NOCHDIR)) {
 -		if (len == sp->fts_pathlen || nitems == 0)
 -			--cp;
 -		*cp = '\0';
 -	}
 +	if (ISSET(FTS_NOCHDIR))
 +		sp->fts_path[cur->fts_pathlen] = '\0';
  
  	/*
  	 * If descended after called from fts_children or after called from
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: jh 
State-Changed-When: Thu Nov 26 19:35:27 UTC 2009 
State-Changed-Why:  
Patched in head (r199844). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=134513 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: kern/134513: commit references a PR
Date: Wed, 30 Dec 2009 17:22:19 +0000 (UTC)

 Author: jh
 Date: Wed Dec 30 17:22:00 2009
 New Revision: 201263
 URL: http://svn.freebsd.org/changeset/base/201263
 
 Log:
   MFC r199844:
   
   Reset path name back to original correctly in fts_build() when
   FTS_NOCHDIR option is used. fts_build() could strip a trailing slash
   from path name in post-order visit if a path pointing to an empty
   directory was given for fts_open().
   
   PR:		bin/133907, kern/134513
   Approved by:	trasz (mentor)
 
 Modified:
   stable/8/lib/libc/gen/fts.c
 Directory Properties:
   stable/8/lib/libc/   (props changed)
   stable/8/lib/libc/stdtime/   (props changed)
 
 Modified: stable/8/lib/libc/gen/fts.c
 ==============================================================================
 --- stable/8/lib/libc/gen/fts.c	Wed Dec 30 17:16:49 2009	(r201262)
 +++ stable/8/lib/libc/gen/fts.c	Wed Dec 30 17:22:00 2009	(r201263)
 @@ -836,11 +836,8 @@ mem1:				saved_errno = errno;
  	 * If not changing directories, reset the path back to original
  	 * state.
  	 */
 -	if (ISSET(FTS_NOCHDIR)) {
 -		if (len == sp->fts_pathlen || nitems == 0)
 -			--cp;
 -		*cp = '\0';
 -	}
 +	if (ISSET(FTS_NOCHDIR))
 +		sp->fts_path[cur->fts_pathlen] = '\0';
  
  	/*
  	 * If descended after called from fts_children or after called from
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: jh 
State-Changed-When: Sun Mar 28 14:25:20 UTC 2010 
State-Changed-Why:  
Fixed in head and stable/8. 

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