aboutsummaryrefslogtreecommitdiffstats
path: root/lib/snmp/src/misc
diff options
context:
space:
mode:
authorMicael Karlberg <[email protected]>2012-03-05 14:07:35 +0100
committerMicael Karlberg <[email protected]>2012-03-05 14:07:35 +0100
commitc5a16ad3551a87a9bb361a8bce220cb849247d43 (patch)
tree27986dd81b336a2526c49c97f73488fe64648736 /lib/snmp/src/misc
parent5834b490b8f426e2bbdec7d0b41459e09fdfb82a (diff)
downloadotp-c5a16ad3551a87a9bb361a8bce220cb849247d43.tar.gz
otp-c5a16ad3551a87a9bb361a8bce220cb849247d43.tar.bz2
otp-c5a16ad3551a87a9bb361a8bce220cb849247d43.zip
[snmp/agent] Some minor improvements to the error messages
OTP-9943
Diffstat (limited to 'lib/snmp/src/misc')
-rw-r--r--lib/snmp/src/misc/snmp_config.erl102
1 files changed, 89 insertions, 13 deletions
diff --git a/lib/snmp/src/misc/snmp_config.erl b/lib/snmp/src/misc/snmp_config.erl
index 6ab20e3e48..8bf0ea945b 100644
--- a/lib/snmp/src/misc/snmp_config.erl
+++ b/lib/snmp/src/misc/snmp_config.erl
@@ -90,6 +90,17 @@
]).
+-export_type([void/0,
+ verify_config_entry_function/0,
+ verify_config_function/0,
+ write_config_function/0]).
+
+
+%%----------------------------------------------------------------------
+
+-type void() :: term(). % Any value - ignored
+
+
%%----------------------------------------------------------------------
%% Handy SNMP configuration
%%----------------------------------------------------------------------
@@ -2356,53 +2367,118 @@ write_sys_config_file_manager_atl_opt(Fid, {seqno, SeqNo}) ->
header() ->
- {Y,Mo,D} = date(),
- {H,Mi,S} = time(),
+ {Y, Mo, D} = date(),
+ {H, Mi, S} = time(),
io_lib:format("%% This file was generated by "
- "snmp_config (version-~s) ~w-~2.2.0w-~2.2.0w "
+ "~w (version-~s) ~w-~2.2.0w-~2.2.0w "
"~2.2.0w:~2.2.0w:~2.2.0w\n",
- [?version,Y,Mo,D,H,Mi,S]).
+ [?MODULE, ?version, Y, Mo, D, H, Mi, S]).
+
+%% *If* these functions are successfull, they successfully return anything
+%% (which is ignored), but they fail with either a throw or an exit or
+%% something similar.
+
+%% Verification of one config entry read from a file
+-type(verify_config_entry_function() ::
+ fun((Entry :: term()) -> ok | {error, Reason :: term()})).
+
+%% Verification of config to be written
+-type(verify_config_function() ::
+ fun(() -> void())).
+
+%% Write config to file (as defined by Fd)
+-type(write_config_function() ::
+ fun((Fd :: file:io_device()) -> void())).
+
+-spec write_config_file(Dir :: string(),
+ FileName :: string(),
+ Verify :: verify_config_function(),
+ Write :: write_config_function()) ->
+ ok | {error, Reason :: term()}.
write_config_file(Dir, FileName, Verify, Write)
when (is_list(Dir) andalso
is_list(FileName) andalso
is_function(Verify) andalso
is_function(Write)) ->
- (catch do_write_config_file(Dir, FileName, Verify, Write)).
+ try
+ begin
+ do_write_config_file(Dir, FileName, Verify, Write)
+ end
+ catch
+ throw:Error ->
+ Error;
+ T:E ->
+ {error, {failed_write, Dir, FileName, T, E}}
+ end.
+
do_write_config_file(Dir, FileName, Verify, Write) ->
+ io:format("do_write_config_file -> entry with"
+ "~n Dir: ~p"
+ "~n FileName: ~p"
+ "~nwhen"
+ "~n FileInfo(Dir): ~p"
+ "~n FileList(Dir): ~p"
+ "~n", [Dir, FileName,
+ file:read_file_info(filename:join(Dir, FileName)),
+ file:list_dir(Dir)]),
Verify(),
case file:open(filename:join(Dir, FileName), [write]) of
{ok, Fd} ->
- (catch Write(Fd)),
- file:close(Fd),
- ok;
+ file_write_and_close(Write, Fd, Dir, FileName);
Error ->
Error
end.
-
append_config_file(Dir, FileName, Verify, Write)
when (is_list(Dir) andalso
is_list(FileName) andalso
is_function(Verify) andalso
is_function(Write)) ->
- (catch do_append_config_file(Dir, FileName, Verify, Write)).
+ try
+ begin
+ do_append_config_file(Dir, FileName, Verify, Write)
+ end
+ catch
+ throw:Error ->
+ Error;
+ T:E ->
+ {error, {failed_append, Dir, FileName, T, E}}
+ end.
do_append_config_file(Dir, FileName, Verify, Write) ->
Verify(),
case file:open(filename:join(Dir, FileName), [read, write]) of
{ok, Fd} ->
file:position(Fd, eof),
- (catch Write(Fd)),
- file:close(Fd),
- ok;
+ file_write_and_close(Write, Fd, Dir, FileName);
Error ->
Error
end.
+file_write_and_close(Write, Fd, Dir, FileName) ->
+ ok = Write(Fd),
+ case file:sync(Fd) of
+ ok ->
+ case file:close(Fd) of
+ ok ->
+ ok;
+ {error, Reason} ->
+ {error, {failed_closing, Dir, FileName, Reason}}
+ end;
+ {error, Reason} ->
+ {error, {failed_syncing, Dir, FileName, Reason}}
+ end.
+
+
+-spec read_config_file(Dir :: string(),
+ FileName :: string(),
+ Verify :: verify_config_entry_function()) ->
+ {ok, Config :: list()} | {error, Reason :: term()}.
+
read_config_file(Dir, FileName, Verify)
when is_list(Dir) andalso is_list(FileName) andalso is_function(Verify) ->
(catch do_read_config_file(Dir, FileName, Verify)).