diff options
author | AlexanderKaraberov <alexander@spotme.com> | 2018-02-14 17:10:32 +0100 |
---|---|---|
committer | Jan Lehnardt <jan@apache.org> | 2018-03-09 19:09:14 +0100 |
commit | b05c1775fb85d7fb979879fd16dba4ed736a2a96 (patch) | |
tree | 16efcf994e650f45d018f8f0fa51b6bbdfd51634 | |
parent | 6f294c1645e1a8b28258a98ee401f06101990da3 (diff) | |
download | couchdb-b05c1775fb85d7fb979879fd16dba4ed736a2a96.tar.gz |
Proper error handling for file:open() call
-rw-r--r-- | src/couch/src/couch_file.erl | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/couch/src/couch_file.erl b/src/couch/src/couch_file.erl index 7b6f519be..8a1b205e0 100644 --- a/src/couch/src/couch_file.erl +++ b/src/couch/src/couch_file.erl @@ -213,8 +213,12 @@ truncate(Fd, Pos) -> %%---------------------------------------------------------------------- sync(Filepath) when is_list(Filepath) -> - {ok, Fd} = file:open(Filepath, [append, raw]), - try ok = file:sync(Fd) after ok = file:close(Fd) end; + case file:open(Filepath, [append, raw]) of + {ok, Fd} -> + try ok = file:sync(Fd) after ok = file:close(Fd) end; + Error -> + throw(Error) + end; sync(Fd) -> gen_server:call(Fd, sync, infinity). @@ -389,14 +393,18 @@ init({Filepath, Options, ReturnPid, Ref}) -> % open in read mode first, so we don't create the file if it doesn't exist. case file:open(Filepath, [read, raw]) of {ok, Fd_Read} -> - {ok, Fd} = file:open(Filepath, OpenOptions), - %% Save Fd in process dictionary for debugging purposes - put(couch_file_fd, {Fd, Filepath}), - ok = file:close(Fd_Read), - maybe_track_open_os_files(Options), - {ok, Eof} = file:position(Fd, eof), - erlang:send_after(?INITIAL_WAIT, self(), maybe_close), - {ok, #file{fd=Fd, eof=Eof, is_sys=IsSys, pread_limit=Limit}}; + case file:open(Filepath, OpenOptions) of + {ok, Fd} -> + %% Save Fd in process dictionary for debugging purposes + put(couch_file_fd, {Fd, Filepath}), + ok = file:close(Fd_Read), + maybe_track_open_os_files(Options), + {ok, Eof} = file:position(Fd, eof), + erlang:send_after(?INITIAL_WAIT, self(), maybe_close), + {ok, #file{fd=Fd, eof=Eof, is_sys=IsSys, pread_limit=Limit}}; + Error -> + init_status_error(ReturnPid, Ref, Error) + end; Error -> init_status_error(ReturnPid, Ref, Error) end |