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.
|
|
|
|
%%
|
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(machi_admin_util).
|
|
|
|
|
2015-04-02 03:38:12 +00:00
|
|
|
%% TODO Move these types to a common header file? (also machi_flu1_client.erl?)
|
|
|
|
-type inet_host() :: inet:ip_address() | inet:hostname().
|
|
|
|
-type inet_port() :: inet:port_number().
|
|
|
|
|
2015-04-01 08:59:40 +00:00
|
|
|
-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-04-02 11:49:45 +00:00
|
|
|
-spec verify_file_checksums_local(port(), machi_flu1_client: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).
|
|
|
|
|
|
|
|
-spec verify_file_checksums_local(inet_host(), inet_port(),
|
|
|
|
machi_flu1_client:epoch_id(), binary()|list()) ->
|
|
|
|
{ok, [tuple()]} | {error, term()}.
|
|
|
|
verify_file_checksums_local(Host, TcpPort, EpochID, Path) ->
|
2015-04-02 03:38:12 +00:00
|
|
|
Sock1 = machi_util:connect(Host, TcpPort),
|
2015-04-02 11:49:45 +00:00
|
|
|
try
|
|
|
|
verify_file_checksums_local2(Sock1, EpochID, Path)
|
|
|
|
after
|
|
|
|
catch gen_tcp:close(Sock1)
|
|
|
|
end.
|
2015-04-02 03:38:12 +00:00
|
|
|
|
2015-04-02 11:49:45 +00:00
|
|
|
-spec verify_file_checksums_remote(port(), machi_flu1_client: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-04-02 11:49:45 +00:00
|
|
|
-spec verify_file_checksums_remote(inet_host(), inet_port(),
|
|
|
|
machi_flu1_client: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-04-01 09:35:10 +00:00
|
|
|
Sock1 = machi_util:connect(Host, TcpPort),
|
2015-04-02 11:49:45 +00:00
|
|
|
try
|
|
|
|
verify_file_checksums_remote2(Sock1, EpochID, File)
|
|
|
|
after
|
|
|
|
catch gen_tcp:close(Sock1)
|
|
|
|
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-04-02 03:38:12 +00:00
|
|
|
{ok, Info} ->
|
|
|
|
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-04-01 09:35:10 +00:00
|
|
|
fun({Offset, Size, CSum}, 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} ->
|
|
|
|
CSum2 = machi_util:checksum(Chunk),
|
|
|
|
if CSum == CSum2 ->
|
|
|
|
Acc;
|
|
|
|
true ->
|
|
|
|
[{Offset, Size, File, CSum, now, CSum2}|Acc]
|
|
|
|
end;
|
|
|
|
_Else ->
|
|
|
|
[{Offset, Size, File, CSum, now, read_failure}|Acc]
|
|
|
|
end
|
|
|
|
end.
|