Use proc_lib for couch_file initialization
Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/0b89f5ac Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/0b89f5ac Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/0b89f5ac Branch: refs/heads/COUCHDB-1342 Commit: 0b89f5ac4110208618e8d36c0a2d14d4520f5a5b Parents: 6c0f7f7 Author: Filipe David Borba Manana <[email protected]> Authored: Mon Nov 28 23:43:59 2011 +0000 Committer: Filipe David Borba Manana <[email protected]> Committed: Mon Nov 28 23:43:59 2011 +0000 ---------------------------------------------------------------------- src/couchdb/couch_file.erl | 42 +++++++++++--------------------------- 1 files changed, 12 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/0b89f5ac/src/couchdb/couch_file.erl ---------------------------------------------------------------------- diff --git a/src/couchdb/couch_file.erl b/src/couchdb/couch_file.erl index 0e007bb..3979eb7 100644 --- a/src/couchdb/couch_file.erl +++ b/src/couchdb/couch_file.erl @@ -56,27 +56,7 @@ open(Filepath) -> open(Filepath, []). open(Filepath, Options) -> - case gen_server:start_link(couch_file, - {Filepath, Options, self(), Ref = make_ref()}, []) of - {ok, Fd} -> - {ok, Fd}; - ignore -> - % get the error - receive - {Ref, Pid, Error} -> - case process_info(self(), trap_exit) of - {trap_exit, true} -> receive {'EXIT', Pid, _} -> ok end; - {trap_exit, false} -> ok - end, - case Error of - {error, eacces} -> {file_permission_error, Filepath}; - _ -> Error - end - end; - Error -> - Error - end. - + proc_lib:start_link(?MODULE, init, [{Filepath, Options}]). %%---------------------------------------------------------------------- %% Purpose: To append an Erlang term to the end of the file. @@ -283,15 +263,9 @@ write_header(Fd, Data) -> ok = gen_server:call(Fd, {write_header, FinalBin}, infinity). - - -init_status_error(ReturnPid, Ref, Error) -> - ReturnPid ! {Ref, self(), Error}, - ignore. - % server functions -init({Filepath, Options, ReturnPid, Ref}) -> +init({Filepath, Options}) -> try ok = maybe_create_file(Filepath, Options), process_flag(trap_exit, true), @@ -299,10 +273,18 @@ init({Filepath, Options, ReturnPid, Ref}) -> {ok, Writer, Eof} = proc_lib:start_link( ?MODULE, spawn_writer, [Filepath, self()]), maybe_track_open_os_files(Options), - {ok, #file{reader = Reader, writer = Writer, eof = Eof}} + proc_lib:init_ack({ok, self()}), + InitState = #file{ + reader = Reader, + writer = Writer, + eof = Eof + }, + gen_server:enter_loop(?MODULE, [], InitState) catch + error:{badmatch, {error, eacces}} -> + proc_lib:init_ack({file_permission_error, Filepath}); error:{badmatch, Error} -> - init_status_error(ReturnPid, Ref, Error) + proc_lib:init_ack(Error) end. maybe_create_file(Filepath, Options) ->
