diff options
-rw-r--r-- | src/catlfish.erl | 35 | ||||
-rw-r--r-- | src/catlfish.hrl | 4 | ||||
-rw-r--r-- | src/catlfish_app.erl | 9 |
3 files changed, 30 insertions, 18 deletions
diff --git a/src/catlfish.erl b/src/catlfish.erl index 6cc741d..5d96278 100644 --- a/src/catlfish.erl +++ b/src/catlfish.erl @@ -5,6 +5,7 @@ -export([add_chain/2, entries/2, entry_and_proof/2]). -export([known_roots/0, update_known_roots/0]). -include_lib("eunit/include/eunit.hrl"). +-include("catlfish.hrl"). -define(PROTOCOL_VERSION, 0). @@ -159,7 +160,7 @@ decode_tls_vector(Binary, LengthLen) -> <<ExtractedBinary:Length/binary-unit:8, Rest2/binary>> = Rest, {ExtractedBinary, Rest2}. --define(ROOTS_TABLE, catlfish_roots). +-define(ROOTS_CACHE_KEY, roots). update_known_roots() -> case application:get_env(catlfish, known_roots_path) of @@ -178,22 +179,20 @@ known_roots() -> -spec known_roots(file:filename(), use_cache|update_tab) -> list(). known_roots(Directory, CacheUsage) -> - case ets:info(?ROOTS_TABLE) of - undefined -> - read_pemfiles_from_dir( - ets:new(?ROOTS_TABLE, [set, protected, named_table]), - Directory); - _ -> - case CacheUsage of - use_cache -> - ets:lookup_element(?ROOTS_TABLE, list, 2); - update_tab -> - read_pemfiles_from_dir(?ROOTS_TABLE, Directory) - end + case CacheUsage of + use_cache -> + case ets:lookup(?CACHE_TABLE, ?ROOTS_CACHE_KEY) of + [] -> + read_pemfiles_from_dir(Directory); + [{roots, DerList}] -> + DerList + end; + update_tab -> + read_pemfiles_from_dir(Directory) end. --spec read_pemfiles_from_dir(ets:tab(), file:filename()) -> list(). -read_pemfiles_from_dir(Tab, Dir) -> +-spec read_pemfiles_from_dir(file:filename()) -> list(). +read_pemfiles_from_dir(Dir) -> DerList = case file:list_dir(Dir) of {error, enoent} -> @@ -208,7 +207,7 @@ read_pemfiles_from_dir(Tab, Dir) -> Filenames), ders_from_pemfiles(Dir, Files) end, - true = ets:insert(Tab, {list, DerList}), + true = ets:insert(?CACHE_TABLE, {?ROOTS_CACHE_KEY, DerList}), DerList. ders_from_pemfiles(Dir, Filenames) -> @@ -251,7 +250,7 @@ read_pemfiles_test_() -> fun() -> {known_roots(?PEMFILES_DIR_OK, use_cache), known_roots(?PEMFILES_DIR_OK, use_cache)} end, - fun(_) -> ets:delete(?ROOTS_TABLE) end, + fun(_) -> ets:delete(?CACHE_TABLE, ?ROOTS_CACHE_KEY) end, fun({L, LCached}) -> [?_assertMatch(7, length(L)), ?_assertEqual(L, LCached)] @@ -260,5 +259,5 @@ read_pemfiles_test_() -> read_pemfiles_fail_test_() -> {setup, fun() -> known_roots(?PEMFILES_DIR_NONEXISTENT, use_cache) end, - fun(_) -> ets:delete(?ROOTS_TABLE) end, + fun(_) -> ets:delete(?CACHE_TABLE, ?ROOTS_CACHE_KEY) end, fun(Empty) -> [?_assertMatch([], Empty)] end}. diff --git a/src/catlfish.hrl b/src/catlfish.hrl new file mode 100644 index 0000000..46e882b --- /dev/null +++ b/src/catlfish.hrl @@ -0,0 +1,4 @@ +%%% Copyright (c) 2014, NORDUnet A/S. +%%% See LICENSE for licensing information. + +-define(CACHE_TABLE, catlfish_cache). diff --git a/src/catlfish_app.erl b/src/catlfish_app.erl index cfb55cd..e24a1bb 100644 --- a/src/catlfish_app.erl +++ b/src/catlfish_app.erl @@ -8,11 +8,20 @@ %% Application callbacks -export([start/2, stop/1]). +-include("catlfish.hrl"). + %% =================================================================== %% Application callbacks %% =================================================================== start(normal, Args) -> + case ets:info(?CACHE_TABLE) of + undefined -> + ok; + _ -> + ets:delete(?CACHE_TABLE) + end, + ets:new(?CACHE_TABLE, [set, public, named_table]), catlfish_sup:start_link(Args). stop(_State) -> |