Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
Reset EOF if a partial write was possible
Browse files Browse the repository at this point in the history
We can't know if one or more bytes were written by a file:write/2 call
that results in an error and so it is not correct to leave #file.eof
at its original value. In the event of error, use file:position(Fd,
eof) to find the new, true length of the file, and update #file{}
accordingly.

COUCHDB-3274
  • Loading branch information
rnewson committed Jan 19, 2017
1 parent 3188736 commit 604edd1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/couch_file.erl
Expand Up @@ -476,7 +476,7 @@ handle_call({append_bin, Bin}, _From, #file{fd = Fd, eof = Pos} = File) ->
ok ->
{reply, {ok, Pos, Size}, File#file{eof = Pos + Size}};
Error ->
{reply, Error, File}
{reply, Error, reset_eof(File)}
end;

handle_call({write_header, Bin}, _From, #file{fd = Fd, eof = Pos} = File) ->
Expand All @@ -492,7 +492,7 @@ handle_call({write_header, Bin}, _From, #file{fd = Fd, eof = Pos} = File) ->
ok ->
{reply, ok, File#file{eof = Pos + iolist_size(FinalBin)}};
Error ->
{reply, Error, File}
{reply, Error, reset_eof(File)}
end;

handle_call(find_header, _From, #file{fd = Fd, eof = Pos} = File) ->
Expand Down Expand Up @@ -738,6 +738,11 @@ get_pread_limit() ->
_ -> infinity
end.

%% in event of a partially successful write.
reset_eof(#file{} = File) ->
{ok, Eof} = file:position(File#file.fd, eof),
File#file{eof = Eof}.

-ifdef(TEST).
-include_lib("couch/include/couch_eunit.hrl").

Expand Down

0 comments on commit 604edd1

Please sign in to comment.