Packages

otpbp

4.60.2
7.18.0 7.16.1 7.15.0 7.14.0 7.13.0 7.12.0 7.11.0 7.10.2 7.9.3 7.9.2 7.9.1 7.8.7 7.8.6 7.8.5 7.8.4 7.7.1 7.7.0 7.6.3 7.6.2 7.6.1 7.6.0 7.5.1 7.5.0 7.4.0 7.3.4 7.3.3 7.3.2 7.3.1 7.3.0 7.2.0 7.1.1 7.1.0 7.0.4 7.0.3 7.0.2 7.0.1 6.26.0 6.25.0 6.24.0 6.23.0 6.22.2 6.21.3 6.21.2 6.21.1 6.20.2 6.19.1 6.19.0 6.18.3 6.18.2 6.18.1 6.18.0 6.17.1 6.17.0 6.16.0 6.15.4 6.15.3 6.15.2 6.15.1 6.15.0 6.14.0 6.13.5 6.13.4 6.13.3 6.13.2 6.13.1 6.13.0 6.12.0 6.11.2 6.11.1 6.10.0 6.9.2 6.9.1 6.9.0 6.8.0 6.7.1 6.7.0 6.6.4 6.6.3 6.6.2 6.6.1 6.6.0 6.5.0 6.4.0 6.3.3 6.3.2 6.3.1 6.3.0 6.2.1 6.2.0 6.1.0 6.0.0 5.39.0 5.38.0 5.37.0 5.36.0 5.35.2 5.34.3 5.34.2 5.34.1 5.33.1 5.32.1 5.32.0 5.31.3 5.31.2 5.31.1 5.31.0 5.30.1 5.30.0 5.29.0 5.28.4 5.28.3 5.28.2 5.28.1 5.28.0 5.27.0 5.26.5 5.26.4 5.26.3 5.26.2 5.26.1 5.26.0 5.25.0 5.24.2 5.24.1 5.23.0 5.22.2 5.22.1 5.22.0 5.21.0 5.20.1 5.20.0 5.19.4 5.19.3 5.19.2 5.19.1 5.19.0 5.18.0 5.17.0 5.16.3 5.16.1 5.16.0 5.15.1 5.15.0 5.14.0 5.13.1 5.13.0 5.12.1 5.12.0 5.11.2 5.11.1 5.10.1 5.10.0 5.9.0 5.8.0 5.7.0 5.6.1 5.6.0 5.5.0 5.4.2 5.4.1 5.4.0 5.3.0 5.2.2 5.2.1 5.2.0 5.1.2 5.1.1 4.79.0 4.78.0 4.77.0 4.76.2 4.75.3 4.75.2 4.75.1 4.74.0 4.73.1 4.73.0 4.72.3 4.72.2 4.72.1 4.72.0 4.71.1 4.71.0 4.70.0 4.69.4 4.69.3 4.69.2 4.69.1 4.69.0 4.68.0 4.67.5 4.67.4 4.67.3 4.67.2 4.67.0 4.66.0 4.65.2 4.65.1 4.64.0 4.63.2 4.63.1 4.63.0 4.62.0 4.61.1 4.61.0 4.60.4 4.60.3 4.60.2 4.60.1 4.60.0 4.59.0 4.58.0 4.57.2 4.57.1 4.57.0 4.56.2 4.56.1 4.56.0 4.55.0 4.54.0 4.53.0 4.52.1 4.52.0 4.51.3 4.51.2 4.51.0 4.50.0 4.49.3 4.49.2

OTP backports

Current section

Files

Jump to
otpbp src otpbp_filelib.erl
Raw

src/otpbp_filelib.erl

