util: add safe write functions (#22160)

* add safe write functions

* log error

* bzfile safe_fopen&safe_flush

* update test_case

* trigger ci

Co-authored-by: Willem Melching <willem.melching@gmail.com>
pull/22740/head
Dean Lee 4 years ago committed by GitHub
parent 585c16cd2a
commit 652c42da2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      selfdrive/common/tests/test_util.cc
  2. 26
      selfdrive/common/util.cc
  3. 5
      selfdrive/common/util.h
  4. 3
      selfdrive/loggerd/logger.h
  5. 10
      selfdrive/loggerd/omx_encoder.cc

@ -93,6 +93,24 @@ TEST_CASE("util::read_files_in_dir") {
} }
} }
TEST_CASE("util::safe_fwrite") {
char filename[] = "/tmp/XXXXXX";
int fd = mkstemp(filename);
close(fd);
std::string dat = random_bytes(1024 * 1024);
FILE *f = util::safe_fopen(filename, "wb");
REQUIRE(f != nullptr);
size_t size = util::safe_fwrite(dat.data(), 1, dat.size(), f);
REQUIRE(size == dat.size());
int ret = util::safe_fflush(f);
REQUIRE(ret == 0);
ret = fclose(f);
REQUIRE(ret == 0);
REQUIRE(dat == util::read_file(filename));
}
TEST_CASE("util::create_directories") { TEST_CASE("util::create_directories") {
system("rm /tmp/test_create_directories -rf"); system("rm /tmp/test_create_directories -rf");
std::string dir = "/tmp/test_create_directories/a/b/c/d/e/f"; std::string dir = "/tmp/test_create_directories/a/b/c/d/e/f";

@ -107,6 +107,32 @@ int write_file(const char* path, const void* data, size_t size, int flags, mode_
return (n >= 0 && (size_t)n == size) ? 0 : -1; return (n >= 0 && (size_t)n == size) ? 0 : -1;
} }
FILE* safe_fopen(const char* filename, const char* mode) {
FILE* fp = NULL;
do {
fp = fopen(filename, mode);
} while ((nullptr == fp) && (errno == EINTR));
return fp;
}
size_t safe_fwrite(const void* ptr, size_t size, size_t count, FILE* stream) {
size_t written = 0;
do {
size_t ret = ::fwrite((void*)((char *)ptr + written * size), size, count - written, stream);
if (ret == 0 && errno != EINTR) break;
written += ret;
} while (written != count);
return written;
}
int safe_fflush(FILE *stream) {
int ret = EOF;
do {
ret = fflush(stream);
} while ((EOF == ret) && (errno == EINTR));
return ret;
}
std::string readlink(const std::string &path) { std::string readlink(const std::string &path) {
char buff[4096]; char buff[4096];
ssize_t len = ::readlink(path.c_str(), buff, sizeof(buff)-1); ssize_t len = ::readlink(path.c_str(), buff, sizeof(buff)-1);

@ -75,6 +75,11 @@ std::string dir_name(std::string const& path);
std::string read_file(const std::string& fn); std::string read_file(const std::string& fn);
std::map<std::string, std::string> read_files_in_dir(const std::string& path); 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 = 0664); int write_file(const char* path, const void* data, size_t size, int flags = O_WRONLY, mode_t mode = 0664);
FILE* safe_fopen(const char* filename, const char* mode);
size_t safe_fwrite(const void * ptr, size_t size, size_t count, FILE * stream);
int safe_fflush(FILE *stream);
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);
bool create_directories(const std::string &dir, mode_t mode); bool create_directories(const std::string &dir, mode_t mode);

@ -23,7 +23,7 @@ const std::string LOG_ROOT = Path::log_root();
class BZFile { class BZFile {
public: public:
BZFile(const char* path) { BZFile(const char* path) {
file = fopen(path, "wb"); file = util::safe_fopen(path, "wb");
assert(file != nullptr); assert(file != nullptr);
int bzerror; int bzerror;
bz_file = BZ2_bzWriteOpen(&bzerror, file, 9, 0, 30); bz_file = BZ2_bzWriteOpen(&bzerror, file, 9, 0, 30);
@ -35,6 +35,7 @@ class BZFile {
if (bzerror != BZ_OK) { if (bzerror != BZ_OK) {
LOGE("BZ2_bzWriteClose error, bzerror=%d", bzerror); LOGE("BZ2_bzWriteClose error, bzerror=%d", bzerror);
} }
util::safe_fflush(file);
int err = fclose(file); int err = fclose(file);
assert(err == 0); assert(err == 0);
} }

@ -344,7 +344,10 @@ void OmxEncoder::handle_out_buf(OmxEncoder *e, OMX_BUFFERHEADERTYPE *out_buf) {
if (e->of) { if (e->of) {
//printf("write %d flags 0x%x\n", out_buf->nFilledLen, out_buf->nFlags); //printf("write %d flags 0x%x\n", out_buf->nFilledLen, out_buf->nFlags);
fwrite(buf_data, out_buf->nFilledLen, 1, e->of); size_t written = util::safe_fwrite(buf_data, 1, out_buf->nFilledLen, e->of);
if (written != out_buf->nFilledLen) {
LOGE("failed to write file.errno=%d", errno);
}
} }
if (e->remuxing) { if (e->remuxing) {
@ -505,11 +508,11 @@ void OmxEncoder::encoder_open(const char* path) {
this->wrote_codec_config = false; this->wrote_codec_config = false;
} else { } else {
if (this->write) { if (this->write) {
this->of = fopen(this->vid_path, "wb"); this->of = util::safe_fopen(this->vid_path, "wb");
assert(this->of); assert(this->of);
#ifndef QCOM2 #ifndef QCOM2
if (this->codec_config_len > 0) { if (this->codec_config_len > 0) {
fwrite(this->codec_config, this->codec_config_len, 1, this->of); util::safe_fwrite(this->codec_config, 1, this->codec_config_len, this->of);
} }
#endif #endif
} }
@ -557,6 +560,7 @@ void OmxEncoder::encoder_close() {
avformat_free_context(this->ofmt_ctx); avformat_free_context(this->ofmt_ctx);
} else { } else {
if (this->of) { if (this->of) {
util::safe_fflush(this->of);
fclose(this->of); fclose(this->of);
this->of = nullptr; this->of = nullptr;
} }

Loading…
Cancel
Save