According to Linux documentation (see e.g. https://linux.die.net/man/3/closedir):
A successful call to closedir() also closes the underlying file
descriptor associated with dirp.
Thus, calling close() after a successful call to closedir() is at
best redundant. Worse, should a different thread open a file in-between
the calls to closedir() and close() and get the same file descriptor,
the call to close() might actually close a different file than was
intended:
// Thread 1.
int fd = ::openat(AT_FDCWD, "foo", O_DIRECTORY);
DIR* stream = ::fdopendir(fd);
int result = ::closedir(stream);
assert(result == 0); // `fd` is now closed.
{
// Thread 2.
int fd2 = ::openat(AT_FDCWD, "BAR", O_DIRECTORY);
assert(fd2 == fd); // Quite possible.
}
// Thread 1.
result = ::close(fd);
assert(result == 0);
// The call to `close` succeeds but closes a different file ("BAR",
// which was never opened by this function).rdar://89251874