-module(otpbp_filelib).
-ifndef(HAVE_filelib__safe_relative_path_2).
% OTP 23.0
-export([safe_relative_path/2]).
-endif.
-ifndef(HAVE_filelib__ensure_path_1).
% OTP 25.0
-export([ensure_path/1]).
-endif.
-ifndef(HAVE_filelib__find_file_2).
% OTP 20.0
-export([find_file/2]).
-endif.
-ifndef(HAVE_filelib__find_file_3).
% OTP 20.0
-export([find_file/3]).
-endif.
-ifndef(HAVE_filelib__find_source_1).
% OTP 20.0
-export([find_source/1]).
-endif.
-ifndef(HAVE_filelib__find_source_2).
% OTP 20.0
-export([find_source/2]).
-endif.
-ifndef(HAVE_filelib__find_source_3).
% OTP 20.0
-export([find_source/3]).
-endif.
-ifndef(HAVE_filelib__find_file_2).
-ifdef(HAVE_filelib__find_file_3).
-import(filelib, [find_file/3]).
-endif.
-endif.
-ifndef(HAVE_filelib__find_source_1).
-ifdef(HAVE_filelib__find_source_2).
-import(filelib, [find_source/2]).
-endif.
-endif.
-ifndef(HAVE_filelib__find_source_2).
-ifdef(HAVE_filelib__find_source_3).
-import(filelib, [find_source/3]).
-endif.
-endif.
-ifndef(HAVE_filelib__safe_relative_path_2).
safe_relative_path(Path, "") -> safe_relative_path(Path, ".");
safe_relative_path(Path, Cwd) -> srp_path(filename:split(Path), Cwd, sets:new(), []).
srp_path([], _Cwd, _Seen, []) -> "";
srp_path([], _Cwd, _Seen, Acc) -> filename:join(Acc);
srp_path([H|Segs], Cwd, Seen, Acc) when H =:= "."; H =:= <<".">> -> srp_path(Segs, Cwd, Seen, Acc);
srp_path([H|_Segs], _Cwd, _Seen, []) when H =:= ".."; H =:= <<"..">> -> unsafe;
srp_path([H|Segs], Cwd, Seen, [_|_] = Acc) when H =:= ".."; H =:= <<"..">> ->
srp_path(Segs, Cwd, Seen, lists:droplast(Acc));
srp_path([clear|Segs], Cwd, _Seen, Acc) -> srp_path(Segs, Cwd, sets:new(), Acc);
srp_path([Seg|_] = Segs, Cwd, Seen, Acc) ->
case filename:pathtype(Seg) of
relative -> srp_segment(Segs, Cwd, Seen, Acc);
_ -> unsafe
end.
srp_segment([Seg|Segs], Cwd, Seen, Acc) ->
Path = filename:join([Cwd|Acc]),
case file:read_link(filename:join(Path, Seg)) of
{ok, LinkPath} -> srp_link(Path, LinkPath, Segs, Cwd, Seen, Acc);
{error, _} -> srp_path(Segs, Cwd, Seen, Acc ++ [Seg])
end.
srp_link(Path, LinkPath, Segs, Cwd, Seen, Acc) ->
FullLinkPath = filename:join(Path, LinkPath),
case sets:is_element(FullLinkPath, Seen) of
true -> unsafe;
false -> srp_path(filename:split(LinkPath) ++ [clear|Segs], Cwd, sets:add_element(FullLinkPath, Seen), Acc)
end.
-compile({inline, [srp_segment/4, srp_link/6]}).
-endif.
-ifndef(HAVE_filelib__ensure_path_1).
ensure_path("/") -> ok;
ensure_path(Path) ->
case filelib:is_dir(Path) of
true -> ok;
false ->
case filename:dirname(Path) of
Path -> {error, einval};
Parent ->
_ = ensure_path(Parent),
case file:make_dir(Path) of
{error, eexist} ->
case filelib:is_dir(Path) of
true -> ok;
false -> {error, eexist}
end;
Other -> Other
end
end
end.
-endif.
-ifndef(HAVE_filelib__find_file_2).
find_file(Filename, Dir) -> find_file(Filename, Dir, []).
-endif.
-ifndef(HAVE_filelib__find_file_3).
find_file(Filename, Dir, []) -> find_file(Filename, Dir, get_search_rules());
find_file(Filename, Dir, Rules) -> try_dir_rules(keep_dir_search_rules(Rules), Filename, Dir).
try_dir_rules([{From, To}|Rest], Filename, Dir) when is_list(From), is_list(To) ->
case try_dir_rule(Dir, Filename, From, To) of
{ok, _File} = R -> R;
error -> try_dir_rules(Rest, Filename, Dir)
end;
try_dir_rules([], _Filename, _Dir) -> {error, not_found}.
try_dir_rule(Dir, Filename, From, To) ->
case lists:suffix(From, Dir) of
true ->
Src = filename:join(lists:sublist(Dir, 1, length(Dir) - length(From)) ++ To, Filename),
case filelib:is_regular(Src) of
true -> {ok, Src};
false -> find_regular_file(filelib:wildcard(Src))
end;
false -> error
end.
find_regular_file([]) -> error;
find_regular_file([File|Files]) ->
case filelib:is_regular(File) of
true -> {ok, File};
false -> find_regular_file(Files)
end.
keep_dir_search_rules(Rules) -> [T || {_, _} = T <- Rules].
-ifndef(NEED_get_search_rules_0).
-define(NEED_get_search_rules_0, true).
-endif.
-endif.
-ifndef(HAVE_filelib__find_source_1).
find_source(FilePath) -> find_source(filename:basename(FilePath), filename:dirname(FilePath)).
-endif.
-ifndef(HAVE_filelib__find_source_2).
find_source(Filename, Dir) -> find_source(Filename, Dir, []).
-endif.
-ifndef(HAVE_filelib__find_source_3).
find_source(Filename, Dir, []) -> find_source(Filename, Dir, get_search_rules());
find_source(Filename, Dir, Rules) -> try_suffix_rules(keep_suffix_search_rules(Rules), Filename, Dir).
try_suffix_rules(Rules, Filename, Dir) ->
Ext = filename:extension(Filename),
try_suffix_rules(Rules, filename:rootname(Filename, Ext), Dir, Ext).
try_suffix_rules([{Ext, Src, Rules}|Rest], Root, Dir, Ext) when is_list(Src), is_list(Rules) ->
case try_dir_rules(add_local_search(Rules), Root ++ Src, Dir) of
{ok, _File} = R -> R;
_Other -> try_suffix_rules(Rest, Root, Dir, Ext)
end;
try_suffix_rules([_|Rest], Root, Dir, Ext) -> try_suffix_rules(Rest, Root, Dir, Ext);
try_suffix_rules([], _Root, _Dir, _Ext) -> {error, not_found}.
add_local_search(Rules) -> [{"", ""}|[X || X <- Rules, X =/= {"", ""}]].
keep_suffix_search_rules(Rules) -> [T || {_, _, _} = T <- Rules].
-ifndef(NEED_get_search_rules_0).
-define(NEED_get_search_rules_0, true).
-endif.
-endif.
-ifdef(NEED_get_search_rules_0).
get_search_rules() ->
case application:get_env(kernel, source_search_rules, []) of
[] -> default_search_rules();
R when is_list(R) -> R
end.
default_search_rules() ->
[%% suffix-speficic rules for source search
{".beam", ".erl", erl_source_search_rules()},
{".erl", ".yrl", []},
{"", ".src", erl_source_search_rules()},
{".so", ".c", c_source_search_rules()},
{".o", ".c", c_source_search_rules()},
{"", ".c", c_source_search_rules()},
{"", ".in", basic_source_search_rules()},
%% plain old directory rules, backwards compatible
{"", ""}] ++ erl_source_search_rules().
basic_source_search_rules() -> erl_source_search_rules() ++ c_source_search_rules().
erl_source_search_rules() ->
[{"ebin", "src"}, {"ebin", "esrc"}, {"ebin", filename:join("src", "*")}, {"ebin", filename:join("esrc", "*")}].
c_source_search_rules() -> [{"priv", "c_src"}, {"priv", "src"}, {"bin", "c_src"}, {"bin", "src"}, {"", "src"}].
-endif.