2015-04-01 08:59:40 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% Copyright (c) 2007-2015 Basho Technologies, Inc. All Rights Reserved.
|
|
|
|
%%
|
|
|
|
%% This file is provided to you under the Apache License,
|
|
|
|
%% Version 2.0 (the "License"); you may not use this file
|
|
|
|
%% except in compliance with the License. You may obtain
|
|
|
|
%% a copy of the License at
|
|
|
|
%%
|
|
|
|
%% http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
%%
|
|
|
|
%% Unless required by applicable law or agreed to in writing,
|
|
|
|
%% software distributed under the License is distributed on an
|
|
|
|
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
%% KIND, either express or implied. See the License for the
|
|
|
|
%% specific language governing permissions and limitations
|
|
|
|
%% under the License.
|
|
|
|
%%
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
|
2015-04-08 05:24:07 +00:00
|
|
|
%% @doc Machi chain replication administration utilities.
|
|
|
|
|
2015-04-01 08:59:40 +00:00
|
|
|
-module(machi_admin_util).
|
|
|
|
|
|
|
|
-export([
|
2015-04-02 11:49:45 +00:00
|
|
|
verify_file_checksums_local/3, verify_file_checksums_local/4,
|
|
|
|
verify_file_checksums_remote/3, verify_file_checksums_remote/4
|
2015-04-01 08:59:40 +00:00
|
|
|
]).
|
|
|
|
-compile(export_all).
|
|
|
|
|
|
|
|
-include("machi.hrl").
|
2015-04-02 11:31:10 +00:00
|
|
|
-include("machi_projection.hrl").
|
2015-04-01 08:59:40 +00:00
|
|
|
|
2015-04-01 09:35:10 +00:00
|
|
|
-define(FLU_C, machi_flu1_client).
|
2015-04-01 08:59:40 +00:00
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec verify_file_checksums_local(port(), machi_dt:epoch_id(), binary()|list()) ->
|
2015-04-02 03:38:12 +00:00
|
|
|
{ok, [tuple()]} | {error, term()}.
|
2015-04-02 11:49:45 +00:00
|
|
|
verify_file_checksums_local(Sock1, EpochID, Path) when is_port(Sock1) ->
|
|
|
|
verify_file_checksums_local2(Sock1, EpochID, Path).
|
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec verify_file_checksums_local(machi_dt:inet_host(), machi_dt:inet_port(),
|
|
|
|
machi_dt:epoch_id(), binary()|list()) ->
|
2015-04-02 11:49:45 +00:00
|
|
|
{ok, [tuple()]} | {error, term()}.
|
|
|
|
verify_file_checksums_local(Host, TcpPort, EpochID, Path) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock1 = ?FLU_C:connect(#p_srvr{address=Host, port=TcpPort}),
|
2015-04-02 11:49:45 +00:00
|
|
|
try
|
|
|
|
verify_file_checksums_local2(Sock1, EpochID, Path)
|
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
catch ?FLU_C:disconnect(Sock1)
|
2015-04-02 11:49:45 +00:00
|
|
|
end.
|
2015-04-02 03:38:12 +00:00
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec verify_file_checksums_remote(port(), machi_dt:epoch_id(), binary()|list()) ->
|
2015-04-02 03:38:12 +00:00
|
|
|
{ok, [tuple()]} | {error, term()}.
|
2015-04-02 11:49:45 +00:00
|
|
|
verify_file_checksums_remote(Sock1, EpochID, File) when is_port(Sock1) ->
|
|
|
|
verify_file_checksums_remote2(Sock1, EpochID, File).
|
2015-04-01 09:35:10 +00:00
|
|
|
|
2015-06-19 07:24:57 +00:00
|
|
|
-spec verify_file_checksums_remote(machi_dt:inet_host(), machi_dt:inet_port(),
|
|
|
|
machi_dt:epoch_id(), binary()|list()) ->
|
2015-04-02 03:38:12 +00:00
|
|
|
{ok, [tuple()]} | {error, term()}.
|
2015-04-02 11:49:45 +00:00
|
|
|
verify_file_checksums_remote(Host, TcpPort, EpochID, File) ->
|
2015-05-17 07:18:30 +00:00
|
|
|
Sock1 = ?FLU_C:connect(#p_srvr{address=Host, port=TcpPort}),
|
2015-04-02 11:49:45 +00:00
|
|
|
try
|
|
|
|
verify_file_checksums_remote2(Sock1, EpochID, File)
|
|
|
|
after
|
2015-05-17 07:18:30 +00:00
|
|
|
catch ?FLU_C:disconnect(Sock1)
|
2015-04-02 11:49:45 +00:00
|
|
|
end.
|
2015-04-01 08:59:40 +00:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2015-04-02 11:49:45 +00:00
|
|
|
verify_file_checksums_local2(Sock1, EpochID, Path0) ->
|
2015-04-02 03:38:12 +00:00
|
|
|
Path = machi_util:make_string(Path0),
|
|
|
|
case file:open(Path, [read, binary, raw]) of
|
|
|
|
{ok, FH} ->
|
|
|
|
File = re:replace(Path, ".*/", "", [{return, binary}]),
|
|
|
|
try
|
|
|
|
ReadChunk = fun(_File, Offset, Size) ->
|
|
|
|
file:pread(FH, Offset, Size)
|
|
|
|
end,
|
2015-04-02 11:49:45 +00:00
|
|
|
verify_file_checksums_common(Sock1, EpochID, File, ReadChunk)
|
2015-04-02 03:38:12 +00:00
|
|
|
after
|
|
|
|
file:close(FH)
|
|
|
|
end;
|
|
|
|
Else ->
|
|
|
|
Else
|
|
|
|
end.
|
|
|
|
|
2015-04-02 11:49:45 +00:00
|
|
|
verify_file_checksums_remote2(Sock1, EpochID, File) ->
|
2015-04-02 03:38:12 +00:00
|
|
|
ReadChunk = fun(File_name, Offset, Size) ->
|
2015-04-02 11:49:45 +00:00
|
|
|
?FLU_C:read_chunk(Sock1, EpochID,
|
2015-04-02 11:31:10 +00:00
|
|
|
File_name, Offset, Size)
|
2015-04-02 03:38:12 +00:00
|
|
|
end,
|
2015-04-02 11:49:45 +00:00
|
|
|
verify_file_checksums_common(Sock1, EpochID, File, ReadChunk).
|
2015-04-02 03:38:12 +00:00
|
|
|
|
2015-04-02 11:49:45 +00:00
|
|
|
verify_file_checksums_common(Sock1, EpochID, File, ReadChunk) ->
|
2015-04-01 09:35:10 +00:00
|
|
|
try
|
2015-04-02 11:49:45 +00:00
|
|
|
case ?FLU_C:checksum_list(Sock1, EpochID, File) of
|
2015-06-30 06:48:35 +00:00
|
|
|
{ok, InfoBin} ->
|
|
|
|
{Info, _} = machi_flu1:split_checksum_list_blob_decode(InfoBin),
|
2015-04-02 03:38:12 +00:00
|
|
|
Res = lists:foldl(verify_chunk_checksum(File, ReadChunk),
|
|
|
|
[], Info),
|
|
|
|
{ok, Res};
|
|
|
|
{error, no_such_file}=Nope ->
|
|
|
|
Nope;
|
|
|
|
{error, _}=Else ->
|
|
|
|
Else
|
|
|
|
end
|
2015-04-01 09:35:10 +00:00
|
|
|
catch
|
|
|
|
What:Why ->
|
|
|
|
{error, {What, Why, erlang:get_stacktrace()}}
|
|
|
|
end.
|
|
|
|
|
2015-04-02 03:38:12 +00:00
|
|
|
verify_chunk_checksum(File, ReadChunk) ->
|
2015-06-01 05:25:55 +00:00
|
|
|
fun({Offset, Size, <<_Tag:1/binary, CSum/binary>>}, Acc) ->
|
2015-04-02 03:38:12 +00:00
|
|
|
case ReadChunk(File, Offset, Size) of
|
2015-04-01 09:35:10 +00:00
|
|
|
{ok, Chunk} ->
|
2015-04-08 05:24:07 +00:00
|
|
|
CSum2 = machi_util:checksum_chunk(Chunk),
|
2015-04-01 09:35:10 +00:00
|
|
|
if CSum == CSum2 ->
|
|
|
|
Acc;
|
|
|
|
true ->
|
|
|
|
[{Offset, Size, File, CSum, now, CSum2}|Acc]
|
|
|
|
end;
|
|
|
|
_Else ->
|
|
|
|
[{Offset, Size, File, CSum, now, read_failure}|Acc]
|
|
|
|
end
|
|
|
|
end.
|