You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@couchdb.apache.org by Andrey Somov <py...@gmail.com> on 2010/06/15 18:10:17 UTC

error in handle_call({write_header, Bin}

Hi,
while reading the CouchDB source I found a question in couch_file.erl,
I am not sure whether it is a bug or not.

Lines 297-311:

handle_call({write_header, Bin}, _From, #file{fd=Fd, eof=Pos}=File) ->
    BinSize = size(Bin),
    case Pos rem ?SIZE_BLOCK of
    0 ->
        Padding = <<>>;
    BlockOffset ->
        Padding = <<0:(8*(?SIZE_BLOCK-BlockOffset))>>
    end,
    FinalBin = [Padding, <<1, BinSize:32/integer>> | make_blocks(1, [Bin])],
    case file:write(Fd, FinalBin) of
    ok ->
        {reply, ok, File#file{eof=Pos+iolist_size(FinalBin)}};
    Error ->
        {reply, Error, File}
    end;


Because <<1, BinSize:32/integer>> occupies 5 bytes make_blocks() shall
use offset=5, but the offset is only 1.
(it should be make_blocks(5, [Bin]))

Since the header is smaller then 4k there is no difference and it
works (the tests succeed with both 1 and 5). But it makes it more
difficult to understand the code for those who study the source to
understand how it works.

-
Thank you,
Andrey

Re: error in handle_call({write_header, Bin}

Posted by Damien Katz <da...@apache.org>.
I think you are right, the call should be make_blocks(5, [Bin]). Checking in a fix now.

-Damien


On Jun 15, 2010, at 9:10 AM, Andrey Somov wrote:

> Hi,
> while reading the CouchDB source I found a question in couch_file.erl,
> I am not sure whether it is a bug or not.
> 
> Lines 297-311:
> 
> handle_call({write_header, Bin}, _From, #file{fd=Fd, eof=Pos}=File) ->
>    BinSize = size(Bin),
>    case Pos rem ?SIZE_BLOCK of
>    0 ->
>        Padding = <<>>;
>    BlockOffset ->
>        Padding = <<0:(8*(?SIZE_BLOCK-BlockOffset))>>
>    end,
>    FinalBin = [Padding, <<1, BinSize:32/integer>> | make_blocks(1, [Bin])],
>    case file:write(Fd, FinalBin) of
>    ok ->
>        {reply, ok, File#file{eof=Pos+iolist_size(FinalBin)}};
>    Error ->
>        {reply, Error, File}
>    end;
> 
> 
> Because <<1, BinSize:32/integer>> occupies 5 bytes make_blocks() shall
> use offset=5, but the offset is only 1.
> (it should be make_blocks(5, [Bin]))
> 
> Since the header is smaller then 4k there is no difference and it
> works (the tests succeed with both 1 and 5). But it makes it more
> difficult to understand the code for those who study the source to
> understand how it works.
> 
> -
> Thank you,
> Andrey