Sync fts(3) with FreeBSD.
authorPeter Avalos <pavalos@theshell.com>
Fri, 27 Feb 2009 08:26:46 +0000 (03:26 -0500)
committerPeter Avalos <pavalos@theshell.com>
Tue, 7 Apr 2009 07:09:33 +0000 (21:09 -1000)
commit59b616d3950abca6b6dbb402817f7beb7bb084a5
tree63ac0db9f1e3f9eb9bc63edc4cd1c1994cc8058c
parent96fea781285603d192ecaa5eaa6e56a5d11918c5
Sync fts(3) with FreeBSD.

Here's some of the relevant commit logs from FreeBSD:

FTSENT now avoids the use of the struct hack, thereby allowing future
extensions to the structure (e.g., for extended attributes) without
rebreaking the ABI.  FTSENT now contains a pointer to the parent stream,
which fts_compar() can then take advantage of, avoiding the undefined
behavior previously warned about.  As a consequence of this change, the
prototype of the comparison function passed to fts_open() has changed
to reflect the required amount of constness for its use.  All callers
in the tree are updated to use the correct prototype.

Comparison functions can now make use of the new parent pointer to
access the new stream-specific private data pointer, which is intended
to assist creation of reentrant library routines which use fts(3)
internally.

The FTS_NOSTAT option is an optimisation that reduces the number
of stat(2) calls by keeping an eye of the number of links a directory
has. It assumes that each subdirectory will have a hard link to its
parent, to represent the ".." node, and stops calling stat(2) when
all links are accounted for in a given directory.

This assumption is really only valid for UNIX-like filesystems: A
concrete example is NTFS. The NTFS "i-node" does contain a link
count, but most/all directories have a link count between 0 and 2
inclusive. The end result is that find on an NTFS volume won't
actually traverse the entire hierarchy of the directories passed
to it. (Those with a link count of two are not traversed at all)

The fix checks the "UFSness" of the filesystem before enabling the
optimisation.

In fts_build(), if we try to chdir and fail (e.g. due to lack of search
permission), try to continue in FTS_DONTCHDIR mode.  Of course this
won't work for long paths, but we can't descend more than one pathname
component beyond the directory anyway if we lack search permission.

Our fts(3) API, as inherited from 4.4BSD, suffers from integer
fields in FTS and FTSENT structs being too narrow.  In addition,
the narrow types creep from there into fts.c.  As a result, fts(3)
consumers, e.g., find(1) or rm(1), can't handle file trees an ordinary
user can create, which can have security implications.

Fix a file descriptor leak.
bin/cp/cp.c
bin/ls/ls.c
include/fts.h
lib/libc/gen/fts.3
lib/libc/gen/fts.c
usr.bin/find/find.c
usr.sbin/mtree/create.c