Skip to content

Commit

Permalink
Fix use of binary string in database_cache_file option
Browse files Browse the repository at this point in the history
  • Loading branch information
g-andrade committed Jul 24, 2024
1 parent 329f5c9 commit 49ef423
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [2.3.10] - 2024-07-24

### Fixed

- use of binary string in `database_cache_file` option

## [2.3.9] - 2024-07-20

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/locus_filesystem_load.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
}.
-export_type([success/0]).

-type path() :: nonempty_string().
-type path() :: file:filename_all().
-export_type([path/0]).

-record(state, {
Expand Down
4 changes: 2 additions & 2 deletions src/locus_filesystem_store.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
{finished, {error, term()}}.
-export_type([msg/0]).

-type path() :: nonempty_string().
-type path() :: file:filename_all().
-export_type([path/0]).

-record(state, {
Expand Down Expand Up @@ -156,7 +156,7 @@ handle_write(State) ->
do_write(State) ->
#state{path = Path, content = Content, modified_on = ModificationDT} = State,
TmpSuffix = ".tmp." ++ integer_to_list(rand:uniform(1 bsl 32), 36),
TmpPath = Path ++ TmpSuffix,
TmpPath = unicode:characters_to_list([Path, TmpSuffix]),
FileInfoMod = #file_info{ mtime = ModificationDT },

ok = filelib:ensure_dir(Path),
Expand Down
20 changes: 11 additions & 9 deletions src/locus_loader.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
{update_period, milliseconds_interval()} |
{error_retries, error_retry_behaviour()} |
no_cache |
{database_cache_file, file:filename()}.
{database_cache_file, file:filename_all()}.
-export_type([loader_opt/0]).

-type milliseconds_interval() :: pos_integer().
Expand Down Expand Up @@ -152,7 +152,7 @@
error_retry_behaviour :: error_retry_behaviour(),
error_retry_behaviour_applies_after_readiness :: boolean(),
use_cache :: boolean(),
database_cache_file :: file:filename() | undefined
database_cache_file :: file:filename_all() | undefined
}).
-type settings() :: #settings{}.

Expand Down Expand Up @@ -342,7 +342,7 @@ validate_loader_opts(MixedOpts, FetcherOpts) ->
% Ensure directory exists
Dirname = filename:dirname(File),
(
has_extenstion(File, ["gz", "mmdb"])
has_extension(File, ["gz", "mmdb"])
and
filelib:is_dir(Dirname)
)
Expand Down Expand Up @@ -466,7 +466,7 @@ schedule_update(Interval, State)
NewTimer = erlang:send_after(Interval, self(), begin_update),
State#state{ update_timer = NewTimer }.

-spec cached_database_path(state()) -> nonempty_string().
-spec cached_database_path(state()) -> file:filename_all().
cached_database_path(#state{
settings = #settings{
database_cache_file = DatabaseCacheFile
Expand All @@ -475,7 +475,6 @@ cached_database_path(#state{
)
when DatabaseCacheFile =/= undefined ->
DatabaseCacheFile;

cached_database_path(State) ->
case State#state.origin of
{maxmind, EditionName} ->
Expand Down Expand Up @@ -878,15 +877,18 @@ extract_mmdb_from_tarball_blob(Tarball) ->

-spec has_mmdb_extension(nonempty_string()) -> boolean().
has_mmdb_extension(Filename) ->
has_extenstion(Filename, ["mmdb"]).
has_extension(Filename, ["mmdb"]).

% Make sure the ExpectedExtensions list is passed in reverse order.
% So if file is "archive.tar.gz", the ExpectedExtensions list is ["gz", "tar"].
-spec has_extenstion(nonempty_string(), [nonempty_string()]) -> boolean().
has_extenstion(Filename, ExpectedExtensions) ->
-spec has_extension(file:filename_all(), [nonempty_string()]) -> boolean().
has_extension(Filename, ExpectedExtensions) ->
ExtensionParts = filename_extension_parts(Filename),
lists:prefix(ExpectedExtensions, ExtensionParts).

filename_extension_parts(<<BinFilename/bytes>>) ->
Filename = unicode:characters_to_list(BinFilename),
filename_extension_parts_recur(Filename, []);
filename_extension_parts(Filename) ->
filename_extension_parts_recur(Filename, []).

Expand Down Expand Up @@ -966,7 +968,7 @@ fetched_database_modification_datetime({cache, _}, #{modified_on := Modification
fetched_database_modification_datetime({filesystem, _}, #{modified_on := ModificationDate}) ->
ModificationDate.

%-spec make_cached_database_blob(nonempty_string(), binary()) -> binary().
%-spec make_cached_database_blob(file:filename_all(), binary()) -> binary().
make_cached_database_blob(CachedTarballPath, EncodedDatabase) ->
?assertMatch(["gz", "mmdb" | _], filename_extension_parts(CachedTarballPath)),
zlib:gzip(EncodedDatabase).
Expand Down

0 comments on commit 49ef423

Please sign in to comment.