未验证 提交 4d5c9347 编写于 作者: C chen qingxiang 提交者: GitHub

fix the data type of crc32c (#3455)

Signed-off-by: Ngodchen0212 <qingxiang.chen@zilliz.com>
上级 e814fb5e
...@@ -94,7 +94,7 @@ ReadHeaderValue(const storage::FSHandlerPtr& fs_ptr, const std::string& file_pat ...@@ -94,7 +94,7 @@ ReadHeaderValue(const storage::FSHandlerPtr& fs_ptr, const std::string& file_pat
return kv.at(key); return kv.at(key);
} }
std::uint8_t std::uint32_t
CalculateSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, bool written) { CalculateSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, bool written) {
if (!fs_ptr->reader_ptr_->Open(file_path.c_str())) { if (!fs_ptr->reader_ptr_->Open(file_path.c_str())) {
std::string err_msg = "Failed to open file: " + file_path + ", error: " + std::strerror(errno); std::string err_msg = "Failed to open file: " + file_path + ", error: " + std::strerror(errno);
...@@ -108,14 +108,14 @@ CalculateSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, ...@@ -108,14 +108,14 @@ CalculateSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path,
} }
char* ch = static_cast<char*>(malloc(size)); char* ch = static_cast<char*>(malloc(size));
fs_ptr->reader_ptr_->Read(ch, size); fs_ptr->reader_ptr_->Read(ch, size);
std::uint8_t result = crc32c::Crc32c(ch, size); std::uint32_t result = crc32c::Crc32c(ch, size);
fs_ptr->reader_ptr_->Close(); fs_ptr->reader_ptr_->Close();
free(ch); free(ch);
return result; return result;
} }
void void
WriteSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, int result, bool written) { WriteSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, uint32_t result, bool written) {
if (!fs_ptr->writer_ptr_->InOpen(file_path.c_str())) { if (!fs_ptr->writer_ptr_->InOpen(file_path.c_str())) {
std::string err_msg = "Failed to open file: " + file_path + ", error: " + std::strerror(errno); std::string err_msg = "Failed to open file: " + file_path + ", error: " + std::strerror(errno);
LOG_ENGINE_ERROR_ << err_msg; LOG_ENGINE_ERROR_ << err_msg;
...@@ -128,29 +128,25 @@ WriteSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, int ...@@ -128,29 +128,25 @@ WriteSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, int
fs_ptr->writer_ptr_->Seekp(0, std::ios_base::end); fs_ptr->writer_ptr_->Seekp(0, std::ios_base::end);
} }
std::string sum = std::to_string(result); fs_ptr->writer_ptr_->Write(&result, SUM_SIZE);
sum.resize(SUM_SIZE, '\0');
fs_ptr->writer_ptr_->Write(sum.data(), SUM_SIZE);
fs_ptr->writer_ptr_->Close(); fs_ptr->writer_ptr_->Close();
} }
bool bool
CheckSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path) { CheckSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path) {
int result = CalculateSum(fs_ptr, file_path, true); uint32_t result = CalculateSum(fs_ptr, file_path, true);
if (!fs_ptr->reader_ptr_->Open(file_path.c_str())) { if (!fs_ptr->reader_ptr_->Open(file_path.c_str())) {
std::string err_msg = "Failed to open file: " + file_path + ", error: " + std::strerror(errno); std::string err_msg = "Failed to open file: " + file_path + ", error: " + std::strerror(errno);
LOG_ENGINE_ERROR_ << err_msg; LOG_ENGINE_ERROR_ << err_msg;
throw Exception(SERVER_WRITE_ERROR, err_msg); throw Exception(SERVER_WRITE_ERROR, err_msg);
} }
fs_ptr->reader_ptr_->Seekg(-SUM_SIZE, std::ios_base::end); fs_ptr->reader_ptr_->Seekg(-SUM_SIZE, std::ios_base::end);
char* record = static_cast<char*>(malloc(SUM_SIZE)); uint32_t record;
fs_ptr->reader_ptr_->Read(record, SUM_SIZE); fs_ptr->reader_ptr_->Read(&record, SUM_SIZE);
fs_ptr->reader_ptr_->Close(); fs_ptr->reader_ptr_->Close();
auto sum = static_cast<uint8_t>(atoi(record)); return record == result;
free(record);
return sum == result;
} }
bool bool
......
...@@ -77,9 +77,9 @@ bool ...@@ -77,9 +77,9 @@ bool
CheckSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path); CheckSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path);
void void
WriteSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, int result, bool written = false); WriteSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, uint32_t result, bool written = false);
std::uint8_t std::uint32_t
CalculateSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, bool written = false); CalculateSum(const storage::FSHandlerPtr& fs_ptr, const std::string& file_path, bool written = false);
std::string std::string
......
...@@ -64,7 +64,7 @@ TEST_F(ExtraFileInfoTest, WriteFileTest) { ...@@ -64,7 +64,7 @@ TEST_F(ExtraFileInfoTest, WriteFileTest) {
ASSERT_TRUE(WriteHeaderValue(fs_ptr, file_path, "github", "github")); ASSERT_TRUE(WriteHeaderValue(fs_ptr, file_path, "github", "github"));
ASSERT_EQ(ReadHeaderValue(fs_ptr, file_path, "github"), "github"); ASSERT_EQ(ReadHeaderValue(fs_ptr, file_path, "github"), "github");
result_sum = CalculateSum(fs_ptr, file_path); result_sum = CalculateSum(fs_ptr, file_path, true);
WriteSum(fs_ptr, file_path, result_sum, true); WriteSum(fs_ptr, file_path, result_sum, true);
ASSERT_TRUE(CheckMagic(fs_ptr, file_path)); ASSERT_TRUE(CheckMagic(fs_ptr, file_path));
ASSERT_TRUE(CheckSum(fs_ptr, file_path)); ASSERT_TRUE(CheckSum(fs_ptr, file_path));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册