Subject: Re: problems with getdents() ??? vs opendir()
To: John Refling <johnr@imageworks.com>
From: Giles Lean <giles@nemeton.com.au>
List: netbsd-help
Date: 07/09/2000 10:12:10
> I've been using getdents() to scan the directory
> structure on my filesystem, and have noticed a few
> strange things:

> 2)  it appears as though the contents of the directory
>     file can contain references to deleted files.

The getdents(2) manual page says this:

     Entries may be separated by extra space.  The d_reclen entry may be used
     as an offset from the start of a dirent structure to the next structure,
     if any.

You should be using a struct dirent to access the getdents() results;
anything else makes non-portable assumptions about the structure
layout.  You might like to look at src/lib/libc/gen/readdir.c for an
example.

I can't see the problem in your code this morning except of course the
natural race condition if some other process removes a file after your
getdents() call reads the directory.

If you clean the code up so that it uses a struct dirent and compiles
cleanly with gcc -Wall by all means post again; it's not impossible
that there is a bug in getdents() ... but it's still looking unlikely.

>     Is opendir() and readdir() preferred over getdents()?

Maybe, maybe not.  They're more standard and they do some of the work
for you, but the price is that readdir() hides I/O errors from you.

Regards,

Giles