add exists to ec_file

Signed-off-by: Jordan Wilberding <diginux@gmail.com>
This commit is contained in:
Eric Merritt 2012-10-19 08:57:23 -05:00 committed by Jordan Wilberding
parent a9f2a771f0
commit f77afd43c3

View file

@ -7,6 +7,7 @@
-module(ec_file). -module(ec_file).
-export([ -export([
exists/1,
copy/2, copy/2,
copy/3, copy/3,
insecure_mkdtemp/0, insecure_mkdtemp/0,
@ -40,6 +41,15 @@
%%%=================================================================== %%%===================================================================
%%% API %%% API
%%%=================================================================== %%%===================================================================
-spec exists(file:filename()) -> boolean().
exists(Filename) ->
case file:read_file_info(Filename) of
{ok, _} ->
true;
{error, _Reason} ->
false
end.
%% @doc copy an entire directory to another location. %% @doc copy an entire directory to another location.
-spec copy(file:name(), file:name(), Options::[option()]) -> ok | {error, Reason::term()}. -spec copy(file:name(), file:name(), Options::[option()]) -> ok | {error, Reason::term()}.
copy(From, To, []) -> copy(From, To, []) ->
@ -314,6 +324,16 @@ setup_base_and_target() ->
ok = file:write_file(NoName, DummyContents), ok = file:write_file(NoName, DummyContents),
{BaseDir, SourceDir, {Name1, Name2, Name3, NoName}}. {BaseDir, SourceDir, {Name1, Name2, Name3, NoName}}.
exists_test() ->
BaseDir = insecure_mkdtemp(),
SourceDir = filename:join([BaseDir, "source1"]),
NoName = filename:join([SourceDir, "noname"]),
ok = file:make_dir(SourceDir),
Name1 = filename:join([SourceDir, "fileone"]),
ok = file:write_file(Name1, <<"Testn">>),
?assertMatch(true, exists(Name1)),
?assertMatch(false, exists(NoName)).
find_test() -> find_test() ->
%% Create a directory in /tmp for the test. Clean everything afterwards %% Create a directory in /tmp for the test. Clean everything afterwards
{BaseDir, _SourceDir, {Name1, Name2, Name3, _NoName}} = setup_base_and_target(), {BaseDir, _SourceDir, {Name1, Name2, Name3, _NoName}} = setup_base_and_target(),