Packages
otpbp
5.3.0
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
Current section
Files
src/otpbp_filename.erl
-module(otpbp_filename).
-ifndef(HAVE_filename__basedir_2).
% OTP-19.0
-export([basedir/2]).
-endif.
-ifndef(HAVE_filename__basedir_3).
% OTP-19.0
-export([basedir/3]).
-endif.
-ifndef(HAVE_filename__safe_relative_path_1).
% OTP-19.3
-export([safe_relative_path/1]).
-endif.
-ifndef(HAVE_filename__basedir_2).
-ifdef(HAVE_filename__basedir_3).
-import(filename, [basedir/3]).
-endif.
-endif.
-ifndef(HAVE_filename__safe_relative_path_1).
safe_relative_path(Path) ->
case filename:pathtype(Path) of
relative -> safe_relative_path(filename:split(Path), []);
_ -> unsafe
end.
safe_relative_path([H|_], []) when H =:= ".."; H =:= <<"..">> -> unsafe;
safe_relative_path([H|T], Acc) when H =:= $.; H =:= <<$.>>; H =:= <<"..">>; H =:= ".." -> safe_relative_path(T, Acc);
safe_relative_path([H|T], Acc) -> safe_relative_path(T, [H|Acc]);
safe_relative_path([], []) -> [];
safe_relative_path([], Acc) -> filename:join(lists:reverse(Acc)).
-endif.
-ifndef(HAVE_filename__basedir_2).
basedir(Type, Application) when is_atom(Type), is_list(Application) orelse is_binary(Application) ->
basedir(Type, Application, #{}).
-endif.
-ifndef(HAVE_filename__basedir_3).
basedir(Type, Application, Opts) when is_atom(Type), is_map(Opts), is_list(Application) orelse is_binary(Application) ->
Os = basedir_os_from_opts(Opts),
Name = basedir_name_from_opts(Os, Application, Opts),
Base = basedir_from_os(Type, Os),
case {Type, Os} of
{user_log, linux} -> filename:join([Base, Name, "log"]);
{user_log, windows} -> filename:join([Base, Name, "Logs"]);
{user_cache, windows} -> filename:join([Base, Name, "Cache"]);
{Type, _} when Type =:= site_config; Type =:= site_data -> [filename:join(B, Name) || B <- Base];
_ -> filename:join(Base, Name)
end.
basedir_os_from_opts(#{os := linux}) -> linux;
basedir_os_from_opts(#{os := windows}) -> windows;
basedir_os_from_opts(#{os := darwin}) -> darwin;
basedir_os_from_opts(#{}) -> basedir_os_type().
basedir_name_from_opts(windows, App, #{author := Author, version := Vsn}) -> filename:join([Author, App, Vsn]);
basedir_name_from_opts(windows, App, #{author := Author}) -> filename:join(Author, App);
basedir_name_from_opts(_, App, #{version := Vsn}) -> filename:join(App, Vsn);
basedir_name_from_opts(_, App, _) -> App.
basedir_from_os(Type, linux) -> basedir_linux(Type);
basedir_from_os(Type, darwin) -> basedir_darwin(Type);
basedir_from_os(Type, windows) -> basedir_windows(Type).
-define(basedir_linux_user_data, ".local/share").
-define(basedir_linux_user_config, ".config").
-define(basedir_linux_user_cache, ".cache").
-define(basedir_linux_user_log, ".cache"). %% .cache/App/log
-define(basedir_linux_site_data, "/usr/local/share/:/usr/share/").
-define(basedir_linux_site_config, "/etc/xdg").
basedir_linux(user_data) -> getenv("XDG_DATA_HOME", ?basedir_linux_user_data, true);
basedir_linux(user_config) -> getenv("XDG_CONFIG_HOME", ?basedir_linux_user_config, true);
basedir_linux(user_cache) -> getenv("XDG_CACHE_HOME", ?basedir_linux_user_cache, true);
basedir_linux(user_log) -> getenv("XDG_CACHE_HOME", ?basedir_linux_user_log, true);
basedir_linux(site_data) -> string:tokens(getenv("XDG_DATA_DIRS", ?basedir_linux_site_data, false), ":");
basedir_linux(site_config) -> string:tokens(getenv("XDG_CONFIG_DIRS", ?basedir_linux_site_config, false), ":").
-define(basedir_darwin_user_data, "Library/Application Support").
-define(basedir_darwin_user_config, "Library/Application Support").
-define(basedir_darwin_user_cache, "Library/Caches").
-define(basedir_darwin_user_log, "Library/Logs").
-define(basedir_darwin_site_data, "/Library/Application Support").
-define(basedir_darwin_site_config, "/Library/Application Support").
basedir_darwin(user_data) -> basedir_join_home(?basedir_darwin_user_data);
basedir_darwin(user_config) -> basedir_join_home(?basedir_darwin_user_config);
basedir_darwin(user_cache) -> basedir_join_home(?basedir_darwin_user_cache);
basedir_darwin(user_log) -> basedir_join_home(?basedir_darwin_user_log);
basedir_darwin(site_data) -> [?basedir_darwin_site_data];
basedir_darwin(site_config) -> [?basedir_darwin_site_config].
-define(basedir_windows_user_data, "Local").
-define(basedir_windows_user_config, "Roaming").
-define(basedir_windows_user_cache, "Local"). %% Cache is added later
-define(basedir_windows_user_log, "Local"). %% Logs is added later
basedir_windows(Type) -> basedir_windows(Type, basedir_windows_appdata()).
basedir_windows(user_data, noappdata) -> basedir_join_home(?basedir_windows_user_data);
basedir_windows(user_config, noappdata) -> basedir_join_home(?basedir_windows_user_config);
basedir_windows(user_config, {ok, AppData}) -> AppData;
basedir_windows(user_cache, noappdata) -> basedir_join_home(?basedir_windows_user_cache);
basedir_windows(user_log, noappdata) -> basedir_join_home(?basedir_windows_user_log);
basedir_windows(Type, noappdata) when Type =:= site_data; Type =:= site_config -> "";
basedir_windows(Type, {ok, AppData}) when Type =:= user_data; Type =:= user_cache; Type =:= user_log ->
getenv("LOCALAPPDATA", AppData);
basedir_windows(Type, {ok, _}) when Type =:= site_data; Type =:= site_config -> "".
basedir_windows_appdata() ->
case os:getenv("APPDATA", "") of
"" -> noappdata;
Val -> {ok, Val}
end.
basedir_join_home(Dir) ->
filename:join(case os:getenv("HOME") of
false ->
{ok, [[Home]]} = init:get_argument(home),
Home;
Home -> Home
end,
Dir).
basedir_os_type() ->
case os:type() of
{unix, darwin} -> darwin;
{win32, _} -> windows;
_ -> linux
end.
getenv(K, Def, false) -> getenv(K, Def);
getenv(K, Def, true) -> getenv(K, basedir_join_home(Def)).
getenv(K, Def) ->
case os:getenv(K) of
Val when Val =:= ""; Val =:= false -> Def;
Val -> Val
end.
-endif.