diff --git a/src/machi_util.erl b/src/machi_util.erl index 4e2dba1..37a2ea2 100644 --- a/src/machi_util.erl +++ b/src/machi_util.erl @@ -33,10 +33,12 @@ make_config_filename/2, make_checksum_filename/4, make_checksum_filename/2, make_data_filename/4, make_data_filename/2, - make_projection_filename/2, + make_projection_filename/2, + is_valid_filename/1, + parse_filename/1, read_max_filenum/2, increment_max_filenum/2, info_msg/2, verb/1, verb/2, - mbytes/1, + mbytes/1, pretty_time/0, pretty_time/2, %% TCP protocol helpers connect/2, connect/3, @@ -45,6 +47,8 @@ combinations/1, ordered_combinations/1, mk_order/2 ]). + +%% TODO: Leave this in place? -compile(export_all). -include("machi.hrl"). @@ -115,6 +119,32 @@ make_projection_filename(DataDir, "") -> make_projection_filename(DataDir, File) -> lists:flatten(io_lib:format("~s/projection/~s", [DataDir, File])). +%% @doc Given a filename, return true if it is a valid machi filename, +%% false otherwise. +-spec is_valid_filename( Filename :: string() ) -> true | false. +is_valid_filename(Filename) -> + case parse_filename(Filename) of + [] -> false; + _ -> true + end. + +%% @doc Given a machi filename, return a set of components in a list. +%% The components will be: +%% +%% +%% Invalid filenames will return an empty list. +-spec parse_filename( Filename :: string() ) -> [ string() ]. +parse_filename(Filename) -> + case string:tokens(Filename, "^") of + [_Prefix, _UUID, _SeqNo] = L -> L; + _ -> [] + end. + + %% @doc Read the file size of a config file, which is used as the %% basis for a minimum sequence number.