empiredan commented on code in PR #1624:
URL:
https://github.com/apache/incubator-pegasus/pull/1624#discussion_r1338014898
##########
src/utils/filesystem.cpp:
##########
@@ -745,6 +717,54 @@ bool link_file(const std::string &src, const std::string
&target)
}
error_code md5sum(const std::string &file_path, /*out*/ std::string &result)
+{
+ result.clear();
+ if (!::dsn::utils::filesystem::file_exists(file_path)) {
+ LOG_ERROR("md5sum error: file {} not exist", file_path);
+ return ERR_OBJECT_NOT_FOUND;
+ }
+
+ std::unique_ptr<rocksdb::SequentialFile> sfile;
+ auto s = rocksdb::Env::Default()->NewSequentialFile(file_path, &sfile,
rocksdb::EnvOptions());
+ if (!sfile) {
+ LOG_ERROR("md5sum error: open file {} failed, err={}", file_path,
s.ToString());
+ return ERR_FILE_OPERATION_FAILED;
+ }
+
+ const int64_t kBufferSize = 4096;
+ char buf[kBufferSize];
+ unsigned char out[MD5_DIGEST_LENGTH] = {0};
+ MD5_CTX c;
+ CHECK_EQ(1, MD5_Init(&c));
+ while (true) {
+ rocksdb::Slice res;
+ s = sfile->Read(kBufferSize, &res, buf);
+ if (!s.ok()) {
+ MD5_Final(out, &c);
+ LOG_ERROR("md5sum error: read file {} failed, err = ", file_path,
s.ToString());
Review Comment:
```suggestion
LOG_ERROR("md5sum error: read file {} failed, err={}",
file_path, s.ToString());
```
##########
src/utils/filesystem.cpp:
##########
@@ -952,23 +957,28 @@ bool check_dir_rw(const std::string &path, std::string
&err_msg)
path.find(broken_disk_dir) == std::string::npos;
});
- std::string fname = "read_write_test_file";
- std::string fpath = path_combine(path, fname);
- if (!create_file(fpath)) {
- err_msg = fmt::format("Fail to create test file {}.", fpath);
+ static const std::string kTestValue = "test_value";
+ static const std::string kFname = "read_write_test_file";
+ std::string fpath = path_combine(path, kFname);
+ auto cleanup = defer([&fpath]() { remove_path(fpath); });
+ auto s = rocksdb::WriteStringToFile(rocksdb::Env::Default(),
+ rocksdb::Slice(kTestValue),
+ fpath,
+ /* should_sync */ true);
+ if (dsn_unlikely(!s.ok())) {
+ err_msg = fmt::format("fail to write file {}, err={}", fpath,
s.ToString());
return false;
}
- auto cleanup = defer([&fpath]() { remove_path(fpath); });
- std::string value = "test_value";
- if (!write_file(fpath, value)) {
- err_msg = fmt::format("Fail to write file {}.", fpath);
+ std::string read_data;
+ s = rocksdb::ReadFileToString(rocksdb::Env::Default(), fpath, &read_data);
+ if (dsn_unlikely(!s.ok())) {
+ err_msg = fmt::format("fail to read file {}, err={}", fpath,
s.ToString());
return false;
}
- std::string buf;
- if (read_file(fpath, buf) != ERR_OK || buf != value) {
- err_msg = fmt::format("Fail to read file {} or get wrong value({}).",
fpath, buf);
+ if (dsn_unlikely(read_data != kTestValue)) {
+ err_msg = fmt::format("get wrong value '{}' from file", read_data,
fpath);
Review Comment:
```suggestion
err_msg = fmt::format("get wrong value '{}' from file {}",
read_data, fpath);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]