Github user davisp commented on a diff in the pull request:

    https://github.com/apache/couchdb-couch/pull/185#discussion_r80056738
  
    --- Diff: src/couch_file.erl ---
    @@ -531,27 +537,96 @@ find_header(Fd, Block) ->
         {ok, Bin} ->
             {ok, Bin};
         _Error ->
    -        find_header(Fd, Block -1)
    +        find_header_pread2(Fd, Block -1)
         end.
     
     load_header(Fd, Block) ->
         {ok, <<1, HeaderLen:32/integer, RestBlock/binary>>} =
             file:pread(Fd, Block * ?SIZE_BLOCK, ?SIZE_BLOCK),
    -    TotalBytes = calculate_total_read_len(5, HeaderLen),
    -    case TotalBytes > byte_size(RestBlock) of
    +    TotalSize = total_read_size(5, HeaderLen),
    +    case TotalSize > byte_size(RestBlock) of
         false ->
    -        <<RawBin:TotalBytes/binary, _/binary>> = RestBlock;
    +        <<RawBin:TotalSize/binary, _/binary>> = RestBlock;
         true ->
             {ok, Missing} = file:pread(
                 Fd, (Block * ?SIZE_BLOCK) + 5 + byte_size(RestBlock),
    -            TotalBytes - byte_size(RestBlock)),
    +            TotalSize - byte_size(RestBlock)),
             RawBin = <<RestBlock/binary, Missing/binary>>
         end,
         <<Md5Sig:16/binary, HeaderBin/binary>> =
             iolist_to_binary(remove_block_prefixes(5, RawBin)),
         Md5Sig = couch_crypto:hash(md5, HeaderBin),
         {ok, HeaderBin}.
     
    +
    +%% Read a configurable number of block locations at a time using
    +%% file:pread/2.  At each block location, read ?PREFIX_SIZE (5) bytes;
    +%% if the first byte is <<1>>, assume it's a header block, and the
    +%% next 4 bytes hold the size of the header.
    --- End diff --
    
    There's no assumption here. A header is by definition at any 4K boundary 
where the first byte is a 1. If we try and load a header and fail there its 
because of some sort of file corruption.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to