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/crash_report_database.h" | |
16 | |
17 #include <rpc.h> | |
18 #include <time.h> | |
19 #include <windows.h> | |
20 | |
21 #include <functional> | |
22 #include <limits> | |
23 | |
24 #include "base/logging.h" | |
25 #include "base/numerics/safe_math.h" | |
26 #include "base/strings/stringprintf.h" | |
27 #include "base/strings/utf_string_conversions.h" | |
28 | |
29 namespace crashpad { | |
30 | |
31 namespace { | |
32 | |
33 const wchar_t kDatabaseDirectoryName[] = L"Crashpad"; | |
34 | |
35 const wchar_t kReportsDirectory[] = L"reports"; | |
36 const wchar_t kMetadataFileName[] = L"metadata"; | |
37 | |
38 const wchar_t kCrashReportFileExtension[] = L"dmp"; | |
39 | |
40 enum class ReportState : int { | |
41 //! \brief Created and filled out by caller, owned by database. | |
42 kPending, | |
43 //! \brief In the process of uploading, owned by caller. | |
44 kUploading, | |
45 //! \brief Upload completed or skipped, owned by database. | |
46 kCompleted, | |
47 }; | |
48 | |
49 using OperationStatus = CrashReportDatabase::OperationStatus; | |
50 | |
51 //! \brief Ensures that the node at path is a directory, and creates it if it | |
52 //! does not exist. | |
53 //! | |
54 //! \return If the path points to a file, rather than a directory, or the | |
55 //! directory could not be created, returns false. Otherwise, returns true, | |
56 //! indicating that path already was or now is a directory. | |
57 bool CreateOrEnsureDirectoryExists(const base::FilePath& path) { | |
58 if (CreateDirectory(path.value().c_str(), nullptr)) { | |
59 return true; | |
60 } else if (GetLastError() == ERROR_ALREADY_EXISTS) { | |
61 DWORD fileattr = GetFileAttributes(path.value().c_str()); | |
62 if (fileattr == INVALID_FILE_ATTRIBUTES) { | |
63 PLOG(ERROR) << "GetFileAttributes"; | |
64 return false; | |
65 } | |
66 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | |
67 return true; | |
68 LOG(ERROR) << "not a directory"; | |
69 return false; | |
70 } else { | |
71 PLOG(ERROR) << "CreateDirectory"; | |
72 return false; | |
73 } | |
74 } | |
75 | |
76 //! \brief A private extension of the Report class that includes additional data | |
77 //! that's stored on disk in the metadata file. | |
78 struct ReportDisk : public CrashReportDatabase::Report { | |
79 //! \brief The current state of the report. | |
80 ReportState state; | |
81 }; | |
82 | |
83 //! \brief A private extension of the NewReport class to hold the UUID during | |
84 //! initial write. We don't store metadata in dump's file attributes, and | |
85 //! use the UUID to identify the dump on write completion. | |
86 struct NewReportDisk : public CrashReportDatabase::NewReport { | |
87 //! \brief The UUID for this crash report. | |
88 UUID uuid; | |
89 }; | |
90 | |
91 //! \brief Manages the metadata for the set of reports, handling serialization | |
92 //! to disk, and queries. Instances of this class should be created by using | |
93 //! CrashReportDatabaseWin::AcquireMetadata(). | |
94 class Metadata { | |
95 public: | |
96 //! \brief Writes any changes if necessary, unlocks and closes the file | |
97 //! handle. | |
98 ~Metadata(); | |
99 | |
100 //! \brief Adds a new report to the set. | |
101 //! | |
102 //! \param[in] new_report_disk The record to add. The #state field must be set | |
103 //! to kPending. | |
104 void AddNewRecord(const ReportDisk& new_report_disk); | |
105 | |
106 //! \brief Finds all reports in a given state. The \a reports vector is only | |
107 //! valid when CrashReportDatabase::kNoError is returned. | |
108 //! | |
109 //! \param[in] desired_state The state to match. | |
110 //! \param[out] reports Matching reports, must be empty on entry. | |
111 OperationStatus FindReports( | |
112 ReportState desired_state, | |
113 std::vector<const CrashReportDatabase::Report>* reports); | |
114 | |
115 //! \brief Finds the report matching the given UUID. | |
116 //! | |
117 //! The returned report is only valid if CrashReportDatabase::kNoError is | |
118 //! returned. | |
119 //! | |
120 //! \param[in] uuid The report identifier. | |
121 //! \param[out] report_disk The found report, valid only if | |
122 //! CrashReportDatabase::kNoError is returned. Ownership is not | |
123 //! transferred to the caller, and the report may not be modified. | |
124 OperationStatus FindSingleReport(const UUID& uuid, | |
125 const ReportDisk** report_disk); | |
126 | |
127 //! \brief Finds a single report matching the given UUID and in the desired | |
128 //! state and calls the client-supplied mutator to modify the report. | |
129 //! | |
130 //! \return #kNoError on success. #kReportNotFound if there was no report with | |
131 //! the specified UUID. #kBusyError if the report was not in the specified | |
132 //! state. | |
133 OperationStatus MutateSingleReport(const UUID& uuid, | |
134 ReportState desired_state, | |
135 std::function<void(ReportDisk*)> mutator); | |
Robert Sesek
2015/02/10 20:32:26
Does this require C++11 library support? Is that O
scottmg
2015/02/10 20:58:45
Sigh, apparently. Done.
| |
136 | |
137 private: | |
138 Metadata(FileHandle handle, const base::FilePath& report_dir); | |
139 | |
140 static scoped_ptr<Metadata> Create(const base::FilePath& metadata_file, | |
Robert Sesek
2015/02/10 20:32:26
Move Create and the friend to protected: so that i
scottmg
2015/02/10 20:58:45
Sorry, I'm not sure what you mean. (Create calls t
Robert Sesek
2015/02/10 22:39:35
Sorry, yes "it" was vague there. The CrashReportDa
scottmg
2015/02/10 22:47:33
Ah, I see. Done.
| |
141 const base::FilePath& report_dir); | |
142 friend class CrashReportDatabaseWin; | |
143 | |
144 void Read(); | |
145 void Write(); | |
146 | |
147 //! \brief Confirms that the corresponding report actually exists on disk | |
148 //! (that is, the dump file has not been removed), that the report is in | |
149 //! the given state. | |
150 static OperationStatus VerifyReport(const ReportDisk& report_disk, | |
151 ReportState desired_state); | |
152 //! \brief Confirms that the corresponding report actually exists on disk | |
153 //! (that is, the dump file has not been removed). | |
154 static OperationStatus VerifyReportAnyState(const ReportDisk& report_disk); | |
155 | |
156 ScopedFileHandle handle_; | |
157 const base::FilePath report_dir_; | |
158 bool dirty_; //! \brief Is a Write() required on destruction? | |
159 std::vector<ReportDisk> reports_; | |
160 | |
161 DISALLOW_COPY_AND_ASSIGN(Metadata); | |
162 }; | |
163 | |
164 Metadata::Metadata(FileHandle handle, const base::FilePath& report_dir) | |
165 : handle_(handle), report_dir_(report_dir), dirty_(false), reports_() { | |
166 } | |
167 | |
168 Metadata::~Metadata() { | |
169 if (dirty_) | |
170 Write(); | |
171 OVERLAPPED overlapped = {0}; | |
172 if (!UnlockFileEx(handle_.get(), 0, MAXDWORD, MAXDWORD, &overlapped)) | |
173 PLOG(ERROR) << "UnlockFileEx"; | |
174 } | |
175 | |
176 // The format of the metadata file is a MetadataFileHeader, followed by a | |
177 // number of fixed size records of MetadataFileReportRecord, followed by a | |
178 // string table in UTF8 format, where each string is \0 terminated. | |
179 | |
180 #pragma pack(push, 1) | |
181 | |
182 struct MetadataFileHeader { | |
183 uint32_t magic; | |
184 uint32_t version; | |
185 uint32_t num_records; | |
186 }; | |
187 | |
188 struct MetadataFileReportRecord { | |
189 UUID uuid; // UUID is a 16 byte, standard layout structure. | |
190 uint32_t file_path_index; // Index into string table. File name is relative | |
191 // to the reports directory when on disk. | |
192 uint32_t id_index; // Index into string table. | |
193 int64_t creation_time; // Holds a time_t. | |
194 int64_t last_upload_attempt_time; // Holds a time_t. | |
195 int32_t upload_attempts; | |
196 int32_t state; // A ReportState. | |
197 uint8_t uploaded; // Boolean, 0 or 1. | |
198 }; | |
199 | |
200 const uint32_t kMetadataFileHeaderMagic = 'CPAD'; | |
201 const uint32_t kMetadataFileVersion = 1; | |
202 | |
203 #pragma pack(pop) | |
204 | |
205 // Reads from the current file position to EOF and returns as uint8_t[]. | |
206 scoped_ptr<uint8_t[]> ReadRestOfFile(FileHandle file) { | |
207 FileOffset read_from = LoggingSeekFile(file, 0, SEEK_CUR); | |
208 FileOffset end = LoggingSeekFile(file, 0, SEEK_END); | |
209 FileOffset original = LoggingSeekFile(file, read_from, SEEK_SET); | |
210 if (read_from == -1 || end == -1 || original == -1) | |
211 return scoped_ptr<uint8_t[]>(); | |
212 DCHECK(read_from == original); | |
213 size_t data_length = static_cast<size_t>(end - read_from); | |
214 scoped_ptr<uint8_t[]> buffer(new uint8_t[data_length]); | |
215 if (!LoggingReadFile(file, buffer.get(), data_length)) | |
216 return scoped_ptr<uint8_t[]>(); | |
217 return buffer.Pass(); | |
218 } | |
219 | |
220 uint32_t AddStringToTable(std::string* string_table, const std::string& str) { | |
221 uint32_t offset = base::checked_cast<uint32_t>(string_table->size()); | |
222 *string_table += str; | |
223 *string_table += '\0'; | |
224 return offset; | |
225 } | |
226 | |
227 uint32_t AddStringToTable(std::string* string_table, const std::wstring& str) { | |
228 return AddStringToTable(string_table, base::UTF16ToUTF8(str)); | |
229 } | |
230 | |
231 // static | |
232 scoped_ptr<Metadata> Metadata::Create(const base::FilePath& metadata_file, | |
233 const base::FilePath& report_dir) { | |
234 FileHandle handle = LoggingOpenFileForWrite(metadata_file, | |
235 FileWriteMode::kReuseOrCreate, | |
236 FilePermissions::kOwnerOnly); | |
237 if (handle == kInvalidFileHandle) | |
238 return scoped_ptr<Metadata>(); | |
239 // Not actually async, LockFileEx requires the Offset fields. | |
240 OVERLAPPED overlapped = {0}; | |
241 if (!LockFileEx(handle, | |
242 LOCKFILE_EXCLUSIVE_LOCK, | |
243 0, | |
244 MAXDWORD, | |
245 MAXDWORD, | |
246 &overlapped)) { | |
247 PLOG(ERROR) << "LockFileEx"; | |
248 return scoped_ptr<Metadata>(); | |
249 } | |
250 | |
251 scoped_ptr<Metadata> metadata(new Metadata(handle, report_dir)); | |
252 // If Read() fails, for whatever reason (corruption, etc.) metadata will not | |
253 // have been modified and will be in a clean empty state. We continue on and | |
254 // return an empty database to hopefully recover. This means that existing | |
255 // crash reports have been lost. | |
256 metadata->Read(); | |
257 return metadata; | |
258 } | |
259 | |
260 void Metadata::Read() { | |
261 if (LoggingSeekFile(handle_.get(), 0, SEEK_SET) == -1) | |
262 return; | |
263 | |
264 MetadataFileHeader header; | |
265 if (!LoggingReadFile(handle_.get(), &header, sizeof(header))) | |
266 return; | |
267 if (header.magic != kMetadataFileHeaderMagic || | |
268 header.version != kMetadataFileVersion) { | |
269 return; | |
270 } | |
271 | |
272 auto records_size = base::CheckedNumeric<uint32_t>(header.num_records) * | |
273 sizeof(MetadataFileReportRecord); | |
274 if (!records_size.IsValid()) | |
275 return; | |
276 | |
277 scoped_ptr<MetadataFileReportRecord[]> records( | |
278 new MetadataFileReportRecord[header.num_records]); | |
279 if (!LoggingReadFile(handle_.get(), records.get(), records_size.ValueOrDie())) | |
280 return; | |
281 | |
282 scoped_ptr<uint8_t[]> string_buffer = ReadRestOfFile(handle_.get()); | |
283 if (!string_buffer) | |
284 return; | |
285 const char* string_table = reinterpret_cast<const char*>(string_buffer.get()); | |
286 | |
287 for (uint32_t i = 0; i < header.num_records; ++i) { | |
288 ReportDisk r; | |
289 const MetadataFileReportRecord* record = &records[i]; | |
290 r.uuid = record->uuid; | |
291 r.file_path = report_dir_.Append( | |
292 base::UTF8ToUTF16(&string_table[record->file_path_index])); | |
Robert Sesek
2015/02/10 20:32:26
You can retain the checks that both indices are le
scottmg
2015/02/10 20:58:45
Done.
| |
293 r.id = &string_table[record->id_index]; | |
294 r.creation_time = record->creation_time; | |
295 r.uploaded = record->uploaded; | |
296 r.last_upload_attempt_time = record->last_upload_attempt_time; | |
297 r.upload_attempts = record->upload_attempts; | |
298 r.state = static_cast<ReportState>(record->state); | |
299 reports_.push_back(r); | |
300 } | |
301 } | |
302 | |
303 void Metadata::Write() { | |
304 if (LoggingSeekFile(handle_.get(), SEEK_SET, 0) == -1) | |
305 return; | |
306 | |
307 size_t num_records = reports_.size(); | |
308 | |
309 // Fill and write out the header. | |
310 MetadataFileHeader header; | |
311 header.magic = kMetadataFileHeaderMagic; | |
312 header.version = kMetadataFileVersion; | |
313 header.num_records = base::checked_cast<uint32_t>(num_records); | |
314 if (!LoggingWriteFile(handle_.get(), &header, sizeof(header))) | |
315 return; | |
316 | |
317 // Build the records and string table we're going to write. | |
318 std::string string_table; | |
319 scoped_ptr<MetadataFileReportRecord[]> records( | |
320 new MetadataFileReportRecord[num_records]); | |
321 for (size_t i = 0; i < num_records; ++i) { | |
322 const ReportDisk& report = reports_[i]; | |
323 MetadataFileReportRecord& record = records[i]; | |
324 record.uuid = report.uuid; | |
325 const std::wstring& path = report.file_path.value(); | |
326 if (path.substr(0, report_dir_.value().size()) != report_dir_.value()) { | |
327 LOG(ERROR) << path.c_str() << " expected to start with " | |
328 << report_dir_.value().c_str() << " and does not"; | |
329 return; | |
330 } | |
331 record.file_path_index = AddStringToTable( | |
332 &string_table, path.substr(report_dir_.value().size() + 1)); | |
333 record.id_index = AddStringToTable(&string_table, report.id); | |
334 record.creation_time = report.creation_time; | |
335 record.uploaded = report.uploaded; | |
336 record.last_upload_attempt_time = report.last_upload_attempt_time; | |
337 record.upload_attempts = report.upload_attempts; | |
338 record.state = static_cast<uint32_t>(report.state); | |
339 } | |
340 | |
341 if (!LoggingWriteFile(handle_.get(), | |
342 records.get(), | |
343 num_records * sizeof(MetadataFileReportRecord))) { | |
344 return; | |
345 } | |
346 if (!LoggingWriteFile( | |
347 handle_.get(), string_table.c_str(), string_table.size())) { | |
348 return; | |
349 } | |
350 } | |
351 | |
352 void Metadata::AddNewRecord(const ReportDisk& new_report_disk) { | |
353 DCHECK(new_report_disk.state == ReportState::kPending); | |
354 reports_.push_back(new_report_disk); | |
355 dirty_ = true; | |
356 } | |
357 | |
358 OperationStatus Metadata::FindReports( | |
359 ReportState desired_state, | |
360 std::vector<const CrashReportDatabase::Report>* reports) { | |
361 DCHECK(reports->empty()); | |
362 for (const auto& report : reports_) { | |
363 if (report.state == desired_state) { | |
364 if (VerifyReport(report, desired_state) != CrashReportDatabase::kNoError) | |
365 continue; | |
366 reports->push_back(report); | |
367 } | |
368 } | |
369 return CrashReportDatabase::kNoError; | |
370 } | |
371 | |
372 OperationStatus Metadata::FindSingleReport(const UUID& uuid, | |
373 const ReportDisk** out_report) { | |
374 for (size_t i = 0; i < reports_.size(); ++i) { | |
375 if (reports_[i].uuid == uuid) { | |
376 OperationStatus os = VerifyReportAnyState(reports_[i]); | |
377 if (os != CrashReportDatabase::kNoError) | |
378 return os; | |
379 *out_report = &reports_[i]; | |
380 return CrashReportDatabase::kNoError; | |
381 } | |
382 } | |
383 return CrashReportDatabase::kReportNotFound; | |
384 } | |
385 | |
386 OperationStatus Metadata::MutateSingleReport( | |
387 const UUID& uuid, | |
388 ReportState desired_state, | |
389 std::function<void(ReportDisk*)> mutator) { | |
390 for (size_t i = 0; i < reports_.size(); ++i) { | |
391 if (reports_[i].uuid == uuid) { | |
392 OperationStatus os = VerifyReport(reports_[i], desired_state); | |
393 if (os != CrashReportDatabase::kNoError) | |
394 return os; | |
395 mutator(&reports_[i]); | |
396 dirty_ = true; | |
397 return CrashReportDatabase::kNoError; | |
398 } | |
399 } | |
400 return CrashReportDatabase::kReportNotFound; | |
401 } | |
402 | |
403 // static | |
404 OperationStatus Metadata::VerifyReportAnyState(const ReportDisk& report_disk) { | |
405 DWORD fileattr = GetFileAttributes(report_disk.file_path.value().c_str()); | |
406 if (fileattr == INVALID_FILE_ATTRIBUTES || | |
407 (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | |
408 return CrashReportDatabase::kReportNotFound; | |
409 } | |
410 return CrashReportDatabase::kNoError; | |
411 } | |
412 | |
413 // static | |
414 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, | |
415 ReportState desired_state) { | |
416 if (report_disk.state != desired_state) | |
417 return CrashReportDatabase::kBusyError; | |
418 return VerifyReportAnyState(report_disk); | |
419 } | |
420 | |
421 class CrashReportDatabaseWin : public CrashReportDatabase { | |
422 public: | |
423 explicit CrashReportDatabaseWin(const base::FilePath& path); | |
424 ~CrashReportDatabaseWin() override; | |
425 | |
426 bool Initialize(); | |
427 | |
428 // CrashReportDatabase: | |
429 OperationStatus PrepareNewCrashReport(NewReport** report) override; | |
430 OperationStatus FinishedWritingCrashReport(NewReport* report, | |
431 UUID* uuid) override; | |
432 OperationStatus ErrorWritingCrashReport(NewReport* report) override; | |
433 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; | |
434 OperationStatus GetPendingReports( | |
435 std::vector<const Report>* reports) override; | |
436 OperationStatus GetCompletedReports( | |
437 std::vector<const Report>* reports) override; | |
438 OperationStatus GetReportForUploading(const UUID& uuid, | |
439 const Report** report) override; | |
440 OperationStatus RecordUploadAttempt(const Report* report, | |
441 bool successful, | |
442 const std::string& id) override; | |
443 OperationStatus SkipReportUpload(const UUID& uuid) override; | |
444 | |
445 private: | |
446 scoped_ptr<Metadata> AcquireMetadata(); | |
447 | |
448 base::FilePath base_dir_; | |
449 | |
450 DISALLOW_COPY_AND_ASSIGN(CrashReportDatabaseWin); | |
451 }; | |
452 | |
453 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) | |
454 : CrashReportDatabase(), base_dir_(path) { | |
455 } | |
456 | |
457 CrashReportDatabaseWin::~CrashReportDatabaseWin() { | |
458 } | |
459 | |
460 bool CrashReportDatabaseWin::Initialize() { | |
461 // Check if the database already exists. | |
462 if (!CreateOrEnsureDirectoryExists(base_dir_)) | |
463 return false; | |
464 | |
465 // Create our reports subdirectory. | |
466 if (!CreateOrEnsureDirectoryExists(base_dir_.Append(kReportsDirectory))) | |
467 return false; | |
468 | |
469 // TODO(scottmg): When are completed reports pruned from disk? Delete here or | |
470 // maybe on AcquireMetadata(). | |
471 | |
472 return true; | |
473 } | |
474 | |
475 OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( | |
476 NewReport** out_report) { | |
477 scoped_ptr<NewReportDisk> report(new NewReportDisk()); | |
478 | |
479 ::UUID system_uuid; | |
480 if (UuidCreate(&system_uuid) != RPC_S_OK) { | |
481 return kFileSystemError; | |
482 } | |
483 static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); | |
484 static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); | |
485 UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); | |
486 | |
487 report->uuid = uuid; | |
488 report->path = | |
489 base_dir_.Append(kReportsDirectory) | |
490 .Append(uuid.ToWideString() + L"." + kCrashReportFileExtension); | |
491 report->handle = LoggingOpenFileForWrite( | |
492 report->path, FileWriteMode::kCreateOrFail, FilePermissions::kOwnerOnly); | |
493 if (report->handle == INVALID_HANDLE_VALUE) | |
494 return kFileSystemError; | |
495 | |
496 *out_report = report.release(); | |
497 return kNoError; | |
498 } | |
499 | |
500 OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( | |
501 NewReport* report, | |
502 UUID* uuid) { | |
503 // Take ownership of the report, and cast to our private version with UUID. | |
504 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | |
505 // Take ownership of the file handle. | |
506 ScopedFileHandle handle(report->handle); | |
507 | |
508 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
509 if (!metadata) | |
510 return kDatabaseError; | |
511 ReportDisk report_disk; | |
512 report_disk.uuid = scoped_report->uuid; | |
513 report_disk.file_path = scoped_report->path; | |
514 report_disk.creation_time = time(nullptr); | |
515 report_disk.state = ReportState::kPending; | |
516 metadata->AddNewRecord(report_disk); | |
517 *uuid = report_disk.uuid; | |
518 return kNoError; | |
519 } | |
520 | |
521 OperationStatus CrashReportDatabaseWin::ErrorWritingCrashReport( | |
522 NewReport* report) { | |
523 // Close the outstanding handle. | |
524 LoggingCloseFile(report->handle); | |
525 | |
526 // Take ownership of the report, and cast to our private version with UUID. | |
527 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | |
528 | |
529 // We failed to write, so remove the dump file. There's no entry in the | |
530 // metadata table yet. | |
531 if (!DeleteFile(scoped_report->path.value().c_str())) { | |
532 PLOG(ERROR) << "DeleteFile " << scoped_report->path.value().c_str(); | |
533 return CrashReportDatabase::kFileSystemError; | |
534 } | |
535 | |
536 return kNoError; | |
537 } | |
538 | |
539 OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, | |
540 Report* report) { | |
541 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
542 if (!metadata) | |
543 return kDatabaseError; | |
544 // Find and return a copy of the matching report. | |
545 const ReportDisk* report_disk; | |
546 OperationStatus os = metadata->FindSingleReport(uuid, &report_disk); | |
547 if (os != kNoError) | |
548 return os; | |
549 *report = *report_disk; | |
550 return kNoError; | |
551 } | |
552 | |
553 OperationStatus CrashReportDatabaseWin::GetPendingReports( | |
554 std::vector<const Report>* reports) { | |
555 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
556 if (!metadata) | |
557 return kDatabaseError; | |
558 return metadata->FindReports(ReportState::kPending, reports); | |
559 } | |
560 | |
561 OperationStatus CrashReportDatabaseWin::GetCompletedReports( | |
562 std::vector<const Report>* reports) { | |
563 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
564 if (!metadata) | |
565 return kDatabaseError; | |
566 return metadata->FindReports(ReportState::kCompleted, reports); | |
567 } | |
568 | |
569 OperationStatus CrashReportDatabaseWin::GetReportForUploading( | |
570 const UUID& uuid, | |
571 const Report** report) { | |
572 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
573 if (!metadata) | |
574 return kDatabaseError; | |
575 return metadata->MutateSingleReport( | |
576 uuid, ReportState::kPending, [report](ReportDisk* report_disk) { | |
577 report_disk->state = ReportState::kUploading; | |
578 // Create a copy for passing back to client. This will be freed in | |
579 // RecordUploadAttempt. | |
580 *report = new Report(*report_disk); | |
581 }); | |
582 } | |
583 | |
584 OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( | |
585 const Report* report, | |
586 bool successful, | |
587 const std::string& id) { | |
588 // Take ownership, allocated in GetReportForUploading. | |
589 scoped_ptr<const Report> upload_report(report); | |
590 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
591 if (!metadata) | |
592 return kDatabaseError; | |
593 return metadata->MutateSingleReport( | |
594 report->uuid, | |
595 ReportState::kUploading, | |
596 [successful, id](ReportDisk* report_disk) { | |
597 report_disk->uploaded = successful; | |
598 report_disk->id = id; | |
599 report_disk->last_upload_attempt_time = time(nullptr); | |
600 report_disk->upload_attempts++; | |
601 report_disk->state = | |
602 successful ? ReportState::kCompleted : ReportState::kPending; | |
603 }); | |
604 } | |
605 | |
606 OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { | |
607 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
608 if (!metadata) | |
609 return kDatabaseError; | |
610 return metadata->MutateSingleReport( | |
611 uuid, ReportState::kPending, [](ReportDisk* report_disk) { | |
612 report_disk->state = ReportState::kCompleted; | |
613 }); | |
614 } | |
615 | |
616 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { | |
617 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); | |
618 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); | |
619 } | |
620 | |
621 } // namespace | |
622 | |
623 // static | |
624 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( | |
625 const base::FilePath& path) { | |
626 scoped_ptr<CrashReportDatabaseWin> database_win( | |
627 new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); | |
628 if (!database_win->Initialize()) | |
629 database_win.reset(); | |
630 | |
631 return scoped_ptr<CrashReportDatabase>(database_win.release()); | |
632 } | |
633 | |
634 } // namespace crashpad | |
OLD | NEW |