Skip to content

readdir support opposite of POSIX direction #1783

@rbtcollins

Description

@rbtcollins

POSIX readdir has reached a sensible point w.r.t. threads since the 2018 edition - https://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html

It is now guaranteed that the buffer is valid until the directory stream (the DIR *) is used in a subsequent posix call, or the calling thread is terminated.

"""The returned pointer, and pointers within the structure, might be invalidated or the structure or the storage areas might be overwritten by a subsequent call to readdir() on the same directory stream.""" plus """They shall not be affected by a call to readdir() on a different directory stream."""

As I read it, this is enough to stop using readdir_r entirely - which is needed to avoid the issues with readdir_r that have led to it being deprecated on a number of platforms. As long as we tie the lifetime of the dirent returned by readdir to the mutable borrow of the DIR * that is.

It does say 'readdir is not required to be thread safe', but in practice it is everywhere - per https://www.gnu.org/software/libc/manual/html_node/Reading_002fClosing-Directory.html

"""
Caution: The pointer returned by readdir points to a buffer within the DIR object. The data in that buffer will be overwritten by the next call to readdir. You must take care, for instance, to copy the d_name string if you need it later.

Because of this, it is not safe to share a DIR object among multiple threads, unless you use your own locking to ensure that no thread calls readdir while another thread is still using the data from the previous call. In the GNU C Library, it is safe to call readdir from multiple threads as long as each thread uses its own DIR object. POSIX.1-2008 does not require this to be safe, but we are not aware of any operating systems where it does not work.
"""

This also speaks to it being safe if the result from readdir is only accessed within the one mutable borrow of the DIR * - and their comment about POSIX.1-2008 is stale since as above 2018 now requires it to be safe.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions