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 <frousseau@google.com>
Reviewed-by: Scott Graham <scottmg@chromium.org>
This commit is contained in:
Francois Rousseau 2019-09-16 15:54:13 -07:00 committed by Commit Bot
parent edbbc4609d
commit c405d0ea2c
3 changed files with 19 additions and 9 deletions

View File

@ -15,6 +15,7 @@
#include "client/prune_crash_reports.h" #include "client/prune_crash_reports.h"
#include <sys/stat.h> #include <sys/stat.h>
#include <stdint.h>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
@ -24,7 +25,7 @@
namespace crashpad { namespace crashpad {
void PruneCrashReportDatabase(CrashReportDatabase* database, size_t PruneCrashReportDatabase(CrashReportDatabase* database,
PruneCondition* condition) { PruneCondition* condition) {
std::vector<CrashReportDatabase::Report> all_reports; std::vector<CrashReportDatabase::Report> all_reports;
CrashReportDatabase::OperationStatus status; CrashReportDatabase::OperationStatus status;
@ -32,14 +33,14 @@ void PruneCrashReportDatabase(CrashReportDatabase* database,
status = database->GetPendingReports(&all_reports); status = database->GetPendingReports(&all_reports);
if (status != CrashReportDatabase::kNoError) { if (status != CrashReportDatabase::kNoError) {
LOG(ERROR) << "PruneCrashReportDatabase: Failed to get pending reports"; LOG(ERROR) << "PruneCrashReportDatabase: Failed to get pending reports";
return; return 0;
} }
std::vector<CrashReportDatabase::Report> completed_reports; std::vector<CrashReportDatabase::Report> completed_reports;
status = database->GetCompletedReports(&completed_reports); status = database->GetCompletedReports(&completed_reports);
if (status != CrashReportDatabase::kNoError) { if (status != CrashReportDatabase::kNoError) {
LOG(ERROR) << "PruneCrashReportDatabase: Failed to get completed reports"; LOG(ERROR) << "PruneCrashReportDatabase: Failed to get completed reports";
return; return 0;
} }
all_reports.insert(all_reports.end(), completed_reports.begin(), all_reports.insert(all_reports.end(), completed_reports.begin(),
completed_reports.end()); completed_reports.end());
@ -50,16 +51,21 @@ void PruneCrashReportDatabase(CrashReportDatabase* database,
return lhs.creation_time > rhs.creation_time; return lhs.creation_time > rhs.creation_time;
}); });
size_t num_pruned = 0;
for (const auto& report : all_reports) { for (const auto& report : all_reports) {
if (condition->ShouldPruneReport(report)) { if (condition->ShouldPruneReport(report)) {
status = database->DeleteReport(report.uuid); status = database->DeleteReport(report.uuid);
if (status != CrashReportDatabase::kNoError) { if (status != CrashReportDatabase::kNoError) {
LOG(ERROR) << "Database Pruning: Failed to remove report " LOG(ERROR) << "Database Pruning: Failed to remove report "
<< report.uuid.ToString(); << report.uuid.ToString();
} else {
num_pruned++;
} }
} }
} }
return num_pruned;
// TODO(rsesek): For databases that do not use a directory structure, it is // TODO(rsesek): For databases that do not use a directory structure, it is
// possible for the metadata sidecar to become corrupted and thus leave // possible for the metadata sidecar to become corrupted and thus leave
// orphaned crash report files on-disk. https://crashpad.chromium.org/bug/66 // orphaned crash report files on-disk. https://crashpad.chromium.org/bug/66

View File

@ -37,8 +37,10 @@ class PruneCondition;
//! \param[in] database The database from which crash reports will be deleted. //! \param[in] database The database from which crash reports will be deleted.
//! \param[in] condition The condition against which all reports in the database //! \param[in] condition The condition against which all reports in the database
//! will be evaluated. //! 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<PruneCondition> GetDefaultDatabasePruneCondition(); std::unique_ptr<PruneCondition> GetDefaultDatabasePruneCondition();

View File

@ -15,6 +15,7 @@
#include "client/prune_crash_reports.h" #include "client/prune_crash_reports.h"
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <algorithm> #include <algorithm>
@ -204,11 +205,12 @@ TEST(PruneCrashReports, PruneOrder) {
using ::testing::Return; using ::testing::Return;
using ::testing::SetArgPointee; using ::testing::SetArgPointee;
const size_t kNumReports = 10;
std::vector<CrashReportDatabase::Report> reports; std::vector<CrashReportDatabase::Report> reports;
for (int i = 0; i < 10; ++i) { for (size_t i = 0; i < kNumReports; ++i) {
CrashReportDatabase::Report temp; CrashReportDatabase::Report temp;
temp.uuid.data_1 = i; temp.uuid.data_1 = static_cast<uint32_t>(i);
temp.creation_time = NDaysAgo(i * 10); temp.creation_time = NDaysAgo(static_cast<int>(i) * 10);
reports.push_back(temp); reports.push_back(temp);
} }
std::mt19937 urng(std::random_device{}()); std::mt19937 urng(std::random_device{}());
@ -231,7 +233,7 @@ TEST(PruneCrashReports, PruneOrder) {
} }
StaticCondition delete_all(true); StaticCondition delete_all(true);
PruneCrashReportDatabase(&db, &delete_all); EXPECT_EQ(PruneCrashReportDatabase(&db, &delete_all), kNumReports);
} }
} // namespace } // namespace