fix: ensure no colon in filenames

This commit is contained in:
Ivan Dyachkov 2023-01-16 14:02:16 +01:00
parent c73ffdf825
commit 676f017ec0
4 changed files with 17 additions and 6 deletions

View File

@ -327,9 +327,9 @@ atom(Bin) -> binary_to_existing_atom(Bin, utf8).
certs_dir(ChainName, ConfigOrID) -> certs_dir(ChainName, ConfigOrID) ->
DirName = dir(ChainName, ConfigOrID), DirName = dir(ChainName, ConfigOrID),
SubDir = iolist_to_binary(filename:join(["authn", DirName])), SubDir = iolist_to_binary(filename:join(["authn", DirName])),
binary:replace(SubDir, <<":">>, <<"-">>, [global]). emqx_misc:safe_filename(SubDir).
dir(ChainName, ID) when is_binary(ID) -> dir(ChainName, ID) when is_binary(ID) ->
binary:replace(iolist_to_binary([to_bin(ChainName), "-", ID]), <<":">>, <<"-">>); emqx_misc:safe_filename(iolist_to_binary([to_bin(ChainName), "-", ID]));
dir(ChainName, Config) when is_map(Config) -> dir(ChainName, Config) when is_map(Config) ->
dir(ChainName, authenticator_id(Config)). dir(ChainName, authenticator_id(Config)).

View File

@ -55,7 +55,8 @@
readable_error_msg/1, readable_error_msg/1,
safe_to_existing_atom/1, safe_to_existing_atom/1,
safe_to_existing_atom/2, safe_to_existing_atom/2,
pub_props_to_packet/1 pub_props_to_packet/1,
safe_filename/1
]). ]).
-export([ -export([
@ -708,3 +709,11 @@ pub_props_to_packet(Properties) ->
true true
end, end,
maps:filtermap(F, Properties). maps:filtermap(F, Properties).
%% fix filename by replacing characters which could be invalid on some filesystems
%% with safe ones
-spec safe_filename(binary() | unicode:chardata()) -> binary() | [unicode:chardata()].
safe_filename(Filename) when is_binary(Filename) ->
binary:replace(Filename, <<":">>, <<"-">>, [global]);
safe_filename(Filename) when is_list(Filename) ->
string:replace(Filename, ":", "-", all).

View File

@ -216,7 +216,8 @@ recreate(Type, Name, Conf, Opts) ->
). ).
create_dry_run(Type, Conf0) -> create_dry_run(Type, Conf0) ->
TmpPath = iolist_to_binary(["bridges-create-dry-run:", emqx_misc:gen_id(8)]), TmpPath0 = iolist_to_binary(["bridges-create-dry-run:", emqx_misc:gen_id(8)]),
TmpPath = emqx_misc:safe_filename(TmpPath0),
Conf = emqx_map_lib:safe_atom_key_map(Conf0), Conf = emqx_map_lib:safe_atom_key_map(Conf0),
case emqx_connector_ssl:convert_certs(TmpPath, Conf) of case emqx_connector_ssl:convert_certs(TmpPath, Conf) of
{error, Reason} -> {error, Reason} ->

View File

@ -871,8 +871,9 @@ queue_count(Q) ->
replayq:count(Q). replayq:count(Q).
disk_queue_dir(Id, Index) -> disk_queue_dir(Id, Index) ->
QDir = binary_to_list(Id) ++ ":" ++ integer_to_list(Index), QDir0 = binary_to_list(Id) ++ ":" ++ integer_to_list(Index),
filename:join([emqx:data_dir(), "resource_worker", node(), QDir]). QDir = filename:join([emqx:data_dir(), "resource_worker", node(), QDir0]),
emqx_misc:safe_filename(QDir).
ensure_flush_timer(Data = #{tref := undefined, batch_time := T}) -> ensure_flush_timer(Data = #{tref := undefined, batch_time := T}) ->
Ref = make_ref(), Ref = make_ref(),