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 #ifndef CRASHPAD_CLIENT_PRUNE_CRASH_REPORTS_H_ 16 #define CRASHPAD_CLIENT_PRUNE_CRASH_REPORTS_H_ 17 18 #include <sys/types.h> 19 #include <time.h> 20 21 #include <memory> 22 23 #include "base/macros.h" 24 #include "client/crash_report_database.h" 25 26 namespace crashpad { 27 28 class PruneCondition; 29 30 //! \brief Deletes crash reports from \a database that match \a condition. 31 //! 32 //! This function can be used to remove old or large reports from the database. 33 //! The \a condition will be evaluated against each report in the \a database, 34 //! sorted in descending order by CrashReportDatabase::Report::creation_time. 35 //! This guarantee allows conditions to be stateful. 36 //! 37 //! \param[in] database The database from which crash reports will be deleted. 38 //! \param[in] condition The condition against which all reports in the database 39 //! will be evaluated. 40 //! 41 //! \return The number of deleted crash reports. 42 size_t PruneCrashReportDatabase(CrashReportDatabase* database, 43 PruneCondition* condition); 44 45 std::unique_ptr<PruneCondition> GetDefaultDatabasePruneCondition(); 46 47 //! \brief An abstract base class for evaluating crash reports for deletion. 48 //! 49 //! When passed to PruneCrashReportDatabase(), each crash report in the 50 //! database will be evaluated according to ShouldPruneReport(). The reports 51 //! are evaluated serially in descending sort order by 52 //! CrashReportDatabase::Report::creation_time. 53 class PruneCondition { 54 public: 55 //! \brief Returns a sensible default condition for removing obsolete crash 56 //! reports. 57 //! 58 //! The default is to keep reports for one year or a maximum database size 59 //! of 128 MB. 60 //! 61 //! \return A PruneCondition for use with PruneCrashReportDatabase(). 62 static std::unique_ptr<PruneCondition> GetDefault(); 63 ~PruneCondition()64 virtual ~PruneCondition() {} 65 66 //! \brief Evaluates a crash report for deletion. 67 //! 68 //! \param[in] report The crash report to evaluate. 69 //! 70 //! \return `true` if the crash report should be deleted, `false` if it 71 //! should be kept. 72 virtual bool ShouldPruneReport(const CrashReportDatabase::Report& report) = 0; 73 }; 74 75 //! \brief A PruneCondition that deletes reports older than the specified number 76 //! days. 77 class AgePruneCondition final : public PruneCondition { 78 public: 79 //! \brief Creates a PruneCondition based on Report::creation_time. 80 //! 81 //! \param[in] max_age_in_days Reports created more than this many days ago 82 //! will be deleted. 83 explicit AgePruneCondition(int max_age_in_days); 84 ~AgePruneCondition(); 85 86 bool ShouldPruneReport(const CrashReportDatabase::Report& report) override; 87 88 private: 89 const time_t oldest_report_time_; 90 91 DISALLOW_COPY_AND_ASSIGN(AgePruneCondition); 92 }; 93 94 //! \brief A PruneCondition that deletes older reports to keep the total 95 //! Crashpad database size under the specified limit. 96 class DatabaseSizePruneCondition final : public PruneCondition { 97 public: 98 //! \brief Creates a PruneCondition that will keep newer reports, until the 99 //! sum of the size of all reports is not smaller than \a max_size_in_kb. 100 //! After the limit is reached, older reports will be pruned. 101 //! 102 //! \param[in] max_size_in_kb The maximum number of kilobytes that all crash 103 //! reports should consume. 104 explicit DatabaseSizePruneCondition(size_t max_size_in_kb); 105 ~DatabaseSizePruneCondition(); 106 107 bool ShouldPruneReport(const CrashReportDatabase::Report& report) override; 108 109 private: 110 const size_t max_size_in_kb_; 111 size_t measured_size_in_kb_; 112 113 DISALLOW_COPY_AND_ASSIGN(DatabaseSizePruneCondition); 114 }; 115 116 //! \brief A PruneCondition that conjoins two other PruneConditions. 117 class BinaryPruneCondition final : public PruneCondition { 118 public: 119 enum Operator { 120 AND, 121 OR, 122 }; 123 124 //! \brief Evaluates two sub-conditions according to the specified logical 125 //! operator. 126 //! 127 //! This implements left-to-right evaluation. For Operator::AND, this means 128 //! if the \a lhs is `false`, the \a rhs will not be consulted. Similarly, 129 //! with Operator::OR, if the \a lhs is `true`, the \a rhs will not be 130 //! consulted. 131 //! 132 //! \param[in] op The logical operator to apply on \a lhs and \a rhs. 133 //! \param[in] lhs The left-hand side of \a op. This class takes ownership. 134 //! \param[in] rhs The right-hand side of \a op. This class takes ownership. 135 BinaryPruneCondition(Operator op, PruneCondition* lhs, PruneCondition* rhs); 136 ~BinaryPruneCondition(); 137 138 bool ShouldPruneReport(const CrashReportDatabase::Report& report) override; 139 140 private: 141 const Operator op_; 142 std::unique_ptr<PruneCondition> lhs_; 143 std::unique_ptr<PruneCondition> rhs_; 144 145 DISALLOW_COPY_AND_ASSIGN(BinaryPruneCondition); 146 }; 147 148 } // namespace crashpad 149 150 #endif // CRASHPAD_CLIENT_PRUNE_CRASH_REPORTS_H_ 151