BurntSushi / walkdir

Rust library for walking directories recursively.
The Unlicense
1.21k stars 106 forks source link

Fix & Improve doc comment on some structures #167

Open minghu6 opened 1 year ago

minghu6 commented 1 year ago

Fix staled comments on DirList, DirEntry, IntoIter

It's said that these structure relies on std::fs::DirEntry which is outdated since we use our crate::dent::DirEntry

Supply additional comment for difference between std DirEntry and our DirEntry

I've no idea if we've noticed that std DirEntry hold a reference of opened dir as the documented mentioned.

In a detail, on Unix implementaions:

ReadDir holds an Arc of InnerReadDir, and it shares it with DirEntry.

readdir

https://github.com/rust-lang/rust/blob/aa8e761defc245d08d2cf226786def8a8bb56e53/library/std/src/sys/unix/fs.rs#L1296

ReadDir

https://github.com/rust-lang/rust/blob/aa8e761defc245d08d2cf226786def8a8bb56e53/library/std/src/sys/unix/fs.rs#L231

Impl Iterator for ReadDir

https://github.com/rust-lang/rust/blob/aa8e761defc245d08d2cf226786def8a8bb56e53/library/std/src/sys/unix/fs.rs#L587

DirEntry

https://github.com/rust-lang/rust/blob/aa8e761defc245d08d2cf226786def8a8bb56e53/library/std/src/sys/unix/fs.rs#L257

The InnderReadDir holds the Dir which is an wrapper of platform native opaque DIR.

DIR

https://github.com/rust-lang/rust/blob/aa8e761defc245d08d2cf226786def8a8bb56e53/library/std/src/sys/unix/fs.rs#L244

When the InnerReadDir is dropped, it calls native closedir would close underlying fd associated.

impl Drop for Dir

https://github.com/rust-lang/rust/blob/aa8e761defc245d08d2cf226786def8a8bb56e53/library/std/src/sys/unix/fs.rs#L687

closedir

https://man7.org/linux/man-pages/man3/closedir.3.html https://pubs.opengroup.org/onlinepubs/9699919799.2016edition/toc.htm

In other words, If we keep the DirEntry, the InnerReader inner Arc wouldn't be dropped, that's: directory fd wouldn't be released even through we drop ReadDir through max_open control ! So, we could encounter Too many open files error when using collect on the iteration when walking a big enough directory !

However, luckily, we use our impl DirEntry instead of std version since it doesnt hold the fd reference. We just need to document it as additional doc comment before our DirEntry!