diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rcl_app_discovery.erl | 82 | ||||
-rw-r--r-- | src/rcl_dscv_util.erl | 144 | ||||
-rw-r--r-- | src/rcl_rel_discovery.erl | 75 |
3 files changed, 208 insertions, 93 deletions
diff --git a/src/rcl_app_discovery.erl b/src/rcl_app_discovery.erl index 20df1cf..43557c4 100644 --- a/src/rcl_app_discovery.erl +++ b/src/rcl_app_discovery.erl @@ -53,39 +53,33 @@ format_error(ErrorDetails) %%% Internal Functions %%%=================================================================== resolve_app_metadata(State, LibDirs) -> - AppMeta0 = lists:flatten(ec_plists:map(fun(LibDir) -> - discover_dir([], LibDir) - end, LibDirs)), - AppMeta1 = setup_overrides(State, AppMeta0), - - Errors = [case El of - {error, Ret} -> Ret; - _ -> El - end - || El <- AppMeta1, - case El of - {error, _} -> + AppMeta0 = lists:flatten(rcl_dscv_util:do(fun discover_dir/2, LibDirs)), + case [case Err of + {error, Ret} -> + Ret + end + || Err <- AppMeta0, + case Err of + {error, _} -> true; _ -> false - end], - - case Errors of + end] of [] -> - AppMeta2 = lists:flatten(AppMeta1), + AppMeta1 = [App || {ok, App} <- setup_overrides(State, AppMeta0)], rcl_log:debug(rcl_state:log(State), fun() -> ["Resolved the following OTP Applications from the system: \n", - [[rcl_app_info:format(1, App), "\n"] || App <- AppMeta2]] + [[rcl_app_info:format(1, App), "\n"] || App <- AppMeta1]] end), - {ok, AppMeta2}; - _ -> + {ok, AppMeta1}; + Errors -> ?RCL_ERROR(Errors) end. app_name({error, _}) -> undefined; -app_name(AppMeta) -> +app_name({ok, AppMeta}) -> rcl_app_info:name(AppMeta). setup_overrides(State, AppMetas0) -> @@ -94,16 +88,15 @@ setup_overrides(State, AppMetas0) -> not lists:keymember(app_name(AppMeta), 1, Overrides)], [case is_valid_otp_app(filename:join([FileName, "ebin", erlang:atom_to_list(AppName) ++ ".app"])) of - [] -> + {noresult, false} -> {error, {invalid_override, AppName, FileName}}; Error = {error, _} -> Error; - App -> - rcl_app_info:link(App, true) + {ok, App} -> + {ok, rcl_app_info:link(App, true)} end || {AppName, FileName} <- Overrides] ++ AppMetas1. - -spec format_detail(ErrorDetail::term()) -> iolist(). format_detail({error, {invalid_override, AppName, FileName}}) -> io_lib:format("Override {~p, ~p} is not a valid OTP App. Perhaps you forgot to build it?", @@ -127,27 +120,16 @@ format_detail({unversioned_app, AppDir, _AppName}) -> format_detail({app_info_error, {Module, Detail}}) -> Module:format_error(Detail). --spec discover_dir([file:name()], - file:name()) -> - [rcl_app_info:t() | {error, Reason::term()}] | - rcl_app_info:t() | {error, Reason::term()}. -discover_dir(IgnoreDirs, File) -> - case (not lists:member(File, IgnoreDirs)) - andalso filelib:is_dir(File) of - true -> - case file:list_dir(File) of - {error, Reason} -> - {error, {accessing, File, Reason}}; - {ok, List} -> - ec_plists:map(fun(LibDir) -> - discover_dir(IgnoreDirs, LibDir) - end, [filename:join([File, Dir]) || Dir <- List]) - end; - false -> - is_valid_otp_app(File) - end. +-spec discover_dir([file:name()], directory | file) -> + {ok, rcl_app_info:t()} | {error, Reason::term()}. +discover_dir(_File, directory) -> + {noresult, true}; +discover_dir(File, file) -> + is_valid_otp_app(File). + +-spec is_valid_otp_app(file:name()) -> {ok, rcl_app_info:t()} | {error, Reason::term()} | + {noresult, false}. --spec is_valid_otp_app(file:name()) -> rcl_app_info:t() | {error, Reason::term()} | []. is_valid_otp_app(File) -> %% Is this an ebin dir? EbinDir = filename:dirname(File), @@ -157,14 +139,14 @@ is_valid_otp_app(File) -> true -> has_at_least_one_beam(EbinDir, File); false -> - [] + {noresult, false} end; _ -> - [] + {noresult, false} end. -spec has_at_least_one_beam(file:name(), file:filename()) -> - rcl_app_info:t() | {error, Reason::term()}. + {ok, rcl_app_info:t()} | {error, Reason::term()}. has_at_least_one_beam(EbinDir, File) -> case file:list_dir(EbinDir) of {ok, List} -> @@ -179,7 +161,7 @@ has_at_least_one_beam(EbinDir, File) -> end. -spec gather_application_info(file:name(), file:filename()) -> - rcl_app_info:t() | {error, Reason::term()}. + {ok, rcl_app_info:t()} | {error, Reason::term()}. gather_application_info(EbinDir, File) -> AppDir = filename:dirname(EbinDir), case file:consult(File) of @@ -192,7 +174,7 @@ gather_application_info(EbinDir, File) -> end. -spec get_vsn(file:name(), atom(), proplists:proplist()) -> - rcl_app_info:t() | {error, Reason::term()}. + {ok, rcl_app_info:t()} | {error, Reason::term()}. get_vsn(AppDir, AppName, AppDetail) -> case proplists:get_value(vsn, AppDetail) of undefined -> @@ -200,7 +182,7 @@ get_vsn(AppDir, AppName, AppDetail) -> AppVsn -> case get_deps(AppDir, AppName, AppVsn, AppDetail) of {ok, App} -> - App; + {ok, App}; {error, Detail} -> {error, {app_info_error, Detail}} end diff --git a/src/rcl_dscv_util.erl b/src/rcl_dscv_util.erl new file mode 100644 index 0000000..4fb6126 --- /dev/null +++ b/src/rcl_dscv_util.erl @@ -0,0 +1,144 @@ +%% -*- erlang-indent-level: 4; indent-tabs-mode: nil; fill-column: 80 -*- +%%% Copyright 2012 Erlware, LLC. All Rights Reserved. +%%% +%%% This file is provided to you under the Apache License, +%%% Version 2.0 (the "License"); you may not use this file +%%% except in compliance with the License. You may obtain +%%% a copy of the License at +%%% +%%% http://www.apache.org/licenses/LICENSE-2.0 +%%% +%%% Unless required by applicable law or agreed to in writing, +%%% software distributed under the License is distributed on an +%%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +%%% KIND, either express or implied. See the License for the +%%% specific language governing permissions and limitations +%%% under the License. +%%%--------------------------------------------------------------------------- +%%% @author Eric Merritt <[email protected]> +%%% @copyright (C) 2012 Erlware, LLC. +%%% +%%% @doc This provider uses the lib_dir setting of the state. It searches the +%%% Lib Dirs looking for all OTP Applications that are available. When it finds +%%% those OTP Applications it loads the information about them and adds them to +%%% the state of available apps. This implements the rcl_provider behaviour. +-module(rcl_dscv_util). + +-export([do/2, + format_error/1]). + +-include_lib("relcool/include/relcool.hrl"). + +%%============================================================================ +%% Types +%%============================================================================ + +-type process_fun(Result) :: fun((filename:name(), file | directory) -> + {ok, Result} | + {error, term()} | + {ok, Result, Recurse::boolean()} | + {noresult, Recurse::boolean()} | + {error, term()}). + +%%============================================================================ +%% API +%%============================================================================ + +%% @doc recursively dig down into the library directories specified in the state +%% looking for OTP Applications +-spec do(process_fun([term()] | term()), [filename:name()]) -> + [term() | {error, term()}]. +do(ProcessDir, LibDirs) -> + lists:flatten(ec_plists:map(fun(LibDir) -> + discover_dir(ProcessDir, LibDir, + ec_file:type(LibDir)) + end, LibDirs)). + +-spec format_error([ErrorDetail::term()]) -> iolist(). +format_error(ErrorDetails) + when erlang:is_list(ErrorDetails) -> + [[format_detail(ErrorDetail), "\n"] || ErrorDetail <- ErrorDetails]. + +%%%=================================================================== +%%% Internal Functions +%%%=================================================================== +-spec format_detail(ErrorDetail::term()) -> iolist(). +format_detail({accessing, File, eaccess}) -> + io_lib:format("permission denied accessing file ~s", [File]); +format_detail({accessing, File, Type}) -> + io_lib:format("error (~p) accessing file ~s", [Type, File]); +format_detail({not_a_directory, EbinDir}) -> + io_lib:format("~s is not a directory when it should be a directory", [EbinDir]). + +-spec discover_dir(process_fun([term()] | term()), + file:name(), + directory | file | symlink) -> + [{ok, term()} + | {error, Reason::term()}] + | [] + | {ok, term()} + | {error, Reason::term()}. +discover_dir(ProcessDir, File, directory) -> + case ProcessDir(File, directory) of + {ok, Result, true} -> + [{ok, Result} | recurse(ProcessDir, File)]; + {noresult, true} -> + recurse(ProcessDir, File); + {ok, Result, _} -> + [{ok, Result}]; + {noresult, _} -> + []; + Err = {error, _} -> + [Err] + end; +discover_dir(ProcessDir, File, file) -> + Res = ProcessDir(File, file), + io:format("Result -> ~p~n", [Res]), + case ProcessDir(File, file) of + {ok, Result} -> + [{ok, Result}]; + {noresult, _} -> + []; + Err = {error, _} -> + [Err] + end; +discover_dir(ProcessDir, File, symlink) -> + case filelib:is_dir(File) of + false -> + discover_dir(ProcessDir, File, file); + true -> + discover_real_symlink_dir(ProcessDir, File) + end. + +discover_real_symlink_dir(ProcessDir, File) -> + {ok, CurCwd} = file:get_cwd(), + ok = file:set_cwd(File), + {ok, ActualRealDir} = file:get_cwd(), + ok = file:set_cwd(CurCwd), + lists:prefix(iolist_to_list(filename:absname(ActualRealDir)), + iolist_to_list(filename:absname(File))), + case ProcessDir(File, directory) of + {ok, Result, true} -> + [{ok, Result} | recurse(ProcessDir, File)]; + {noresult, true} -> + recurse(ProcessDir, File); + {ok, Result, _} -> + [{ok, Result}]; + {noresult, _} -> + []; + Err = {error, _} -> + [Err] + end. + +recurse(ProcessDir, File) -> + case file:list_dir(File) of + {error, Reason} -> + {error, {accessing, File, Reason}}; + {ok, List} -> + ec_plists:map(fun(LibDir) -> + discover_dir(ProcessDir, LibDir, ec_file:type(LibDir)) + end, [filename:join([File, Dir]) || Dir <- List]) + end. + +iolist_to_list(IoList) -> + erlang:binary_to_list(erlang:iolist_to_binary(IoList)). diff --git a/src/rcl_rel_discovery.erl b/src/rcl_rel_discovery.erl index 0f923a0..38d7913 100644 --- a/src/rcl_rel_discovery.erl +++ b/src/rcl_rel_discovery.erl @@ -54,31 +54,32 @@ format_error(ErrorDetails) %%% Internal Functions %%%=================================================================== resolve_rel_metadata(State, LibDirs, AppMeta) -> - ReleaseMeta0 = - lists:flatten(ec_plists:map(fun(LibDir) -> - discover_dir([], LibDir, AppMeta) - end, LibDirs)), - Errors = [case El of - {error, Ret} -> Ret; - _ -> El - end - || El <- ReleaseMeta0, - case El of - {error, _} -> - true; - _ -> - false - end], + ReleaseMeta0 = lists:flatten(rcl_dscv_util:do(fun(LibDir, FileType) -> + discover_dir(LibDir, + AppMeta, + FileType) + end, LibDirs)), + + Errors = [case El of + {error, Ret} -> Ret; + _ -> El + end + || El <- ReleaseMeta0, + case El of + {error, _} -> + true; + _ -> + false + end], case Errors of [] -> - ReleaseMeta1 = lists:flatten(ReleaseMeta0), rcl_log:debug(rcl_state:log(State), fun() -> ["Resolved the following OTP Releases from the system: \n", - [[rcl_release:format(1, Rel), "\n"] || Rel <- ReleaseMeta1]] + [[rcl_release:format(1, Rel), "\n"] || Rel <- ReleaseMeta0]] end), - {ok, ReleaseMeta1}; + {ok, ReleaseMeta0}; _ -> ?RCL_ERROR(Errors) end. @@ -89,38 +90,26 @@ format_detail({accessing, File, eaccess}) -> format_detail({accessing, File, Type}) -> io_lib:format("error (~p) accessing file ~s", [Type, File]). --spec discover_dir([file:name()], file:name(), [rcl_app_info:t()]) -> - [rcl_release:t() | {error, Reason::term()}] - | rcl_release:t() - | {error, Reason::term()}. -discover_dir(IgnoreDirs, File, AppMeta) -> - case (not lists:member(File, IgnoreDirs)) - andalso filelib:is_dir(File) of - true -> - case file:list_dir(File) of - {error, Reason} -> - {error, {accessing, File, Reason}}; - {ok, List} -> - ec_plists:map(fun(LibDir) -> - discover_dir(IgnoreDirs, LibDir, AppMeta) - end, - [filename:join([File, Dir]) || Dir <- List]) - end; - false -> - is_valid_release(File, AppMeta) - end. +-spec discover_dir(file:name(), [rcl_app_info:t()], directory | file) -> + {ok, rcl_release:t()} + | {error, Reason::term()} + | {noresult, false}. +discover_dir(_File, _AppMeta, directory) -> + {noresult, true}; +discover_dir(File, AppMeta, file) -> + is_valid_release(File, AppMeta). -spec is_valid_release(file:name(), [rcl_app_info:t()]) -> - rcl_release:t() - | {error, Reason::term()} - | []. + {ok, rcl_release:t()} + | {error, Reason::term()} + | {noresult, false}. is_valid_release(File, AppMeta) -> case lists:suffix(".rel", File) of true -> resolve_release(File, AppMeta); false -> - [] + {noresult, false} end. resolve_release(RelFile, AppMeta) -> @@ -141,7 +130,7 @@ build_release(RelName, RelVsn, ErtsVsn, Apps, AppMeta) -> resolve_apps(Apps, AppMeta, Release, []). resolve_apps([], _AppMeta, Release, Acc) -> - rcl_release:application_details(Release, Acc); + {ok, rcl_release:application_details(Release, Acc)}; resolve_apps([AppInfo | Apps], AppMeta, Release, Acc) -> AppName = erlang:element(1, AppInfo), AppVsn = ec_semver:parse(erlang:element(2, AppInfo)), |