OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "client/prune_crash_reports.h" |
| 16 |
| 17 #include <sys/stat.h> |
| 18 |
| 19 #include <algorithm> |
| 20 #include <vector> |
| 21 |
| 22 #include "base/logging.h" |
| 23 |
| 24 namespace crashpad { |
| 25 |
| 26 void PruneCrashReportDatabase(CrashReportDatabase* database, |
| 27 PruneCondition* condition) { |
| 28 std::vector<CrashReportDatabase::Report> all_reports, temp; |
| 29 CrashReportDatabase::OperationStatus status; |
| 30 |
| 31 status = database->GetPendingReports(&temp); |
| 32 if (status != CrashReportDatabase::kNoError) { |
| 33 LOG(ERROR) << "PruneCrashReportDatabase: Failed to get pending reports"; |
| 34 return; |
| 35 } |
| 36 all_reports.insert(all_reports.end(), temp.begin(), temp.end()); |
| 37 temp.clear(); |
| 38 |
| 39 status = database->GetCompletedReports(&temp); |
| 40 if (status != CrashReportDatabase::kNoError) { |
| 41 LOG(ERROR) << "PruneCrashReportDatabase: Failed to get completed reports"; |
| 42 return; |
| 43 } |
| 44 all_reports.insert(all_reports.end(), temp.begin(), temp.end()); |
| 45 |
| 46 std::sort(all_reports.begin(), all_reports.end(), |
| 47 [](const CrashReportDatabase::Report& lhs, |
| 48 const CrashReportDatabase::Report& rhs) { |
| 49 return lhs.creation_time > rhs.creation_time; |
| 50 }); |
| 51 |
| 52 for (const auto& report : all_reports) { |
| 53 if (condition->ShouldPruneReport(report)) { |
| 54 status = database->DeleteReport(report.uuid); |
| 55 if (status != CrashReportDatabase::kNoError) { |
| 56 LOG(ERROR) << "Database Pruning: Failed to remove report " |
| 57 << report.uuid.ToString(); |
| 58 } |
| 59 } |
| 60 } |
| 61 } |
| 62 |
| 63 // static |
| 64 scoped_ptr<PruneCondition> PruneCondition::GetDefault() { |
| 65 // DatabaseSizePruneCondition must be the LHS so that it is always evaluated, |
| 66 // due to the short-circuting behavior of BinaryPruneCondition. |
| 67 return make_scoped_ptr(new BinaryPruneCondition(BinaryPruneCondition::OR, |
| 68 new DatabaseSizePruneCondition(1024 * 128), new AgePruneCondition(365))); |
| 69 } |
| 70 |
| 71 const time_t kSecondsInDay = 60 * 60 * 24; |
| 72 |
| 73 AgePruneCondition::AgePruneCondition(int max_age_in_days) |
| 74 : oldest_report_time_( |
| 75 ((time(nullptr) - (max_age_in_days * kSecondsInDay)) |
| 76 / kSecondsInDay) * kSecondsInDay) {} |
| 77 |
| 78 AgePruneCondition::~AgePruneCondition() {} |
| 79 |
| 80 bool AgePruneCondition::ShouldPruneReport( |
| 81 const CrashReportDatabase::Report& report) { |
| 82 return report.creation_time < oldest_report_time_; |
| 83 } |
| 84 |
| 85 DatabaseSizePruneCondition::DatabaseSizePruneCondition(size_t max_size_in_kb) |
| 86 : max_size_in_kb_(max_size_in_kb), measured_size_(0) {} |
| 87 |
| 88 DatabaseSizePruneCondition::~DatabaseSizePruneCondition() {} |
| 89 |
| 90 bool DatabaseSizePruneCondition::ShouldPruneReport( |
| 91 const CrashReportDatabase::Report& report) { |
| 92 #if defined(OS_POSIX) |
| 93 struct stat statbuf; |
| 94 if (stat(report.file_path.value().c_str(), &statbuf) == 0) { |
| 95 #elif defined(OS_WIN) |
| 96 struct _stati64 statbuf; |
| 97 if (_wstat64(report.file_path.value().c_str(), &statbuf) == 0) { |
| 98 #else |
| 99 #error "Not implemented" |
| 100 #endif |
| 101 measured_size_ += statbuf.st_size; |
| 102 } |
| 103 return (measured_size_ / 1024.0f) > max_size_in_kb_; |
| 104 } |
| 105 |
| 106 BinaryPruneCondition::BinaryPruneCondition( |
| 107 Operator op, PruneCondition* lhs, PruneCondition* rhs) |
| 108 : op_(op), lhs_(lhs), rhs_(rhs) {} |
| 109 |
| 110 BinaryPruneCondition::~BinaryPruneCondition() {} |
| 111 |
| 112 bool BinaryPruneCondition::ShouldPruneReport( |
| 113 const CrashReportDatabase::Report& report) { |
| 114 switch (op_) { |
| 115 case AND: |
| 116 return lhs_->ShouldPruneReport(report) && rhs_->ShouldPruneReport(report); |
| 117 case OR: |
| 118 return lhs_->ShouldPruneReport(report) || rhs_->ShouldPruneReport(report); |
| 119 default: |
| 120 NOTREACHED(); |
| 121 return false; |
| 122 } |
| 123 } |
| 124 |
| 125 } // namespace crashpad |
OLD | NEW |