Leaf directories (Re: Why is find so slow)

Chris Torek chris at mimsy.umd.edu
Sun Oct 14 00:03:32 AEST 1990


In article <F&0u96p2 at cs.psu.edu> flee at guardian.cs.psu.edu (Felix Lee) writes:
>Leaf directories are directories with no subdirectories.  A leaf
>directory has a link count <= 2.

It is easier than that (and harder than this, which leaves out error
checking):

	d = opendir(dir);
	fstat(dirfd(d), &st);
	nsubdirs = st.st_nlink - 2;
	while ((dp = readdir(d)) != NULL) {
		if (need_stat_anyway || nsubdirs > 0) {
			get_stat(dir, dp->d_name, &st);
			if (S_ISDIR(st.st_mode))
				nsubdirs--;
		}
		...
	}

(pre-POSIX programmers pwill pneed, er, will need to write
	(st.st_mode & S_IFMT) == S_IFDIR
rather than S_ISDIR(st.st_mode)).

This is susceptible to error if the file system is active, but that
is no change; find has always had this problem.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix.shell mailing list