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