2011-04-03 18:10:37 -05:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% @copyright (C) 2011, Erlware LLC
|
|
|
|
%%% @doc
|
|
|
|
%%% Helper functions for working with files.
|
|
|
|
%%% @end
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(ec_file).
|
|
|
|
|
|
|
|
-export([
|
2011-09-26 11:48:14 -05:00
|
|
|
copy/2,
|
|
|
|
copy/3,
|
2012-06-05 17:56:45 -05:00
|
|
|
insecure_mkdtemp/0,
|
2011-09-26 11:48:14 -05:00
|
|
|
mkdir_path/1,
|
2012-09-18 17:18:34 -07:00
|
|
|
mkdir_p/1,
|
2011-09-26 11:48:14 -05:00
|
|
|
find/2,
|
|
|
|
is_symlink/1,
|
|
|
|
remove/1,
|
|
|
|
remove/2,
|
|
|
|
md5sum/1,
|
|
|
|
read/1,
|
|
|
|
write/2,
|
|
|
|
write_term/2,
|
|
|
|
consult/1
|
|
|
|
]).
|
2011-04-03 18:10:37 -05:00
|
|
|
|
2011-04-22 09:30:03 -05:00
|
|
|
-export_type([
|
2011-09-26 11:48:14 -05:00
|
|
|
option/0
|
|
|
|
]).
|
2011-04-03 18:10:37 -05:00
|
|
|
|
|
|
|
-include_lib("kernel/include/file.hrl").
|
|
|
|
|
2011-04-22 09:30:03 -05:00
|
|
|
-define(CHECK_PERMS_MSG,
|
2011-09-26 11:48:14 -05:00
|
|
|
"Try checking that you have the correct permissions and try again~n").
|
2011-04-03 18:10:37 -05:00
|
|
|
|
|
|
|
%%============================================================================
|
|
|
|
%% Types
|
|
|
|
%%============================================================================
|
2012-09-04 20:27:19 -05:00
|
|
|
-type option() :: recursive.
|
|
|
|
-type void() :: ok.
|
2011-04-03 18:10:37 -05:00
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
%% @doc copy an entire directory to another location.
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec copy(file:name(), file:name(), Options::[option()]) -> void().
|
2011-04-03 18:10:37 -05:00
|
|
|
copy(From, To, []) ->
|
|
|
|
copy(From, To);
|
|
|
|
copy(From, To, [recursive] = Options) ->
|
|
|
|
case filelib:is_dir(From) of
|
2011-09-26 11:48:14 -05:00
|
|
|
false ->
|
|
|
|
copy(From, To);
|
|
|
|
true ->
|
|
|
|
make_dir_if_dir(To),
|
|
|
|
copy_subfiles(From, To, Options)
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc copy a file including timestamps,ownership and mode etc.
|
2012-09-05 17:01:11 -05:00
|
|
|
-spec copy(From::file:filename(), To::file:filename()) -> ok | {error, Reason::term()}.
|
2011-04-03 18:10:37 -05:00
|
|
|
copy(From, To) ->
|
2012-09-05 17:01:11 -05:00
|
|
|
case file:copy(From, To) of
|
|
|
|
{ok, _} ->
|
|
|
|
case file:read_file_info(From) of
|
|
|
|
{ok, FileInfo} ->
|
|
|
|
case file:write_file_info(To, FileInfo) of
|
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, WFError} ->
|
|
|
|
{error, {write_file_info_failed, WFError}}
|
|
|
|
end;
|
|
|
|
{error, RFError} ->
|
|
|
|
{error, {read_file_info_failed, RFError}}
|
|
|
|
end;
|
|
|
|
{error, Error} ->
|
|
|
|
{error, {copy_failed, Error}}
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc return an md5 checksum string or a binary. Same as unix utility of
|
|
|
|
%% same name.
|
|
|
|
-spec md5sum(string() | binary()) -> string().
|
|
|
|
md5sum(Value) ->
|
|
|
|
hex(binary_to_list(erlang:md5(Value))).
|
|
|
|
|
|
|
|
%% @doc delete a file. Use the recursive option for directories.
|
|
|
|
%% <pre>
|
|
|
|
%% Example: remove("./tmp_dir", [recursive]).
|
|
|
|
%% </pre>
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec remove(file:name(), Options::[option()]) -> ok | {error, Reason::term()}.
|
2011-04-03 18:10:37 -05:00
|
|
|
remove(Path, Options) ->
|
2012-09-05 17:01:11 -05:00
|
|
|
case lists:member(recursive, Options) of
|
|
|
|
false -> file:delete(Path);
|
|
|
|
true -> remove_recursive(Path, Options)
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
|
2012-09-05 17:01:11 -05:00
|
|
|
|
2011-04-22 09:30:03 -05:00
|
|
|
%% @doc delete a file.
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec remove(file:name()) -> ok | {error, Reason::term()}.
|
2011-04-03 18:10:37 -05:00
|
|
|
remove(Path) ->
|
|
|
|
remove(Path, []).
|
|
|
|
|
|
|
|
%% @doc indicates witha boolean if the path supplied refers to symlink.
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec is_symlink(file:name()) -> boolean().
|
2011-04-03 18:10:37 -05:00
|
|
|
is_symlink(Path) ->
|
2011-04-22 10:11:37 -05:00
|
|
|
case file:read_link_info(Path) of
|
2011-09-26 11:48:14 -05:00
|
|
|
{ok, #file_info{type = symlink}} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
|
2011-04-22 10:11:37 -05:00
|
|
|
|
2011-04-22 09:30:03 -05:00
|
|
|
%% @doc make a unique temorory directory. Similar function to BSD stdlib
|
2011-04-03 18:10:37 -05:00
|
|
|
%% function of the same name.
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec insecure_mkdtemp() -> TmpDirPath::file:name().
|
2012-06-05 17:56:45 -05:00
|
|
|
insecure_mkdtemp() ->
|
2012-06-05 17:54:46 -05:00
|
|
|
random:seed(now()),
|
|
|
|
UniqueNumber = erlang:integer_to_list(erlang:trunc(random:uniform() * 1000000000000)),
|
2011-04-03 18:10:37 -05:00
|
|
|
TmpDirPath =
|
2011-09-26 11:48:14 -05:00
|
|
|
filename:join([tmp(), lists:flatten([".tmp_dir", UniqueNumber])]),
|
2012-06-05 17:54:46 -05:00
|
|
|
|
2012-09-05 17:01:11 -05:00
|
|
|
case mkdir_path(TmpDirPath) of
|
|
|
|
ok -> TmpDirPath;
|
|
|
|
Error -> Error
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc Makes a directory including parent dirs if they are missing.
|
2012-09-05 17:01:11 -05:00
|
|
|
-spec mkdir_p(file:name()) -> ok | {error, Reason::term()}.
|
|
|
|
mkdir_p(Path) ->
|
2011-10-27 09:56:22 -05:00
|
|
|
%% 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
|
2011-04-03 18:10:37 -05:00
|
|
|
DirName = filename:join([filename:absname(Path), "tmp"]),
|
2012-09-05 17:01:11 -05:00
|
|
|
filelib:ensure_dir(DirName).
|
|
|
|
|
|
|
|
|
|
|
|
%% @doc Makes a directory including parent dirs if they are missing.
|
|
|
|
-spec mkdir_path(file:name()) -> ok | {error, Reason::term()}.
|
|
|
|
mkdir_path(Path) ->
|
|
|
|
mkdir_p(Path).
|
2011-04-22 09:30:03 -05:00
|
|
|
|
|
|
|
|
2011-04-03 18:10:37 -05:00
|
|
|
%% @doc consult an erlang term file from the file system.
|
|
|
|
%% Provide user readible exeption on failure.
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec consult(FilePath::file:name()) -> term().
|
2011-04-03 18:10:37 -05:00
|
|
|
consult(FilePath) ->
|
|
|
|
case file:consult(FilePath) of
|
2011-09-26 11:48:14 -05:00
|
|
|
{ok, [Term]} ->
|
|
|
|
Term;
|
2012-09-05 17:01:11 -05:00
|
|
|
Error ->
|
|
|
|
Error
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
%% @doc read a file from the file system. Provide UEX exeption on failure.
|
2012-09-05 17:01:11 -05:00
|
|
|
-spec read(FilePath::file:filename()) -> binary() | {error, Reason::term()}.
|
2011-04-03 18:10:37 -05:00
|
|
|
read(FilePath) ->
|
2012-09-05 17:01:11 -05:00
|
|
|
%% Now that we are moving away from exceptions again this becomes
|
|
|
|
%% a bit redundant but we want to be backwards compatible as much
|
|
|
|
%% as possible in the api.
|
|
|
|
file:read_file(FilePath).
|
|
|
|
|
2011-04-03 18:10:37 -05:00
|
|
|
|
|
|
|
%% @doc write a file to the file system. Provide UEX exeption on failure.
|
2012-09-05 17:01:11 -05:00
|
|
|
-spec write(FileName::file:filename(), Contents::string()) -> ok | {error, Reason::term()}.
|
2011-04-03 18:10:37 -05:00
|
|
|
write(FileName, Contents) ->
|
2012-09-05 17:01:11 -05:00
|
|
|
%% Now that we are moving away from exceptions again this becomes
|
|
|
|
%% a bit redundant but we want to be backwards compatible as much
|
|
|
|
%% as possible in the api.
|
|
|
|
file:write_file(FileName, Contents).
|
2011-04-03 18:10:37 -05:00
|
|
|
|
|
|
|
%% @doc write a term out to a file so that it can be consulted later.
|
2012-09-05 17:01:11 -05:00
|
|
|
-spec write_term(file:filename(), term()) -> ok | {error, Reason::term()}.
|
2011-04-03 18:10:37 -05:00
|
|
|
write_term(FileName, Term) ->
|
|
|
|
write(FileName, lists:flatten(io_lib:fwrite("~p. ", [Term]))).
|
|
|
|
|
|
|
|
%% @doc Finds files and directories that match the regexp supplied in
|
|
|
|
%% the TargetPattern regexp.
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec find(FromDir::file:name(), TargetPattern::string()) -> [file:name()].
|
2011-04-03 18:10:37 -05:00
|
|
|
find([], _) ->
|
|
|
|
[];
|
|
|
|
find(FromDir, TargetPattern) ->
|
|
|
|
case filelib:is_dir(FromDir) of
|
2011-09-26 11:48:14 -05:00
|
|
|
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
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal Functions
|
|
|
|
%%%===================================================================
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec find_in_subdirs(file:name(), string()) -> [file:name()].
|
2011-04-03 18:10:37 -05:00
|
|
|
find_in_subdirs(FromDir, TargetPattern) ->
|
|
|
|
lists:foldl(fun(CheckFromDir, Acc)
|
2011-09-26 11:48:14 -05:00
|
|
|
when CheckFromDir == FromDir ->
|
|
|
|
Acc;
|
|
|
|
(ChildFromDir, Acc) ->
|
|
|
|
case find(ChildFromDir, TargetPattern) of
|
|
|
|
[] -> Acc;
|
|
|
|
Res -> Res ++ Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[],
|
|
|
|
filelib:wildcard(filename:join(FromDir, "*"))).
|
2011-04-22 09:30:03 -05:00
|
|
|
|
2011-04-03 18:10:37 -05:00
|
|
|
|
2012-09-05 17:01:11 -05:00
|
|
|
-spec remove_recursive(file:name(), Options::list()) -> ok | {error, Reason::term()}.
|
2011-04-03 18:10:37 -05:00
|
|
|
remove_recursive(Path, Options) ->
|
|
|
|
case filelib:is_dir(Path) of
|
2011-09-26 11:48:14 -05:00
|
|
|
false ->
|
|
|
|
file:delete(Path);
|
|
|
|
true ->
|
|
|
|
lists:foreach(fun(ChildPath) ->
|
|
|
|
remove_recursive(ChildPath, Options)
|
|
|
|
end, filelib:wildcard(filename:join(Path, "*"))),
|
2012-09-05 17:01:11 -05:00
|
|
|
file:del_dir(Path)
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec tmp() -> file:name().
|
2011-04-03 18:10:37 -05:00
|
|
|
tmp() ->
|
|
|
|
case erlang:system_info(system_architecture) of
|
2011-09-26 11:48:14 -05:00
|
|
|
"win32" ->
|
|
|
|
"./tmp";
|
|
|
|
_SysArch ->
|
|
|
|
"/tmp"
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Copy the subfiles of the From directory to the to directory.
|
2012-09-04 20:27:19 -05:00
|
|
|
-spec copy_subfiles(file:name(), file:name(), [option()]) -> void().
|
2011-04-03 18:10:37 -05:00
|
|
|
copy_subfiles(From, To, Options) ->
|
2011-04-22 09:30:03 -05:00
|
|
|
Fun =
|
2011-09-26 11:48:14 -05:00
|
|
|
fun(ChildFrom) ->
|
|
|
|
ChildTo = filename:join([To, filename:basename(ChildFrom)]),
|
|
|
|
copy(ChildFrom, ChildTo, Options)
|
|
|
|
end,
|
2011-04-03 18:10:37 -05:00
|
|
|
lists:foreach(Fun, filelib:wildcard(filename:join(From, "*"))).
|
|
|
|
|
2012-09-05 17:01:11 -05:00
|
|
|
-spec make_dir_if_dir(file:name()) -> ok | {error, Reason::term()}.
|
2011-04-03 18:10:37 -05:00
|
|
|
make_dir_if_dir(File) ->
|
|
|
|
case filelib:is_dir(File) of
|
2011-09-26 11:48:14 -05:00
|
|
|
true -> ok;
|
2012-09-05 17:01:11 -05:00
|
|
|
false -> mkdir_path(File)
|
2011-04-03 18:10:37 -05:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% @doc convert a list of integers into hex.
|
2011-04-22 09:30:03 -05:00
|
|
|
-spec hex(string() | non_neg_integer()) -> string().
|
2011-04-03 18:10:37 -05:00
|
|
|
hex(L) when is_list (L) ->
|
|
|
|
lists:flatten([hex(I) || I <- L]);
|
|
|
|
hex(I) when I > 16#f ->
|
|
|
|
[hex0((I band 16#f0) bsr 4), hex0((I band 16#0f))];
|
|
|
|
hex(I) ->
|
|
|
|
[$0, hex0(I)].
|
|
|
|
|
|
|
|
hex0(10) -> $a;
|
|
|
|
hex0(11) -> $b;
|
|
|
|
hex0(12) -> $c;
|
|
|
|
hex0(13) -> $d;
|
|
|
|
hex0(14) -> $e;
|
|
|
|
hex0(15) -> $f;
|
|
|
|
hex0(I) -> $0 + I.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Test Functions
|
|
|
|
%%%===================================================================
|
|
|
|
|
|
|
|
-ifndef(NOTEST).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
|
|
|
setup_test() ->
|
2012-06-05 17:56:45 -05:00
|
|
|
Dir = insecure_mkdtemp(),
|
2012-06-05 17:55:07 -05:00
|
|
|
mkdir_path(Dir),
|
|
|
|
?assertMatch(false, is_symlink(Dir)),
|
|
|
|
?assertMatch(true, filelib:is_dir(Dir)).
|
2011-04-22 09:30:03 -05:00
|
|
|
|
2011-04-03 18:10:37 -05:00
|
|
|
md5sum_test() ->
|
2011-04-22 09:30:03 -05:00
|
|
|
?assertMatch("cfcd208495d565ef66e7dff9f98764da", md5sum("0")).
|
2011-04-03 18:10:37 -05:00
|
|
|
|
|
|
|
file_test() ->
|
2012-06-05 17:56:45 -05:00
|
|
|
Dir = insecure_mkdtemp(),
|
2012-06-05 17:55:07 -05:00
|
|
|
TermFile = filename:join(Dir, "ec_file/dir/file.term"),
|
|
|
|
TermFileCopy = filename:join(Dir, "ec_file/dircopy/file.term"),
|
|
|
|
filelib:ensure_dir(TermFile),
|
|
|
|
filelib:ensure_dir(TermFileCopy),
|
2011-04-03 18:10:37 -05:00
|
|
|
write_term(TermFile, "term"),
|
|
|
|
?assertMatch("term", consult(TermFile)),
|
2012-09-05 17:01:11 -05:00
|
|
|
?assertMatch({ok, <<"\"term\". ">>}, read(TermFile)),
|
2011-04-03 18:10:37 -05:00
|
|
|
copy(filename:dirname(TermFile),
|
2011-09-26 11:48:14 -05:00
|
|
|
filename:dirname(TermFileCopy),
|
|
|
|
[recursive]),
|
2011-04-03 18:10:37 -05:00
|
|
|
?assertMatch("term", consult(TermFileCopy)).
|
|
|
|
|
|
|
|
teardown_test() ->
|
2012-06-05 17:56:45 -05:00
|
|
|
Dir = insecure_mkdtemp(),
|
2012-06-05 17:55:07 -05:00
|
|
|
remove(Dir, [recursive]),
|
|
|
|
?assertMatch(false, filelib:is_dir(Dir)).
|
2011-04-03 18:10:37 -05:00
|
|
|
|
|
|
|
setup_base_and_target() ->
|
2012-06-05 17:56:45 -05:00
|
|
|
BaseDir = insecure_mkdtemp(),
|
2011-04-03 18:10:37 -05:00
|
|
|
DummyContents = <<"This should be deleted">>,
|
|
|
|
SourceDir = filename:join([BaseDir, "source"]),
|
|
|
|
ok = file:make_dir(SourceDir),
|
|
|
|
Name1 = filename:join([SourceDir, "fileone"]),
|
|
|
|
Name2 = filename:join([SourceDir, "filetwo"]),
|
|
|
|
Name3 = filename:join([SourceDir, "filethree"]),
|
|
|
|
NoName = filename:join([SourceDir, "noname"]),
|
|
|
|
|
|
|
|
ok = file:write_file(Name1, DummyContents),
|
|
|
|
ok = file:write_file(Name2, DummyContents),
|
|
|
|
ok = file:write_file(Name3, DummyContents),
|
|
|
|
ok = file:write_file(NoName, DummyContents),
|
|
|
|
{BaseDir, SourceDir, {Name1, Name2, Name3, NoName}}.
|
|
|
|
|
|
|
|
find_test() ->
|
2011-10-27 09:56:22 -05:00
|
|
|
%% Create a directory in /tmp for the test. Clean everything afterwards
|
2012-06-05 17:55:07 -05:00
|
|
|
{BaseDir, _SourceDir, {Name1, Name2, Name3, _NoName}} = setup_base_and_target(),
|
|
|
|
?assertMatch([Name2,
|
|
|
|
Name3,
|
|
|
|
Name1],
|
|
|
|
find(BaseDir, "file[a-z]+\$")),
|
|
|
|
remove(BaseDir, [recursive]).
|
2011-04-03 18:10:37 -05:00
|
|
|
|
|
|
|
-endif.
|