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 private: | |
141 Metadata(FileHandle handle, const base::FilePath& report_dir); | |
142 | |
143 static scoped_ptr<Metadata> Create(const base::FilePath& metadata_file, | |
144 const base::FilePath& report_dir); | |
145 friend class CrashReportDatabaseWin; | |
146 | |
147 void Read(); | |
148 void Write(); | |
149 | |
150 //! \brief Confirms that the corresponding report actually exists on disk | |
151 //! (that is, the dump file has not been removed), that the report is in | |
152 //! the given state. | |
153 static OperationStatus VerifyReport(const ReportDisk& report_disk, | |
154 ReportState desired_state); | |
155 //! \brief Confirms that the corresponding report actually exists on disk | |
156 //! (that is, the dump file has not been removed). | |
157 static OperationStatus VerifyReportAnyState(const ReportDisk& report_disk); | |
158 | |
159 ScopedFileHandle handle_; | |
160 const base::FilePath report_dir_; | |
161 bool dirty_; //! \brief Is a Write() required on destruction? | |
162 std::vector<ReportDisk> reports_; | |
163 | |
164 DISALLOW_COPY_AND_ASSIGN(Metadata); | |
165 }; | |
166 | |
167 Metadata::Metadata(FileHandle handle, const base::FilePath& report_dir) | |
168 : handle_(handle), report_dir_(report_dir), dirty_(false), reports_() { | |
169 } | |
170 | |
171 Metadata::~Metadata() { | |
172 if (dirty_) | |
173 Write(); | |
174 OVERLAPPED overlapped = {0}; | |
175 if (!UnlockFileEx(handle_.get(), 0, MAXDWORD, MAXDWORD, &overlapped)) | |
176 PLOG(ERROR) << "UnlockFileEx"; | |
177 } | |
178 | |
179 // The format of the metadata file is a MetadataFileHeader, followed by a | |
180 // number of fixed size records of MetadataFileReportRecord, followed by a | |
181 // string table in UTF8 format, where each string is \0 terminated. | |
182 | |
183 #pragma pack(push, 1) | |
184 | |
185 struct MetadataFileHeader { | |
186 uint32_t magic; | |
187 uint32_t version; | |
188 uint32_t num_records; | |
189 }; | |
190 | |
191 struct MetadataFileReportRecord { | |
192 UUID uuid; // UUID is a 16 byte, standard layout structure. | |
193 uint32_t file_path_index; // Index into string table. File name is relative | |
194 // to the reports directory when on disk. | |
195 uint32_t id_index; // Index into string table. | |
196 int64_t creation_time; // Holds a time_t. | |
197 int64_t last_upload_attempt_time; // Holds a time_t. | |
198 int32_t upload_attempts; | |
199 int32_t state; // A ReportState. | |
200 uint8_t uploaded; // Boolean, 0 or 1. | |
201 }; | |
202 | |
203 const uint32_t kMetadataFileHeaderMagic = 'CPAD'; | |
204 const uint32_t kMetadataFileVersion = 1; | |
205 | |
206 #pragma pack(pop) | |
207 | |
208 // Reads from the current file position to EOF and returns as uint8_t[]. | |
209 std::vector<uint8_t> ReadRestOfFile(FileHandle file) { | |
210 FileOffset read_from = LoggingSeekFile(file, 0, SEEK_CUR); | |
211 FileOffset end = LoggingSeekFile(file, 0, SEEK_END); | |
212 FileOffset original = LoggingSeekFile(file, read_from, SEEK_SET); | |
213 if (read_from == -1 || end == -1 || original == -1) | |
214 return std::vector<uint8_t>(); | |
215 DCHECK(read_from == original); | |
216 size_t data_length = static_cast<size_t>(end - read_from); | |
217 std::vector<uint8_t> buffer; | |
218 buffer.resize(data_length); | |
219 if (!LoggingReadFile(file, &buffer[0], data_length)) | |
220 return std::vector<uint8_t>(); | |
221 return buffer; | |
222 } | |
223 | |
224 uint32_t AddStringToTable(std::string* string_table, const std::string& str) { | |
225 uint32_t offset = base::checked_cast<uint32_t>(string_table->size()); | |
226 *string_table += str; | |
227 *string_table += '\0'; | |
228 return offset; | |
229 } | |
230 | |
231 uint32_t AddStringToTable(std::string* string_table, const std::wstring& str) { | |
232 return AddStringToTable(string_table, base::UTF16ToUTF8(str)); | |
233 } | |
234 | |
235 // static | |
236 scoped_ptr<Metadata> Metadata::Create(const base::FilePath& metadata_file, | |
237 const base::FilePath& report_dir) { | |
238 FileHandle handle = LoggingOpenFileForWrite(metadata_file, | |
239 FileWriteMode::kReuseOrCreate, | |
240 FilePermissions::kOwnerOnly); | |
241 if (handle == kInvalidFileHandle) | |
242 return scoped_ptr<Metadata>(); | |
243 // Not actually async, LockFileEx requires the Offset fields. | |
244 OVERLAPPED overlapped = {0}; | |
245 if (!LockFileEx(handle, | |
Mark Mentovai
2015/02/10 22:16:29
If two threads in the same process try to get the
scottmg
2015/02/10 22:44:01
Hmm, I had only been considering separate processe
Mark Mentovai
2015/02/10 22:49:29
scottmg wrote:
scottmg
2015/02/10 23:28:37
Done in ps#21.
Er, wait! Writing the comment belo
Mark Mentovai
2015/02/10 23:37:03
scottmg wrote:
scottmg
2015/02/10 23:42:16
Oh dear. And in writing a test for this, I realize
scottmg
2015/02/10 23:54:40
Got it. LockFileEx doesn't strictly protect access
Mark Mentovai
2015/02/11 00:31:35
scottmg wrote:
Mark Mentovai
2015/02/11 00:31:36
scottmg wrote:
scottmg
2015/02/11 01:13:40
That makes sense.
I dithered a little on this, bu
Robert Sesek
2015/02/11 03:16:07
It would only return kBusyError if the concurrent
| |
246 LOCKFILE_EXCLUSIVE_LOCK, | |
247 0, | |
248 MAXDWORD, | |
249 MAXDWORD, | |
250 &overlapped)) { | |
251 PLOG(ERROR) << "LockFileEx"; | |
252 return scoped_ptr<Metadata>(); | |
253 } | |
254 | |
255 scoped_ptr<Metadata> metadata(new Metadata(handle, report_dir)); | |
256 // If Read() fails, for whatever reason (corruption, etc.) metadata will not | |
257 // have been modified and will be in a clean empty state. We continue on and | |
258 // return an empty database to hopefully recover. This means that existing | |
259 // crash reports have been lost. | |
260 metadata->Read(); | |
261 return metadata; | |
262 } | |
263 | |
264 void Metadata::Read() { | |
265 if (LoggingSeekFile(handle_.get(), 0, SEEK_SET) == -1) | |
266 return; | |
267 | |
268 MetadataFileHeader header; | |
269 if (!LoggingReadFile(handle_.get(), &header, sizeof(header))) | |
270 return; | |
271 if (header.magic != kMetadataFileHeaderMagic || | |
272 header.version != kMetadataFileVersion) { | |
273 return; | |
274 } | |
275 | |
276 auto records_size = base::CheckedNumeric<uint32_t>(header.num_records) * | |
277 sizeof(MetadataFileReportRecord); | |
278 if (!records_size.IsValid()) | |
279 return; | |
280 | |
281 scoped_ptr<MetadataFileReportRecord[]> records( | |
282 new MetadataFileReportRecord[header.num_records]); | |
283 if (!LoggingReadFile(handle_.get(), records.get(), records_size.ValueOrDie())) | |
284 return; | |
285 | |
286 std::vector<uint8_t> string_buffer = ReadRestOfFile(handle_.get()); | |
287 if (string_buffer.empty() || string_buffer.back() != 0) | |
288 return; | |
289 const char* string_table = reinterpret_cast<const char*>(&string_buffer[0]); | |
290 | |
291 for (uint32_t i = 0; i < header.num_records; ++i) { | |
292 ReportDisk r; | |
293 const MetadataFileReportRecord* record = &records[i]; | |
294 r.uuid = record->uuid; | |
295 if (record->file_path_index >= string_buffer.size() || | |
296 record->id_index >= string_buffer.size()) { | |
297 reports_.clear(); | |
298 return; | |
299 } | |
300 r.file_path = report_dir_.Append( | |
301 base::UTF8ToUTF16(&string_table[record->file_path_index])); | |
302 r.id = &string_table[record->id_index]; | |
303 r.creation_time = record->creation_time; | |
304 r.uploaded = record->uploaded; | |
305 r.last_upload_attempt_time = record->last_upload_attempt_time; | |
306 r.upload_attempts = record->upload_attempts; | |
307 r.state = static_cast<ReportState>(record->state); | |
308 reports_.push_back(r); | |
309 } | |
310 } | |
311 | |
312 void Metadata::Write() { | |
313 if (LoggingSeekFile(handle_.get(), SEEK_SET, 0) == -1) | |
314 return; | |
315 | |
316 size_t num_records = reports_.size(); | |
317 | |
318 // Fill and write out the header. | |
319 MetadataFileHeader header; | |
320 header.magic = kMetadataFileHeaderMagic; | |
321 header.version = kMetadataFileVersion; | |
322 header.num_records = base::checked_cast<uint32_t>(num_records); | |
323 if (!LoggingWriteFile(handle_.get(), &header, sizeof(header))) | |
324 return; | |
325 | |
326 // Build the records and string table we're going to write. | |
327 std::string string_table; | |
328 scoped_ptr<MetadataFileReportRecord[]> records( | |
329 new MetadataFileReportRecord[num_records]); | |
330 for (size_t i = 0; i < num_records; ++i) { | |
331 const ReportDisk& report = reports_[i]; | |
332 MetadataFileReportRecord& record = records[i]; | |
333 record.uuid = report.uuid; | |
334 const std::wstring& path = report.file_path.value(); | |
335 if (path.substr(0, report_dir_.value().size()) != report_dir_.value()) { | |
336 LOG(ERROR) << path.c_str() << " expected to start with " | |
337 << report_dir_.value().c_str() << " and does not"; | |
338 return; | |
339 } | |
340 record.file_path_index = AddStringToTable( | |
341 &string_table, path.substr(report_dir_.value().size() + 1)); | |
342 record.id_index = AddStringToTable(&string_table, report.id); | |
343 record.creation_time = report.creation_time; | |
344 record.uploaded = report.uploaded; | |
345 record.last_upload_attempt_time = report.last_upload_attempt_time; | |
346 record.upload_attempts = report.upload_attempts; | |
347 record.state = static_cast<uint32_t>(report.state); | |
348 } | |
349 | |
350 if (!LoggingWriteFile(handle_.get(), | |
351 records.get(), | |
352 num_records * sizeof(MetadataFileReportRecord))) { | |
353 return; | |
354 } | |
355 if (!LoggingWriteFile( | |
356 handle_.get(), string_table.c_str(), string_table.size())) { | |
357 return; | |
358 } | |
359 } | |
360 | |
361 void Metadata::AddNewRecord(const ReportDisk& new_report_disk) { | |
362 DCHECK(new_report_disk.state == ReportState::kPending); | |
363 reports_.push_back(new_report_disk); | |
364 dirty_ = true; | |
365 } | |
366 | |
367 OperationStatus Metadata::FindReports( | |
368 ReportState desired_state, | |
369 std::vector<const CrashReportDatabase::Report>* reports) { | |
370 DCHECK(reports->empty()); | |
371 for (const auto& report : reports_) { | |
372 if (report.state == desired_state) { | |
373 if (VerifyReport(report, desired_state) != CrashReportDatabase::kNoError) | |
374 continue; | |
375 reports->push_back(report); | |
376 } | |
377 } | |
378 return CrashReportDatabase::kNoError; | |
379 } | |
380 | |
381 OperationStatus Metadata::FindSingleReport(const UUID& uuid, | |
382 const ReportDisk** out_report) { | |
383 for (size_t i = 0; i < reports_.size(); ++i) { | |
384 if (reports_[i].uuid == uuid) { | |
385 OperationStatus os = VerifyReportAnyState(reports_[i]); | |
386 if (os != CrashReportDatabase::kNoError) | |
387 return os; | |
388 *out_report = &reports_[i]; | |
389 return CrashReportDatabase::kNoError; | |
390 } | |
391 } | |
392 return CrashReportDatabase::kReportNotFound; | |
393 } | |
394 | |
395 template <class T> | |
396 OperationStatus Metadata::MutateSingleReport( | |
397 const UUID& uuid, | |
398 ReportState desired_state, | |
399 const T& mutator) { | |
400 for (size_t i = 0; i < reports_.size(); ++i) { | |
401 if (reports_[i].uuid == uuid) { | |
402 OperationStatus os = VerifyReport(reports_[i], desired_state); | |
403 if (os != CrashReportDatabase::kNoError) | |
404 return os; | |
405 mutator(&reports_[i]); | |
406 dirty_ = true; | |
407 return CrashReportDatabase::kNoError; | |
408 } | |
409 } | |
410 return CrashReportDatabase::kReportNotFound; | |
411 } | |
412 | |
413 // static | |
414 OperationStatus Metadata::VerifyReportAnyState(const ReportDisk& report_disk) { | |
415 DWORD fileattr = GetFileAttributes(report_disk.file_path.value().c_str()); | |
416 if (fileattr == INVALID_FILE_ATTRIBUTES || | |
417 (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | |
418 return CrashReportDatabase::kReportNotFound; | |
419 } | |
420 return CrashReportDatabase::kNoError; | |
421 } | |
422 | |
423 // static | |
424 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, | |
425 ReportState desired_state) { | |
426 if (report_disk.state != desired_state) | |
427 return CrashReportDatabase::kBusyError; | |
428 return VerifyReportAnyState(report_disk); | |
429 } | |
430 | |
431 class CrashReportDatabaseWin : public CrashReportDatabase { | |
432 public: | |
433 explicit CrashReportDatabaseWin(const base::FilePath& path); | |
434 ~CrashReportDatabaseWin() override; | |
435 | |
436 bool Initialize(); | |
437 | |
438 // CrashReportDatabase: | |
439 OperationStatus PrepareNewCrashReport(NewReport** report) override; | |
440 OperationStatus FinishedWritingCrashReport(NewReport* report, | |
441 UUID* uuid) override; | |
442 OperationStatus ErrorWritingCrashReport(NewReport* report) override; | |
443 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; | |
444 OperationStatus GetPendingReports( | |
445 std::vector<const Report>* reports) override; | |
446 OperationStatus GetCompletedReports( | |
447 std::vector<const Report>* reports) override; | |
448 OperationStatus GetReportForUploading(const UUID& uuid, | |
449 const Report** report) override; | |
450 OperationStatus RecordUploadAttempt(const Report* report, | |
451 bool successful, | |
452 const std::string& id) override; | |
453 OperationStatus SkipReportUpload(const UUID& uuid) override; | |
454 | |
455 private: | |
456 scoped_ptr<Metadata> AcquireMetadata(); | |
457 | |
458 base::FilePath base_dir_; | |
459 | |
460 DISALLOW_COPY_AND_ASSIGN(CrashReportDatabaseWin); | |
461 }; | |
462 | |
463 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) | |
464 : CrashReportDatabase(), base_dir_(path) { | |
465 } | |
466 | |
467 CrashReportDatabaseWin::~CrashReportDatabaseWin() { | |
468 } | |
469 | |
470 bool CrashReportDatabaseWin::Initialize() { | |
471 // Check if the database already exists. | |
472 if (!CreateOrEnsureDirectoryExists(base_dir_)) | |
473 return false; | |
474 | |
475 // Create our reports subdirectory. | |
476 if (!CreateOrEnsureDirectoryExists(base_dir_.Append(kReportsDirectory))) | |
477 return false; | |
478 | |
479 // TODO(scottmg): When are completed reports pruned from disk? Delete here or | |
480 // maybe on AcquireMetadata(). | |
481 | |
482 return true; | |
483 } | |
484 | |
485 OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( | |
486 NewReport** out_report) { | |
487 scoped_ptr<NewReportDisk> report(new NewReportDisk()); | |
488 | |
489 ::UUID system_uuid; | |
490 if (UuidCreate(&system_uuid) != RPC_S_OK) { | |
491 return kFileSystemError; | |
492 } | |
493 static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); | |
494 static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); | |
495 UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); | |
496 | |
497 report->uuid = uuid; | |
498 report->path = | |
499 base_dir_.Append(kReportsDirectory) | |
500 .Append(uuid.ToWideString() + L"." + kCrashReportFileExtension); | |
501 report->handle = LoggingOpenFileForWrite( | |
502 report->path, FileWriteMode::kCreateOrFail, FilePermissions::kOwnerOnly); | |
503 if (report->handle == INVALID_HANDLE_VALUE) | |
504 return kFileSystemError; | |
505 | |
506 *out_report = report.release(); | |
507 return kNoError; | |
508 } | |
509 | |
510 OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( | |
511 NewReport* report, | |
512 UUID* uuid) { | |
513 // Take ownership of the report, and cast to our private version with UUID. | |
514 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | |
515 // Take ownership of the file handle. | |
516 ScopedFileHandle handle(report->handle); | |
517 | |
518 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
519 if (!metadata) | |
520 return kDatabaseError; | |
521 ReportDisk report_disk; | |
522 report_disk.uuid = scoped_report->uuid; | |
523 report_disk.file_path = scoped_report->path; | |
524 report_disk.creation_time = time(nullptr); | |
525 report_disk.state = ReportState::kPending; | |
526 metadata->AddNewRecord(report_disk); | |
527 *uuid = report_disk.uuid; | |
528 return kNoError; | |
529 } | |
530 | |
531 OperationStatus CrashReportDatabaseWin::ErrorWritingCrashReport( | |
532 NewReport* report) { | |
533 // Close the outstanding handle. | |
534 LoggingCloseFile(report->handle); | |
535 | |
536 // Take ownership of the report, and cast to our private version with UUID. | |
537 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | |
538 | |
539 // We failed to write, so remove the dump file. There's no entry in the | |
540 // metadata table yet. | |
541 if (!DeleteFile(scoped_report->path.value().c_str())) { | |
542 PLOG(ERROR) << "DeleteFile " << scoped_report->path.value().c_str(); | |
543 return CrashReportDatabase::kFileSystemError; | |
544 } | |
545 | |
546 return kNoError; | |
547 } | |
548 | |
549 OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, | |
550 Report* report) { | |
551 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
552 if (!metadata) | |
553 return kDatabaseError; | |
554 // Find and return a copy of the matching report. | |
555 const ReportDisk* report_disk; | |
556 OperationStatus os = metadata->FindSingleReport(uuid, &report_disk); | |
557 if (os != kNoError) | |
558 return os; | |
559 *report = *report_disk; | |
560 return kNoError; | |
561 } | |
562 | |
563 OperationStatus CrashReportDatabaseWin::GetPendingReports( | |
564 std::vector<const Report>* reports) { | |
565 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
566 if (!metadata) | |
567 return kDatabaseError; | |
568 return metadata->FindReports(ReportState::kPending, reports); | |
569 } | |
570 | |
571 OperationStatus CrashReportDatabaseWin::GetCompletedReports( | |
572 std::vector<const Report>* reports) { | |
573 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
574 if (!metadata) | |
575 return kDatabaseError; | |
576 return metadata->FindReports(ReportState::kCompleted, reports); | |
577 } | |
578 | |
579 OperationStatus CrashReportDatabaseWin::GetReportForUploading( | |
Mark Mentovai
2015/02/10 22:16:29
If two handlers wind up running out of the same di
scottmg
2015/02/10 22:44:01
If understand the situation you're describing, the
Mark Mentovai
2015/02/10 22:49:29
scottmg wrote:
scottmg
2015/02/10 23:28:37
Not at the moment. If there's only one uploader it
Mark Mentovai
2015/02/10 23:37:03
I’d actually like to change to an interface where
scottmg
2015/02/10 23:54:40
OK. Attempted to describe what we will need to do
Mark Mentovai
2015/02/11 00:31:35
scottmg wrote:
| |
580 const UUID& uuid, | |
581 const Report** report) { | |
582 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
583 if (!metadata) | |
584 return kDatabaseError; | |
585 return metadata->MutateSingleReport( | |
586 uuid, ReportState::kPending, [report](ReportDisk* report_disk) { | |
587 report_disk->state = ReportState::kUploading; | |
588 // Create a copy for passing back to client. This will be freed in | |
589 // RecordUploadAttempt. | |
590 *report = new Report(*report_disk); | |
591 }); | |
592 } | |
593 | |
594 OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( | |
595 const Report* report, | |
596 bool successful, | |
597 const std::string& id) { | |
598 // Take ownership, allocated in GetReportForUploading. | |
599 scoped_ptr<const Report> upload_report(report); | |
600 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
601 if (!metadata) | |
602 return kDatabaseError; | |
603 return metadata->MutateSingleReport( | |
604 report->uuid, | |
605 ReportState::kUploading, | |
606 [successful, id](ReportDisk* report_disk) { | |
607 report_disk->uploaded = successful; | |
608 report_disk->id = id; | |
609 report_disk->last_upload_attempt_time = time(nullptr); | |
610 report_disk->upload_attempts++; | |
611 report_disk->state = | |
612 successful ? ReportState::kCompleted : ReportState::kPending; | |
613 }); | |
614 } | |
615 | |
616 OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { | |
617 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
618 if (!metadata) | |
619 return kDatabaseError; | |
620 return metadata->MutateSingleReport( | |
621 uuid, ReportState::kPending, [](ReportDisk* report_disk) { | |
622 report_disk->state = ReportState::kCompleted; | |
623 }); | |
624 } | |
625 | |
626 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { | |
627 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); | |
628 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); | |
629 } | |
630 | |
631 } // namespace | |
632 | |
633 // static | |
634 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( | |
635 const base::FilePath& path) { | |
636 scoped_ptr<CrashReportDatabaseWin> database_win( | |
637 new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); | |
638 if (!database_win->Initialize()) | |
639 database_win.reset(); | |
640 | |
641 return scoped_ptr<CrashReportDatabase>(database_win.release()); | |
642 } | |
643 | |
644 } // namespace crashpad | |
OLD | NEW |