diff options
author | Alexandru Scvortov <alexandru@rabbitmq.com> | 2011-09-21 12:44:56 +0100 |
---|---|---|
committer | Alexandru Scvortov <alexandru@rabbitmq.com> | 2011-09-21 12:44:56 +0100 |
commit | 496b4284d232a3c45136a5a1deac5b56780b953c (patch) | |
tree | fa0961d7fe899f622af5534f93df5f8202558e6a | |
parent | 201f9a85d737aba9b25e2550e395cd3f5b8e01c5 (diff) | |
download | rabbitmq-server-496b4284d232a3c45136a5a1deac5b56780b953c.tar.gz |
guard all prim_file uses
-rw-r--r-- | src/file_handle_cache.erl | 6 | ||||
-rw-r--r-- | src/filelib2.erl | 428 | ||||
-rw-r--r-- | src/rabbit_misc.erl | 125 | ||||
-rw-r--r-- | src/rabbit_queue_index.erl | 29 | ||||
-rw-r--r-- | src/serialiser.erl | 75 |
5 files changed, 113 insertions, 550 deletions
diff --git a/src/file_handle_cache.erl b/src/file_handle_cache.erl index 1e8320d9..6c3f1b5f 100644 --- a/src/file_handle_cache.erl +++ b/src/file_handle_cache.erl @@ -474,7 +474,11 @@ set_maximum_since_use(MaximumAge) -> end. obtain() -> - gen_server2:call(?SERVER, {obtain, self()}, infinity). + %% If the FHC isn't running, obtains succeed immediately. + case whereis(?SERVER) of + undefined -> ok; + _ -> gen_server2:call(?SERVER, {obtain, self()}, infinity) + end. release() -> gen_server2:cast(?SERVER, {release, self()}). diff --git a/src/filelib2.erl b/src/filelib2.erl deleted file mode 100644 index e92396ea..00000000 --- a/src/filelib2.erl +++ /dev/null @@ -1,428 +0,0 @@ -%% -%% %CopyrightBegin% -%% -%% Copyright Ericsson AB 1997-2009. 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% - -%% This is the 'filelib' module ported from R13B03. The only changes -%% are replacing calls to 'file' with calls to 'prim_file', removing -%% most of the specs and inlining the call to error. - --module(filelib2). - -%% File utilities. - --export([wildcard/1, wildcard/2, is_dir/1, is_file/1, is_regular/1, - compile_wildcard/1]). --export([fold_files/5, last_modified/1, file_size/1, ensure_dir/1]). - --export([wildcard/3, is_dir/2, is_file/2, is_regular/2]). --export([fold_files/6, last_modified/2, file_size/2]). - --include_lib("kernel/include/file.hrl"). - --define(HANDLE_ERROR(Expr), - try - Expr - catch - error:{badpattern,_}=UnUsUalVaRiAbLeNaMe -> - %% Get the stack backtrace correct. - erlang:error(UnUsUalVaRiAbLeNaMe) - end). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -wildcard(Pattern) when is_list(Pattern) -> - ?HANDLE_ERROR(do_wildcard(Pattern, file)). - -wildcard(Pattern, Cwd) when is_list(Pattern), is_list(Cwd) -> - ?HANDLE_ERROR(do_wildcard(Pattern, Cwd, file)); -wildcard(Pattern, Mod) when is_list(Pattern), is_atom(Mod) -> - ?HANDLE_ERROR(do_wildcard(Pattern, Mod)). - -wildcard(Pattern, Cwd, Mod) - when is_list(Pattern), is_list(Cwd), is_atom(Mod) -> - ?HANDLE_ERROR(do_wildcard(Pattern, Cwd, Mod)). - -is_dir(Dir) -> - do_is_dir(Dir, file). - -is_dir(Dir, Mod) when is_atom(Mod) -> - do_is_dir(Dir, Mod). - -is_file(File) -> - do_is_file(File, file). - -is_file(File, Mod) when is_atom(Mod) -> - do_is_file(File, Mod). - -is_regular(File) -> - do_is_regular(File, file). - -is_regular(File, Mod) when is_atom(Mod) -> - do_is_regular(File, Mod). - -fold_files(Dir, RegExp, Recursive, Fun, Acc) -> - do_fold_files(Dir, RegExp, Recursive, Fun, Acc, file). - -fold_files(Dir, RegExp, Recursive, Fun, Acc, Mod) when is_atom(Mod) -> - do_fold_files(Dir, RegExp, Recursive, Fun, Acc, Mod). - -last_modified(File) -> - do_last_modified(File, file). - -last_modified(File, Mod) when is_atom(Mod) -> - do_last_modified(File, Mod). - -file_size(File) -> - do_file_size(File, file). - -file_size(File, Mod) when is_atom(Mod) -> - do_file_size(File, Mod). - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -do_wildcard(Pattern, Mod) when is_list(Pattern) -> - do_wildcard_comp(do_compile_wildcard(Pattern), Mod). - -do_wildcard_comp({compiled_wildcard,{exists,File}}, Mod) -> - case eval_read_file_info(File, Mod) of - {ok,_} -> [File]; - _ -> [] - end; -do_wildcard_comp({compiled_wildcard,[Base|Rest]}, Mod) -> - do_wildcard_1([Base], Rest, Mod). - -do_wildcard(Pattern, Cwd, Mod) when is_list(Pattern), is_list(Cwd) -> - do_wildcard_comp(do_compile_wildcard(Pattern), Cwd, Mod). - -do_wildcard_comp({compiled_wildcard,{exists,File}}, Cwd, Mod) -> - case eval_read_file_info(filename:absname(File, Cwd), Mod) of - {ok,_} -> [File]; - _ -> [] - end; -do_wildcard_comp({compiled_wildcard,[current|Rest]}, Cwd0, Mod) -> - Cwd = filename:join([Cwd0]), %Slash away redundant slashes. - PrefixLen = length(Cwd)+1, - [lists:nthtail(PrefixLen, N) || N <- do_wildcard_1([Cwd], Rest, Mod)]; -do_wildcard_comp({compiled_wildcard,[Base|Rest]}, _Cwd, Mod) -> - do_wildcard_1([Base], Rest, Mod). - -do_is_dir(Dir, Mod) -> - case eval_read_file_info(Dir, Mod) of - {ok, #file_info{type=directory}} -> - true; - _ -> - false - end. - -do_is_file(File, Mod) -> - case eval_read_file_info(File, Mod) of - {ok, #file_info{type=regular}} -> - true; - {ok, #file_info{type=directory}} -> - true; - _ -> - false - end. - -do_is_regular(File, Mod) -> - case eval_read_file_info(File, Mod) of - {ok, #file_info{type=regular}} -> - true; - _ -> - false - end. - -%% fold_files(Dir, RegExp, Recursive, Fun, AccIn). - -%% folds the function Fun(F, Acc) -> Acc1 over -%% all files <F> in <Dir> that match the regular expression <RegExp> -%% If <Recursive> is true all sub-directories to <Dir> are processed - -do_fold_files(Dir, RegExp, Recursive, Fun, Acc, Mod) -> - {ok, Re1} = re:compile(RegExp), - do_fold_files1(Dir, Re1, Recursive, Fun, Acc, Mod). - -do_fold_files1(Dir, RegExp, Recursive, Fun, Acc, Mod) -> - case eval_list_dir(Dir, Mod) of - {ok, Files} -> do_fold_files2(Files, Dir, RegExp, Recursive, Fun, Acc, Mod); - {error, _} -> Acc - end. - -do_fold_files2([], _Dir, _RegExp, _Recursive, _Fun, Acc, _Mod) -> - Acc; -do_fold_files2([File|T], Dir, RegExp, Recursive, Fun, Acc0, Mod) -> - FullName = filename:join(Dir, File), - case do_is_regular(FullName, Mod) of - true -> - case re:run(File, RegExp, [{capture,none}]) of - match -> - Acc = Fun(FullName, Acc0), - do_fold_files2(T, Dir, RegExp, Recursive, Fun, Acc, Mod); - nomatch -> - do_fold_files2(T, Dir, RegExp, Recursive, Fun, Acc0, Mod) - end; - false -> - case Recursive andalso do_is_dir(FullName, Mod) of - true -> - Acc1 = do_fold_files1(FullName, RegExp, Recursive, - Fun, Acc0, Mod), - do_fold_files2(T, Dir, RegExp, Recursive, Fun, Acc1, Mod); - false -> - do_fold_files2(T, Dir, RegExp, Recursive, Fun, Acc0, Mod) - end - end. - -do_last_modified(File, Mod) -> - case eval_read_file_info(File, Mod) of - {ok, Info} -> - Info#file_info.mtime; - _ -> - 0 - end. - -do_file_size(File, Mod) -> - case eval_read_file_info(File, Mod) of - {ok, Info} -> - Info#file_info.size; - _ -> - 0 - end. - -%%---------------------------------------------------------------------- -%% +type ensure_dir(X) -> ok | {error, Reason}. -%% +type X = filename() | dirname() -%% ensures that the directory name required to create D exists - -ensure_dir("/") -> - ok; -ensure_dir(F) -> - Dir = filename:dirname(F), - case do_is_dir(Dir, file) of - true -> - ok; - false -> - ensure_dir(Dir), - prim_file:make_dir(Dir) - end. - - -%%% -%%% Pattern matching using a compiled wildcard. -%%% - -do_wildcard_1(Files, Pattern, Mod) -> - do_wildcard_2(Files, Pattern, [], Mod). - -do_wildcard_2([File|Rest], Pattern, Result, Mod) -> - do_wildcard_2(Rest, Pattern, do_wildcard_3(File, Pattern, Result, Mod), Mod); -do_wildcard_2([], _, Result, _Mod) -> - Result. - -do_wildcard_3(Base, [Pattern|Rest], Result, Mod) -> - case do_list_dir(Base, Mod) of - {ok, Files0} -> - Files = lists:sort(Files0), - Matches = wildcard_4(Pattern, Files, Base, []), - do_wildcard_2(Matches, Rest, Result, Mod); - _ -> - Result - end; -do_wildcard_3(Base, [], Result, _Mod) -> - [Base|Result]. - -wildcard_4(Pattern, [File|Rest], Base, Result) -> - case wildcard_5(Pattern, File) of - true -> - wildcard_4(Pattern, Rest, Base, [join(Base, File)|Result]); - false -> - wildcard_4(Pattern, Rest, Base, Result) - end; -wildcard_4(_Patt, [], _Base, Result) -> - Result. - -wildcard_5([question|Rest1], [_|Rest2]) -> - wildcard_5(Rest1, Rest2); -wildcard_5([accept], _) -> - true; -wildcard_5([star|Rest], File) -> - do_star(Rest, File); -wildcard_5([{one_of, Ordset}|Rest], [C|File]) -> - case ordsets:is_element(C, Ordset) of - true -> wildcard_5(Rest, File); - false -> false - end; -wildcard_5([{alt, Alts}], File) -> - do_alt(Alts, File); -wildcard_5([C|Rest1], [C|Rest2]) when is_integer(C) -> - wildcard_5(Rest1, Rest2); -wildcard_5([X|_], [Y|_]) when is_integer(X), is_integer(Y) -> - false; -wildcard_5([], []) -> - true; -wildcard_5([], [_|_]) -> - false; -wildcard_5([_|_], []) -> - false. - -do_star(Pattern, [X|Rest]) -> - case wildcard_5(Pattern, [X|Rest]) of - true -> true; - false -> do_star(Pattern, Rest) - end; -do_star(Pattern, []) -> - wildcard_5(Pattern, []). - -do_alt([Alt|Rest], File) -> - case wildcard_5(Alt, File) of - true -> true; - false -> do_alt(Rest, File) - end; -do_alt([], _File) -> - false. - -do_list_dir(current, Mod) -> eval_list_dir(".", Mod); -do_list_dir(Dir, Mod) -> eval_list_dir(Dir, Mod). - -join(current, File) -> File; -join(Base, File) -> filename:join(Base, File). - - -%%% Compiling a wildcard. - -compile_wildcard(Pattern) -> - ?HANDLE_ERROR(do_compile_wildcard(Pattern)). - -do_compile_wildcard(Pattern) -> - {compiled_wildcard,compile_wildcard_1(Pattern)}. - -compile_wildcard_1(Pattern) -> - [Root|Rest] = filename:split(Pattern), - case filename:pathtype(Root) of - relative -> - compile_wildcard_2([Root|Rest], current); - _ -> - compile_wildcard_2(Rest, [Root]) - end. - -compile_wildcard_2([Part|Rest], Root) -> - case compile_part(Part) of - Part -> - compile_wildcard_2(Rest, join(Root, Part)); - Pattern -> - compile_wildcard_3(Rest, [Pattern,Root]) - end; -compile_wildcard_2([], Root) -> {exists,Root}. - -compile_wildcard_3([Part|Rest], Result) -> - compile_wildcard_3(Rest, [compile_part(Part)|Result]); -compile_wildcard_3([], Result) -> - lists:reverse(Result). - -compile_part(Part) -> - compile_part(Part, false, []). - -compile_part_to_sep(Part) -> - compile_part(Part, true, []). - -compile_part([], true, _) -> - erlang:error({badpattern, missing_delimiter}); -compile_part([$,|Rest], true, Result) -> - {ok, $,, lists:reverse(Result), Rest}; -compile_part([$}|Rest], true, Result) -> - {ok, $}, lists:reverse(Result), Rest}; -compile_part([$?|Rest], Upto, Result) -> - compile_part(Rest, Upto, [question|Result]); -compile_part([$*], Upto, Result) -> - compile_part([], Upto, [accept|Result]); -compile_part([$*|Rest], Upto, Result) -> - compile_part(Rest, Upto, [star|Result]); -compile_part([$[|Rest], Upto, Result) -> - case compile_charset(Rest, ordsets:new()) of - {ok, Charset, Rest1} -> - compile_part(Rest1, Upto, [Charset|Result]); - error -> - compile_part(Rest, Upto, [$[|Result]) - end; -compile_part([${|Rest], Upto, Result) -> - case compile_alt(Rest) of - {ok, Alt} -> - lists:reverse(Result, [Alt]); - error -> - compile_part(Rest, Upto, [${|Result]) - end; -compile_part([X|Rest], Upto, Result) -> - compile_part(Rest, Upto, [X|Result]); -compile_part([], _Upto, Result) -> - lists:reverse(Result). - -compile_charset([$]|Rest], Ordset) -> - compile_charset1(Rest, ordsets:add_element($], Ordset)); -compile_charset([$-|Rest], Ordset) -> - compile_charset1(Rest, ordsets:add_element($-, Ordset)); -compile_charset([], _Ordset) -> - error; -compile_charset(List, Ordset) -> - compile_charset1(List, Ordset). - -compile_charset1([Lower, $-, Upper|Rest], Ordset) when Lower =< Upper -> - compile_charset1(Rest, compile_range(Lower, Upper, Ordset)); -compile_charset1([$]|Rest], Ordset) -> - {ok, {one_of, Ordset}, Rest}; -compile_charset1([X|Rest], Ordset) -> - compile_charset1(Rest, ordsets:add_element(X, Ordset)); -compile_charset1([], _Ordset) -> - error. - -compile_range(Lower, Current, Ordset) when Lower =< Current -> - compile_range(Lower, Current-1, ordsets:add_element(Current, Ordset)); -compile_range(_, _, Ordset) -> - Ordset. - -compile_alt(Pattern) -> - compile_alt(Pattern, []). - -compile_alt(Pattern, Result) -> - case compile_part_to_sep(Pattern) of - {ok, $,, AltPattern, Rest} -> - compile_alt(Rest, [AltPattern|Result]); - {ok, $}, AltPattern, Rest} -> - NewResult = [AltPattern|Result], - RestPattern = compile_part(Rest), - {ok, {alt, [Alt++RestPattern || Alt <- NewResult]}}; - Pattern -> - error - end. - -eval_read_file_info(File, file) -> - prim_file:read_file_info(File); -eval_read_file_info(File, erl_prim_loader) -> - case erl_prim_loader:read_file_info(File) of - error -> {error, erl_prim_loader}; - Res-> Res - end; -eval_read_file_info(File, Mod) -> - Mod:read_file_info(File). - -eval_list_dir(Dir, file) -> - prim_file:list_dir(Dir); -eval_list_dir(Dir, erl_prim_loader) -> - case erl_prim_loader:list_dir(Dir) of - error -> {error, erl_prim_loader}; - Res-> Res - end; -eval_list_dir(Dir, Mod) -> - Mod:list_dir(Dir). diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl index 77a9b5f2..cd28bd54 100644 --- a/src/rabbit_misc.erl +++ b/src/rabbit_misc.erl @@ -42,13 +42,15 @@ -export([dirty_read_all/1, dirty_foreach_key/2, dirty_dump_log/1]). -export([read_term_file/1, write_term_file/2, write_file/2, write_file/3]). -export([append_file/2, ensure_parent_dirs_exist/1]). +-export([is_file/1, is_dir/1, file_size/1, ensure_dir/1, wildcard/2, list_dir/1]). -export([format_stderr/2, with_local_io/1]). -export([start_applications/1, stop_applications/1]). -export([unfold/2, ceil/1, queue_fold/3]). -export([sort_field_table/1]). -export([pid_to_string/1, string_to_pid/1]). -export([version_compare/2, version_compare/3]). --export([recursive_delete/1, recursive_copy/2, dict_cons/3, orddict_cons/3]). +-export([delete/1, recursive_delete/1, recursive_copy/2]). +-export([dict_cons/3, orddict_cons/3]). -export([get_options/2]). -export([all_module_attributes/1, build_acyclic_graph/3]). -export([now_ms/0]). @@ -59,6 +61,7 @@ -export([pget/2, pget/3, pget_or_die/2]). -export([format_message_queue/2]). -export([append_rpc_all_nodes/4]). +-export([with_fhc_handle/1]). %%---------------------------------------------------------------------------- @@ -528,7 +531,7 @@ dirty_dump_log1(LH, {K, Terms, BadBytes}) -> read_term_file(File) -> try - {ok, Data} = prim_file:read_file(File), + {ok, Data} = with_fhc_handle(fun () -> prim_file:read_file(File) end), {ok, Tokens, _} = erl_scan:string(binary:bin_to_list(Data)), TokenGroups = group_tokens(Tokens), {ok, [begin @@ -573,15 +576,17 @@ write_file(Path, Data, Modes) -> Modes1 = [binary, write | (Modes -- [binary, write])], case make_binary(Data) of Bin when is_binary(Bin) -> - case prim_file:open(Path, Modes1) of - {ok, Hdl} -> try prim_file:write(Hdl, Bin) of - ok -> prim_file:sync(Hdl); - {error, _} = E -> E - after - prim_file:close(Hdl) - end; - {error, _} = E -> E - end; + with_fhc_handle( + fun () -> case prim_file:open(Path, Modes1) of + {ok, Hdl} -> try prim_file:write(Hdl, Bin) of + ok -> prim_file:sync(Hdl); + {error, _} = E -> E + after + prim_file:close(Hdl) + end; + {error, _} = E -> E + end + end); {error, _} = E -> E end. @@ -596,7 +601,7 @@ make_binary(List) -> append_file(File, Suffix) -> - case prim_file:read_file_info(File) of + case read_file_info(File) of {ok, FInfo} -> append_file(File, FInfo#file_info.size, Suffix); {error, enoent} -> append_file(File, 0, Suffix); Error -> Error @@ -605,23 +610,73 @@ append_file(File, Suffix) -> append_file(_, _, "") -> ok; append_file(File, 0, Suffix) -> - case prim_file:open([File, Suffix], [append]) of - {ok, Fd} -> prim_file:close(Fd); - Error -> Error - end; + with_fhc_handle(fun () -> + case prim_file:open([File, Suffix], [append]) of + {ok, Fd} -> prim_file:close(Fd); + Error -> Error + end + end); append_file(File, _, Suffix) -> - case prim_file:read_file(File) of + case with_fhc_handle(fun () -> prim_file:read_file(File) end) of {ok, Data} -> write_file([File, Suffix], Data, [append]); Error -> Error end. ensure_parent_dirs_exist(Filename) -> - case filelib2:ensure_dir(Filename) of + case ensure_dir(Filename) of ok -> ok; {error, Reason} -> throw({error, {cannot_create_parent_dirs, Filename, Reason}}) end. +is_file(File) -> + case read_file_info(File) of + {ok, #file_info{type=regular}} -> true; + {ok, #file_info{type=directory}} -> true; + _ -> false + end. + +is_dir(Dir) -> is_dir_internal(read_file_info(Dir)). +is_dir_no_handle(Dir) -> is_dir_internal(prim_file:read_file_info(Dir)). + +is_dir_internal({ok, #file_info{type=directory}}) -> true; +is_dir_internal(_) -> false. + +file_size(File) -> + case read_file_info(File) of + {ok, #file_info{size=Size}} -> Size; + _ -> 0 + end. + +ensure_dir(File) -> with_fhc_handle(fun () -> ensure_dir_internal(File) end). + +ensure_dir_internal("/") -> + ok; +ensure_dir_internal(File) -> + Dir = filename:dirname(File), + case is_dir_no_handle(Dir) of + true -> ok; + false -> ensure_dir_internal(Dir), + prim_file:make_dir(Dir) + end. + +wildcard(Pattern, Dir) -> + {ok, Files} = list_dir(Dir), + {ok, RE} = re:compile(Pattern, [anchored]), + [File || File <- Files, match =:= re:run(File, RE, [{capture, none}])]. + +list_dir(Dir) -> + with_fhc_handle(fun () -> prim_file:list_dir(Dir) end). + +read_file_info(File) -> + with_fhc_handle(fun () -> prim_file:read_file_info(File) end). + +with_fhc_handle(Fun) -> + ok = file_handle_cache:obtain(), + try Fun() + after ok = file_handle_cache:release() + end. + format_stderr(Fmt, Args) -> case os:type() of {unix, _} -> @@ -772,13 +827,17 @@ version_compare(A, B) -> dropdot(A) -> lists:dropwhile(fun (X) -> X =:= $. end, A). +delete(File) -> with_fhc_handle(fun () -> prim_file:delete(File) end). + recursive_delete(Files) -> - lists:foldl(fun (Path, ok ) -> recursive_delete1(Path); - (_Path, {error, _Err} = Error) -> Error - end, ok, Files). + with_fhc_handle( + fun () -> lists:foldl(fun (Path, ok) -> recursive_delete1(Path); + (_Path, {error, _Err} = Error) -> Error + end, ok, Files) + end). recursive_delete1(Path) -> - case filelib2:is_dir(Path) and not(is_symlink(Path)) of + case is_dir_no_handle(Path) and not(is_symlink_no_handle(Path)) of false -> case prim_file:delete(Path) of ok -> ok; {error, enoent} -> ok; %% Path doesn't exist anyway @@ -806,22 +865,24 @@ recursive_delete1(Path) -> end end. -is_symlink(Name) -> - case file:read_link(Name) of +is_symlink_no_handle(File) -> + case prim_file:read_link(File) of {ok, _} -> true; _ -> false end. recursive_copy(Src, Dest) -> - case filelib2:is_dir(Src) of - false -> case prim_file:copy(Src, Dest, infinity) of + %% Note that this uses the 'file' module and, hence, shouldn't be + %% run on many processes at once. + case is_dir(Src) of + false -> case file:copy(Src, Dest) of {ok, _Bytes} -> ok; {error, enoent} -> ok; %% Path doesn't exist anyway {error, Err} -> {error, {Src, Dest, Err}} end; - true -> case prim_file:list_dir(Src) of + true -> case file:list_dir(Src) of {ok, FileNames} -> - case prim_file:make_dir(Dest) of + case file:make_dir(Dest) of ok -> lists:foldl( fun (FileName, ok) -> @@ -931,10 +992,12 @@ build_acyclic_graph(VertexFun, EdgeFun, Graph) -> %% TODO: When we stop supporting Erlang prior to R14, this should be %% replaced with file:open [write, exclusive] lock_file(Path) -> - case filelib2:is_file(Path) of + case is_file(Path) of true -> {error, eexist}; - false -> {ok, Lock} = prim_file:open(Path, [write]), - ok = prim_file:close(Lock) + false -> with_fhc_handle( + fun () -> {ok, Lock} = prim_file:open(Path, [write]), + ok = prim_file:close(Lock) + end) end. const_ok() -> ok. diff --git a/src/rabbit_queue_index.erl b/src/rabbit_queue_index.erl index d4b26d06..9febaf47 100644 --- a/src/rabbit_queue_index.erl +++ b/src/rabbit_queue_index.erl @@ -229,7 +229,7 @@ init(Name, OnSyncFun) -> State = #qistate { dir = Dir } = blank_state(Name), - false = filelib2:is_file(Dir), %% is_file == is file or dir + false = rabbit_misc:is_file(Dir), %% is_file == is file or dir State #qistate { on_sync = OnSyncFun }. shutdown_terms(Name) -> @@ -256,9 +256,7 @@ terminate(Terms, State) -> delete_and_terminate(State) -> {_SegmentCounts, State1 = #qistate { dir = Dir }} = terminate(State), - ok = file_handle_cache:obtain(), ok = rabbit_misc:recursive_delete([Dir]), - ok = file_handle_cache:release(), State1. publish(MsgId, SeqId, MsgProps, IsPersistent, @@ -368,9 +366,9 @@ recover(DurableQueues) -> {DurableTerms, {fun queue_index_walker/1, {start, DurableQueueNames}}}. all_queue_directory_names(Dir) -> - case prim_file:list_dir(Dir) of + case rabbit_misc:list_dir(Dir) of {ok, Entries} -> [ Entry || Entry <- Entries, - filelib2:is_dir( + rabbit_misc:is_dir( filename:join(Dir, Entry)) ]; {error, enoent} -> [] end. @@ -394,7 +392,7 @@ blank_state(QueueName) -> clean_file_name(Dir) -> filename:join(Dir, ?CLEAN_FILENAME). detect_clean_shutdown(Dir) -> - case prim_file:delete(clean_file_name(Dir)) of + case rabbit_misc:delete(clean_file_name(Dir)) of ok -> true; {error, enoent} -> false end. @@ -404,7 +402,7 @@ read_shutdown_terms(Dir) -> store_clean_shutdown(Terms, Dir) -> CleanFileName = clean_file_name(Dir), - ok = filelib2:ensure_dir(CleanFileName), + ok = rabbit_misc:ensure_dir(CleanFileName), rabbit_misc:write_term_file(CleanFileName, Terms). init_clean(RecoveredCounts, State) -> @@ -605,8 +603,8 @@ flush_journal(State = #qistate { segments = Segments }) -> Segments1 = segment_fold( fun (#segment { unacked = 0, path = Path }, SegmentsN) -> - case filelib2:is_file(Path) of - true -> ok = prim_file:delete(Path); + case rabbit_misc:is_file(Path) of + true -> ok = rabbit_misc:delete(Path); false -> ok end, SegmentsN; @@ -632,7 +630,7 @@ append_journal_to_segment(#segment { journal_entries = JEntries, get_journal_handle(State = #qistate { journal_handle = undefined, dir = Dir }) -> Path = filename:join(Dir, ?JOURNAL_FILENAME), - ok = filelib2:ensure_dir(Path), + ok = rabbit_misc:ensure_dir(Path), {ok, Hdl} = file_handle_cache:open(Path, ?WRITE_MODE, [{write_buffer, infinity}]), {Hdl, State #qistate { journal_handle = Hdl }}; @@ -737,7 +735,7 @@ all_segment_nums(#qistate { dir = Dir, segments = Segments }) -> lists:takewhile(fun (C) -> $0 =< C andalso C =< $9 end, SegName)), Set) end, sets:from_list(segment_nums(Segments)), - filelib2:wildcard("*" ++ ?SEGMENT_EXTENSION, Dir)))). + rabbit_misc:wildcard(".*\\" ++ ?SEGMENT_EXTENSION, Dir)))). segment_find_or_new(Seg, Dir, Segments) -> case segment_find(Seg, Segments) of @@ -838,7 +836,7 @@ segment_entries_foldr(Fun, Init, %% %% Does not do any combining with the journal at all. load_segment(KeepAcked, #segment { path = Path }) -> - case filelib2:is_file(Path) of + case rabbit_misc:is_file(Path) of false -> {array_new(), 0}; true -> {ok, Hdl} = file_handle_cache:open(Path, ?READ_AHEAD_MODE, []), {ok, 0} = file_handle_cache:position(Hdl, bof), @@ -1042,12 +1040,12 @@ foreach_queue_index(Funs) -> transform_queue(Dir, Gatherer, {JournalFun, SegmentFun}) -> ok = transform_file(filename:join(Dir, ?JOURNAL_FILENAME), JournalFun), [ok = transform_file(filename:join(Dir, Seg), SegmentFun) - || Seg <- filelib2:wildcard("*" ++ ?SEGMENT_EXTENSION, Dir)], + || Seg <- rabbit_misc:wildcard(".*\\" ++ ?SEGMENT_EXTENSION, Dir)], ok = gatherer:finish(Gatherer). transform_file(Path, Fun) -> PathTmp = Path ++ ".upgrade", - case filelib2:file_size(Path) of + case rabbit_misc:file_size(Path) of 0 -> ok; Size -> {ok, PathTmpHdl} = file_handle_cache:open(PathTmp, ?WRITE_MODE, @@ -1061,7 +1059,8 @@ transform_file(Path, Fun) -> ok = drive_transform_fun(Fun, PathTmpHdl, Content), ok = file_handle_cache:close(PathTmpHdl), - ok = prim_file:rename(PathTmp, Path) + ok = rabbit_misc:with_fhc_handle( + fun () -> prim_file:rename(PathTmp, Path) end) end. drive_transform_fun(Fun, Hdl, Contents) -> diff --git a/src/serialiser.erl b/src/serialiser.erl deleted file mode 100644 index 0f9bcf17..00000000 --- a/src/serialiser.erl +++ /dev/null @@ -1,75 +0,0 @@ -%% The contents of this file are subject to the Mozilla Public License -%% Version 1.1 (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.mozilla.org/MPL/ -%% -%% 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. -%% -%% The Original Code is RabbitMQ. -%% -%% The Initial Developer of the Original Code is VMware, Inc. -%% Copyright (c) 2011 VMware, Inc. All rights reserved. -%% - --module(serialiser). - --behaviour(gen_server2). - --export([start_link/0, submit/2]). - --export([init/1, handle_call/3, handle_cast/2, handle_info/2, - terminate/2, code_change/3]). - -%%---------------------------------------------------------------------------- - --ifdef(use_specs). - --spec(start_link/0 :: () -> {'ok', pid()} | {'error', any()}). --spec(submit/2 :: - (pid() | atom(), fun (() -> A) | {atom(), atom(), [any()]}) -> A). - --endif. - -%%---------------------------------------------------------------------------- - --define(HIBERNATE_AFTER_MIN, 1000). --define(DESIRED_HIBERNATE, 10000). - -%%---------------------------------------------------------------------------- - -start_link() -> - gen_server2:start_link(?MODULE, [], [{timeout, infinity}]). - -submit(Pid, Fun) -> - gen_server2:call(Pid, {run, Fun}, infinity). - -%%---------------------------------------------------------------------------- - -init([]) -> - {ok, nostate, hibernate, - {backoff, ?HIBERNATE_AFTER_MIN, ?HIBERNATE_AFTER_MIN, ?DESIRED_HIBERNATE}}. - -handle_call({run, Fun}, _From, State) -> - {reply, run(Fun), State, hibernate}; -handle_call(Msg, _From, State) -> - {stop, {unexpected_call, Msg}, State}. - -handle_cast(Msg, State) -> - {stop, {unexpected_cast, Msg}, State}. - -handle_info(Msg, State) -> - {stop, {unexpected_info, Msg}, State}. - -code_change(_OldVsn, State, _Extra) -> - {ok, State}. - -terminate(_Reason, State) -> - State. - -%%---------------------------------------------------------------------------- - -run({M, F, A}) -> apply(M, F, A); -run(Fun) -> Fun(). |