Add filename parse and validation functions

This commit is contained in:
Mark Allen 2015-10-13 21:12:14 -05:00
parent f8707c61c0
commit fe71b72494

View file

@ -34,6 +34,8 @@
make_checksum_filename/4, make_checksum_filename/2, make_checksum_filename/4, make_checksum_filename/2,
make_data_filename/4, make_data_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, read_max_filenum/2, increment_max_filenum/2,
info_msg/2, verb/1, verb/2, info_msg/2, verb/1, verb/2,
mbytes/1, mbytes/1,
@ -45,6 +47,8 @@
combinations/1, ordered_combinations/1, combinations/1, ordered_combinations/1,
mk_order/2 mk_order/2
]). ]).
%% TODO: Leave this in place?
-compile(export_all). -compile(export_all).
-include("machi.hrl"). -include("machi.hrl").
@ -115,6 +119,32 @@ make_projection_filename(DataDir, "") ->
make_projection_filename(DataDir, File) -> make_projection_filename(DataDir, File) ->
lists:flatten(io_lib:format("~s/projection/~s", [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:
%% <ul>
%% <li>Prefix</li>
%% <li>UUID</li>
%% <li>Sequence number</li>
%% </ul>
%%
%% 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 %% @doc Read the file size of a config file, which is used as the
%% basis for a minimum sequence number. %% basis for a minimum sequence number.