From f5d06318806e57f72ec88f1c27114db8d402c1e5 Mon Sep 17 00:00:00 2001 From: "Damien F. Katz" Date: Fri, 17 Jul 2009 23:23:03 +0000 Subject: MD5 term checking by using leading bit in 32 bit length to indicate MD5 presence. git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@795277 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_db.erl | 6 +----- src/couchdb/couch_file.erl | 45 +++++++++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/couchdb/couch_db.erl b/src/couchdb/couch_db.erl index 9dc1fce8..21bb1e5e 100644 --- a/src/couchdb/couch_db.erl +++ b/src/couchdb/couch_db.erl @@ -870,12 +870,8 @@ doc_meta_info(#doc_info{high_seq=Seq,revs=[#rev_info{rev=Rev}|RestInfo]}, RevTre read_doc(#db{fd=Fd}, OldStreamPointer) when is_tuple(OldStreamPointer) -> % 09 UPGRADE CODE couch_stream:old_read_term(Fd, OldStreamPointer); -read_doc(#db{header=#db_header{disk_version=Version},fd=Fd}, Pos) - when Version == 3 -> - % 09 UPGRADE CODE - couch_file:pread_term(Fd, Pos); read_doc(#db{fd=Fd}, Pos) -> - couch_file:pread_term_md5(Fd, Pos). + couch_file:pread_term(Fd, Pos). doc_to_tree(#doc{revs={Start, RevIds}}=Doc) -> diff --git a/src/couchdb/couch_file.erl b/src/couchdb/couch_file.erl index 4c450163..e8b37aa5 100644 --- a/src/couchdb/couch_file.erl +++ b/src/couchdb/couch_file.erl @@ -25,8 +25,7 @@ -export([open/1, open/2, close/1, bytes/1, sync/1, append_binary/2,old_pread/3]). -export([append_term/2, pread_term/2, pread_iolist/2, write_header/2]). -export([pread_binary/2, read_header/1, truncate/2, upgrade_old_header/2]). --export([append_term_md5/2, pread_iolist_md5/2, pread_binary_md5/2]). --export([pread_term_md5/2]). +-export([append_term_md5/2,append_binary_md5/2]). -export([init/1, terminate/2, handle_call/3, handle_cast/2, code_change/3, handle_info/2]). %%---------------------------------------------------------------------- @@ -84,12 +83,13 @@ append_term_md5(Fd, Term) -> append_binary(Fd, Bin) -> Size = iolist_size(Bin), - gen_server:call(Fd, {append_bin, [<>, Bin]}, infinity). + gen_server:call(Fd, {append_bin, + [<<0:1/integer,Size:31/integer>>, Bin]}, infinity). append_binary_md5(Fd, Bin) -> Size = iolist_size(Bin), gen_server:call(Fd, {append_bin, - [<>, erlang:md5(Bin), Bin]}, infinity). + [<<1:1/integer,Size:31/integer>>, erlang:md5(Bin), Bin]}, infinity). %%---------------------------------------------------------------------- @@ -104,10 +104,6 @@ pread_term(Fd, Pos) -> {ok, Bin} = pread_binary(Fd, Pos), {ok, binary_to_term(Bin)}. -pread_term_md5(Fd, Pos) -> - {ok, Bin} = pread_binary_md5(Fd, Pos), - {ok, binary_to_term(Bin)}. - %%---------------------------------------------------------------------- %% Purpose: Reads a binrary from a file that was written with append_binary @@ -121,21 +117,26 @@ pread_binary(Fd, Pos) -> {ok, iolist_to_binary(L)}. pread_iolist(Fd, Pos) -> - {ok, LenIolist, NextPos} =read_raw_iolist(Fd, Pos, 4), - <> = iolist_to_binary(LenIolist), - {ok, Iolist, _} = read_raw_iolist(Fd, NextPos, Len), - {ok, Iolist}. - -pread_binary_md5(Fd, Pos) -> - {ok, L} = pread_iolist_md5(Fd, Pos), - {ok, iolist_to_binary(L)}. - -pread_iolist_md5(Fd, Pos) -> {ok, LenIolist, NextPos} =read_raw_iolist(Fd, Pos, 20), - <> = iolist_to_binary(LenIolist), - {ok, Iolist, _} = read_raw_iolist(Fd, NextPos, Len), - Md5 = erlang:md5(Iolist), - {ok, Iolist}. + case iolist_to_binary(LenIolist) of + <<1:1/integer,Len:31/integer,Md5/binary>> -> + {ok, Iolist, _} = read_raw_iolist(Fd, NextPos, Len), + case erlang:md5(Iolist) of + Md5 -> ok; + _ -> throw(file_corruption) + end, + {ok, Iolist}; + <<0:1/integer,Len:31/integer,First16Bytes/binary>> -> + if Len =< 16 -> + <> = First16Bytes, + {ok, Final}; + true -> + {ok, Iolist, _} = read_raw_iolist(Fd, NextPos, Len - 16), + {ok, [First16Bytes, Iolist]} + end + end. + + read_raw_iolist(Fd, Pos, Len) -> BlockOffset = Pos rem ?SIZE_BLOCK, -- cgit v1.2.3