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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2013-2019. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
-module(snmpa_mib_storage_ets).
-behaviour(snmpa_mib_storage).
%%%-----------------------------------------------------------------
%%% This module implements the snmpa_mib_storage behaviour.
%%% It uses ets for storage.
%%%-----------------------------------------------------------------
-export([
open/5,
close/1,
read/2,
write/2,
delete/1,
delete/2,
match_object/2,
match_delete/2,
tab2list/1,
info/1, info/2,
sync/1,
backup/2
]).
-define(VMODULE,"MS-ETS").
-include("snmp_verbosity.hrl").
-record(tab, {id, rec_name, file, checksum = false}).
%% ---------------------------------------------------------------
%% open
%%
%% Open or create an ets table.
%% Possibly also read data from a (specified) file (mirror) and
%% populate the table from that (the dir option).
%%
%% Opts - A list of implementation dependent options
%% ets_open_options() = [ets_open_option()]
%% ets_open_option() = {dir, filename()} |
%% {action, keep | clear} |
%% {checksum, boolean()}
%%
%% The RecName and Fields arguments are not used in this
%% implementation.
%%
%% ---------------------------------------------------------------
%% This function creates the ets table
open(Name, RecName, _Fields, Type, Opts) ->
?vtrace("open table ~p", [Name]),
case lists:keysearch(dir, 1, Opts) of
{value, {dir, Dir}} ->
Action = snmp_misc:get_option(action, Opts, keep),
Checksum = snmp_misc:get_option(checksum, Opts, false),
?vtrace("open ~p database ~p - check if file exist", [Type, Name]),
File = filename:join(Dir, atom_to_list(Name) ++ ".db"),
case file:read_file_info(File) of
{ok, _} ->
?vdebug("open ~p database ~p - file exist - try reading",
[Type, Name]),
case ets:file2tab(File, [{verify, Checksum}]) of
{ok, ID} ->
?vtrace("open ~p database ~p - "
"data read from file", [Type, Name]),
{ok, #tab{id = ID,
rec_name = RecName,
file = File,
checksum = Checksum}};
{error, Reason} when (Action =:= keep) ->
?vinfo("open ~p database ~p - "
"failed reading from file (keep): "
"~n ~p",
[Type, Name, Reason]),
{error, {file2tab, Reason}};
{error, Reason} ->
?vlog("open ~p database ~p - "
"failed reading from file (clear): "
"~n ~p", [Type, Name, Reason]),
user_err("Warning: could not read file - "
"create new (empty): "
"~n File: ~p"
"~n Reason: ~p", [File, Reason]),
ID = ets:new(Name, [Type, protected, {keypos, 2}]),
write_ets_file(ID, File, Checksum),
{ok, #tab{id = ID,
rec_name = RecName,
file = File,
checksum = Checksum}}
end;
{error, enoent} ->
%% No such file - create it
?vdebug("open ~p database ~p - "
"file does *not* exist - create",
[Type, Name]),
ID = ets:new(Name, [Type, protected, {keypos, 2}]),
write_ets_file(ID, File, Checksum),
{ok, #tab{id = ID,
rec_name = RecName,
file = File,
checksum = Checksum}};
{error, Reason} when (Action =:= keep) ->
?vinfo("open ~p database ~p - "
"failed reading file info (keep): "
"~n ~p",
[Type, Name, Reason]),
{error, {read_file_info, Reason}};
{error, Reason} ->
?vlog("open ~p database ~p - "
"failed reading file info (clear): "
"~n ~p",
[Type, Name, Reason]),
user_err("Warning: could not read file info - "
"create new file: "
"~n File: ~p"
"~n Reason: ~p", [File, Reason]),
ID = ets:new(Name, [Type, protected, {keypos, 2}]),
write_ets_file(ID, File, Checksum),
{ok, #tab{id = ID,
rec_name = RecName,
file = File,
checksum = Checksum}}
end;
false ->
?vdebug("open ~p database ~p - ok", [Type, Name]),
ID = ets:new(Name, [Type, protected, {keypos, 2}]),
{ok, #tab{id = ID, rec_name = RecName}}
end.
%% ---------------------------------------------------------------
%% close
%%
%% Close the mib-storage table.
%% We will delete the table and if there is a file component,
%% will also be written to file.
%% ---------------------------------------------------------------
close(#tab{id = ID, file = undefined}) ->
?vtrace("close (delete) table ~p", [ID]),
ets:delete(ID);
close(#tab{id = ID, file = File, checksum = Checksum}) ->
?vtrace("close (delete) table ~p", [ID]),
write_ets_file(ID, File, Checksum),
ets:delete(ID).
%% ---------------------------------------------------------------
%% read
%%
%% Retrieve a record from the mib-storage table.
%% ---------------------------------------------------------------
read(#tab{id = ID}, Key) ->
?vtrace("read from table ~p: ~p", [ID, Key]),
case ets:lookup(ID, Key) of
[Rec|_] -> {value, Rec};
_ -> false
end.
%% ---------------------------------------------------------------
%% write
%%
%% Write a record to the mib-storage table.
%% ---------------------------------------------------------------
%% This is a very crude guard test is used instead of: is_record(Rec, RecName)
write(#tab{id = ID, rec_name = RecName}, Rec)
when (is_tuple(Rec) andalso (element(1, Rec) =:= RecName)) ->
?vtrace("write to table ~p", [ID]),
ets:insert(ID, Rec),
ok.
%% ---------------------------------------------------------------
%% delete
%%
%% Delete the mib-storage table.
%% ---------------------------------------------------------------
delete(#tab{id = ID, file = undefined}) ->
?vtrace("delete table ~p", [ID]),
ets:delete(ID);
delete(#tab{id = ID, file = File}) ->
?vtrace("delete table ~p", [ID]),
file:delete(File),
ets:delete(ID).
%% ---------------------------------------------------------------
%% delete
%%
%% Delete a record from the mib-storage table.
%% ---------------------------------------------------------------
delete(#tab{id = ID}, Key) ->
?vtrace("delete from table ~p: ~p", [ID, Key]),
ets:delete(ID, Key),
ok.
%% ---------------------------------------------------------------
%% match_object
%%
%% Search the mib-storage table for records witch matches
%% the pattern.
%% ---------------------------------------------------------------
match_object(#tab{id = ID}, Pattern) ->
?vtrace("match_object in ~p of ~p", [ID, Pattern]),
ets:match_object(ID, Pattern).
%% ---------------------------------------------------------------
%% match_delete
%%
%% Search the mib-storage table for records witch matches
%% the pattern and deletes them from the table.
%% ---------------------------------------------------------------
match_delete(#tab{id = ID}, Pattern) ->
?vtrace("match_delete in ~p with pattern ~p", [ID, Pattern]),
Recs = ets:match_object(ID, Pattern),
ets:match_delete(ID, Pattern),
Recs.
%% ---------------------------------------------------------------
%% tab2list
%%
%% Return all records in the mib-storage table in the form
%% of a list.
%% ---------------------------------------------------------------
tab2list(#tab{id = ID}) ->
?vtrace("tab2list -> list of ~p", [ID]),
ets:tab2list(ID).
%% ---------------------------------------------------------------
%% info/1,2
%%
%% Retrieve implementation dependent mib-storage table
%% information.
%% ---------------------------------------------------------------
info(#tab{id = ID}) ->
?vtrace("info on ~p", [ID]),
case ets:info(ID) of
undefined ->
[];
L ->
L
end.
info(TabId, all = _Item) ->
info(TabId);
info(#tab{id = ID}, Item) ->
?vtrace("info on ~p", [ID]),
ets:info(ID, Item).
%% ---------------------------------------------------------------
%% sync
%%
%% Dump mib-storage table to disc (if there is a file compionent)
%% ---------------------------------------------------------------
sync(#tab{file = undefined}) ->
ok;
sync(#tab{id = ID, file = File, checksum = Checksum}) ->
?vtrace("sync ~p", [ID]),
write_ets_file(ID, File, Checksum).
%% ---------------------------------------------------------------
%% backup
%%
%% Make a backup copy of the mib-storage table. Only valid id
%% there is a file component.
%% ---------------------------------------------------------------
backup(#tab{file = undefined}, _BackupDir) ->
ok;
backup(#tab{id = ID, file = File, checksum = Checksum}, BackupDir) ->
?vtrace("backup ~p to ~p", [ID, BackupDir]),
Filename = filename:basename(File),
case filename:join(BackupDir, Filename) of
File ->
%% Oups: backup-dir and db-dir the same
{error, db_dir};
BackupFile ->
write_ets_file(ID, BackupFile, Checksum)
end.
%%----------------------------------------------------------------------
write_ets_file(ID, File, Checksum) when (Checksum =:= true) ->
do_write_ets_file(ID, File, [{extended_info, [md5sum]}]);
write_ets_file(ID, File, Checksum) when (Checksum =:= false) ->
do_write_ets_file(ID, File, []).
do_write_ets_file(ID, File, Options) ->
TmpFile = File ++ ".tmp",
case ets:tab2file(ID, TmpFile, Options) of
ok ->
case file:rename(TmpFile, File) of
ok ->
ok;
Else ->
user_err("Warning: could not move file ~p"
" (~p)", [File, Else])
end;
{error, Reason} ->
user_err("Warning: could not save file ~p (~p)",
[File, Reason])
end.
%%----------------------------------------------------------------------
user_err(F, A) ->
snmpa_error:user_err(F, A).
|