diff --git a/selfdrive/loggerd/bootlog.cc b/selfdrive/loggerd/bootlog.cc index 481b3ee47..520995837 100644 --- a/selfdrive/loggerd/bootlog.cc +++ b/selfdrive/loggerd/bootlog.cc @@ -56,6 +56,7 @@ static kj::Array build_boot_log() { } int main(int argc, char** argv) { + clear_locks(LOG_ROOT); const std::string path = LOG_ROOT + "/boot/" + logger_get_route_name() + ".bz2"; LOGW("bootlog to %s", path.c_str()); diff --git a/selfdrive/loggerd/logger.cc b/selfdrive/loggerd/logger.cc index a73fefb8c..81cfd131f 100644 --- a/selfdrive/loggerd/logger.cc +++ b/selfdrive/loggerd/logger.cc @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -266,3 +267,15 @@ void lh_close(LoggerHandle* h) { } pthread_mutex_unlock(&h->lock); } + +int clear_locks_fn(const char* fpath, const struct stat *sb, int tyupeflag) { + const char* dot = strrchr(fpath, '.'); + if (dot && strcmp(dot, ".lock") == 0) { + unlink(fpath); + } + return 0; +} + +void clear_locks(const std::string log_root) { + ftw(log_root.c_str(), clear_locks_fn, 16); +} diff --git a/selfdrive/loggerd/logger.h b/selfdrive/loggerd/logger.h index bdda9d691..e85d7810e 100644 --- a/selfdrive/loggerd/logger.h +++ b/selfdrive/loggerd/logger.h @@ -96,3 +96,4 @@ void logger_log(LoggerState *s, uint8_t* data, size_t data_size, bool in_qlog); void lh_log(LoggerHandle* h, uint8_t* data, size_t data_size, bool in_qlog); void lh_close(LoggerHandle* h); +void clear_locks(const std::string log_root); diff --git a/selfdrive/loggerd/loggerd.cc b/selfdrive/loggerd/loggerd.cc index 40065b585..e3444f331 100644 --- a/selfdrive/loggerd/loggerd.cc +++ b/selfdrive/loggerd/loggerd.cc @@ -149,18 +149,6 @@ void encoder_thread(LoggerdState *s, const LogCameraInfo &cam_info) { } } -int clear_locks_fn(const char* fpath, const struct stat *sb, int tyupeflag) { - const char* dot = strrchr(fpath, '.'); - if (dot && strcmp(dot, ".lock") == 0) { - unlink(fpath); - } - return 0; -} - -void clear_locks() { - ftw(LOG_ROOT.c_str(), clear_locks_fn, 16); -} - void logger_rotate(LoggerdState *s) { { std::unique_lock lk(s->rotate_lock); @@ -190,8 +178,6 @@ void rotate_if_needed(LoggerdState *s) { } void loggerd_thread() { - clear_locks(); - // setup messaging typedef struct QlogState { int counter, freq; diff --git a/selfdrive/loggerd/loggerd.h b/selfdrive/loggerd/loggerd.h index b3e45adfa..448927ff8 100644 --- a/selfdrive/loggerd/loggerd.h +++ b/selfdrive/loggerd/loggerd.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include diff --git a/selfdrive/loggerd/tests/test_loggerd.cc b/selfdrive/loggerd/tests/test_loggerd.cc index d84185cbb..849b350ee 100644 --- a/selfdrive/loggerd/tests/test_loggerd.cc +++ b/selfdrive/loggerd/tests/test_loggerd.cc @@ -91,3 +91,21 @@ TEST_CASE("trigger_rotate") { REQUIRE(frame_id == start_frame_id + encoder_seg * (SEGMENT_LENGTH * MAIN_FPS)); } } + +TEST_CASE("clear_locks") { + std::vector dirs; + for (int i = 0; i < 10; ++i) { + std::string &path = dirs.emplace_back(LOG_ROOT + "/" + std::to_string(i)); + REQUIRE(util::create_directories(path, 0775)); + std::ofstream{path + "/.lock"}; + REQUIRE(util::file_exists(path + "/.lock")); + } + + clear_locks(LOG_ROOT); + + for (const auto &dir : dirs) { + std::string lock_file = dir + "/.lock"; + REQUIRE(util::file_exists(lock_file) == false); + rmdir(dir.c_str()); + } +}