Adding File.exists? #276

merged 2 commits into from May 5, 2012


None yet

3 participants

philss commented May 3, 2012

Check if the file exists by taking last modified.

This pull request passes (merged 4da2aaa into 39c316d).


Hey @philss, you can use the current File.is_regular? function to check if a file exists or not. Wouldn't that be enough?

philss commented May 3, 2012

Hi @josevalim,
The File.regular? function is not enough.
I was thinking about check any kind of file, like unix sockets, devices(unix) and directories. The current File.regular? function only works for regular files. Should I include more tests?


@philss good point! so maybe you want to use the filelib:is_file() function from Erlang instead of last_modified()? I believe is_file() should return true for all the cases you mentioned above.

philss commented May 3, 2012

@josevalim I tried with filelib:is_file() but it doesn't works with sockets, devices and named pipes files.


Thanks for checking @philss, so yes, let's get this function in. Regarding the implementation, last_modified() simply calls file:read_file_info() in erlang source code, so maybe we could change our implementation to simply call it as well and check if { :ok, _ } is returned?

Thanks a lot!

@philss philss Changing File.exists? implementation.
Now using file:read_file_info() from Erlang.

This pull request passes (merged 7a6a711 into 39c316d).

philss commented May 5, 2012

Nice! Using file:read_file_info() now. I was in doubt about files without permission( eacces ). The File.exists? function returns false if I don't have permission to read the file. But I think this is ok. In Ruby, File.exists? have the same behavior.

Thanks @josevalim! :)

@josevalim josevalim merged commit c35019d into elixir-lang:master May 5, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment