Discussion:
Bug in current versions of /usr/include/dirent.h ?
Frank Leonhardt
2021-04-20 18:21:45 UTC
Permalink
Weird one this! I'm currently using 12.2, but this appears to be a
problem in recent versions.

In FreeBSD 8, dirent.h contains the following:

/* structure describing an open directory. */
typedef struct _dirdesc {
        int     dd_fd;          /* file descriptor associated with
directory */
        long    dd_loc;         /* offset in current buffer */
        long    dd_size;        /* amount of data returned by
getdirentries */
        char    *dd_buf;        /* data buffer */
        int     dd_len;         /* size of data buffer */
        long    dd_seek;        /* magic cookie returned by
getdirentries */
        long    dd_rewind;      /* magic cookie for rewinding */
        int     dd_flags;       /* flags for readdir */
        struct pthread_mutex    *dd_lock;       /* lock */
        struct _telldir *dd_td; /* telldir position recording */
} DIR;

Nothing wrong there. It's the structure used by opendir() etc in the
standard C library.

In 12.2 we've not got a structure definition, but instead a forward
reference at line 87. DIR is typedefed on the following line. However,
nowhere can I find where this forward reference is later resolved -
meaning it isn't.

Has anyone got the faintest idea what's going on? I've had a look
through the source code to see where a DIR structure is used, and it may
as well be a (void *) - it's used as a handle returned by opendir() in
subsequent operations but never dereferenced.

And before anyone questions why de-referencing it is necessary - first
off, it's always been a published structure so  it's fair game.
Secondly, full access to the dd_fd field was handy. If access to this
structure is "deprecated" after 40 years, at the very least the handle
should have been typedefed into something safer than a forward reference.

So what's the sane explanation?

Thanks, Frank.
andrew clarke
2021-04-21 11:19:46 UTC
Permalink
Weird one this! I'm currently using 12.2, but this appears to be a problem
in recent versions.
/* structure describing an open directory. */
typedef struct _dirdesc {
        int     dd_fd;          /* file descriptor associated with directory
*/
        long    dd_loc;         /* offset in current buffer */
        long    dd_size;        /* amount of data returned by getdirentries
*/
        char    *dd_buf;        /* data buffer */
        int     dd_len;         /* size of data buffer */
        long    dd_seek;        /* magic cookie returned by getdirentries */
        long    dd_rewind;      /* magic cookie for rewinding */
        int     dd_flags;       /* flags for readdir */
        struct pthread_mutex    *dd_lock;       /* lock */
        struct _telldir *dd_td; /* telldir position recording */
} DIR;
Nothing wrong there. It's the structure used by opendir() etc in the
standard C library.
FWIW opendir() is a POSIX extension, not standard C.
In 12.2 we've not got a structure definition, but instead a forward
reference at line 87. DIR is typedefed on the following line. However,
nowhere can I find where this forward reference is later resolved - meaning
it isn't.
Has anyone got the faintest idea what's going on? I've had a look through
the source code to see where a DIR structure is used, and it may as well be
a (void *) - it's used as a handle returned by opendir() in subsequent
operations but never dereferenced.
POSIX doesn't guarantee DIR is a struct, just a "directory stream" type.

I suspect the FreeBSD folks have deprecated dereferencing DIR, perhaps in an effort to discourage people writing non-portable code.

See this commit from 2012:

"Hide DIR definition by making it an opaque struct typedef."

https://github.com/freebsd/freebsd-src/commit/0bb2aabf26842b91fbf14efa8e4e2030f0f4d2e4#diff-e2affeccb18763e4ad00c347282781dda3f60646e1e3f6c8fd534932fdc0ac8d
And before anyone questions why de-referencing it is necessary - first off,
it's always been a published structure so  it's fair game. Secondly, full
access to the dd_fd field was handy. If access to this structure is
"deprecated" after 40 years, at the very least the handle should have been
typedefed into something safer than a forward reference.
There is the dirfd() function if you really need access to the file descriptor.

DIR *dir;
dir = opendir(".");
if (dir)
{
int fd = dirfd(dir);
closedir(dir);
}

Andrew

Loading...