From fe71b7249468b2c2c2680f6b99ade6f1c8ed26a1 Mon Sep 17 00:00:00 2001 From: Mark Allen Date: Tue, 13 Oct 2015 21:12:14 -0500 Subject: [PATCH] Add filename parse and validation functions --- src/machi_util.erl | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) 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.