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/files/file_path.h" | |
24 #include "base/logging.h" | |
25 #include "base/memory/scoped_ptr.h" | |
26 #include "base/numerics/safe_conversions.h" | |
27 #include "base/strings/stringprintf.h" | |
28 #include "base/strings/utf_string_conversions.h" | |
29 #include "util/misc/uuid.h" | |
30 | |
31 namespace crashpad { | |
32 | |
33 namespace { | |
34 | |
35 const wchar_t kDatabaseDirectoryName[] = L"Crashpad"; | |
36 | |
37 const wchar_t kReportsDirectory[] = L"reports"; | |
38 const wchar_t kMetadataFileName[] = L"metadata"; | |
39 const wchar_t kMetadataTempFileName[] = L"metadata.tmp"; | |
40 const wchar_t kMetadataMutexName[] = L"crashpad.metadata.lock"; | |
41 | |
42 const wchar_t kCrashReportFileExtension[] = L"dmp"; | |
43 | |
44 enum class ReportState : int { | |
45 //! \brief Just created, owned by caller and being written. | |
46 kNew, | |
47 //! \brief Fully filled out, owned by database. | |
48 kPending, | |
49 //! \brief In the process of uploading, owned by caller. | |
50 kUploading, | |
51 //! \brief Upload completed or skipped, owned by database. | |
52 kCompleted, | |
53 | |
54 //! \brief Used during query to indicate a report in any state is acceptable. | |
55 //! Not a valid state for a report to be in. | |
56 kAny, | |
57 }; | |
58 | |
59 using OperationStatus = CrashReportDatabase::OperationStatus; | |
60 | |
61 //! \brief Ensures that the node at path is a directory, and creates it if it | |
62 //! does not exist. | |
63 //! | |
64 //! \return If the path points to a file, rather than a directory, or the | |
65 //! directory could not be created, returns false. Otherwise, returns true, | |
66 //! indicating that path already was or now is a directory. | |
67 bool CreateOrEnsureDirectoryExists(const base::FilePath& path) { | |
68 if (CreateDirectory(path.value().c_str(), nullptr)) { | |
69 return true; | |
70 } else if (GetLastError() == ERROR_ALREADY_EXISTS) { | |
71 DWORD fileattr = GetFileAttributes(path.value().c_str()); | |
72 if (fileattr == INVALID_FILE_ATTRIBUTES) { | |
73 PLOG(ERROR) << "GetFileAttributes"; | |
74 return false; | |
75 } | |
76 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | |
77 return true; | |
78 LOG(ERROR) << "not a directory"; | |
79 return false; | |
80 } else { | |
81 PLOG(ERROR) << "CreateDirectory"; | |
82 return false; | |
83 } | |
84 } | |
85 | |
86 //! \brief Splits the given string on a the given character, returning a vector | |
87 //! of StringPieces. The lifetime of the input string must therefore outlive | |
88 //! the call to this function. | |
89 std::vector<base::StringPiece> SplitString(base::StringPiece str, char on) { | |
90 std::vector<base::StringPiece> result; | |
91 size_t last = 0; | |
92 size_t end = str.size(); | |
93 for (size_t i = 0; i <= end; ++i) { | |
94 if (i == end || str[i] == on) { | |
95 result.push_back(base::StringPiece(str.begin() + last, i - last)); | |
96 last = i + 1; | |
97 } | |
98 } | |
99 return result; | |
100 } | |
101 | |
102 //! \brief A private extension of the Report class that includes additional data | |
103 //! that's stored on disk in the metadata file. | |
104 struct ReportDisk : public CrashReportDatabase::Report { | |
105 //! \brief The current state of the report. | |
106 ReportState state; | |
107 }; | |
108 | |
109 //! \brief A private extension of the NewReport class to hold the UUID during | |
110 //! initial write. We don't store metadata in dump's file attributes, and | |
111 //! use the UUID to identify the dump on write completion. | |
112 struct NewReportDisk : public CrashReportDatabase::NewReport { | |
113 //! \brief The UUID for this crash report. | |
114 UUID uuid; | |
115 }; | |
116 | |
117 //! \brief Manages the metadata for the set of reports, handling serialization | |
118 //! to disk, and queries. Instances of this class should be created by using | |
119 //! CrashReportDatabaseWin::AcquireMetadata, rather than direct | |
Robert Sesek
2015/02/05 18:43:09
nit: () in method calls
scottmg
2015/02/05 19:27:55
Done.
| |
120 //! instantiation. | |
121 class Metadata { | |
122 public: | |
123 //! \param[in] base_dir Root of storage for data files. | |
124 //! \param[in] mutex_to_release An acquired mutex guarding access to the | |
125 //! metadata file. The lock is owned by this object and will be released | |
126 //! on destruction. The lifetime of the HANDLE is not owned. | |
127 Metadata(const base::FilePath& base_dir, HANDLE mutex_to_release); | |
128 ~Metadata(); | |
129 | |
130 //! \brief Reads the database from disk. May only be called once without an | |
131 //! interceding call to Write(). | |
132 bool Read(); | |
133 | |
134 //! \brief Flushes the database back to disk. It is not necessary to call this | |
135 //! method if the reports were not mutated, however, Read() may not be | |
136 //! called again until Write() is used. | |
137 bool Write(); | |
138 | |
139 //! \brief Adds a new report to the set. Write() must be called to persist | |
140 //! after adding new records. | |
141 void AddNewRecord(const NewReportDisk& new_report); | |
142 | |
143 //! \brief Finds all reports in a given state. The reports vector is only | |
144 //! valid when #kNoError is returned. | |
145 OperationStatus FindReports( | |
146 ReportState desired_state, | |
147 std::vector<const CrashReportDatabase::Report>* reports); | |
148 | |
149 //! \brief Finds the report matching the given UUID and the desired state, | |
150 //! returns a mutable pointer to it. Only valid if | |
151 //! CrashReportDatabase::kNoError is returned. If the report is mutated by | |
152 //! the caller, the caller is also responsible for calling Write() before | |
153 //! destruction to persist the changes. | |
154 //! | |
155 //! \param[in] uuid The report identifier. | |
156 //! \param[in] desired_state A specific state that the given report must be | |
157 //! in. If the report is located, but is in a different state, | |
158 //! CrashReportDatabase::kBusyError will be returned. If no filtering by | |
159 //! state is required, ReportState::kAny can be used. | |
160 //! \param[out] report_disk The found report, valid only if | |
161 //! CrashReportDatabase::kNoError is returned. Ownership is not | |
162 //! transferred to the caller. | |
163 OperationStatus Metadata::FindSingleReport(const UUID& uuid, | |
164 ReportState desired_state, | |
165 ReportDisk** report_disk); | |
166 | |
167 private: | |
168 //! \brief Confirms that the corresponding report actually exists on disk | |
169 //! (that is, the dump file has not been removed), and if desired_state | |
170 //! is not ReportState::kAny, confirms that the report is in the given | |
171 //! state. | |
172 static OperationStatus VerifyReport(const ReportDisk& report_win, | |
173 ReportState desired_state); | |
174 | |
175 base::FilePath base_dir_; | |
176 bool loaded_; | |
177 HANDLE mutex_; | |
178 std::vector<ReportDisk> reports_; | |
179 }; | |
180 | |
181 Metadata::Metadata(const base::FilePath& base_dir, HANDLE mutex_to_release) | |
182 : base_dir_(base_dir), | |
183 loaded_(false), | |
184 mutex_(mutex_to_release), | |
185 reports_() { | |
186 } | |
187 | |
188 Metadata::~Metadata() { | |
189 ReleaseMutex(mutex_); | |
190 } | |
191 | |
192 // The format of the metadata file is a MetadataFileHeader, followed by a | |
193 // number of fixed size records of MetadataFileReportRecord, followed by a | |
194 // string table in UTF8 format, where each string is \0 terminated. | |
195 | |
196 #pragma pack(push, 1) | |
197 | |
198 struct MetadataFileHeader { | |
199 uint32_t magic; | |
200 uint32_t version; | |
201 uint32_t remaining_file_size; | |
202 uint32_t num_records; | |
203 }; | |
204 | |
205 struct MetadataFileReportRecord { | |
206 UUID uuid; // UUID is a 16 byte, standard layout structure. | |
207 uint32_t file_path_index; | |
208 uint32_t id_index; | |
209 int64_t creation_time; | |
210 uint8_t uploaded; | |
211 int64_t last_upload_attempt_time; | |
212 int32_t upload_attempts; | |
213 int32_t state; | |
214 }; | |
215 | |
216 const uint32_t kMetadataFileHeaderMagic = 'CPAD'; | |
217 const uint32_t kMetadataFileVersion = 1; | |
218 | |
219 #pragma pack(pop) | |
220 | |
221 uint32_t AddStringToTable(std::string* string_table, const std::string& str) { | |
222 uint32_t offset = base::checked_cast<uint32_t>(string_table->size()); | |
223 *string_table += str; | |
224 *string_table += '\0'; | |
225 return offset; | |
226 } | |
227 | |
228 uint32_t AddStringToTable(std::string* string_table, const std::wstring& str) { | |
229 return AddStringToTable(string_table, base::UTF16ToUTF8(str)); | |
230 } | |
231 | |
232 bool Metadata::Read() { | |
233 DCHECK(!loaded_); | |
234 base::FilePath path = base_dir_.Append(kMetadataFileName); | |
235 ScopedFileHandle input_file = ScopedFileHandle(LoggingOpenFileForRead(path)); | |
236 // The file not existing is OK, we may not have created it yet. | |
237 if (input_file.get() == INVALID_HANDLE_VALUE) { | |
238 loaded_ = true; | |
239 return true; | |
240 } | |
241 | |
242 MetadataFileHeader header; | |
243 if (!LoggingReadFile(input_file.get(), &header, sizeof(header))) | |
244 return false; | |
245 if (header.magic != kMetadataFileHeaderMagic || | |
246 header.version != kMetadataFileVersion) { | |
Robert Sesek
2015/02/05 18:43:09
For v2 we'll need to figure out how to handle a da
scottmg
2015/02/05 19:27:55
Acknowledged.
| |
247 return false; | |
248 } | |
249 scoped_ptr<uint8_t> buffer(new uint8_t[header.remaining_file_size]); | |
250 if (!LoggingReadFile( | |
251 input_file.get(), buffer.get(), header.remaining_file_size)) { | |
252 return false; | |
253 } | |
254 | |
Robert Sesek
2015/02/05 18:43:09
The two fields num_records and remainig_file_size
scottmg
2015/02/05 19:27:55
Done. (with > instead of < in last if)
Robert Sesek
2015/02/05 22:46:17
Ooops!
| |
255 const char* string_table = reinterpret_cast<const char*>( | |
256 buffer.get() + header.num_records * sizeof(MetadataFileReportRecord)); | |
Robert Sesek
2015/02/05 18:43:09
And then use records_size.ValueOrDie() here.
scottmg
2015/02/05 19:27:55
Done.
| |
257 | |
258 for (uint32_t i = 0; i < header.num_records; ++i) { | |
259 ReportDisk r; | |
260 const MetadataFileReportRecord* record = | |
261 reinterpret_cast<const MetadataFileReportRecord*>( | |
262 buffer.get() + i * sizeof(MetadataFileReportRecord)); | |
263 r.uuid = record->uuid; | |
264 r.file_path = base::FilePath( | |
265 base::UTF8ToUTF16(&string_table[record->file_path_index])); | |
266 r.id = &string_table[record->id_index]; | |
267 r.creation_time = record->creation_time; | |
268 r.uploaded = record->uploaded; | |
269 r.last_upload_attempt_time = record->last_upload_attempt_time; | |
270 r.upload_attempts = record->upload_attempts; | |
271 r.state = static_cast<ReportState>(record->state); | |
272 reports_.push_back(r); | |
273 } | |
274 loaded_ = true; | |
275 return true; | |
276 } | |
277 | |
278 bool Metadata::Write() { | |
279 DCHECK(loaded_); | |
280 base::FilePath path = base_dir_.Append(kMetadataTempFileName); | |
281 | |
282 // Write data to a temporary file. | |
283 { | |
284 ScopedFileHandle output_file = ScopedFileHandle(LoggingOpenFileForWrite( | |
285 path, FileWriteMode::kTruncateOrCreate, FilePermissions::kOwnerOnly)); | |
286 | |
287 // Build the records and string table we're going to write. | |
288 size_t num_records = reports_.size(); | |
289 std::string string_table; | |
290 scoped_ptr<MetadataFileReportRecord[]> records( | |
291 new MetadataFileReportRecord[num_records]); | |
292 for (size_t i = 0; i < num_records; ++i) { | |
293 const ReportDisk& report = reports_[i]; | |
294 MetadataFileReportRecord& record = records[i]; | |
295 record.uuid = report.uuid; | |
296 record.file_path_index = | |
297 AddStringToTable(&string_table, report.file_path.value()); | |
298 record.id_index = AddStringToTable(&string_table, report.id); | |
299 record.creation_time = report.creation_time; | |
300 record.uploaded = report.uploaded; | |
301 record.last_upload_attempt_time = report.last_upload_attempt_time; | |
302 record.upload_attempts = report.upload_attempts; | |
303 record.state = static_cast<uint32_t>(report.state); | |
304 } | |
305 | |
306 // Now we have the size of the file, fill out the header. | |
307 MetadataFileHeader header; | |
308 header.magic = kMetadataFileHeaderMagic; | |
309 header.version = kMetadataFileVersion; | |
310 header.remaining_file_size = | |
311 num_records * sizeof(MetadataFileReportRecord) + string_table.size(); | |
Robert Sesek
2015/02/05 18:43:09
A pathologically large num_records could be proble
scottmg
2015/02/05 19:27:55
Done.
| |
312 header.num_records = base::checked_cast<uint32_t>(num_records); | |
313 | |
314 if (!LoggingWriteFile(output_file.get(), &header, sizeof(header))) | |
315 return false; | |
316 if (!LoggingWriteFile(output_file.get(), | |
317 records.get(), | |
318 num_records * sizeof(MetadataFileReportRecord))) { | |
319 return false; | |
320 } | |
321 if (!LoggingWriteFile( | |
322 output_file.get(), string_table.c_str(), string_table.size())) { | |
323 return false; | |
324 } | |
325 } | |
326 | |
327 // While this is not guaranteed to be atomic, it is in practical cases. We | |
328 // aren't racing ourself here as we're holding the mutex here, we use | |
329 // MoveFileEx only to avoid corrupting data in case of failure during write. | |
330 bool result = MoveFileEx(path.value().c_str(), | |
331 base_dir_.Append(kMetadataFileName).value().c_str(), | |
332 MOVEFILE_REPLACE_EXISTING); | |
333 loaded_ = !result; | |
334 return result; | |
335 } | |
336 | |
337 void Metadata::AddNewRecord(const NewReportDisk& new_report) { | |
338 ReportDisk report; | |
339 report.uuid = new_report.uuid; | |
340 report.file_path = new_report.path; | |
341 report.state = ReportState::kNew; | |
342 reports_.push_back(report); | |
343 } | |
344 | |
345 OperationStatus Metadata::FindReports( | |
346 ReportState desired_state, | |
347 std::vector<const CrashReportDatabase::Report>* reports) { | |
348 DCHECK(reports->empty()); | |
349 for (const auto& report : reports_) { | |
350 if (report.state == desired_state) { | |
351 if (VerifyReport(report, desired_state) != CrashReportDatabase::kNoError) | |
352 continue; | |
353 reports->push_back(report); | |
354 } | |
355 } | |
356 return CrashReportDatabase::kNoError; | |
357 } | |
358 | |
359 OperationStatus Metadata::FindSingleReport(const UUID& uuid, | |
360 ReportState desired_state, | |
361 ReportDisk** out_report) { | |
362 for (size_t i = 0; i < reports_.size(); ++i) { | |
363 if (reports_[i].uuid == uuid) { | |
364 OperationStatus os = VerifyReport(reports_[i], desired_state); | |
365 if (os != CrashReportDatabase::kNoError) | |
366 return os; | |
367 *out_report = &reports_[i]; | |
368 return CrashReportDatabase::kNoError; | |
369 } | |
370 } | |
371 return CrashReportDatabase::kReportNotFound; | |
372 } | |
373 | |
374 // static | |
375 OperationStatus Metadata::VerifyReport(const ReportDisk& report_win, | |
376 ReportState desired_state) { | |
377 if (desired_state != ReportState::kAny && report_win.state != desired_state) | |
378 return CrashReportDatabase::kBusyError; | |
379 if (GetFileAttributes(report_win.file_path.value().c_str()) == | |
380 INVALID_FILE_ATTRIBUTES) { | |
381 return CrashReportDatabase::kReportNotFound; | |
382 } | |
383 return CrashReportDatabase::kNoError; | |
384 } | |
385 | |
386 class CrashReportDatabaseWin : public CrashReportDatabase { | |
387 public: | |
388 explicit CrashReportDatabaseWin(const base::FilePath& path); | |
389 ~CrashReportDatabaseWin() override; | |
390 | |
391 bool Initialize(); | |
392 | |
393 // CrashReportDatabase:: | |
394 OperationStatus PrepareNewCrashReport(NewReport** report) override; | |
395 OperationStatus FinishedWritingCrashReport(NewReport* report, | |
396 UUID* uuid) override; | |
397 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; | |
398 OperationStatus GetPendingReports( | |
399 std::vector<const Report>* reports) override; | |
400 OperationStatus GetCompletedReports( | |
401 std::vector<const Report>* reports) override; | |
402 OperationStatus GetReportForUploading(const UUID& uuid, | |
403 const Report** report) override; | |
404 OperationStatus RecordUploadAttempt(const Report* report, | |
405 bool successful, | |
406 const std::string& id) override; | |
407 OperationStatus SkipReportUpload(const UUID& uuid) override; | |
408 | |
409 private: | |
410 scoped_ptr<Metadata> AcquireMetadata(); | |
411 | |
412 base::FilePath base_dir_; | |
413 ScopedKernelHANDLE mutex_; | |
414 | |
415 DISALLOW_COPY_AND_ASSIGN(CrashReportDatabaseWin); | |
416 }; | |
417 | |
418 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) | |
419 : CrashReportDatabase(), base_dir_(path), mutex_() { | |
420 } | |
421 | |
422 CrashReportDatabaseWin::~CrashReportDatabaseWin() { | |
423 } | |
424 | |
425 bool CrashReportDatabaseWin::Initialize() { | |
426 // Check if the database already exists. | |
427 if (!CreateOrEnsureDirectoryExists(base_dir_)) | |
428 return false; | |
429 | |
430 // Create our reports subdirectory. | |
431 if (!CreateOrEnsureDirectoryExists(base_dir_.Append(kReportsDirectory))) | |
432 return false; | |
433 | |
434 mutex_.reset(CreateMutex(nullptr, false, kMetadataMutexName)); | |
435 | |
436 // TODO(scottmg): When are completed reports pruned from disk? Delete here or | |
437 // maybe on Read(). | |
438 | |
439 return true; | |
440 } | |
441 | |
442 OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( | |
443 NewReport** out_report) { | |
444 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
445 if (!metadata) | |
446 return kFileSystemError; | |
447 | |
448 scoped_ptr<NewReportDisk> report(new NewReportDisk()); | |
449 | |
450 ::UUID system_uuid; | |
451 if (UuidCreate(&system_uuid) != RPC_S_OK) { | |
452 return kFileSystemError; | |
453 } | |
454 static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); | |
455 static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); | |
456 UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); | |
457 | |
458 report->uuid = uuid; | |
459 report->path = | |
460 base_dir_.Append(kReportsDirectory) | |
461 .Append(uuid.ToWideString() + L"." + kCrashReportFileExtension); | |
462 report->handle = LoggingOpenFileForWrite(report->path, | |
463 FileWriteMode::kTruncateOrCreate, | |
464 FilePermissions::kOwnerOnly); | |
465 if (report->handle == INVALID_HANDLE_VALUE) | |
466 return kFileSystemError; | |
467 | |
468 metadata->AddNewRecord(*report); | |
469 if (!metadata->Write()) | |
470 return kDatabaseError; | |
471 *out_report = report.release(); | |
472 return kNoError; | |
473 } | |
474 | |
475 OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( | |
476 NewReport* report, | |
477 UUID* uuid) { | |
478 // Take ownership of the report, and cast to our private version with UUID. | |
479 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | |
480 // Take ownership of the file handle. | |
481 ScopedFileHandle handle(report->handle); | |
482 | |
483 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
484 if (!metadata) | |
485 return kFileSystemError; | |
486 | |
487 ReportDisk* report_disk; | |
488 OperationStatus os = metadata->FindSingleReport( | |
489 scoped_report->uuid, ReportState::kNew, &report_disk); | |
490 if (os != kNoError) | |
491 return os; | |
492 report_disk->state = ReportState::kPending; | |
493 report_disk->creation_time = time(nullptr); | |
494 if (!metadata->Write()) | |
495 return kDatabaseError; | |
496 *uuid = report_disk->uuid; | |
497 return kNoError; | |
498 } | |
499 | |
500 OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, | |
501 Report* report) { | |
502 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
503 if (!metadata) | |
504 return kFileSystemError; | |
505 | |
506 ReportDisk* report_disk; | |
507 OperationStatus os = | |
508 metadata->FindSingleReport(uuid, ReportState::kAny, &report_disk); | |
509 if (os != kNoError) | |
510 return os; | |
511 *report = *report_disk; | |
512 return kNoError; | |
513 } | |
514 | |
515 OperationStatus CrashReportDatabaseWin::GetPendingReports( | |
516 std::vector<const Report>* reports) { | |
517 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
518 if (!metadata) | |
519 return kFileSystemError; | |
520 return metadata->FindReports(ReportState::kPending, reports); | |
521 } | |
522 | |
523 OperationStatus CrashReportDatabaseWin::GetCompletedReports( | |
524 std::vector<const Report>* reports) { | |
525 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
526 if (!metadata) | |
527 return kFileSystemError; | |
528 return metadata->FindReports(ReportState::kCompleted, reports); | |
529 } | |
530 | |
531 OperationStatus CrashReportDatabaseWin::GetReportForUploading( | |
532 const UUID& uuid, | |
533 const Report** report) { | |
534 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
535 if (!metadata) | |
536 return kFileSystemError; | |
537 | |
538 ReportDisk* report_disk; | |
539 OperationStatus os = | |
540 metadata->FindSingleReport(uuid, ReportState::kPending, &report_disk); | |
541 if (os != kNoError) | |
542 return os; | |
543 report_disk->state = ReportState::kUploading; | |
544 // Create a copy for passing back to client. This will be freed in | |
545 // RecordUploadAttempt. | |
546 *report = new Report(*report_disk); | |
547 if (!metadata->Write()) | |
548 return kDatabaseError; | |
549 return kNoError; | |
550 } | |
551 | |
552 OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( | |
553 const Report* report, | |
554 bool successful, | |
555 const std::string& id) { | |
556 // Take ownership, allocated in GetReportForUploading. | |
557 scoped_ptr<const Report> upload_report(report); | |
558 | |
559 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
560 if (!metadata) | |
561 return kFileSystemError; | |
562 | |
563 ReportDisk* report_disk; | |
564 OperationStatus os = metadata->FindSingleReport( | |
565 report->uuid, ReportState::kUploading, &report_disk); | |
566 if (os != kNoError) | |
567 return os; | |
568 report_disk->uploaded = successful; | |
569 report_disk->id = id; | |
570 report_disk->last_upload_attempt_time = time(nullptr); | |
571 report_disk->upload_attempts++; | |
572 report_disk->state = | |
573 successful ? ReportState::kCompleted : ReportState::kPending; | |
574 if (!metadata->Write()) | |
575 return kDatabaseError; | |
576 return kNoError; | |
577 } | |
578 | |
579 OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { | |
580 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
581 if (!metadata) | |
582 return kFileSystemError; | |
583 | |
584 ReportDisk* report_disk; | |
585 OperationStatus os = | |
586 metadata->FindSingleReport(uuid, ReportState::kPending, &report_disk); | |
587 if (os != kNoError) | |
588 return os; | |
589 report_disk->state = ReportState::kCompleted; | |
590 if (!metadata->Write()) | |
591 return kDatabaseError; | |
592 return kNoError; | |
593 } | |
594 | |
595 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { | |
596 if (WaitForSingleObject(mutex_.get(), INFINITE) != WAIT_OBJECT_0) | |
597 return nullptr; | |
598 scoped_ptr<Metadata> metadata(new Metadata(base_dir_, mutex_.get())); | |
599 if (!metadata->Read()) { | |
600 LOG(ERROR) << "metadata file could not be loaded"; | |
601 return nullptr; | |
602 } | |
603 return metadata; | |
604 } | |
605 | |
606 } // namespace | |
607 | |
608 // static | |
609 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( | |
610 const base::FilePath& path) { | |
611 scoped_ptr<CrashReportDatabaseWin> database_win( | |
612 new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); | |
613 if (!database_win->Initialize()) | |
614 database_win.reset(); | |
615 | |
616 return scoped_ptr<CrashReportDatabase>(database_win.release()); | |
617 } | |
618 | |
619 } // namespace crashpad | |
OLD | NEW |