[comp.unix.wizards] directory functions

gwyn@BRL.ARPA (VLD/VMB) (05/09/87)

Re: BFS version of directory library returns no entries in directory

I'm willing to bet that your 4.2BSD variant OS creates, at least on
occasion, directories that don't conform to the 4.2BSD specifications.
I first encountered this about a week ago on our Alliant FX/8 running
Concentrix Release 2.0.  As you probably know, 4.2BSD (but not 4.3BSD)
would create an initially empty directory of length 24 rather than a
full 512 bytes.  We have discovered that occasionally activity on such
a directory can result in it expanding not to an integral multiple of
512 bytes, but instead to some such size as 536 or 1048 bytes (usually
24 bytes + an integral multiple of the blocksize).  The last entry in
such a directory contains a record-length field that claims its tail
end lives outside the space allocated for the directory file.  This is
clearly a kernel bug, but I don't know how to fix it other than by
upgrading to 4.3BSD.

Anyway, there is a simple work-around you can apply to the directory
library routines I posted to mod.sources.  In the getdents() emulation,
there are a couple of checks for directory corruption, one near the
very end of the source file.  The code is ultra-paranoid and will
refuse to return any directory entries if a directory block appears to
be corrupted.  Disable the last corruption check (use #ifndef BFS) to
permit the return of entries from such an illegally-formatted directory.