util/read_files_in_dir: return map by value (#21815)

pull/21811/head^2
Dean Lee 4 years ago committed by GitHub
parent 1d88538d31
commit 11ffbc6936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      selfdrive/common/params.cc
  2. 2
      selfdrive/common/params.h
  3. 9
      selfdrive/common/util.cc
  4. 2
      selfdrive/common/util.h
  5. 3
      selfdrive/loggerd/bootlog.cc
  6. 3
      selfdrive/loggerd/logger.cc

@ -327,12 +327,12 @@ std::string Params::get(const char *key, bool block) {
} }
} }
int Params::readAll(std::map<std::string, std::string> *params) { std::map<std::string, std::string> Params::readAll() {
FileLock file_lock(params_path + "/.lock", LOCK_SH); FileLock file_lock(params_path + "/.lock", LOCK_SH);
std::lock_guard<FileLock> lk(file_lock); std::lock_guard<FileLock> lk(file_lock);
std::string key_path = params_path + "/d"; std::string key_path = params_path + "/d";
return util::read_files_in_dir(key_path, params); return util::read_files_in_dir(key_path);
} }
void Params::clearAll(ParamKeyType key_type) { void Params::clearAll(ParamKeyType key_type) {

@ -33,7 +33,7 @@ public:
void clearAll(ParamKeyType type); void clearAll(ParamKeyType type);
// read all values // read all values
int readAll(std::map<std::string, std::string> *params); std::map<std::string, std::string> readAll();
// helpers for reading values // helpers for reading values
std::string get(const char *key, bool block = false); std::string get(const char *key, bool block = false);

@ -76,19 +76,20 @@ std::string read_file(const std::string& fn) {
return std::string(); return std::string();
} }
int read_files_in_dir(const std::string &path, std::map<std::string, std::string> *contents) { std::map<std::string, std::string> read_files_in_dir(const std::string &path) {
std::map<std::string, std::string> ret;
DIR *d = opendir(path.c_str()); DIR *d = opendir(path.c_str());
if (!d) return -1; if (!d) return ret;
struct dirent *de = NULL; struct dirent *de = NULL;
while ((de = readdir(d))) { while ((de = readdir(d))) {
if (isalnum(de->d_name[0])) { if (isalnum(de->d_name[0])) {
(*contents)[de->d_name] = util::read_file(path + "/" + de->d_name); ret[de->d_name] = util::read_file(path + "/" + de->d_name);
} }
} }
closedir(d); closedir(d);
return 0; return ret;
} }
int write_file(const char* path, const void* data, size_t size, int flags, mode_t mode) { int write_file(const char* path, const void* data, size_t size, int flags, mode_t mode) {

@ -62,7 +62,7 @@ bool is_valid_dongle_id(std::string const& dongle_id);
// **** file fhelpers ***** // **** file fhelpers *****
std::string read_file(const std::string& fn); std::string read_file(const std::string& fn);
int read_files_in_dir(const std::string& path, std::map<std::string, std::string>* contents); std::map<std::string, std::string> read_files_in_dir(const std::string& path);
int write_file(const char* path, const void* data, size_t size, int flags = O_WRONLY, mode_t mode = 0777); int write_file(const char* path, const void* data, size_t size, int flags = O_WRONLY, mode_t mode = 0777);
std::string readlink(const std::string& path); std::string readlink(const std::string& path);
bool file_exists(const std::string& fn); bool file_exists(const std::string& fn);

@ -12,8 +12,7 @@ static kj::Array<capnp::word> build_boot_log() {
boot.setWallTimeNanos(nanos_since_epoch()); boot.setWallTimeNanos(nanos_since_epoch());
std::string pstore = "/sys/fs/pstore"; std::string pstore = "/sys/fs/pstore";
std::map<std::string, std::string> pstore_map; std::map<std::string, std::string> pstore_map = util::read_files_in_dir(pstore);
util::read_files_in_dir(pstore, &pstore_map);
const std::vector<std::string> log_keywords = {"Kernel panic"}; const std::vector<std::string> log_keywords = {"Kernel panic"};
auto lpstore = boot.initPstore().initEntries(pstore_map.size()); auto lpstore = boot.initPstore().initEntries(pstore_map.size());

@ -101,8 +101,7 @@ kj::Array<capnp::word> logger_build_init_data() {
init.setPassive(params.getBool("Passive")); init.setPassive(params.getBool("Passive"));
init.setDongleId(params.get("DongleId")); init.setDongleId(params.get("DongleId"));
{ {
std::map<std::string, std::string> params_map; std::map<std::string, std::string> params_map = params.readAll();
params.readAll(&params_map);
auto lparams = init.initParams().initEntries(params_map.size()); auto lparams = init.initParams().initEntries(params_map.size());
int i = 0; int i = 0;
for (auto& kv : params_map) { for (auto& kv : params_map) {

Loading…
Cancel
Save