collector: use non-throwing std::filesystem overloads Replaced throwing std::filesystem APIs with std::error_code overloads in fru_collector and sel_collector to prevent crashes. Tested: blaze test //third_party/milotic/external/cc/tlbmc/collector/... Google-Bug-Id: 478925741 PiperOrigin-RevId: 897503496 Change-Id: I33fcbb73979677724e1df42881bfad8fbc364a44
diff --git a/tlbmc/collector/fru_collector.cc b/tlbmc/collector/fru_collector.cc index 3e3795d..7b79050 100644 --- a/tlbmc/collector/fru_collector.cc +++ b/tlbmc/collector/fru_collector.cc
@@ -419,14 +419,20 @@ void FruCollector::WriteCachedFru(const RawFruTable& fru_table, const std::string& cached_fru_table_path) { std::filesystem::path path(cached_fru_table_path); - std::error_code error_code; std::filesystem::path parent_path = path.parent_path(); - if (!std::filesystem::exists(parent_path)) { - std::filesystem::create_directories(path.parent_path(), error_code); - if (error_code) { + std::error_code ec; + bool exists_result = std::filesystem::exists(parent_path, ec); + if (ec) { + LOG(ERROR) << "Error checking exists for path '" << parent_path + << "': " << ec.message(); + return; + } + if (!exists_result) { + std::filesystem::create_directories(parent_path, ec); + if (ec) { LOG(WARNING) << "Error creating directories for cached FRU table file at " "path: " - << path.parent_path(); + << parent_path; return; } }
diff --git a/tlbmc/collector/sel_collector.cc b/tlbmc/collector/sel_collector.cc index b7ee32b..59d756b 100644 --- a/tlbmc/collector/sel_collector.cc +++ b/tlbmc/collector/sel_collector.cc
@@ -231,9 +231,23 @@ bool found_any_sel_file = false; std::string last_file_path; std::error_code ec; - for (const std::filesystem::directory_entry& entry : - std::filesystem::directory_iterator(config_.sel_save_path(), ec)) { - if (!entry.is_regular_file()) { + auto it = std::filesystem::directory_iterator(config_.sel_save_path(), ec); + if (ec) { + return absl::InternalError(absl::StrCat( + "Failed to iterate over SEL save path: ", config_.sel_save_path())); + } + auto end = std::filesystem::directory_iterator(); + for (; it != end; it.increment(ec)) { + const std::filesystem::directory_entry& entry = *it; + + std::error_code reg_ec; + bool is_reg = entry.is_regular_file(reg_ec); + if (reg_ec) { + LOG(ERROR) << "Error checking is_regular_file for path '" + << entry.path().string() << "': " << reg_ec.message(); + continue; + } + if (!is_reg) { continue; } std::string file_name = entry.path().filename().string();