Do not interleave readdir() calls with deletion of directory entries.
Do not interleave readdir() calls with deletion of directory entries.

On macOS and some other non-Linux OSes, on some filesystems, readdir(dir) may
return NULL (with zero errno) after an entry in dir has been deleted. We thus
need to readdir() all directory entries before starting to delete them.

A benchmark (deleting 10 copies of the Linux kernel source) seems to show
that the new approach is approximately as fast as the previous one (slightly
faster on Linux, slightly slower on Mac), and in any case, is noticeably
faster than the system's /bin/rm.

Bazel's previous unix_jni DeleteTreesBelow implementation:
6.987 s (Linux/ext4); 89.44 s (Mac/APFS)

New Bazel unix_jni DeleteTreesBelow implementation:
6.971 s (Linux/ext4); 90.46 s (Mac/APFS)

`rm -rf`:
7.323 s (Linux/ext4); 99.09 s (Mac/APFS)

RELNOTES: None.
PiperOrigin-RevId: 346790610

ButterflyNetwork/bazel,katre/bazel,katre/bazel,bazelbuild/bazel,katre/bazel,bazelbuild/bazel,twitter-forks/bazel,ButterflyNetwork/bazel,twitter-forks/bazel,perezd/bazel,cushon/bazel,perezd/bazel,perezd/bazel,meteorcloudy/bazel,perezd/bazel,perezd/bazel,safarmer/bazel,bazelbuild/bazel,perezd/bazel,safarmer/bazel,perezd/bazel,cushon/bazel,safarmer/bazel,twitter-forks/bazel,katre/bazel,bazelbuild/bazel,twitter-forks/bazel,safarmer/bazel,twitter-forks/bazel,katre/bazel,safarmer/bazel,meteorcloudy/bazel,meteorcloudy/bazel,twitter-forks/bazel,meteorcloudy/bazel,bazelbuild/bazel,meteorcloudy/bazel,cushon/bazel,ButterflyNetwork/bazel,safarmer/bazel,ButterflyNetwork/bazel,meteorcloudy/bazel,twitter-forks/bazel,ButterflyNetwork/bazel,meteorcloudy/bazel,katre/bazel,cushon/bazel,cushon/bazel,ButterflyNetwork/bazel,cushon/bazel,bazelbuild/bazel