Adding File.exists? #276

Merged
merged 2 commits into from May 5, 2012

Projects

None yet

3 participants

@philss
Contributor
philss commented May 3, 2012

Check if the file exists by taking last modified.

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

Owner

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

Contributor
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?

Owner

@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.

Contributor
philss commented May 3, 2012

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

Owner

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?

http://www.erlang.org/doc/man/file.html#read_file_info-1

Thanks a lot!

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

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

Contributor
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