Date: Wed, 21 Apr 2021 21:19:46 +1000 From: andrew clarke <mail@ozzmosis.com> To: Frank Leonhardt <freebsd-doc@fjl.co.uk> Cc: freebsd-questions@freebsd.org Subject: Re: Bug in current versions of /usr/include/dirent.h ? Message-ID: <20210421111946.w2qsqvom5tse4w6m@ozzmosis.com> In-Reply-To: <fa2c4f47-f8e6-7553-6fff-2454f3cac2d8@fjl.co.uk> References: <fa2c4f47-f8e6-7553-6fff-2454f3cac2d8@fjl.co.uk>
next in thread | previous in thread | raw e-mail | index | archive | help
On 2021-04-20 19:21:45, Frank Leonhardt (freebsd-doc@fjl.co.uk) wrote: > 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. 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20210421111946.w2qsqvom5tse4w6m>