Re: file:list_dir/1 cannot lists dot(.) and dot-dot(..) directories
Yao Bao <[email protected]>
| Newsgroups | gmane.comp.lang.erlang.general |
|---|---|
| Message-ID | <[email protected]> |
Hello, By exploring the source code, I found file:list_dir/1 is implemented as a NIF, which is implemented in C. And, under UNIX, it uses opendir() and readdir() to get the directory entries, dot(.) and dot-dot(..) is ignored by design. The specified source code is here (efile_list_dir and is_ignored_name): https://github.com/erlang/otp/blob/master/erts/emulator/nifs/unix/unix_prim_file.c#L894 <https://github.com/erlang/otp/blob/master/erts/emulator/nifs/unix/unix_prim_file.c#L894> https://github.com/erlang/otp/blob/master/erts/emulator/nifs/unix/unix_prim_file.c#L884 <https://github.com/erlang/otp/blob/master/erts/emulator/nifs/unix/unix_prim_file.c#L884> But no comment saying why dot(.) and dot-dot(..) is ignored. Cheers, Yao > 在 2021年6月29日,08:29,Yao Bao <[email protected]> 写道: > > Hello, > > I'm trying to list all files in a directory, then I found file:list_dir/1, it works fine but dot(.) and dot-dot(..) is missing. > > I would expect the result from file:list_dir/1 in Erlang is the same as readdir(dirp) in C. > > Is there any (historical) reason why these two special directories missing from the return? > > Cheers, > Yao