aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1/src/asn1_db.erl
blob: 557796972768737083554fb0e4027d033e68d4d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2013. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
-module(asn1_db).

-export([dbstart/1,dbnew/2,dbload/1,dbload/3,dbsave/2,dbput/2,
	 dbput/3,dbget/2]).
-export([dbstop/0]).

-record(state, {parent, monitor, includes, table}).

%% Interface
dbstart(Includes0) ->
    Includes = case Includes0 of
		   [] -> ["."];
		   [_|_] -> Includes0
	       end,
    Parent = self(),
    undefined = get(?MODULE),			%Assertion.
    put(?MODULE, spawn_link(fun() -> init(Parent, Includes) end)),
    ok.

dbload(Module, Erule, Mtime) ->
    req({load, Module, Erule, Mtime}).

dbload(Module) ->
    req({load, Module, any, {{0,0,0},{0,0,0}}}).

dbnew(Module, Erule)       -> req({new, Module, Erule}).
dbsave(OutFile, Module)    -> cast({save, OutFile, Module}).
dbput(Module, K, V)        -> cast({set, Module, K, V}).
dbput(Module, Kvs)         -> cast({set, Module, Kvs}).
dbget(Module, K)           -> req({get, Module, K}).
dbstop()                   -> Resp = req(stop), erase(?MODULE), Resp.

%% Internal functions
-define(MAGIC_KEY, '__version_and_erule__').

req(Request) ->
    DbPid = get(?MODULE),
    Ref = erlang:monitor(process,DbPid),
    get(?MODULE) ! {{Ref, self()}, Request},
    receive 
	{{Ref,?MODULE}, Reply} ->
	    erlang:demonitor(Ref,[flush]),
	    Reply; 
	{'DOWN',Ref,_,_,Info} -> 
	    exit({db_error,Info}) 
    end.

cast(Request) ->
    get(?MODULE) ! Request,
    ok.

reply({Ref,From}, Response) ->
    From ! {{Ref,?MODULE}, Response},
    ok.

init(Parent, Includes) ->
    MRef = erlang:monitor(process, Parent),
    loop(#state{parent = Parent, monitor = MRef, includes = Includes,
                table = ets:new(?MODULE, [])}).

loop(#state{parent = Parent, monitor = MRef, table = Table,
            includes = Includes} = State) ->
    receive
        {set, Mod, K2, V} ->
            [{_, Modtab}] = ets:lookup(Table, Mod),
            ets:insert(Modtab, {K2, V}),
            loop(State);
        {set, Mod, Kvs} ->
            [{_, Modtab}] = ets:lookup(Table, Mod),
            ets:insert(Modtab, Kvs),
            loop(State);
        {From, {get, Mod, K2}} ->
	    %% XXX If there is no information for Mod, get_table/3
	    %% will attempt to load information from an .asn1db
	    %% file, without comparing its timestamp against the
	    %% source file. This is known to happen when check_*
	    %% functions for DER are generated, but it could possibly
	    %% happen in other circumstances. Ideally, this issue should
	    %% be rectified in some way, perhaps by ensuring that
	    %% the module has been loaded (using dbload/4) prior
	    %% to calling dbget/2.
            case get_table(Table, Mod, Includes) of
                {ok,Tab} -> reply(From, lookup(Tab, K2));
                error -> reply(From, undefined)
            end,
            loop(State);
        {save, OutFile, Mod} ->
            [{_,Mtab}] = ets:lookup(Table, Mod),
            ok = ets:tab2file(Mtab, OutFile),
            loop(State);
        {From, {new, Mod, Erule}} ->
            [] = ets:lookup(Table, Mod),	%Assertion.
            ModTableId = ets:new(list_to_atom(lists:concat(["asn1_",Mod])), []),
            ets:insert(Table, {Mod, ModTableId}),
	    ets:insert(ModTableId, {?MAGIC_KEY, info(Erule)}),
            reply(From, ok),
            loop(State);
	{From, {load, Mod, Erule, Mtime}} ->
	    case ets:member(Table, Mod) of
		true ->
		    reply(From, ok);
		false ->
		    case load_table(Mod, Erule, Mtime, Includes) of
			{ok, ModTableId} ->
			    ets:insert(Table, {Mod, ModTableId}),
			    reply(From, ok);
			error ->
			    reply(From, error)
		    end
	    end,
	    loop(State);
        {From, stop} ->
            reply(From, stopped); %% Nothing to store
        {'DOWN', MRef, process, Parent, Reason} ->
            exit(Reason)
    end.

get_table(Table, Mod, Includes) ->
    case ets:lookup(Table, Mod) of
	[{Mod,Tab}] ->
	    {ok,Tab};
	[] ->
	    load_table(Mod, any, {{0,0,0},{0,0,0}}, Includes)
    end.

lookup(Tab, K) ->
    case ets:lookup(Tab, K) of
        []      -> undefined;
        [{K,V}] -> V
    end.

info(Erule) ->
    {asn1ct:vsn(),Erule}.

load_table(Mod, Erule, Mtime, Includes) ->
    Base = lists:concat([Mod, ".asn1db"]),
    case path_find(Includes, Mtime, Base) of
	error ->
	    error;
	{ok,ModTab} when Erule =:= any ->
	    {ok,ModTab};
	{ok,ModTab} ->
	    Vsn = asn1ct:vsn(),
	    case ets:lookup(ModTab, ?MAGIC_KEY) of
		[{_,{Vsn,Erule}}] ->
		    %% Correct version and encoding rule.
		    {ok,ModTab};
		_ ->
		    %% Missing key or wrong version/encoding rule.
		    ets:delete(ModTab),
		    error
	    end
    end.

path_find([H|T], Mtime, Base) ->
    File = filename:join(H, Base),
    case filelib:last_modified(File) of
	0 ->
	    path_find(T, Mtime, Base);
	DbMtime when DbMtime >= Mtime ->
	    case ets:file2tab(File) of
		{ok,_}=Ret ->
		    Ret;
		_ ->
		    path_find(T, Mtime, Base)
	    end;
	_ ->
	    path_find(T, Mtime, Base)
    end;
path_find([], _, _) -> error.