OLD | NEW |
---|---|
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "client/crash_report_database.h" | 15 #include "client/crash_report_database.h" |
16 | 16 |
17 #include <rpc.h> | 17 #include <rpc.h> |
18 #include <string.h> | 18 #include <string.h> |
19 #include <time.h> | 19 #include <time.h> |
20 #include <windows.h> | 20 #include <windows.h> |
21 | 21 |
22 #include "base/logging.h" | 22 #include "base/logging.h" |
23 #include "base/numerics/safe_math.h" | 23 #include "base/numerics/safe_math.h" |
24 #include "base/strings/string16.h" | |
24 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
25 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
26 | 27 |
27 namespace crashpad { | 28 namespace crashpad { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 const wchar_t kDatabaseDirectoryName[] = L"Crashpad"; | 32 const wchar_t kDatabaseDirectoryName[] = L"Crashpad"; |
32 | 33 |
33 const wchar_t kReportsDirectory[] = L"reports"; | 34 const wchar_t kReportsDirectory[] = L"reports"; |
34 const wchar_t kMetadataFileName[] = L"metadata"; | 35 const wchar_t kMetadataFileName[] = L"metadata"; |
35 | 36 |
36 const wchar_t kCrashReportFileExtension[] = L"dmp"; | 37 const wchar_t kCrashReportFileExtension[] = L"dmp"; |
37 | 38 |
38 enum class ReportState : int { | 39 const uint32_t kMetadataFileHeaderMagic = 'CPAD'; |
40 const uint32_t kMetadataFileVersion = 1; | |
41 | |
42 using OperationStatus = CrashReportDatabase::OperationStatus; | |
43 | |
44 // Helpers --------------------------------------------------------------------- | |
45 | |
46 // Adds a string to the string table and returns the byte index where it was | |
47 // added. | |
48 uint32_t AddStringToTable(std::string* string_table, const std::string& str) { | |
49 uint32_t offset = base::checked_cast<uint32_t>(string_table->size()); | |
50 *string_table += str; | |
51 *string_table += '\0'; | |
52 return offset; | |
53 } | |
54 | |
55 // Converts |str| to UTF8, adds the result to the string table and returns the | |
56 // byte index where it was added. | |
57 uint32_t AddStringToTable(std::string* string_table, | |
58 const base::string16& str) { | |
59 return AddStringToTable(string_table, base::UTF16ToUTF8(str)); | |
60 } | |
61 | |
62 // Reads from the current file position to EOF and returns as a string of bytes. | |
63 std::string ReadRestOfFileAsString(FileHandle file) { | |
64 FileOffset read_from = LoggingSeekFile(file, 0, SEEK_CUR); | |
65 FileOffset end = LoggingSeekFile(file, 0, SEEK_END); | |
66 FileOffset original = LoggingSeekFile(file, read_from, SEEK_SET); | |
67 if (read_from == -1 || end == -1 || original == -1 || read_from == end) | |
68 return std::string(); | |
69 DCHECK_EQ(read_from, original); | |
70 DCHECK_GT(end, read_from); | |
71 size_t data_length = static_cast<size_t>(end - read_from); | |
72 std::string buffer(data_length, '\0'); | |
73 return LoggingReadFile(file, &buffer[0], data_length) ? buffer | |
74 : std::string(); | |
75 } | |
76 | |
77 // Helper structures, and conversions ------------------------------------------ | |
78 | |
79 // The format of the on disk metadata file is a MetadataFileHeader, followed by | |
80 // a number of fixed size records of MetadataFileReportRecord, followed by a | |
81 // string table in UTF8 format, where each string is \0 terminated. | |
82 struct MetadataFileHeader { | |
83 uint32_t magic; | |
84 uint32_t version; | |
85 uint32_t num_records; | |
86 uint32_t padding; | |
87 }; | |
88 | |
89 struct ReportDisk; | |
90 | |
91 enum class ReportState { | |
39 //! \brief Created and filled out by caller, owned by database. | 92 //! \brief Created and filled out by caller, owned by database. |
40 kPending, | 93 kPending, |
41 //! \brief In the process of uploading, owned by caller. | 94 //! \brief In the process of uploading, owned by caller. |
42 kUploading, | 95 kUploading, |
43 //! \brief Upload completed or skipped, owned by database. | 96 //! \brief Upload completed or skipped, owned by database. |
44 kCompleted, | 97 kCompleted, |
45 }; | 98 }; |
46 | 99 |
47 using OperationStatus = CrashReportDatabase::OperationStatus; | 100 struct MetadataFileReportRecord { |
101 MetadataFileReportRecord() {} | |
Mark Mentovai
2015/02/17 19:40:40
Regarding initialization: Leave a comment here exp
scottmg
2015/02/17 19:52:20
Done.
| |
48 | 102 |
49 //! \brief Ensures that the node at path is a directory, and creates it if it | 103 // Constructs from a ReportDisk, adding to |string_table| and storing indices |
50 //! does not exist. | 104 // as strings into that table. |
51 //! | 105 MetadataFileReportRecord(const ReportDisk& report, std::string* string_table); |
52 //! \return If the path points to a file, rather than a directory, or the | 106 |
53 //! directory could not be created, returns `false`. Otherwise, returns | 107 UUID uuid; // UUID is a 16 byte, standard layout structure. |
54 //! `true`, indicating that path already was or now is a directory. | 108 uint32_t file_path_index; // Index into string table. File name is relative |
55 bool CreateOrEnsureDirectoryExists(const base::FilePath& path) { | 109 // to the reports directory when on disk. |
56 if (CreateDirectory(path.value().c_str(), nullptr)) { | 110 uint32_t id_index; // Index into string table. |
57 return true; | 111 int64_t creation_time; // Holds a time_t. |
58 } else if (GetLastError() == ERROR_ALREADY_EXISTS) { | 112 int64_t last_upload_attempt_time; // Holds a time_t. |
59 DWORD fileattr = GetFileAttributes(path.value().c_str()); | 113 int32_t upload_attempts; |
60 if (fileattr == INVALID_FILE_ATTRIBUTES) { | 114 int32_t state; // A ReportState. |
61 PLOG(ERROR) << "GetFileAttributes"; | 115 uint8_t uploaded; // Boolean, 0 or 1. |
62 return false; | 116 uint8_t padding[7]; |
63 } | 117 }; |
64 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | |
65 return true; | |
66 LOG(ERROR) << "not a directory"; | |
67 return false; | |
68 } else { | |
69 PLOG(ERROR) << "CreateDirectory"; | |
70 return false; | |
71 } | |
72 } | |
73 | 118 |
74 //! \brief A private extension of the Report class that includes additional data | 119 //! \brief A private extension of the Report class that includes additional data |
75 //! that's stored on disk in the metadata file. | 120 //! that's stored on disk in the metadata file. |
76 struct ReportDisk : public CrashReportDatabase::Report { | 121 struct ReportDisk : public CrashReportDatabase::Report { |
122 ReportDisk(const MetadataFileReportRecord& record, | |
123 const base::FilePath& report_dir, | |
124 const std::string& string_table); | |
125 | |
126 ReportDisk(const UUID& uuid, | |
127 const base::FilePath& path, | |
128 time_t creation_tim, | |
129 ReportState state); | |
130 | |
77 //! \brief The current state of the report. | 131 //! \brief The current state of the report. |
78 ReportState state; | 132 ReportState state; |
79 }; | 133 }; |
80 | 134 |
135 MetadataFileReportRecord::MetadataFileReportRecord(const ReportDisk& report, | |
136 std::string* string_table) | |
137 : uuid(report.uuid), | |
Mark Mentovai
2015/02/17 19:40:40
Regarding no superclass: you’re right. I was throw
scottmg
2015/02/17 19:52:20
The implementation of MFRR has to go below the def
| |
138 file_path_index( | |
139 AddStringToTable(string_table, report.file_path.BaseName().value())), | |
140 id_index(AddStringToTable(string_table, report.id)), | |
141 creation_time(report.creation_time), | |
142 last_upload_attempt_time(report.last_upload_attempt_time), | |
143 upload_attempts(report.upload_attempts), | |
144 state(static_cast<uint32_t>(report.state)), | |
145 uploaded(report.uploaded) { | |
146 memset(&padding, 0, sizeof(padding)); | |
147 } | |
148 | |
149 ReportDisk::ReportDisk(const MetadataFileReportRecord& record, | |
150 const base::FilePath& report_dir, | |
151 const std::string& string_table) | |
152 : Report() { | |
153 uuid = record.uuid; | |
154 file_path = report_dir.Append( | |
155 base::UTF8ToUTF16(&string_table[record.file_path_index])); | |
156 id = &string_table[record.id_index]; | |
157 creation_time = record.creation_time; | |
158 uploaded = record.uploaded; | |
159 last_upload_attempt_time = record.last_upload_attempt_time; | |
160 upload_attempts = record.upload_attempts; | |
161 state = static_cast<ReportState>(record.state); | |
162 } | |
163 | |
164 ReportDisk::ReportDisk(const UUID& uuid, | |
165 const base::FilePath& path, | |
166 time_t creation_time, | |
167 ReportState state) | |
168 : Report() { | |
169 this->uuid = uuid; | |
170 this->file_path = path; | |
171 this->creation_time = creation_time; | |
172 this->state = state; | |
173 } | |
174 | |
81 //! \brief A private extension of the NewReport class to hold the UUID during | 175 //! \brief A private extension of the NewReport class to hold the UUID during |
82 //! initial write. We don't store metadata in dump's file attributes, and | 176 //! initial write. We don't store metadata in dump's file attributes, so we |
83 //! use the UUID to identify the dump on write completion. | 177 //! use the UUID to identify the dump on write completion. |
84 struct NewReportDisk : public CrashReportDatabase::NewReport { | 178 struct NewReportDisk : public CrashReportDatabase::NewReport { |
85 //! \brief The UUID for this crash report. | 179 //! \brief The UUID for this crash report. |
86 UUID uuid; | 180 UUID uuid; |
87 }; | 181 }; |
88 | 182 |
183 // Metadata -------------------------------------------------------------------- | |
184 | |
89 //! \brief Manages the metadata for the set of reports, handling serialization | 185 //! \brief Manages the metadata for the set of reports, handling serialization |
90 //! to disk, and queries. Instances of this class should be created by using | 186 //! to disk, and queries. |
91 //! CrashReportDatabaseWin::AcquireMetadata(). | |
92 class Metadata { | 187 class Metadata { |
93 public: | 188 public: |
94 //! \brief Writes any changes if necessary, unlocks and closes the file | 189 //! \brief Writes any changes if necessary, unlocks and closes the file |
95 //! handle. | 190 //! handle. |
96 ~Metadata(); | 191 ~Metadata(); |
97 | 192 |
193 static scoped_ptr<Metadata> Create(const base::FilePath& metadata_file, | |
194 const base::FilePath& report_dir); | |
195 | |
98 //! \brief Adds a new report to the set. | 196 //! \brief Adds a new report to the set. |
99 //! | 197 //! |
100 //! \param[in] new_report_disk The record to add. The #state field must be set | 198 //! \param[in] new_report_disk The record to add. The #state field must be set |
101 //! to kPending. | 199 //! to kPending. |
102 void AddNewRecord(const ReportDisk& new_report_disk); | 200 void AddNewRecord(const ReportDisk& new_report_disk); |
103 | 201 |
104 //! \brief Finds all reports in a given state. The \a reports vector is only | 202 //! \brief Finds all reports in a given state. The \a reports vector is only |
105 //! valid when CrashReportDatabase::kNoError is returned. | 203 //! valid when CrashReportDatabase::kNoError is returned. |
106 //! | 204 //! |
107 //! \param[in] desired_state The state to match. | 205 //! \param[in] desired_state The state to match. |
108 //! \param[out] reports Matching reports, must be empty on entry. | 206 //! \param[out] reports Matching reports, must be empty on entry. |
109 OperationStatus FindReports( | 207 OperationStatus FindReports( |
110 ReportState desired_state, | 208 ReportState desired_state, |
111 std::vector<const CrashReportDatabase::Report>* reports); | 209 std::vector<const CrashReportDatabase::Report>* reports) const; |
112 | 210 |
113 //! \brief Finds the report matching the given UUID. | 211 //! \brief Finds the report matching the given UUID. |
114 //! | 212 //! |
115 //! The returned report is only valid if CrashReportDatabase::kNoError is | 213 //! The returned report is only valid if CrashReportDatabase::kNoError is |
116 //! returned. | 214 //! returned. |
117 //! | 215 //! |
118 //! \param[in] uuid The report identifier. | 216 //! \param[in] uuid The report identifier. |
119 //! \param[out] report_disk The found report, valid only if | 217 //! \param[out] report_disk The found report, valid only if |
120 //! CrashReportDatabase::kNoError is returned. Ownership is not | 218 //! CrashReportDatabase::kNoError is returned. Ownership is not |
121 //! transferred to the caller, and the report may not be modified. | 219 //! transferred to the caller, and the report may not be modified. |
122 OperationStatus FindSingleReport(const UUID& uuid, | 220 OperationStatus FindSingleReport(const UUID& uuid, |
123 const ReportDisk** report_disk); | 221 const ReportDisk** report_disk) const; |
124 | 222 |
125 //! \brief Finds a single report matching the given UUID and in the desired | 223 //! \brief Finds a single report matching the given UUID and in the desired |
126 //! state and calls the client-supplied mutator to modify the report. | 224 //! state, and returns a mutable ReportDisk* if found. |
127 //! | 225 //! |
128 //! The mutator object must have an operator()(ReportDisk*) which makes the | 226 //! This marks the metadata as dirty, and on destruction, changes will be |
129 //! desired changes. | 227 //! written to disk via Write(). |
130 //! | 228 //! |
131 //! \return #kNoError on success. #kReportNotFound if there was no report with | 229 //! \return #kNoError on success. #kReportNotFound if there was no report with |
132 //! the specified UUID. #kBusyError if the report was not in the specified | 230 //! the specified UUID. #kBusyError if the report was not in the specified |
133 //! state. | 231 //! state. |
134 template <class T> | 232 OperationStatus FindSingleReportAndMarkDirty(const UUID& uuid, |
135 OperationStatus MutateSingleReport(const UUID& uuid, | 233 ReportState desired_state, |
136 ReportState desired_state, | 234 ReportDisk** report_disk); |
137 const T& mutator); | |
138 | 235 |
139 private: | 236 private: |
140 static scoped_ptr<Metadata> Create(const base::FilePath& metadata_file, | |
141 const base::FilePath& report_dir); | |
142 friend class CrashReportDatabaseWin; | |
143 | |
144 Metadata(FileHandle handle, const base::FilePath& report_dir); | 237 Metadata(FileHandle handle, const base::FilePath& report_dir); |
145 | 238 |
146 bool Rewind(); | 239 bool Rewind(); |
147 | 240 |
148 void Read(); | 241 void Read(); |
149 void Write(); | 242 void Write(); |
150 | 243 |
151 //! \brief Confirms that the corresponding report actually exists on disk | 244 //! \brief Confirms that the corresponding report actually exists on disk |
152 //! (that is, the dump file has not been removed), that the report is in | 245 //! (that is, the dump file has not been removed), and that the report is |
153 //! the given state. | 246 //! in the given state. |
154 static OperationStatus VerifyReport(const ReportDisk& report_disk, | 247 static OperationStatus VerifyReport(const ReportDisk& report_disk, |
155 ReportState desired_state); | 248 ReportState desired_state); |
156 //! \brief Confirms that the corresponding report actually exists on disk | 249 //! \brief Confirms that the corresponding report actually exists on disk |
157 //! (that is, the dump file has not been removed). | 250 //! (that is, the dump file has not been removed). |
158 static OperationStatus VerifyReportAnyState(const ReportDisk& report_disk); | 251 static OperationStatus VerifyReportAnyState(const ReportDisk& report_disk); |
159 | 252 |
160 ScopedFileHandle handle_; | 253 ScopedFileHandle handle_; |
161 const base::FilePath report_dir_; | 254 const base::FilePath report_dir_; |
162 bool dirty_; //! \brief Is a Write() required on destruction? | 255 bool dirty_; //! \brief `true` when a Write() is required on destruction. |
163 std::vector<ReportDisk> reports_; | 256 std::vector<ReportDisk> reports_; |
164 | 257 |
165 DISALLOW_COPY_AND_ASSIGN(Metadata); | 258 DISALLOW_COPY_AND_ASSIGN(Metadata); |
166 }; | 259 }; |
167 | 260 |
168 Metadata::Metadata(FileHandle handle, const base::FilePath& report_dir) | |
169 : handle_(handle), report_dir_(report_dir), dirty_(false), reports_() { | |
170 } | |
171 | |
172 Metadata::~Metadata() { | 261 Metadata::~Metadata() { |
173 if (dirty_) | 262 if (dirty_) |
174 Write(); | 263 Write(); |
175 // Not actually async, UnlockFileEx requires the Offset fields. | 264 // Not actually async, UnlockFileEx requires the Offset fields. |
176 OVERLAPPED overlapped = {0}; | 265 OVERLAPPED overlapped = {0}; |
177 if (!UnlockFileEx(handle_.get(), 0, MAXDWORD, MAXDWORD, &overlapped)) | 266 if (!UnlockFileEx(handle_.get(), 0, MAXDWORD, MAXDWORD, &overlapped)) |
178 PLOG(ERROR) << "UnlockFileEx"; | 267 PLOG(ERROR) << "UnlockFileEx"; |
179 } | 268 } |
180 | 269 |
181 // The format of the metadata file is a MetadataFileHeader, followed by a | |
182 // number of fixed size records of MetadataFileReportRecord, followed by a | |
183 // string table in UTF8 format, where each string is \0 terminated. | |
184 | |
185 #pragma pack(push, 1) | |
186 | |
187 struct MetadataFileHeader { | |
188 uint32_t magic; | |
189 uint32_t version; | |
190 uint32_t num_records; | |
191 uint32_t padding; | |
192 }; | |
193 | |
194 struct MetadataFileReportRecord { | |
195 UUID uuid; // UUID is a 16 byte, standard layout structure. | |
196 uint32_t file_path_index; // Index into string table. File name is relative | |
197 // to the reports directory when on disk. | |
198 uint32_t id_index; // Index into string table. | |
199 int64_t creation_time; // Holds a time_t. | |
200 int64_t last_upload_attempt_time; // Holds a time_t. | |
201 int32_t upload_attempts; | |
202 int32_t state; // A ReportState. | |
203 uint8_t uploaded; // Boolean, 0 or 1. | |
204 uint8_t padding[7]; | |
205 }; | |
206 | |
207 const uint32_t kMetadataFileHeaderMagic = 'CPAD'; | |
208 const uint32_t kMetadataFileVersion = 1; | |
209 | |
210 #pragma pack(pop) | |
211 | |
212 // Reads from the current file position to EOF and returns as uint8_t[]. | |
213 std::string ReadRestOfFileAsString(FileHandle file) { | |
214 FileOffset read_from = LoggingSeekFile(file, 0, SEEK_CUR); | |
215 FileOffset end = LoggingSeekFile(file, 0, SEEK_END); | |
216 FileOffset original = LoggingSeekFile(file, read_from, SEEK_SET); | |
217 if (read_from == -1 || end == -1 || original == -1) | |
218 return std::string(); | |
219 DCHECK_EQ(read_from, original); | |
220 DCHECK_GE(end, read_from); | |
221 size_t data_length = static_cast<size_t>(end - read_from); | |
222 std::string buffer(data_length, '\0'); | |
223 if (!LoggingReadFile(file, &buffer[0], data_length)) | |
224 return std::string(); | |
225 return buffer; | |
226 } | |
227 | |
228 uint32_t AddStringToTable(std::string* string_table, const std::string& str) { | |
229 uint32_t offset = base::checked_cast<uint32_t>(string_table->size()); | |
230 *string_table += str; | |
231 *string_table += '\0'; | |
232 return offset; | |
233 } | |
234 | |
235 uint32_t AddStringToTable(std::string* string_table, const std::wstring& str) { | |
236 return AddStringToTable(string_table, base::UTF16ToUTF8(str)); | |
237 } | |
238 | |
239 // static | 270 // static |
240 scoped_ptr<Metadata> Metadata::Create(const base::FilePath& metadata_file, | 271 scoped_ptr<Metadata> Metadata::Create(const base::FilePath& metadata_file, |
241 const base::FilePath& report_dir) { | 272 const base::FilePath& report_dir) { |
242 // It is important that dwShareMode be non-zero so that concurrent access to | 273 // It is important that dwShareMode be non-zero so that concurrent access to |
243 // this file results in a successful open. This allows us to get to LockFileEx | 274 // this file results in a successful open. This allows us to get to LockFileEx |
244 // which then blocks to guard access. | 275 // which then blocks to guard access. |
245 FileHandle handle = CreateFile(metadata_file.value().c_str(), | 276 FileHandle handle = CreateFile(metadata_file.value().c_str(), |
246 GENERIC_READ | GENERIC_WRITE, | 277 GENERIC_READ | GENERIC_WRITE, |
247 FILE_SHARE_READ | FILE_SHARE_WRITE, | 278 FILE_SHARE_READ | FILE_SHARE_WRITE, |
248 nullptr, | 279 nullptr, |
(...skipping 16 matching lines...) Expand all Loading... | |
265 | 296 |
266 scoped_ptr<Metadata> metadata(new Metadata(handle, report_dir)); | 297 scoped_ptr<Metadata> metadata(new Metadata(handle, report_dir)); |
267 // If Read() fails, for whatever reason (corruption, etc.) metadata will not | 298 // If Read() fails, for whatever reason (corruption, etc.) metadata will not |
268 // have been modified and will be in a clean empty state. We continue on and | 299 // have been modified and will be in a clean empty state. We continue on and |
269 // return an empty database to hopefully recover. This means that existing | 300 // return an empty database to hopefully recover. This means that existing |
270 // crash reports have been orphaned. | 301 // crash reports have been orphaned. |
271 metadata->Read(); | 302 metadata->Read(); |
272 return metadata; | 303 return metadata; |
273 } | 304 } |
274 | 305 |
306 void Metadata::AddNewRecord(const ReportDisk& new_report_disk) { | |
307 DCHECK(new_report_disk.state == ReportState::kPending); | |
308 reports_.push_back(new_report_disk); | |
309 dirty_ = true; | |
310 } | |
311 | |
312 OperationStatus Metadata::FindReports( | |
313 ReportState desired_state, | |
314 std::vector<const CrashReportDatabase::Report>* reports) const { | |
315 DCHECK(reports->empty()); | |
316 for (const auto& report : reports_) { | |
317 if (report.state == desired_state && | |
318 VerifyReport(report, desired_state) == CrashReportDatabase::kNoError) { | |
319 reports->push_back(report); | |
320 } | |
321 } | |
322 return CrashReportDatabase::kNoError; | |
323 } | |
324 | |
325 OperationStatus Metadata::FindSingleReport( | |
326 const UUID& uuid, | |
327 const ReportDisk** out_report) const { | |
328 auto report_iter = std::find_if( | |
329 reports_.begin(), reports_.end(), [uuid](const ReportDisk& report) { | |
330 return report.uuid == uuid; | |
331 }); | |
332 if (report_iter == reports_.end()) | |
333 return CrashReportDatabase::kReportNotFound; | |
334 OperationStatus os = VerifyReportAnyState(*report_iter); | |
335 if (os == CrashReportDatabase::kNoError) | |
336 *out_report = &*report_iter; | |
337 return os; | |
338 } | |
339 | |
340 OperationStatus Metadata::FindSingleReportAndMarkDirty( | |
341 const UUID& uuid, | |
342 ReportState desired_state, | |
343 ReportDisk** report_disk) { | |
344 auto report_iter = std::find_if( | |
345 reports_.begin(), reports_.end(), [uuid](const ReportDisk& report) { | |
346 return report.uuid == uuid; | |
347 }); | |
348 if (report_iter == reports_.end()) | |
349 return CrashReportDatabase::kReportNotFound; | |
350 OperationStatus os = VerifyReport(*report_iter, desired_state); | |
351 if (os == CrashReportDatabase::kNoError) { | |
352 dirty_ = true; | |
353 *report_disk = &*report_iter; | |
354 } | |
355 return os; | |
356 } | |
357 | |
358 Metadata::Metadata(FileHandle handle, const base::FilePath& report_dir) | |
359 : handle_(handle), report_dir_(report_dir), dirty_(false), reports_() { | |
360 } | |
361 | |
275 bool Metadata::Rewind() { | 362 bool Metadata::Rewind() { |
276 FileOffset result = LoggingSeekFile(handle_.get(), 0, SEEK_SET); | 363 FileOffset result = LoggingSeekFile(handle_.get(), 0, SEEK_SET); |
277 DCHECK_EQ(result, 0); | 364 DCHECK_EQ(result, 0); |
278 return result == 0; | 365 return result == 0; |
279 } | 366 } |
280 | 367 |
281 void Metadata::Read() { | 368 void Metadata::Read() { |
282 FileOffset length = LoggingSeekFile(handle_.get(), 0, SEEK_END); | 369 FileOffset length = LoggingSeekFile(handle_.get(), 0, SEEK_END); |
283 if (length <= 0) // Failed, or empty: Abort. | 370 if (length <= 0) // Failed, or empty: Abort. |
284 return; | 371 return; |
285 if (!Rewind()) { | 372 if (!Rewind()) { |
286 LOG(ERROR) << "failed to rewind to read"; | 373 LOG(ERROR) << "failed to rewind to read"; |
287 return; | 374 return; |
288 } | 375 } |
289 | 376 |
290 MetadataFileHeader header; | 377 MetadataFileHeader header; |
291 if (!LoggingReadFile(handle_.get(), &header, sizeof(header))) { | 378 if (!LoggingReadFile(handle_.get(), &header, sizeof(header))) { |
292 LOG(ERROR) << "failed to read header"; | 379 LOG(ERROR) << "failed to read header"; |
293 return; | 380 return; |
294 } | 381 } |
295 if (header.magic != kMetadataFileHeaderMagic || | 382 if (header.magic != kMetadataFileHeaderMagic || |
296 header.version != kMetadataFileVersion) { | 383 header.version != kMetadataFileVersion) { |
297 LOG(ERROR) << "unexpected header"; | 384 LOG(ERROR) << "unexpected header"; |
298 return; | 385 return; |
299 } | 386 } |
300 | 387 |
301 auto records_size = base::CheckedNumeric<uint32_t>(header.num_records) * | 388 base::CheckedNumeric<uint32_t> records_size = |
302 sizeof(MetadataFileReportRecord); | 389 base::CheckedNumeric<uint32_t>(header.num_records) * |
390 sizeof(MetadataFileReportRecord); | |
303 if (!records_size.IsValid()) { | 391 if (!records_size.IsValid()) { |
304 LOG(ERROR) << "record size out of range"; | 392 LOG(ERROR) << "record size out of range"; |
305 return; | 393 return; |
306 } | 394 } |
307 | 395 |
308 scoped_ptr<MetadataFileReportRecord[]> records( | 396 std::vector<MetadataFileReportRecord> records(header.num_records); |
309 new MetadataFileReportRecord[header.num_records]); | 397 if (!LoggingReadFile(handle_.get(), &records[0], records_size.ValueOrDie())) { |
310 if (!LoggingReadFile( | |
311 handle_.get(), records.get(), records_size.ValueOrDie())) { | |
312 LOG(ERROR) << "failed to read records"; | 398 LOG(ERROR) << "failed to read records"; |
313 return; | 399 return; |
314 } | 400 } |
315 | 401 |
316 std::string string_table = ReadRestOfFileAsString(handle_.get()); | 402 std::string string_table = ReadRestOfFileAsString(handle_.get()); |
317 if (string_table.empty() || string_table.back() != '\0') { | 403 if (string_table.empty() || string_table.back() != '\0') { |
318 LOG(ERROR) << "bad string table"; | 404 LOG(ERROR) << "bad string table"; |
319 return; | 405 return; |
320 } | 406 } |
321 for (uint32_t i = 0; i < header.num_records; ++i) { | 407 |
322 ReportDisk r; | 408 std::vector<ReportDisk> reports; |
323 const MetadataFileReportRecord* record = &records[i]; | 409 for (const auto& record : records) { |
324 r.uuid = record->uuid; | 410 if (record.file_path_index >= string_table.size() || |
325 if (record->file_path_index >= string_table.size() || | 411 record.id_index >= string_table.size()) { |
326 record->id_index >= string_table.size()) { | |
327 reports_.clear(); | |
328 LOG(ERROR) << "invalid string table index"; | 412 LOG(ERROR) << "invalid string table index"; |
329 return; | 413 return; |
330 } | 414 } |
331 r.file_path = report_dir_.Append( | 415 reports.push_back(ReportDisk(record, report_dir_, string_table)); |
332 base::UTF8ToUTF16(&string_table[record->file_path_index])); | |
333 r.id = &string_table[record->id_index]; | |
334 r.creation_time = record->creation_time; | |
335 r.uploaded = record->uploaded; | |
336 r.last_upload_attempt_time = record->last_upload_attempt_time; | |
337 r.upload_attempts = record->upload_attempts; | |
338 r.state = static_cast<ReportState>(record->state); | |
339 reports_.push_back(r); | |
340 } | 416 } |
417 reports_.swap(reports); | |
341 } | 418 } |
342 | 419 |
343 void Metadata::Write() { | 420 void Metadata::Write() { |
344 if (!Rewind()) { | 421 if (!Rewind()) { |
345 LOG(ERROR) << "failed to rewind to write"; | 422 LOG(ERROR) << "failed to rewind to write"; |
346 return; | 423 return; |
347 } | 424 } |
348 | 425 |
349 // Truncate to ensure that a partial write doesn't cause a mix of old and new | 426 // Truncate to ensure that a partial write doesn't cause a mix of old and new |
350 // data causing an incorrect interpretation on read. | 427 // data causing an incorrect interpretation on read. |
351 if (!SetEndOfFile(handle_.get())) { | 428 if (!SetEndOfFile(handle_.get())) { |
352 PLOG(ERROR) << "failed to truncate"; | 429 PLOG(ERROR) << "failed to truncate"; |
353 return; | 430 return; |
354 } | 431 } |
355 | 432 |
356 size_t num_records = reports_.size(); | 433 size_t num_records = reports_.size(); |
357 | 434 |
358 // Fill and write out the header. | 435 // Fill and write out the header. |
359 MetadataFileHeader header = {0}; | 436 MetadataFileHeader header = {0}; |
360 header.magic = kMetadataFileHeaderMagic; | 437 header.magic = kMetadataFileHeaderMagic; |
361 header.version = kMetadataFileVersion; | 438 header.version = kMetadataFileVersion; |
362 header.num_records = base::checked_cast<uint32_t>(num_records); | 439 header.num_records = base::checked_cast<uint32_t>(num_records); |
363 if (!LoggingWriteFile(handle_.get(), &header, sizeof(header))) { | 440 if (!LoggingWriteFile(handle_.get(), &header, sizeof(header))) { |
364 LOG(ERROR) << "failed to write header"; | 441 LOG(ERROR) << "failed to write header"; |
365 return; | 442 return; |
366 } | 443 } |
367 | 444 |
368 // Build the records and string table we're going to write. | 445 // Build the records and string table we're going to write. |
369 std::string string_table; | 446 std::string string_table; |
370 scoped_ptr<MetadataFileReportRecord[]> records( | 447 std::vector<MetadataFileReportRecord> records; |
371 new MetadataFileReportRecord[num_records]); | 448 records.reserve(num_records); |
372 memset(records.get(), 0, sizeof(MetadataFileReportRecord) * num_records); | 449 for (const auto& report : reports_) { |
373 for (size_t i = 0; i < num_records; ++i) { | |
374 const ReportDisk& report = reports_[i]; | |
375 MetadataFileReportRecord& record = records[i]; | |
376 record.uuid = report.uuid; | |
377 const base::FilePath& path = report.file_path; | 450 const base::FilePath& path = report.file_path; |
378 if (path.DirName() != report_dir_) { | 451 if (path.DirName() != report_dir_) { |
379 LOG(ERROR) << path.value().c_str() << " expected to start with " | 452 LOG(ERROR) << path.value().c_str() << " expected to start with " |
380 << report_dir_.value().c_str(); | 453 << report_dir_.value().c_str(); |
381 return; | 454 return; |
382 } | 455 } |
383 record.file_path_index = | 456 records.push_back(MetadataFileReportRecord(report, &string_table)); |
384 AddStringToTable(&string_table, path.BaseName().value().c_str()); | |
385 record.id_index = AddStringToTable(&string_table, report.id); | |
386 record.creation_time = report.creation_time; | |
387 record.uploaded = report.uploaded; | |
388 record.last_upload_attempt_time = report.last_upload_attempt_time; | |
389 record.upload_attempts = report.upload_attempts; | |
390 record.state = static_cast<uint32_t>(report.state); | |
391 } | 457 } |
392 | 458 |
393 if (!LoggingWriteFile(handle_.get(), | 459 if (!LoggingWriteFile(handle_.get(), |
394 records.get(), | 460 &records[0], |
395 num_records * sizeof(MetadataFileReportRecord))) { | 461 records.size() * sizeof(MetadataFileReportRecord))) { |
396 LOG(ERROR) << "failed to write records"; | 462 LOG(ERROR) << "failed to write records"; |
397 return; | 463 return; |
398 } | 464 } |
399 if (!LoggingWriteFile( | 465 if (!LoggingWriteFile( |
400 handle_.get(), string_table.c_str(), string_table.size())) { | 466 handle_.get(), string_table.c_str(), string_table.size())) { |
401 LOG(ERROR) << "failed to write string table"; | 467 LOG(ERROR) << "failed to write string table"; |
402 return; | 468 return; |
403 } | 469 } |
404 } | 470 } |
405 | 471 |
406 void Metadata::AddNewRecord(const ReportDisk& new_report_disk) { | |
407 DCHECK(new_report_disk.state == ReportState::kPending); | |
408 reports_.push_back(new_report_disk); | |
409 dirty_ = true; | |
410 } | |
411 | |
412 OperationStatus Metadata::FindReports( | |
413 ReportState desired_state, | |
414 std::vector<const CrashReportDatabase::Report>* reports) { | |
415 DCHECK(reports->empty()); | |
416 for (const auto& report : reports_) { | |
417 if (report.state == desired_state) { | |
418 if (VerifyReport(report, desired_state) != CrashReportDatabase::kNoError) | |
419 continue; | |
420 reports->push_back(report); | |
421 } | |
422 } | |
423 return CrashReportDatabase::kNoError; | |
424 } | |
425 | |
426 OperationStatus Metadata::FindSingleReport(const UUID& uuid, | |
427 const ReportDisk** out_report) { | |
428 for (size_t i = 0; i < reports_.size(); ++i) { | |
429 if (reports_[i].uuid == uuid) { | |
430 OperationStatus os = VerifyReportAnyState(reports_[i]); | |
431 if (os != CrashReportDatabase::kNoError) | |
432 return os; | |
433 *out_report = &reports_[i]; | |
434 return CrashReportDatabase::kNoError; | |
435 } | |
436 } | |
437 return CrashReportDatabase::kReportNotFound; | |
438 } | |
439 | |
440 template <class T> | |
441 OperationStatus Metadata::MutateSingleReport( | |
442 const UUID& uuid, | |
443 ReportState desired_state, | |
444 const T& mutator) { | |
445 for (size_t i = 0; i < reports_.size(); ++i) { | |
446 if (reports_[i].uuid == uuid) { | |
447 OperationStatus os = VerifyReport(reports_[i], desired_state); | |
448 if (os != CrashReportDatabase::kNoError) | |
449 return os; | |
450 mutator(&reports_[i]); | |
451 dirty_ = true; | |
452 return CrashReportDatabase::kNoError; | |
453 } | |
454 } | |
455 return CrashReportDatabase::kReportNotFound; | |
456 } | |
457 | |
458 // static | 472 // static |
459 OperationStatus Metadata::VerifyReportAnyState(const ReportDisk& report_disk) { | 473 OperationStatus Metadata::VerifyReportAnyState(const ReportDisk& report_disk) { |
460 DWORD fileattr = GetFileAttributes(report_disk.file_path.value().c_str()); | 474 DWORD fileattr = GetFileAttributes(report_disk.file_path.value().c_str()); |
461 if (fileattr == INVALID_FILE_ATTRIBUTES) | 475 if (fileattr == INVALID_FILE_ATTRIBUTES) |
462 return CrashReportDatabase::kReportNotFound; | 476 return CrashReportDatabase::kReportNotFound; |
463 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | 477 return (fileattr & FILE_ATTRIBUTE_DIRECTORY) |
464 return CrashReportDatabase::kFileSystemError; | 478 ? CrashReportDatabase::kFileSystemError |
465 return CrashReportDatabase::kNoError; | 479 : CrashReportDatabase::kNoError; |
466 } | 480 } |
467 | 481 |
468 // static | 482 // static |
469 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, | 483 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, |
470 ReportState desired_state) { | 484 ReportState desired_state) { |
471 if (report_disk.state != desired_state) | 485 return (report_disk.state == desired_state) |
472 return CrashReportDatabase::kBusyError; | 486 ? VerifyReportAnyState(report_disk) |
473 return VerifyReportAnyState(report_disk); | 487 : CrashReportDatabase::kBusyError; |
474 } | 488 } |
475 | 489 |
490 //! \brief Ensures that the node at path is a directory, and creates it if it | |
491 //! does not exist. | |
492 //! | |
493 //! \return If the path points to a file, rather than a directory, or the | |
494 //! directory could not be created, returns `false`. Otherwise, returns | |
495 //! `true`, indicating that path already was or now is a directory. | |
496 bool CreateDirectoryIfNecessary(const base::FilePath& path) { | |
497 if (CreateDirectory(path.value().c_str(), nullptr)) | |
498 return true; | |
499 if (GetLastError() != ERROR_ALREADY_EXISTS) { | |
500 PLOG(ERROR) << "CreateDirectory"; | |
501 return false; | |
502 } | |
503 DWORD fileattr = GetFileAttributes(path.value().c_str()); | |
504 if (fileattr == INVALID_FILE_ATTRIBUTES) { | |
505 PLOG(ERROR) << "GetFileAttributes"; | |
506 return false; | |
507 } | |
508 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | |
509 return true; | |
510 LOG(ERROR) << "not a directory"; | |
511 return false; | |
512 } | |
513 | |
514 // CrashReportDatabaseWin ------------------------------------------------------ | |
515 | |
476 class CrashReportDatabaseWin : public CrashReportDatabase { | 516 class CrashReportDatabaseWin : public CrashReportDatabase { |
477 public: | 517 public: |
478 explicit CrashReportDatabaseWin(const base::FilePath& path); | 518 explicit CrashReportDatabaseWin(const base::FilePath& path); |
479 ~CrashReportDatabaseWin() override; | 519 ~CrashReportDatabaseWin() override; |
480 | 520 |
481 bool Initialize(); | 521 bool Initialize(); |
482 | 522 |
483 // CrashReportDatabase: | 523 // CrashReportDatabase: |
484 OperationStatus PrepareNewCrashReport(NewReport** report) override; | 524 OperationStatus PrepareNewCrashReport(NewReport** report) override; |
485 OperationStatus FinishedWritingCrashReport(NewReport* report, | 525 OperationStatus FinishedWritingCrashReport(NewReport* report, |
(...skipping 20 matching lines...) Expand all Loading... | |
506 }; | 546 }; |
507 | 547 |
508 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) | 548 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) |
509 : CrashReportDatabase(), base_dir_(path) { | 549 : CrashReportDatabase(), base_dir_(path) { |
510 } | 550 } |
511 | 551 |
512 CrashReportDatabaseWin::~CrashReportDatabaseWin() { | 552 CrashReportDatabaseWin::~CrashReportDatabaseWin() { |
513 } | 553 } |
514 | 554 |
515 bool CrashReportDatabaseWin::Initialize() { | 555 bool CrashReportDatabaseWin::Initialize() { |
516 // Check if the database already exists. | 556 // Ensure the database and report subdirectories exist. |
517 if (!CreateOrEnsureDirectoryExists(base_dir_)) | 557 if (!CreateDirectoryIfNecessary(base_dir_) || |
518 return false; | 558 !CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory))) |
519 | |
520 // Create our reports subdirectory. | |
521 if (!CreateOrEnsureDirectoryExists(base_dir_.Append(kReportsDirectory))) | |
522 return false; | 559 return false; |
523 | 560 |
524 // TODO(scottmg): When are completed reports pruned from disk? Delete here or | 561 // TODO(scottmg): When are completed reports pruned from disk? Delete here or |
525 // maybe on AcquireMetadata(). | 562 // maybe on AcquireMetadata(). |
526 | 563 |
527 return true; | 564 return true; |
528 } | 565 } |
529 | 566 |
530 OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( | 567 OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( |
531 NewReport** out_report) { | 568 NewReport** report) { |
532 scoped_ptr<NewReportDisk> report(new NewReportDisk()); | |
533 | |
534 ::UUID system_uuid; | 569 ::UUID system_uuid; |
535 if (UuidCreate(&system_uuid) != RPC_S_OK) { | 570 if (UuidCreate(&system_uuid) != RPC_S_OK) |
536 return kFileSystemError; | 571 return kFileSystemError; |
537 } | |
538 static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); | 572 static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); |
539 static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); | 573 static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); |
540 UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); | 574 UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); |
541 | 575 |
542 report->uuid = uuid; | 576 scoped_ptr<NewReportDisk> new_report(new NewReportDisk()); |
543 report->path = | 577 new_report->uuid = uuid; |
578 new_report->path = | |
544 base_dir_.Append(kReportsDirectory) | 579 base_dir_.Append(kReportsDirectory) |
545 .Append(uuid.ToWideString() + L"." + kCrashReportFileExtension); | 580 .Append(uuid.ToString16() + L"." + kCrashReportFileExtension); |
546 report->handle = LoggingOpenFileForWrite( | 581 new_report->handle = LoggingOpenFileForWrite(new_report->path, |
547 report->path, FileWriteMode::kCreateOrFail, FilePermissions::kOwnerOnly); | 582 FileWriteMode::kCreateOrFail, |
548 if (report->handle == INVALID_HANDLE_VALUE) | 583 FilePermissions::kOwnerOnly); |
584 if (new_report->handle == INVALID_HANDLE_VALUE) | |
549 return kFileSystemError; | 585 return kFileSystemError; |
550 | 586 |
551 *out_report = report.release(); | 587 *report = new_report.release(); |
552 return kNoError; | 588 return kNoError; |
553 } | 589 } |
554 | 590 |
555 OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( | 591 OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( |
556 NewReport* report, | 592 NewReport* report, |
557 UUID* uuid) { | 593 UUID* uuid) { |
558 // Take ownership of the report, and cast to our private version with UUID. | 594 // Take ownership of the report, and cast to our private version with UUID. |
559 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | 595 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); |
560 // Take ownership of the file handle. | 596 // Take ownership of the file handle. |
561 ScopedFileHandle handle(report->handle); | 597 ScopedFileHandle handle(report->handle); |
562 | 598 |
563 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 599 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
564 if (!metadata) | 600 if (!metadata) |
565 return kDatabaseError; | 601 return kDatabaseError; |
566 ReportDisk report_disk; | 602 metadata->AddNewRecord(ReportDisk(scoped_report->uuid, |
567 report_disk.uuid = scoped_report->uuid; | 603 scoped_report->path, |
568 report_disk.file_path = scoped_report->path; | 604 time(nullptr), |
569 report_disk.creation_time = time(nullptr); | 605 ReportState::kPending)); |
570 report_disk.state = ReportState::kPending; | 606 *uuid = scoped_report->uuid; |
571 metadata->AddNewRecord(report_disk); | |
572 *uuid = report_disk.uuid; | |
573 return kNoError; | 607 return kNoError; |
574 } | 608 } |
575 | 609 |
576 OperationStatus CrashReportDatabaseWin::ErrorWritingCrashReport( | 610 OperationStatus CrashReportDatabaseWin::ErrorWritingCrashReport( |
577 NewReport* report) { | 611 NewReport* report) { |
578 // Take ownership of the report, and cast to our private version with UUID. | 612 // Take ownership of the report, and cast to our private version with UUID. |
579 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | 613 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); |
580 | 614 |
581 // Close the outstanding handle. | 615 // Close the outstanding handle. |
582 LoggingCloseFile(report->handle); | 616 LoggingCloseFile(report->handle); |
583 | 617 |
584 // We failed to write, so remove the dump file. There's no entry in the | 618 // We failed to write, so remove the dump file. There's no entry in the |
585 // metadata table yet. | 619 // metadata table yet. |
586 if (!DeleteFile(scoped_report->path.value().c_str())) { | 620 if (!DeleteFile(scoped_report->path.value().c_str())) { |
587 PLOG(ERROR) << "DeleteFile " << scoped_report->path.value().c_str(); | 621 PLOG(ERROR) << "DeleteFile " << scoped_report->path.value().c_str(); |
588 return CrashReportDatabase::kFileSystemError; | 622 return CrashReportDatabase::kFileSystemError; |
589 } | 623 } |
590 | 624 |
591 return kNoError; | 625 return kNoError; |
592 } | 626 } |
593 | 627 |
594 OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, | 628 OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, |
595 Report* report) { | 629 Report* report) { |
596 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 630 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
597 if (!metadata) | 631 if (!metadata) |
598 return kDatabaseError; | 632 return kDatabaseError; |
599 // Find and return a copy of the matching report. | 633 // Find and return a copy of the matching report. |
600 const ReportDisk* report_disk; | 634 const ReportDisk* report_disk; |
601 OperationStatus os = metadata->FindSingleReport(uuid, &report_disk); | 635 OperationStatus os = metadata->FindSingleReport(uuid, &report_disk); |
602 if (os != kNoError) | 636 if (os == kNoError) |
603 return os; | 637 *report = *report_disk; |
604 *report = *report_disk; | 638 return os; |
605 return kNoError; | |
606 } | 639 } |
607 | 640 |
608 OperationStatus CrashReportDatabaseWin::GetPendingReports( | 641 OperationStatus CrashReportDatabaseWin::GetPendingReports( |
609 std::vector<const Report>* reports) { | 642 std::vector<const Report>* reports) { |
610 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 643 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
611 if (!metadata) | 644 return metadata ? metadata->FindReports(ReportState::kPending, reports) |
612 return kDatabaseError; | 645 : kDatabaseError; |
613 return metadata->FindReports(ReportState::kPending, reports); | |
614 } | 646 } |
615 | 647 |
616 OperationStatus CrashReportDatabaseWin::GetCompletedReports( | 648 OperationStatus CrashReportDatabaseWin::GetCompletedReports( |
617 std::vector<const Report>* reports) { | 649 std::vector<const Report>* reports) { |
618 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 650 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
619 if (!metadata) | 651 return metadata ? metadata->FindReports(ReportState::kCompleted, reports) |
620 return kDatabaseError; | 652 : kDatabaseError; |
621 return metadata->FindReports(ReportState::kCompleted, reports); | |
622 } | 653 } |
623 | 654 |
624 OperationStatus CrashReportDatabaseWin::GetReportForUploading( | 655 OperationStatus CrashReportDatabaseWin::GetReportForUploading( |
625 const UUID& uuid, | 656 const UUID& uuid, |
626 const Report** report) { | 657 const Report** report) { |
627 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 658 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
628 if (!metadata) | 659 if (!metadata) |
629 return kDatabaseError; | 660 return kDatabaseError; |
630 // TODO(scottmg): After returning this report to the client, there is no way | 661 // TODO(scottmg): After returning this report to the client, there is no way |
631 // to reap this report if the uploader fails to call RecordUploadAttempt() or | 662 // to reap this report if the uploader fails to call RecordUploadAttempt() or |
632 // SkipReportUpload() (if it crashed or was otherwise buggy). To resolve this, | 663 // SkipReportUpload() (if it crashed or was otherwise buggy). To resolve this, |
633 // one possibility would be to change the interface to be FileHandle based, so | 664 // one possibility would be to change the interface to be FileHandle based, so |
634 // that instead of giving the file_path back to the client and changing state | 665 // that instead of giving the file_path back to the client and changing state |
635 // to kUploading, we return an exclusive access handle, and use that as the | 666 // to kUploading, we return an exclusive access handle, and use that as the |
636 // signal that the upload is pending, rather than an update to state in the | 667 // signal that the upload is pending, rather than an update to state in the |
637 // metadata. Alternatively, there could be a "garbage collection" at startup | 668 // metadata. Alternatively, there could be a "garbage collection" at startup |
638 // where any reports that are orphaned in the kUploading state are either | 669 // where any reports that are orphaned in the kUploading state are either |
639 // reset to kPending to retry, or discarded. | 670 // reset to kPending to retry, or discarded. |
640 return metadata->MutateSingleReport( | 671 ReportDisk* report_disk; |
641 uuid, ReportState::kPending, [report](ReportDisk* report_disk) { | 672 OperationStatus os = metadata->FindSingleReportAndMarkDirty( |
642 report_disk->state = ReportState::kUploading; | 673 uuid, ReportState::kPending, &report_disk); |
643 // Create a copy for passing back to client. This will be freed in | 674 if (os == CrashReportDatabase::kNoError) { |
644 // RecordUploadAttempt. | 675 report_disk->state = ReportState::kUploading; |
645 *report = new Report(*report_disk); | 676 // Create a copy for passing back to client. This will be freed in |
646 }); | 677 // RecordUploadAttempt. |
678 *report = new Report(*report_disk); | |
679 } | |
680 return os; | |
647 } | 681 } |
648 | 682 |
649 OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( | 683 OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( |
650 const Report* report, | 684 const Report* report, |
651 bool successful, | 685 bool successful, |
652 const std::string& id) { | 686 const std::string& id) { |
653 // Take ownership, allocated in GetReportForUploading. | 687 // Take ownership, allocated in GetReportForUploading. |
654 scoped_ptr<const Report> upload_report(report); | 688 scoped_ptr<const Report> upload_report(report); |
655 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 689 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
656 if (!metadata) | 690 if (!metadata) |
657 return kDatabaseError; | 691 return kDatabaseError; |
658 return metadata->MutateSingleReport( | 692 ReportDisk* report_disk; |
659 report->uuid, | 693 OperationStatus os = metadata->FindSingleReportAndMarkDirty( |
660 ReportState::kUploading, | 694 report->uuid, ReportState::kUploading, &report_disk); |
661 [successful, id](ReportDisk* report_disk) { | 695 if (os == CrashReportDatabaseWin::kNoError) { |
662 report_disk->uploaded = successful; | 696 report_disk->uploaded = successful; |
663 report_disk->id = id; | 697 report_disk->id = id; |
664 report_disk->last_upload_attempt_time = time(nullptr); | 698 report_disk->last_upload_attempt_time = time(nullptr); |
665 report_disk->upload_attempts++; | 699 report_disk->upload_attempts++; |
666 report_disk->state = | 700 report_disk->state = |
667 successful ? ReportState::kCompleted : ReportState::kPending; | 701 successful ? ReportState::kCompleted : ReportState::kPending; |
668 }); | 702 } |
703 return os; | |
669 } | 704 } |
670 | 705 |
671 OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { | 706 OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { |
672 scoped_ptr<Metadata> metadata(AcquireMetadata()); | 707 scoped_ptr<Metadata> metadata(AcquireMetadata()); |
673 if (!metadata) | 708 if (!metadata) |
674 return kDatabaseError; | 709 return kDatabaseError; |
675 return metadata->MutateSingleReport( | 710 ReportDisk* report_disk; |
676 uuid, ReportState::kPending, [](ReportDisk* report_disk) { | 711 OperationStatus os = metadata->FindSingleReportAndMarkDirty( |
677 report_disk->state = ReportState::kCompleted; | 712 uuid, ReportState::kPending, &report_disk); |
678 }); | 713 if (os == CrashReportDatabase::kNoError) |
714 report_disk->state = ReportState::kCompleted; | |
715 return os; | |
679 } | 716 } |
680 | 717 |
681 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { | 718 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { |
682 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); | 719 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); |
683 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); | 720 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); |
684 } | 721 } |
685 | 722 |
686 } // namespace | 723 } // namespace |
687 | 724 |
688 // static | 725 // static |
689 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( | 726 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( |
690 const base::FilePath& path) { | 727 const base::FilePath& path) { |
691 scoped_ptr<CrashReportDatabaseWin> database_win( | 728 scoped_ptr<CrashReportDatabaseWin> database_win( |
692 new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); | 729 new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); |
693 if (!database_win->Initialize()) | 730 return database_win->Initialize() ? database_win.Pass() |
694 database_win.reset(); | 731 : scoped_ptr<CrashReportDatabaseWin>(); |
695 | |
696 return scoped_ptr<CrashReportDatabase>(database_win.release()); | |
697 } | 732 } |
698 | 733 |
699 } // namespace crashpad | 734 } // namespace crashpad |
OLD | NEW |