Move checksum file related code to machi_csum_table #11
4 changed files with 28 additions and 24 deletions
|
@ -95,7 +95,7 @@ verify_file_checksums_common(Sock1, EpochID, File, ReadChunk) ->
|
|||
try
|
||||
case ?FLU_C:checksum_list(Sock1, EpochID, File) of
|
||||
{ok, InfoBin} ->
|
||||
{Info, _} = machi_checksums:split_checksum_list_blob_decode(InfoBin),
|
||||
{Info, _} = machi_csum_table:split_checksum_list_blob_decode(InfoBin),
|
||||
Res = lists:foldl(verify_chunk_checksum(File, ReadChunk),
|
||||
[], Info),
|
||||
{ok, Res};
|
||||
|
|
|
@ -59,11 +59,12 @@ open(CSumFilename, _Opts) ->
|
|||
-spec find(table(), machi_dt:chunk_pos(), machi_dt:chunk_size()) ->
|
||||
{ok, machi_dt:chunk_csum()} | {error, trimmed|notfound}.
|
||||
find(#machi_csum_table{table=T}, Offset, Size) ->
|
||||
%% TODO: Check whether all bytes here are written or not
|
||||
case ets:lookup(T, Offset) of
|
||||
[{Offset, Size, trimmed}] -> {error, trimmed};
|
||||
[{Offset, Size, Checksum}] -> {ok, Checksum};
|
||||
[{Offset, _, Checksum}] -> {ok, Checksum};
|
||||
[] -> {error, notfound}
|
||||
[{Offset, _, _}] -> {error, unknown_chunk};
|
||||
[] -> {error, unknown_chunk}
|
||||
end.
|
||||
|
||||
-spec write(table(), machi_dt:chunk_pos(), machi_dt:chunk_size(),
|
||||
|
|
|
@ -597,9 +597,10 @@ handle_write(FHd, CsumTable, Filename, TaggedCsum, Offset, Data, U) ->
|
|||
case machi_csum_table:find(CsumTable, Offset, Size) of
|
||||
{error, trimmed} = Error ->
|
||||
Error;
|
||||
{error, notfound} ->
|
||||
%% Just unknown pair of {offset, size}
|
||||
%% given. Trust U and return as it is used
|
||||
{error, unknown_chunk} ->
|
||||
%% The specified has some bytes written, while
|
||||
%% it's not in the checksum table. Trust U and
|
||||
%% return as it is used.
|
||||
{error, written};
|
||||
{ok, TaggedCsum} ->
|
||||
case do_read(FHd, Filename, TaggedCsum, Offset, Size) of
|
||||
|
|
|
@ -3,27 +3,29 @@
|
|||
|
||||
smoke_test() ->
|
||||
Filename = "./temp-checksum-dumb-file",
|
||||
{ok, MC} = machi_checksums:new(Filename),
|
||||
_ = file:delete(Filename),
|
||||
{ok, MC} = machi_csum_table:open(Filename, []),
|
||||
{Offset, Size, Checksum} = {64, 34, <<"deadbeef">>},
|
||||
{error, unwritten} = machi_checksums:find(MC, Offset, Size),
|
||||
ok = machi_checksums:write(MC, Offset, Size, Checksum),
|
||||
{ok, Checksum} = machi_checksums:find(MC, Offset, Size),
|
||||
ok = machi_checksums:trim(MC, Offset, Size),
|
||||
{error, trimmed} = machi_checksums:find(MC, Offset, Size),
|
||||
ok = machi_checksums:destroy(MC),
|
||||
ok = machi_checksums:delete_file(MC).
|
||||
{error, unknown_chunk} = machi_csum_table:find(MC, Offset, Size),
|
||||
ok = machi_csum_table:write(MC, Offset, Size, Checksum),
|
||||
{ok, Checksum} = machi_csum_table:find(MC, Offset, Size),
|
||||
ok = machi_csum_table:trim(MC, Offset, Size),
|
||||
{error, trimmed} = machi_csum_table:find(MC, Offset, Size),
|
||||
ok = machi_csum_table:close(MC),
|
||||
ok = machi_csum_table:delete(MC).
|
||||
|
||||
close_test() ->
|
||||
Filename = "./temp-checksum-dumb-file-2",
|
||||
{ok, MC} = machi_checksums:new(Filename),
|
||||
_ = file:delete(Filename),
|
||||
{ok, MC} = machi_csum_table:open(Filename, []),
|
||||
{Offset, Size, Checksum} = {64, 34, <<"deadbeef">>},
|
||||
{error, unwritten} = machi_checksums:find(MC, Offset, Size),
|
||||
ok = machi_checksums:write(MC, Offset, Size, Checksum),
|
||||
{ok, Checksum} = machi_checksums:find(MC, Offset, Size),
|
||||
ok = machi_checksums:destroy(MC),
|
||||
{error, unknown_chunk} = machi_csum_table:find(MC, Offset, Size),
|
||||
ok = machi_csum_table:write(MC, Offset, Size, Checksum),
|
||||
{ok, Checksum} = machi_csum_table:find(MC, Offset, Size),
|
||||
ok = machi_csum_table:close(MC),
|
||||
|
||||
{ok, MC2} = machi_checksums:new(Filename),
|
||||
{ok, Checksum} = machi_checksums:find(MC2, Offset, Size),
|
||||
ok = machi_checksums:trim(MC2, Offset, Size),
|
||||
{error, trimmed} = machi_checksums:find(MC2, Offset, Size),
|
||||
ok = machi_checksums:delete_file(MC2).
|
||||
{ok, MC2} = machi_csum_table:open(Filename, []),
|
||||
{ok, Checksum} = machi_csum_table:find(MC2, Offset, Size),
|
||||
ok = machi_csum_table:trim(MC2, Offset, Size),
|
||||
{error, trimmed} = machi_csum_table:find(MC2, Offset, Size),
|
||||
ok = machi_csum_table:delete(MC2).
|
||||
|
|
Loading…
Reference in a new issue