file:list_dir/1 cannot lists dot(.) and dot-dot(..) directories

Yao Bao free7by@REDACTED
Thu Jul 1 02:28:49 CEST 2021


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 <free7by@REDACTED> 写道:
> 
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210701/98f4c5ca/attachment.htm>


More information about the erlang-questions mailing list