add ec_file:is_dir/1

use ec_file:is_dir/1 to identify symlinks which are directories
Ensure contents of symlinked directories are copied when using ec_file:copy/3
This commit is contained in:
Jamie Winsor 2014-07-09 13:24:10 -07:00
parent c2d67e6f76
commit 7a32f52e7d

View file

@ -16,6 +16,7 @@
mkdir_p/1, mkdir_p/1,
find/2, find/2,
is_symlink/1, is_symlink/1,
is_dir/1,
type/1, type/1,
real_dir_path/1, real_dir_path/1,
remove/1, remove/1,
@ -58,7 +59,7 @@ exists(Filename) ->
copy(From, To, []) -> copy(From, To, []) ->
copy(From, To); copy(From, To);
copy(From, To, [recursive] = Options) -> copy(From, To, [recursive] = Options) ->
case filelib:is_dir(From) of case is_dir(From) of
false -> false ->
copy(From, To); copy(From, To);
true -> true ->
@ -130,6 +131,15 @@ is_symlink(Path) ->
_ -> _ ->
false false
end. end.
is_dir(Path) ->
case file:read_file_info(Path) of
{ok, #file_info{type = directory}} ->
true;
_ ->
false
end.
%% @doc returns the type of the file. %% @doc returns the type of the file.
-spec type(file:name()) -> file | symlink | directory | undefined. -spec type(file:name()) -> file | symlink | directory | undefined.
type(Path) -> type(Path) ->
@ -140,10 +150,11 @@ type(Path) ->
case is_symlink(Path) of case is_symlink(Path) of
true -> true ->
symlink; symlink;
false -> case filelib:is_dir(Path) of false ->
true -> directory; case is_dir(Path) of
false -> undefined true -> directory;
end false -> undefined
end
end end
end. end.
@ -218,7 +229,7 @@ write_term(FileName, Term) ->
find([], _) -> find([], _) ->
[]; [];
find(FromDir, TargetPattern) -> find(FromDir, TargetPattern) ->
case filelib:is_dir(FromDir) of case is_dir(FromDir) of
false -> false ->
case re:run(FromDir, TargetPattern) of case re:run(FromDir, TargetPattern) of
{match, _} -> [FromDir]; {match, _} -> [FromDir];
@ -253,7 +264,7 @@ find_in_subdirs(FromDir, TargetPattern) ->
-spec remove_recursive(file:name(), Options::list()) -> ok | {error, Reason::term()}. -spec remove_recursive(file:name(), Options::list()) -> ok | {error, Reason::term()}.
remove_recursive(Path, Options) -> remove_recursive(Path, Options) ->
case filelib:is_dir(Path) of case is_dir(Path) of
false -> false ->
file:delete(Path); file:delete(Path);
true -> true ->
@ -284,7 +295,7 @@ copy_subfiles(From, To, Options) ->
-spec make_dir_if_dir(file:name()) -> ok | {error, Reason::term()}. -spec make_dir_if_dir(file:name()) -> ok | {error, Reason::term()}.
make_dir_if_dir(File) -> make_dir_if_dir(File) ->
case filelib:is_dir(File) of case is_dir(File) of
true -> ok; true -> ok;
false -> mkdir_path(File) false -> mkdir_path(File)
end. end.