Skip to content

Commit

Permalink
refactor: move function verify_file to file_system (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
hycdong authored Jun 4, 2020
1 parent e5e3603 commit c88bc00
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 51 deletions.
5 changes: 5 additions & 0 deletions include/dsn/utility/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ std::pair<error_code, bool> is_directory_empty(const std::string &dirname);

error_code read_file(const std::string &fname, /*out*/ std::string &buf);

// compare file metadata calculated by fname with expected md5 and file_size
bool verify_file(const std::string &fname,
const std::string &expected_md5,
const int64_t &expected_fsize);

} // namespace filesystem
} // namespace utils
} // namespace dsn
30 changes: 30 additions & 0 deletions src/core/core/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,36 @@ error_code read_file(const std::string &fname, std::string &buf)
return ERR_OK;
}

bool verify_file(const std::string &fname,
const std::string &expected_md5,
const int64_t &expected_fsize)
{
if (!file_exists(fname)) {
derror_f("file({}) is not existed", fname);
return false;
}
int64_t f_size = 0;
if (!file_size(fname, f_size)) {
derror_f("verify file({}) failed, becaused failed to get file size", fname);
return false;
}
std::string md5;
if (md5sum(fname, md5) != ERR_OK) {
derror_f("verify file({}) failed, becaused failed to get file md5", fname);
return false;
}
if (f_size != expected_fsize || md5 != expected_md5) {
derror_f("verify file({}) failed, because file damaged, size: {} VS {}, md5: {} VS {}",
fname,
f_size,
expected_fsize,
md5,
expected_md5);
return false;
}
return true;
}

} // namespace filesystem
} // namespace utils
} // namespace dsn
30 changes: 30 additions & 0 deletions src/core/tests/file_system_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2017-present, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#include <dsn/utility/filesystem.h>
#include <gtest/gtest.h>

namespace dsn {
namespace utils {
namespace filesystem {

TEST(verify_file, verify_file_test)
{
const std::string &fname = "test_file";
std::string expected_md5;
int64_t expected_fsize;
create_file(fname);
md5sum(fname, expected_md5);
file_size(fname, expected_fsize);

ASSERT_TRUE(verify_file(fname, expected_md5, expected_fsize));
ASSERT_FALSE(verify_file(fname, "wrong_md5", 10086));
ASSERT_FALSE(verify_file("file_not_exists", "wrong_md5", 10086));

remove_path(fname);
}

} // namespace filesystem
} // namespace utils
} // namespace dsn
26 changes: 0 additions & 26 deletions src/dist/block_service/block_service_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,32 +214,6 @@ error_code block_service_manager::download_file(const std::string &remote_dir,
return download_err;
}

bool block_service_manager::verify_file(const replication::file_meta &f_meta,
const std::string &local_dir)
{
const std::string local_file = utils::filesystem::path_combine(local_dir, f_meta.name);
int64_t f_size = 0;
if (!utils::filesystem::file_size(local_file, f_size)) {
derror_f("verify file({}) failed, becaused failed to get file size", local_file);
return false;
}
std::string md5;
if (utils::filesystem::md5sum(local_file, md5) != ERR_OK) {
derror_f("verify file({}) failed, becaused failed to get file md5", local_file);
return false;
}
if (f_size != f_meta.size || md5 != f_meta.md5) {
derror_f("verify file({}) failed, because file damaged, size: {} VS {}, md5: {} VS {}",
local_file,
f_size,
f_meta.size,
md5,
f_meta.md5);
return false;
}
return true;
}

} // namespace block_service
} // namespace dist
} // namespace dsn
3 changes: 0 additions & 3 deletions src/dist/block_service/block_service_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class block_service_manager
block_filesystem *fs,
/*out*/ uint64_t &download_file_size);

// compare file metadata calculated by file and file_meta parsed from metadata file
bool verify_file(const replication::file_meta &f_meta, const std::string &local_dir);

private:
block_service_registry &_registry_holder;

Expand Down
21 changes: 0 additions & 21 deletions src/dist/block_service/test/block_service_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ class block_service_manager_test : public ::testing::Test
PROVIDER, LOCAL_DIR, FILE_NAME, _fs.get(), download_size);
}

bool test_verify_file(int64_t size, const std::string &md5)
{
_file_meta.name = FILE_NAME;
_file_meta.size = size;
_file_meta.md5 = md5;
return _block_service_manager.verify_file(_file_meta, LOCAL_DIR);
}

void create_local_file(const std::string &file_name)
{
std::string whole_name = utils::filesystem::path_combine(LOCAL_DIR, file_name);
Expand Down Expand Up @@ -103,19 +95,6 @@ TEST_F(block_service_manager_test, do_download_succeed)
ASSERT_EQ(test_download_file(), ERR_OK);
}

// verify_file unit tests
TEST_F(block_service_manager_test, verify_file_failed)
{
create_local_file(FILE_NAME);
ASSERT_FALSE(test_verify_file(_file_meta.size, "wrong_md5"));
}

TEST_F(block_service_manager_test, verify_file_succeed)
{
create_local_file(FILE_NAME);
ASSERT_TRUE(test_verify_file(_file_meta.size, _file_meta.md5));
}

} // namespace block_service
} // namespace dist
} // namespace dsn
5 changes: 4 additions & 1 deletion src/dist/replication/lib/bulk_load/replica_bulk_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ error_code replica_bulk_loader::download_sst_files(const std::string &app_name,
uint64_t f_size = 0;
error_code ec = _stub->_block_service_manager.download_file(
remote_dir, local_dir, f_meta.name, fs, f_size);
if (ec == ERR_OK && !_stub->_block_service_manager.verify_file(f_meta, local_dir)) {
const std::string &file_name =
utils::filesystem::path_combine(local_dir, f_meta.name);
if (ec == ERR_OK &&
!utils::filesystem::verify_file(file_name, f_meta.md5, f_meta.size)) {
ec = ERR_CORRUPTION;
}
if (ec != ERR_OK) {
Expand Down

0 comments on commit c88bc00

Please sign in to comment.