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); | |
Mark Mentovai
2015/10/07 03:54:27
Why don’t you fetch this right into all_reports? T
Robert Sesek
2015/10/07 16:24:34
Must be empty on entry. http://docs.crashpad.googl
Mark Mentovai
2015/10/07 16:40:13
It will be.
1. Fetch pending into all_reports.
2.
Robert Sesek
2015/10/07 17:16:28
Done.
| |
32 if (status != CrashReportDatabase::kNoError) { | |
33 LOG(ERROR) << "Database Pruning: Failed to get pending reports"; | |
Mark Mentovai
2015/10/07 03:54:27
Local style is predominantly “PruneCrashReportData
Robert Sesek
2015/10/07 16:24:34
Done.
| |
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) << "Database Pruning: 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 scoped_ptr<PruneCondition> GetDefaultDatabasePruneCondition() { | |
64 return make_scoped_ptr(new BinaryPruneCondition(BinaryPruneCondition::OR, | |
65 new AgePruneCondition(365), new DatabaseSizePruneCondition(1024 * 128))); | |
66 } | |
67 | |
68 AgePruneCondition::AgePruneCondition(int max_age_in_days) | |
69 : oldest_report_time_(time(nullptr) - (max_age_in_days * 60 * 60 * 24)) {} | |
Mark Mentovai
2015/10/07 03:54:27
mod this whole thing to a round UTC day?
Robert Sesek
2015/10/07 16:24:34
Done.
| |
70 | |
71 AgePruneCondition::~AgePruneCondition() {} | |
72 | |
73 bool AgePruneCondition::ShouldPruneReport( | |
74 const CrashReportDatabase::Report& report) { | |
75 return report.creation_time < oldest_report_time_; | |
76 } | |
77 | |
78 DatabaseSizePruneCondition::DatabaseSizePruneCondition(size_t max_size_in_bytes) | |
79 : max_size_in_bytes_(max_size_in_bytes), measured_size_(0) {} | |
80 | |
81 DatabaseSizePruneCondition::~DatabaseSizePruneCondition() {} | |
82 | |
83 bool DatabaseSizePruneCondition::ShouldPruneReport( | |
84 const CrashReportDatabase::Report& report) { | |
85 #if defined(OS_POSIX) | |
86 struct stat statbuf; | |
87 if (stat(report.file_path.value().c_str(), &statbuf) == 0) { | |
88 #elif defined(OS_WIN) | |
89 struct _stat statbuf; | |
Mark Mentovai
2015/10/07 03:54:27
_stati64 for a full-sized st_size.
Robert Sesek
2015/10/07 16:24:34
Done.
| |
90 if (_wstat(report.file_path.value().c_str(), &statbuf) == 0) { | |
Mark Mentovai
2015/10/07 03:54:27
_wstati64
Robert Sesek
2015/10/07 16:24:34
Done.
| |
91 #else | |
92 #error "Not implemented" | |
93 #endif | |
94 measured_size_ += statbuf.st_size; | |
95 } | |
96 return measured_size_ > max_size_in_bytes_; | |
97 } | |
98 | |
99 BinaryPruneCondition::BinaryPruneCondition( | |
100 Operator op, PruneCondition* lhs, PruneCondition* rhs) | |
101 : op_(op), lhs_(lhs), rhs_(rhs) {} | |
102 | |
103 BinaryPruneCondition::~BinaryPruneCondition() {} | |
104 | |
105 bool BinaryPruneCondition::ShouldPruneReport( | |
106 const CrashReportDatabase::Report& report) { | |
107 switch (op_) { | |
108 case AND: | |
109 return lhs_->ShouldPruneReport(report) && rhs_->ShouldPruneReport(report); | |
110 case OR: | |
111 return lhs_->ShouldPruneReport(report) || rhs_->ShouldPruneReport(report); | |
112 } | |
113 } | |
scottmg
2015/10/06 22:22:07
On Windows:
d:\src\crashpad\crashpad\client\prune
Robert Sesek
2015/10/07 16:24:34
Done.
| |
114 | |
115 } // namespace crashpad | |
OLD | NEW |