Packages
locus
2.3.2
2.3.15
2.3.14
2.3.13
2.3.12
2.3.11
2.3.10
2.3.9
2.3.8
2.3.7
2.3.6
2.3.5
2.3.4
2.3.3
retired
2.3.2
2.3.1
2.3.0
2.2.2
2.2.1
2.2.0
2.1.0
2.0.0
1.16.1
1.16.0
1.15.0
1.14.1
1.14.0
1.13.2
1.13.1
1.13.0
1.12.2
1.12.1
1.12.0
1.11.0
1.11.0-beta
1.10.2
1.10.1
1.10.0
1.9.0
1.9.0-beta
1.8.0
1.7.0
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.2
1.0.1
1.0.0
MMDB reader for geolocation and ASN lookup of IP addresses, supporting MaxMind GeoLite2/GeoIP2 and other providers
Current section
Files
Jump to
Current section
Files
src/locus_filesystem_store.erl
%% Copyright (c) 2017-2022 Guilherme Andrade
%%
%% Permission is hereby granted, free of charge, to any person obtaining a
%% copy of this software and associated documentation files (the "Software"),
%% to deal in the Software without restriction, including without limitation
%% the rights to use, copy, modify, merge, publish, distribute, sublicense,
%% and/or sell copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
%% DEALINGS IN THE SOFTWARE.
%%
%% locus is an independent project and has not been authorized, sponsored,
%% or otherwise approved by MaxMind.
%% @doc Stores a file in the filesystem without blocking the caller
-module(locus_filesystem_store).
-behaviour(gen_server).
-include_lib("kernel/include/file.hrl").
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export(
[start_link/3
]).
-ignore_xref(
[start_link/3
]).
%% ------------------------------------------------------------------
%% gen_server Function Exports
%% ------------------------------------------------------------------
-export(
[init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3
]).
%% ------------------------------------------------------------------
%% Record and Type Definitions
%% ------------------------------------------------------------------
-type msg() ::
{finished, success} |
{finished, {error, term()}}.
-export_type([msg/0]).
-type path() :: nonempty_string().
-export_type([path/0]).
-record(state, {
owner_pid :: pid(),
path :: path(),
content :: iodata(),
modified_on :: calendar:datetime()
}).
-type state() :: #state{}.
%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
-spec start_link(path(), iodata(), calendar:datetime()) -> {ok, pid()}.
%% @private
start_link(Path, Content, ModificationDT) ->
gen_server:start_link(?MODULE, [self(), Path, Content, ModificationDT], []).
%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------
-spec init([InitArg, ...]) -> {ok, state()}
when InitArg :: OwnerPid | Path | Content | ModificationDT,
OwnerPid :: pid(),
Path :: path(),
Content :: iodata(),
ModificationDT :: calendar:datetime().
%% @private
init([OwnerPid, Path, Content, ModificationDT]) ->
_ = process_flag(trap_exit, true),
self() ! write,
{ok, #state{
owner_pid = OwnerPid,
path = Path,
content = Content,
modified_on = ModificationDT
}}.
-spec handle_call(term(), {pid(), reference()}, state())
-> {stop, unexpected_call, state()}.
%% @private
handle_call(_Call, _From, State) ->
{stop, unexpected_call, State}.
-spec handle_cast(term(), state())
-> {stop, unexpected_cast, state()}.
%% @private
handle_cast(_Cast, State) ->
{stop, unexpected_cast, State}.
-spec handle_info(term(), state())
-> {stop, normal, state()} |
{stop, unexpected_info, state()}.
%% @private
handle_info(write, State) ->
handle_write(State);
handle_info(_Info, State) ->
{stop, unexpected_info, State}.
-spec terminate(term(), state()) -> ok.
%% @private
terminate(_Reason, _State) ->
ok.
-spec code_change(term(), state(), term()) -> {ok, state()}.
%% @private
code_change(_OldVsn, #state{} = State, _Extra) ->
{ok, State}.
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
-spec handle_write(state()) -> {stop, normal, state()}.
handle_write(State) ->
try do_write(State) of
ok ->
notify_owner({finished, success}, State),
{stop, normal, State}
catch
Class:Reason:Stacktrace ->
SaferReason = locus_util:purge_term_of_very_large_binaries(Reason),
SaferStacktrace = locus_util:purge_term_of_very_large_binaries(Stacktrace),
notify_owner({finished, {error, {Class, SaferReason, SaferStacktrace}}}, State),
{stop, normal, State}
end.
-spec do_write(state()) -> ok | no_return().
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,
FileInfoMod = #file_info{ mtime = ModificationDT },
ok = filelib:ensure_dir(Path),
{ok, IoDevice} = file:open(TmpPath, [write, exclusive, raw]),
ok = file:write(IoDevice, Content),
ok = file:close(IoDevice),
ok = file:write_file_info(TmpPath, FileInfoMod, [{time, universal}]),
ok = file:rename(TmpPath, Path).
%% ------------------------------------------------------------------
%% Internal Function Definitions - Events
%% ------------------------------------------------------------------
%-spec notify_owner(msg(), state()) -> ok.
notify_owner(Msg, State) ->
#state{owner_pid = OwnerPid} = State,
_ = erlang:send(OwnerPid, {self(), Msg}, [noconnect]),
ok.