Skip to content

Commit

Permalink
refactor(configuration): use rocksdb API to read/write file (#1626)
Browse files Browse the repository at this point in the history
#887

There is no functional changes, but only refactor the files read/write method
of configuration module.
  • Loading branch information
acelyc111 authored Sep 27, 2023
1 parent c65c39a commit c2d9aff
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/utils/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@
* xxxx-xx-xx, author, fix bug about xxx
*/

#include <errno.h>
#include <fmt/core.h>
#include <rocksdb/env.h>
#include <rocksdb/status.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <utility>

#include "utils/configuration.h"
#include "utils/filesystem.h"
#include "utils/env.h"
#include "utils/strings.h"

namespace dsn {
Expand All @@ -67,33 +69,17 @@ bool configuration::load(const char *file_name, const char *arguments)
{
_file_name = std::string(file_name);

FILE *fd = ::fopen(file_name, "rb");
if (fd == nullptr) {
std::string cdir;
dsn::utils::filesystem::get_current_directory(cdir);
printf("ERROR: cannot open file %s in %s, err = %s\n",
file_name,
cdir.c_str(),
strerror(errno));
return false;
}
::fseek(fd, 0, SEEK_END);
int len = ftell(fd);
if (len == -1 || len == 0) {
printf("ERROR: cannot get length of %s, err = %s\n", file_name, strerror(errno));
::fclose(fd);
auto s = rocksdb::ReadFileToString(
dsn::utils::PegasusEnv(dsn::utils::FileDataType::kNonSensitive), _file_name, &_file_data);
if (!s.ok()) {
fmt::print(stderr, "ERROR: read file '{}' failed, err = {}\n", _file_name, s.ToString());
return false;
}

_file_data.resize(len + 1);
::fseek(fd, 0, SEEK_SET);
auto sz = ::fread((char *)_file_data.c_str(), len, 1, fd);
::fclose(fd);
if (sz != 1) {
printf("ERROR: cannot read correct data of %s, err = %s\n", file_name, strerror(errno));
if (_file_data.empty()) {
fmt::print(stderr, "ERROR: file '{}' is empty\n", _file_name);
return false;
}
_file_data[len] = '\n';

// replace data with arguments
if (arguments != nullptr) {
Expand Down

0 comments on commit c2d9aff

Please sign in to comment.