1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
%% -*- 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),
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)).
|