From c405d0ea2cbd76a30d001ceae3823cb41af650fb Mon Sep 17 00:00:00 2001 From: Francois Rousseau Date: Mon, 16 Sep 2019 15:54:13 -0700 Subject: [PATCH] make PruneCrashReportDatabase return the number of pruned crash reports Change-Id: I270ea8df5054ede9731c7a0a22439a1409eee6d9 Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/1808138 Commit-Queue: Francois Rousseau Reviewed-by: Scott Graham --- client/prune_crash_reports.cc | 12 +++++++++--- client/prune_crash_reports.h | 6 ++++-- client/prune_crash_reports_test.cc | 10 ++++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/client/prune_crash_reports.cc b/client/prune_crash_reports.cc index c6fa389b..8eed18d4 100644 --- a/client/prune_crash_reports.cc +++ b/client/prune_crash_reports.cc @@ -15,6 +15,7 @@ #include "client/prune_crash_reports.h" #include +#include #include #include @@ -24,7 +25,7 @@ namespace crashpad { -void PruneCrashReportDatabase(CrashReportDatabase* database, +size_t PruneCrashReportDatabase(CrashReportDatabase* database, PruneCondition* condition) { std::vector all_reports; CrashReportDatabase::OperationStatus status; @@ -32,14 +33,14 @@ void PruneCrashReportDatabase(CrashReportDatabase* database, status = database->GetPendingReports(&all_reports); if (status != CrashReportDatabase::kNoError) { LOG(ERROR) << "PruneCrashReportDatabase: Failed to get pending reports"; - return; + return 0; } std::vector completed_reports; status = database->GetCompletedReports(&completed_reports); if (status != CrashReportDatabase::kNoError) { LOG(ERROR) << "PruneCrashReportDatabase: Failed to get completed reports"; - return; + return 0; } all_reports.insert(all_reports.end(), completed_reports.begin(), completed_reports.end()); @@ -50,16 +51,21 @@ void PruneCrashReportDatabase(CrashReportDatabase* database, return lhs.creation_time > rhs.creation_time; }); + size_t num_pruned = 0; for (const auto& report : all_reports) { if (condition->ShouldPruneReport(report)) { status = database->DeleteReport(report.uuid); if (status != CrashReportDatabase::kNoError) { LOG(ERROR) << "Database Pruning: Failed to remove report " << report.uuid.ToString(); + } else { + num_pruned++; } } } + return num_pruned; + // TODO(rsesek): For databases that do not use a directory structure, it is // possible for the metadata sidecar to become corrupted and thus leave // orphaned crash report files on-disk. https://crashpad.chromium.org/bug/66 diff --git a/client/prune_crash_reports.h b/client/prune_crash_reports.h index 6dac5f30..07a70980 100644 --- a/client/prune_crash_reports.h +++ b/client/prune_crash_reports.h @@ -37,8 +37,10 @@ class PruneCondition; //! \param[in] database The database from which crash reports will be deleted. //! \param[in] condition The condition against which all reports in the database //! will be evaluated. -void PruneCrashReportDatabase(CrashReportDatabase* database, - PruneCondition* condition); +//! +//! \return The number of deleted crash reports. +size_t PruneCrashReportDatabase(CrashReportDatabase* database, + PruneCondition* condition); std::unique_ptr GetDefaultDatabasePruneCondition(); diff --git a/client/prune_crash_reports_test.cc b/client/prune_crash_reports_test.cc index c8c43444..dbe8c0a5 100644 --- a/client/prune_crash_reports_test.cc +++ b/client/prune_crash_reports_test.cc @@ -15,6 +15,7 @@ #include "client/prune_crash_reports.h" #include +#include #include #include @@ -204,11 +205,12 @@ TEST(PruneCrashReports, PruneOrder) { using ::testing::Return; using ::testing::SetArgPointee; + const size_t kNumReports = 10; std::vector reports; - for (int i = 0; i < 10; ++i) { + for (size_t i = 0; i < kNumReports; ++i) { CrashReportDatabase::Report temp; - temp.uuid.data_1 = i; - temp.creation_time = NDaysAgo(i * 10); + temp.uuid.data_1 = static_cast(i); + temp.creation_time = NDaysAgo(static_cast(i) * 10); reports.push_back(temp); } std::mt19937 urng(std::random_device{}()); @@ -231,7 +233,7 @@ TEST(PruneCrashReports, PruneOrder) { } StaticCondition delete_all(true); - PruneCrashReportDatabase(&db, &delete_all); + EXPECT_EQ(PruneCrashReportDatabase(&db, &delete_all), kNumReports); } } // namespace