fix reading procfs files

old-commit-hash: 6f21993915
commatwo_master
Comma Device 4 years ago
parent 5dddcac027
commit 6f27d70605
  1. 20
      selfdrive/common/util.cc

@ -1,4 +1,5 @@
#include <errno.h> #include <errno.h>
#include <sstream>
#include "common/util.h" #include "common/util.h"
@ -51,11 +52,20 @@ std::string read_file(const std::string& fn) {
std::ifstream ifs(fn, std::ios::binary | std::ios::ate); std::ifstream ifs(fn, std::ios::binary | std::ios::ate);
if (ifs) { if (ifs) {
std::ifstream::pos_type pos = ifs.tellg(); std::ifstream::pos_type pos = ifs.tellg();
std::string result; if (pos != std::ios::beg) {
result.resize(pos); std::string result;
ifs.seekg(0, std::ios::beg); result.resize(pos);
ifs.read(result.data(), pos); ifs.seekg(0, std::ios::beg);
if (ifs) return result; ifs.read(result.data(), pos);
if (ifs) {
return result;
}
} else {
// handle files created on read, e.g. procfs
std::stringstream buffer;
buffer << ifs.rdbuf();
return buffer.str();
}
} }
return ""; return "";
} }

Loading…
Cancel
Save