Minor reformatting/whitespace removal
Signed-off-by: Jordan Wilberding <jwilberding@gmail.com>
This commit is contained in:
parent
284ada2bd9
commit
694e805012
5 changed files with 279 additions and 279 deletions
246
src/ec_file.erl
246
src/ec_file.erl
|
@ -7,38 +7,38 @@
|
|||
-module(ec_file).
|
||||
|
||||
-export([
|
||||
copy/2,
|
||||
copy/3,
|
||||
mkdtemp/0,
|
||||
mkdir_path/1,
|
||||
find/2,
|
||||
is_symlink/1,
|
||||
remove/1,
|
||||
remove/2,
|
||||
md5sum/1,
|
||||
read/1,
|
||||
write/2,
|
||||
write_term/2,
|
||||
consult/1
|
||||
]).
|
||||
copy/2,
|
||||
copy/3,
|
||||
mkdtemp/0,
|
||||
mkdir_path/1,
|
||||
find/2,
|
||||
is_symlink/1,
|
||||
remove/1,
|
||||
remove/2,
|
||||
md5sum/1,
|
||||
read/1,
|
||||
write/2,
|
||||
write_term/2,
|
||||
consult/1
|
||||
]).
|
||||
|
||||
-export_type([
|
||||
path/0,
|
||||
option/0
|
||||
]).
|
||||
path/0,
|
||||
option/0
|
||||
]).
|
||||
|
||||
-include_lib("kernel/include/file.hrl").
|
||||
|
||||
%% User friendly exception message (remove line and module info once we
|
||||
%% get them in stack traces)
|
||||
-define(UEX(Exception, UMSG, UVARS),
|
||||
{uex, {?MODULE,
|
||||
?LINE,
|
||||
Exception,
|
||||
lists:flatten(io_lib:fwrite(UMSG, UVARS))}}).
|
||||
{uex, {?MODULE,
|
||||
?LINE,
|
||||
Exception,
|
||||
lists:flatten(io_lib:fwrite(UMSG, UVARS))}}).
|
||||
|
||||
-define(CHECK_PERMS_MSG,
|
||||
"Try checking that you have the correct permissions and try again~n").
|
||||
"Try checking that you have the correct permissions and try again~n").
|
||||
|
||||
%%============================================================================
|
||||
%% Types
|
||||
|
@ -55,20 +55,20 @@ copy(From, To, []) ->
|
|||
copy(From, To);
|
||||
copy(From, To, [recursive] = Options) ->
|
||||
case filelib:is_dir(From) of
|
||||
false ->
|
||||
copy(From, To);
|
||||
true ->
|
||||
make_dir_if_dir(To),
|
||||
copy_subfiles(From, To, Options)
|
||||
false ->
|
||||
copy(From, To);
|
||||
true ->
|
||||
make_dir_if_dir(To),
|
||||
copy_subfiles(From, To, Options)
|
||||
end.
|
||||
|
||||
%% @doc copy a file including timestamps,ownership and mode etc.
|
||||
-spec copy(From::string(), To::string()) -> ok.
|
||||
copy(From, To) ->
|
||||
try
|
||||
ec_file_copy(From, To)
|
||||
ec_file_copy(From, To)
|
||||
catch
|
||||
_C:E -> throw(?UEX({copy_failed, E}, ?CHECK_PERMS_MSG, []))
|
||||
_C:E -> throw(?UEX({copy_failed, E}, ?CHECK_PERMS_MSG, []))
|
||||
end.
|
||||
|
||||
%% @doc return an md5 checksum string or a binary. Same as unix utility of
|
||||
|
@ -84,9 +84,9 @@ md5sum(Value) ->
|
|||
-spec remove(path(), Options::[option()]) -> ok | {error, Reason::term()}.
|
||||
remove(Path, Options) ->
|
||||
try
|
||||
ok = ec_file_remove(Path, Options)
|
||||
ok = ec_file_remove(Path, Options)
|
||||
catch
|
||||
_C:E -> throw(?UEX({remove_failed, E}, ?CHECK_PERMS_MSG, []))
|
||||
_C:E -> throw(?UEX({remove_failed, E}, ?CHECK_PERMS_MSG, []))
|
||||
end.
|
||||
|
||||
%% @doc delete a file.
|
||||
|
@ -98,10 +98,10 @@ remove(Path) ->
|
|||
-spec is_symlink(path()) -> boolean().
|
||||
is_symlink(Path) ->
|
||||
case file:read_link_info(Path) of
|
||||
{ok, #file_info{type = symlink}} ->
|
||||
true;
|
||||
_ ->
|
||||
false
|
||||
{ok, #file_info{type = symlink}} ->
|
||||
true;
|
||||
_ ->
|
||||
false
|
||||
end.
|
||||
|
||||
|
||||
|
@ -111,27 +111,27 @@ is_symlink(Path) ->
|
|||
mkdtemp() ->
|
||||
UniqueNumber = integer_to_list(element(3, now())),
|
||||
TmpDirPath =
|
||||
filename:join([tmp(), lists:flatten([".tmp_dir", UniqueNumber])]),
|
||||
filename:join([tmp(), lists:flatten([".tmp_dir", UniqueNumber])]),
|
||||
try
|
||||
ok = mkdir_path(TmpDirPath),
|
||||
TmpDirPath
|
||||
ok = mkdir_path(TmpDirPath),
|
||||
TmpDirPath
|
||||
catch
|
||||
_C:E -> throw(?UEX({mkdtemp_failed, E}, ?CHECK_PERMS_MSG, []))
|
||||
_C:E -> throw(?UEX({mkdtemp_failed, E}, ?CHECK_PERMS_MSG, []))
|
||||
end.
|
||||
|
||||
|
||||
%% @doc Makes a directory including parent dirs if they are missing.
|
||||
-spec mkdir_path(path()) -> ok.
|
||||
mkdir_path(Path) ->
|
||||
% We are exploiting a feature of ensuredir that that creates all
|
||||
% directories up to the last element in the filename, then ignores
|
||||
% that last element. This way we ensure that the dir is created
|
||||
% and not have any worries about path names
|
||||
% We are exploiting a feature of ensuredir that that creates all
|
||||
% directories up to the last element in the filename, then ignores
|
||||
% that last element. This way we ensure that the dir is created
|
||||
% and not have any worries about path names
|
||||
DirName = filename:join([filename:absname(Path), "tmp"]),
|
||||
try
|
||||
ok = filelib:ensure_dir(DirName)
|
||||
ok = filelib:ensure_dir(DirName)
|
||||
catch
|
||||
_C:E -> throw(?UEX({mkdir_path_failed, E}, ?CHECK_PERMS_MSG, []))
|
||||
_C:E -> throw(?UEX({mkdir_path_failed, E}, ?CHECK_PERMS_MSG, []))
|
||||
end.
|
||||
|
||||
|
||||
|
@ -140,43 +140,43 @@ mkdir_path(Path) ->
|
|||
-spec consult(FilePath::path()) -> term().
|
||||
consult(FilePath) ->
|
||||
case file:consult(FilePath) of
|
||||
{ok, [Term]} ->
|
||||
Term;
|
||||
{error, Error} ->
|
||||
Msg = "The file at ~p~n" ++
|
||||
"is either not a valid Erlang term, does not to exist~n" ++
|
||||
"or you lack the permissions to read it. Please check~n" ++
|
||||
"to see if the file exists and that it has the correct~n" ++
|
||||
"permissions~n",
|
||||
throw(?UEX({failed_to_consult_file, {FilePath, Error}},
|
||||
Msg, [FilePath]))
|
||||
{ok, [Term]} ->
|
||||
Term;
|
||||
{error, Error} ->
|
||||
Msg = "The file at ~p~n" ++
|
||||
"is either not a valid Erlang term, does not to exist~n" ++
|
||||
"or you lack the permissions to read it. Please check~n" ++
|
||||
"to see if the file exists and that it has the correct~n" ++
|
||||
"permissions~n",
|
||||
throw(?UEX({failed_to_consult_file, {FilePath, Error}},
|
||||
Msg, [FilePath]))
|
||||
end.
|
||||
|
||||
%% @doc read a file from the file system. Provide UEX exeption on failure.
|
||||
-spec read(FilePath::string()) -> binary().
|
||||
read(FilePath) ->
|
||||
try
|
||||
{ok, FileBin} = file:read_file(FilePath),
|
||||
FileBin
|
||||
{ok, FileBin} = file:read_file(FilePath),
|
||||
FileBin
|
||||
catch
|
||||
_C:E -> throw(?UEX({read_failed, {FilePath, E}},
|
||||
"Read failed for the file ~p with ~p~n" ++
|
||||
?CHECK_PERMS_MSG,
|
||||
[FilePath, E]))
|
||||
_C:E -> throw(?UEX({read_failed, {FilePath, E}},
|
||||
"Read failed for the file ~p with ~p~n" ++
|
||||
?CHECK_PERMS_MSG,
|
||||
[FilePath, E]))
|
||||
end.
|
||||
|
||||
%% @doc write a file to the file system. Provide UEX exeption on failure.
|
||||
-spec write(FileName::string(), Contents::string()) -> ok.
|
||||
write(FileName, Contents) ->
|
||||
case file:write_file(FileName, Contents) of
|
||||
ok ->
|
||||
ok;
|
||||
{error, Reason} ->
|
||||
Msg = "Writing the file ~s to disk failed with reason ~p.~n" ++
|
||||
?CHECK_PERMS_MSG,
|
||||
throw(?UEX({write_file_failure, {FileName, Reason}},
|
||||
Msg,
|
||||
[FileName, Reason]))
|
||||
ok ->
|
||||
ok;
|
||||
{error, Reason} ->
|
||||
Msg = "Writing the file ~s to disk failed with reason ~p.~n" ++
|
||||
?CHECK_PERMS_MSG,
|
||||
throw(?UEX({write_file_failure, {FileName, Reason}},
|
||||
Msg,
|
||||
[FileName, Reason]))
|
||||
end.
|
||||
|
||||
%% @doc write a term out to a file so that it can be consulted later.
|
||||
|
@ -191,18 +191,18 @@ find([], _) ->
|
|||
[];
|
||||
find(FromDir, TargetPattern) ->
|
||||
case filelib:is_dir(FromDir) of
|
||||
false ->
|
||||
case re:run(FromDir, TargetPattern) of
|
||||
{match, _} -> [FromDir];
|
||||
_ -> []
|
||||
end;
|
||||
true ->
|
||||
FoundDir = case re:run(FromDir, TargetPattern) of
|
||||
{match, _} -> [FromDir];
|
||||
_ -> []
|
||||
end,
|
||||
List = find_in_subdirs(FromDir, TargetPattern),
|
||||
FoundDir ++ List
|
||||
false ->
|
||||
case re:run(FromDir, TargetPattern) of
|
||||
{match, _} -> [FromDir];
|
||||
_ -> []
|
||||
end;
|
||||
true ->
|
||||
FoundDir = case re:run(FromDir, TargetPattern) of
|
||||
{match, _} -> [FromDir];
|
||||
_ -> []
|
||||
end,
|
||||
List = find_in_subdirs(FromDir, TargetPattern),
|
||||
FoundDir ++ List
|
||||
end.
|
||||
%%%===================================================================
|
||||
%%% Internal Functions
|
||||
|
@ -210,53 +210,53 @@ find(FromDir, TargetPattern) ->
|
|||
-spec find_in_subdirs(path(), string()) -> [path()].
|
||||
find_in_subdirs(FromDir, TargetPattern) ->
|
||||
lists:foldl(fun(CheckFromDir, Acc)
|
||||
when CheckFromDir == FromDir ->
|
||||
Acc;
|
||||
(ChildFromDir, Acc) ->
|
||||
case find(ChildFromDir, TargetPattern) of
|
||||
[] -> Acc;
|
||||
Res -> Res ++ Acc
|
||||
end
|
||||
end,
|
||||
[],
|
||||
filelib:wildcard(filename:join(FromDir, "*"))).
|
||||
when CheckFromDir == FromDir ->
|
||||
Acc;
|
||||
(ChildFromDir, Acc) ->
|
||||
case find(ChildFromDir, TargetPattern) of
|
||||
[] -> Acc;
|
||||
Res -> Res ++ Acc
|
||||
end
|
||||
end,
|
||||
[],
|
||||
filelib:wildcard(filename:join(FromDir, "*"))).
|
||||
|
||||
-spec ec_file_remove(path(), [{atom(), any()}]) -> ok.
|
||||
ec_file_remove(Path, Options) ->
|
||||
case lists:member(recursive, Options) of
|
||||
false -> file:delete(Path);
|
||||
true -> remove_recursive(Path, Options)
|
||||
false -> file:delete(Path);
|
||||
true -> remove_recursive(Path, Options)
|
||||
end.
|
||||
|
||||
-spec remove_recursive(path(), Options::list()) -> ok.
|
||||
remove_recursive(Path, Options) ->
|
||||
case filelib:is_dir(Path) of
|
||||
false ->
|
||||
file:delete(Path);
|
||||
true ->
|
||||
lists:foreach(fun(ChildPath) ->
|
||||
remove_recursive(ChildPath, Options)
|
||||
end, filelib:wildcard(filename:join(Path, "*"))),
|
||||
ok = file:del_dir(Path)
|
||||
false ->
|
||||
file:delete(Path);
|
||||
true ->
|
||||
lists:foreach(fun(ChildPath) ->
|
||||
remove_recursive(ChildPath, Options)
|
||||
end, filelib:wildcard(filename:join(Path, "*"))),
|
||||
ok = file:del_dir(Path)
|
||||
end.
|
||||
|
||||
-spec tmp() -> path().
|
||||
tmp() ->
|
||||
case erlang:system_info(system_architecture) of
|
||||
"win32" ->
|
||||
"./tmp";
|
||||
_SysArch ->
|
||||
"/tmp"
|
||||
"win32" ->
|
||||
"./tmp";
|
||||
_SysArch ->
|
||||
"/tmp"
|
||||
end.
|
||||
|
||||
%% Copy the subfiles of the From directory to the to directory.
|
||||
-spec copy_subfiles(path(), path(), [option()]) -> ok.
|
||||
copy_subfiles(From, To, Options) ->
|
||||
Fun =
|
||||
fun(ChildFrom) ->
|
||||
ChildTo = filename:join([To, filename:basename(ChildFrom)]),
|
||||
copy(ChildFrom, ChildTo, Options)
|
||||
end,
|
||||
fun(ChildFrom) ->
|
||||
ChildTo = filename:join([To, filename:basename(ChildFrom)]),
|
||||
copy(ChildFrom, ChildTo, Options)
|
||||
end,
|
||||
lists:foreach(Fun, filelib:wildcard(filename:join(From, "*"))).
|
||||
|
||||
-spec ec_file_copy(path(), path()) -> ok.
|
||||
|
@ -268,8 +268,8 @@ ec_file_copy(From, To) ->
|
|||
-spec make_dir_if_dir(path()) -> ok.
|
||||
make_dir_if_dir(File) ->
|
||||
case filelib:is_dir(File) of
|
||||
true -> ok;
|
||||
false -> ok = mkdir_path(File)
|
||||
true -> ok;
|
||||
false -> ok = mkdir_path(File)
|
||||
end.
|
||||
|
||||
%% @doc convert a list of integers into hex.
|
||||
|
@ -298,10 +298,10 @@ hex0(I) -> $0 + I.
|
|||
|
||||
setup_test() ->
|
||||
case filelib:is_dir("/tmp/ec_file") of
|
||||
true ->
|
||||
remove("/tmp/ec_file", [recursive]);
|
||||
false ->
|
||||
ok
|
||||
true ->
|
||||
remove("/tmp/ec_file", [recursive]);
|
||||
false ->
|
||||
ok
|
||||
end,
|
||||
mkdir_path("/tmp/ec_file/dir"),
|
||||
?assertMatch(false, is_symlink("/tmp/ec_file/dir")),
|
||||
|
@ -318,8 +318,8 @@ file_test() ->
|
|||
?assertMatch("term", consult(TermFile)),
|
||||
?assertMatch(<<"\"term\". ">>, read(TermFile)),
|
||||
copy(filename:dirname(TermFile),
|
||||
filename:dirname(TermFileCopy),
|
||||
[recursive]),
|
||||
filename:dirname(TermFileCopy),
|
||||
[recursive]),
|
||||
?assertMatch("term", consult(TermFileCopy)).
|
||||
|
||||
teardown_test() ->
|
||||
|
@ -343,18 +343,18 @@ setup_base_and_target() ->
|
|||
{BaseDir, SourceDir, {Name1, Name2, Name3, NoName}}.
|
||||
|
||||
find_test() ->
|
||||
% Create a directory in /tmp for the test. Clean everything afterwards
|
||||
% Create a directory in /tmp for the test. Clean everything afterwards
|
||||
|
||||
{setup,
|
||||
fun setup_base_and_target/0,
|
||||
fun ({BaseDir, _, _}) ->
|
||||
ewl_file:delete_dir(BaseDir)
|
||||
ewl_file:delete_dir(BaseDir)
|
||||
end,
|
||||
fun ({BaseDir, _, {Name1, Name2, Name3, _}}) ->
|
||||
?assertMatch([Name2,
|
||||
Name3,
|
||||
Name1],
|
||||
ewl_file:find(BaseDir, "file[a-z]+\$"))
|
||||
end}.
|
||||
?assertMatch([Name2,
|
||||
Name3,
|
||||
Name1],
|
||||
ewl_file:find(BaseDir, "file[a-z]+\$"))
|
||||
end}.
|
||||
|
||||
-endif.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue