summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Bakken <luke@bakken.io>2021-12-18 08:30:54 -0800
committerMichael Klishin <michael@clojurewerkz.org>2021-12-20 12:36:08 +0500
commit555c7efa271efc9c43ba9539bdd2a7c109df248c (patch)
tree5d766df032023825e3ceec556249b0174e3dda22
parentbc1a912af8d29c27206d65452254d04da2c16113 (diff)
downloadrabbitmq-server-git-555c7efa271efc9c43ba9539bdd2a7c109df248c.tar.gz
read term file using with_handle/1
-rw-r--r--deps/rabbit/src/rabbit_file.erl29
1 files changed, 16 insertions, 13 deletions
diff --git a/deps/rabbit/src/rabbit_file.erl b/deps/rabbit/src/rabbit_file.erl
index 6f2ae5e6a5..2060c354c5 100644
--- a/deps/rabbit/src/rabbit_file.erl
+++ b/deps/rabbit/src/rabbit_file.erl
@@ -90,19 +90,22 @@ read_file_info(File) ->
read_term_file(File) ->
try
- {ok, FInfo} = file:read_file_info(File, [raw]),
- {ok, Fd} = file:open(File, [read, raw, binary]),
- try
- {ok, Data} = file:read(Fd, FInfo#file_info.size),
- {ok, Tokens, _} = erl_scan:string(binary_to_list(Data)),
- TokenGroups = group_tokens(Tokens),
- {ok, [begin
- {ok, Term} = erl_parse:parse_term(Tokens1),
- Term
- end || Tokens1 <- TokenGroups]}
- after
- ok = file:close(Fd)
- end
+ F = fun() ->
+ {ok, FInfo} = file:read_file_info(File, [raw]),
+ {ok, Fd} = file:open(File, [read, raw, binary]),
+ try
+ file:read(Fd, FInfo#file_info.size)
+ after
+ file:close(Fd)
+ end
+ end,
+ {ok, Data} = with_handle(F),
+ {ok, Tokens, _} = erl_scan:string(binary_to_list(Data)),
+ TokenGroups = group_tokens(Tokens),
+ {ok, [begin
+ {ok, Term} = erl_parse:parse_term(Tokens1),
+ Term
+ end || Tokens1 <- TokenGroups]}
catch
error:{badmatch, Error} -> Error
end.