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> | |
Mark Mentovai
2015/02/11 17:25:50
Unused?
scottmg
2015/02/11 19:24:54
Done.
| |
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, | |
Mark Mentovai
2015/02/11 17:25:50
Tiny minor nit: true and false in their literal se
scottmg
2015/02/11 19:24:55
Done.
| |
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; | |
Mark Mentovai
2015/02/11 17:25:50
Friends go in the private section. http://google-s
scottmg
2015/02/11 19:24:54
Done.
| |
144 | |
145 private: | |
146 Metadata(FileHandle handle, const base::FilePath& report_dir); | |
147 | |
148 bool 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}; | |
Mark Mentovai
2015/02/11 17:25:50
Duplicate the “not actually async” comment from wh
scottmg
2015/02/11 19:24:54
Done.
| |
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 }; | |
Mark Mentovai
2015/02/11 17:25:49
I think it’s worth making the padding explicit wit
scottmg
2015/02/11 19:24:55
Done (padded header to 64b too). Any particular re
Mark Mentovai
2015/02/11 19:39:56
scottmg wrote:
| |
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); | |
Robert Sesek
2015/02/11 17:24:31
DCHECK_EQ
Mark Mentovai
2015/02/11 17:25:50
DCHECK_EQ(a, b) instead of DCHECK(a == b). Also, a
scottmg
2015/02/11 19:24:54
Done.
scottmg
2015/02/11 19:24:54
Done.
| |
217 size_t data_length = static_cast<size_t>(end - read_from); | |
218 std::vector<uint8_t> buffer; | |
Mark Mentovai
2015/02/11 17:25:50
These two lines can be combined into one: std::vec
scottmg
2015/02/11 19:24:55
Done.
| |
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 | |
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. | |
Mark Mentovai
2015/02/11 17:25:50
Orphaned. The reports are still on disk (for now),
scottmg
2015/02/11 19:24:55
Right. Done.
| |
268 if (!metadata->Read()) | |
269 LOG(ERROR) << "failed to read metadata; database reset"; | |
270 return metadata; | |
271 } | |
272 | |
273 bool Metadata::Read() { | |
274 // Check if the file's empty, which we want to count as a successful read. | |
275 FileOffset length = LoggingSeekFile(handle_.get(), 0, SEEK_END); | |
276 if (length == -1) | |
277 return false; | |
278 if (length == 0) | |
279 return true; | |
280 // Rewind to beginning of file. | |
281 if (LoggingSeekFile(handle_.get(), 0, SEEK_SET) == -1) | |
Mark Mentovai
2015/02/11 17:25:50
Add a private helper method or function to rewind
scottmg
2015/02/11 19:24:54
Done.
| |
282 return false; | |
283 | |
284 MetadataFileHeader header; | |
285 if (!LoggingReadFile(handle_.get(), &header, sizeof(header))) | |
286 return false; | |
287 if (header.magic != kMetadataFileHeaderMagic || | |
288 header.version != kMetadataFileVersion) { | |
289 return false; | |
Mark Mentovai
2015/02/11 17:25:50
Log something about the mismatch, and about any ot
scottmg
2015/02/11 19:24:55
Done.
| |
290 } | |
291 | |
292 auto records_size = base::CheckedNumeric<uint32_t>(header.num_records) * | |
293 sizeof(MetadataFileReportRecord); | |
294 if (!records_size.IsValid()) | |
295 return false; | |
296 | |
297 scoped_ptr<MetadataFileReportRecord[]> records( | |
298 new MetadataFileReportRecord[header.num_records]); | |
299 if (!LoggingReadFile(handle_.get(), records.get(), records_size.ValueOrDie())) | |
300 return false; | |
301 | |
302 std::vector<uint8_t> string_buffer = ReadRestOfFile(handle_.get()); | |
303 if (string_buffer.empty() || string_buffer.back() != 0) | |
304 return false; | |
305 const char* string_table = reinterpret_cast<const char*>(&string_buffer[0]); | |
Mark Mentovai
2015/02/11 17:25:50
This is fine if you have a reason to maintain the
scottmg
2015/02/11 19:24:54
Done (as std::string)
| |
306 | |
307 for (uint32_t i = 0; i < header.num_records; ++i) { | |
308 ReportDisk r; | |
309 const MetadataFileReportRecord* record = &records[i]; | |
310 r.uuid = record->uuid; | |
311 if (record->file_path_index >= string_buffer.size() || | |
312 record->id_index >= string_buffer.size()) { | |
313 reports_.clear(); | |
314 return false; | |
315 } | |
316 r.file_path = report_dir_.Append( | |
317 base::UTF8ToUTF16(&string_table[record->file_path_index])); | |
318 r.id = &string_table[record->id_index]; | |
319 r.creation_time = record->creation_time; | |
320 r.uploaded = record->uploaded; | |
321 r.last_upload_attempt_time = record->last_upload_attempt_time; | |
322 r.upload_attempts = record->upload_attempts; | |
323 r.state = static_cast<ReportState>(record->state); | |
324 reports_.push_back(r); | |
325 } | |
326 | |
327 return true; | |
328 } | |
329 | |
330 void Metadata::Write() { | |
331 if (LoggingSeekFile(handle_.get(), SEEK_SET, 0) == -1) | |
332 return; | |
333 | |
334 size_t num_records = reports_.size(); | |
335 | |
336 // Fill and write out the header. | |
337 MetadataFileHeader header; | |
338 header.magic = kMetadataFileHeaderMagic; | |
339 header.version = kMetadataFileVersion; | |
340 header.num_records = base::checked_cast<uint32_t>(num_records); | |
341 if (!LoggingWriteFile(handle_.get(), &header, sizeof(header))) | |
Mark Mentovai
2015/02/11 17:25:50
You’re recycling the existing file, right? Shouldn
scottmg
2015/02/11 19:24:54
Done.
| |
342 return; | |
343 | |
344 // Build the records and string table we're going to write. | |
345 std::string string_table; | |
346 scoped_ptr<MetadataFileReportRecord[]> records( | |
347 new MetadataFileReportRecord[num_records]); | |
348 for (size_t i = 0; i < num_records; ++i) { | |
349 const ReportDisk& report = reports_[i]; | |
350 MetadataFileReportRecord& record = records[i]; | |
351 record.uuid = report.uuid; | |
352 const std::wstring& path = report.file_path.value(); | |
353 if (path.substr(0, report_dir_.value().size()) != report_dir_.value()) { | |
Mark Mentovai
2015/02/11 17:25:50
For this to be correct, you need to ensure that re
scottmg
2015/02/11 19:24:54
I'm not sure why I didn't use FilePath members. Fi
| |
354 LOG(ERROR) << path.c_str() << " expected to start with " | |
355 << report_dir_.value().c_str() << " and does not"; | |
356 return; | |
357 } | |
358 record.file_path_index = AddStringToTable( | |
359 &string_table, path.substr(report_dir_.value().size() + 1)); | |
Mark Mentovai
2015/02/11 17:25:50
Take extra care with that concern on this line too
scottmg
2015/02/11 19:24:54
Done.
| |
360 record.id_index = AddStringToTable(&string_table, report.id); | |
361 record.creation_time = report.creation_time; | |
362 record.uploaded = report.uploaded; | |
363 record.last_upload_attempt_time = report.last_upload_attempt_time; | |
364 record.upload_attempts = report.upload_attempts; | |
365 record.state = static_cast<uint32_t>(report.state); | |
366 } | |
367 | |
368 if (!LoggingWriteFile(handle_.get(), | |
369 records.get(), | |
370 num_records * sizeof(MetadataFileReportRecord))) { | |
371 return; | |
372 } | |
373 if (!LoggingWriteFile( | |
374 handle_.get(), string_table.c_str(), string_table.size())) { | |
375 return; | |
376 } | |
377 } | |
378 | |
379 void Metadata::AddNewRecord(const ReportDisk& new_report_disk) { | |
380 DCHECK(new_report_disk.state == ReportState::kPending); | |
381 reports_.push_back(new_report_disk); | |
382 dirty_ = true; | |
383 } | |
384 | |
385 OperationStatus Metadata::FindReports( | |
386 ReportState desired_state, | |
387 std::vector<const CrashReportDatabase::Report>* reports) { | |
388 DCHECK(reports->empty()); | |
389 for (const auto& report : reports_) { | |
390 if (report.state == desired_state) { | |
391 if (VerifyReport(report, desired_state) != CrashReportDatabase::kNoError) | |
392 continue; | |
393 reports->push_back(report); | |
394 } | |
395 } | |
396 return CrashReportDatabase::kNoError; | |
397 } | |
398 | |
399 OperationStatus Metadata::FindSingleReport(const UUID& uuid, | |
400 const ReportDisk** out_report) { | |
401 for (size_t i = 0; i < reports_.size(); ++i) { | |
402 if (reports_[i].uuid == uuid) { | |
403 OperationStatus os = VerifyReportAnyState(reports_[i]); | |
404 if (os != CrashReportDatabase::kNoError) | |
405 return os; | |
406 *out_report = &reports_[i]; | |
407 return CrashReportDatabase::kNoError; | |
408 } | |
409 } | |
410 return CrashReportDatabase::kReportNotFound; | |
411 } | |
412 | |
413 template <class T> | |
414 OperationStatus Metadata::MutateSingleReport( | |
415 const UUID& uuid, | |
416 ReportState desired_state, | |
417 const T& mutator) { | |
418 for (size_t i = 0; i < reports_.size(); ++i) { | |
419 if (reports_[i].uuid == uuid) { | |
420 OperationStatus os = VerifyReport(reports_[i], desired_state); | |
421 if (os != CrashReportDatabase::kNoError) | |
422 return os; | |
423 mutator(&reports_[i]); | |
424 dirty_ = true; | |
425 return CrashReportDatabase::kNoError; | |
426 } | |
427 } | |
428 return CrashReportDatabase::kReportNotFound; | |
429 } | |
430 | |
431 // static | |
432 OperationStatus Metadata::VerifyReportAnyState(const ReportDisk& report_disk) { | |
433 DWORD fileattr = GetFileAttributes(report_disk.file_path.value().c_str()); | |
434 if (fileattr == INVALID_FILE_ATTRIBUTES || | |
435 (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | |
Mark Mentovai
2015/02/11 17:25:50
If it’s a directory, that can be kFilesystemError.
scottmg
2015/02/11 19:24:54
Done.
| |
436 return CrashReportDatabase::kReportNotFound; | |
437 } | |
438 return CrashReportDatabase::kNoError; | |
439 } | |
440 | |
441 // static | |
442 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, | |
443 ReportState desired_state) { | |
444 if (report_disk.state != desired_state) | |
445 return CrashReportDatabase::kBusyError; | |
446 return VerifyReportAnyState(report_disk); | |
447 } | |
448 | |
449 class CrashReportDatabaseWin : public CrashReportDatabase { | |
450 public: | |
451 explicit CrashReportDatabaseWin(const base::FilePath& path); | |
452 ~CrashReportDatabaseWin() override; | |
453 | |
454 bool Initialize(); | |
455 | |
456 // CrashReportDatabase: | |
457 OperationStatus PrepareNewCrashReport(NewReport** report) override; | |
458 OperationStatus FinishedWritingCrashReport(NewReport* report, | |
459 UUID* uuid) override; | |
460 OperationStatus ErrorWritingCrashReport(NewReport* report) override; | |
461 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; | |
462 OperationStatus GetPendingReports( | |
463 std::vector<const Report>* reports) override; | |
464 OperationStatus GetCompletedReports( | |
465 std::vector<const Report>* reports) override; | |
466 OperationStatus GetReportForUploading(const UUID& uuid, | |
467 const Report** report) override; | |
468 OperationStatus RecordUploadAttempt(const Report* report, | |
469 bool successful, | |
470 const std::string& id) override; | |
471 OperationStatus SkipReportUpload(const UUID& uuid) override; | |
472 | |
473 private: | |
474 scoped_ptr<Metadata> AcquireMetadata(); | |
475 | |
476 base::FilePath base_dir_; | |
477 | |
478 DISALLOW_COPY_AND_ASSIGN(CrashReportDatabaseWin); | |
479 }; | |
480 | |
481 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) | |
482 : CrashReportDatabase(), base_dir_(path) { | |
483 } | |
484 | |
485 CrashReportDatabaseWin::~CrashReportDatabaseWin() { | |
486 } | |
487 | |
488 bool CrashReportDatabaseWin::Initialize() { | |
489 // Check if the database already exists. | |
490 if (!CreateOrEnsureDirectoryExists(base_dir_)) | |
491 return false; | |
492 | |
493 // Create our reports subdirectory. | |
494 if (!CreateOrEnsureDirectoryExists(base_dir_.Append(kReportsDirectory))) | |
495 return false; | |
496 | |
497 // TODO(scottmg): When are completed reports pruned from disk? Delete here or | |
498 // maybe on AcquireMetadata(). | |
499 | |
500 return true; | |
501 } | |
502 | |
503 OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( | |
504 NewReport** out_report) { | |
505 scoped_ptr<NewReportDisk> report(new NewReportDisk()); | |
506 | |
507 ::UUID system_uuid; | |
508 if (UuidCreate(&system_uuid) != RPC_S_OK) { | |
509 return kFileSystemError; | |
510 } | |
511 static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); | |
512 static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); | |
513 UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); | |
514 | |
515 report->uuid = uuid; | |
516 report->path = | |
517 base_dir_.Append(kReportsDirectory) | |
518 .Append(uuid.ToWideString() + L"." + kCrashReportFileExtension); | |
519 report->handle = LoggingOpenFileForWrite( | |
520 report->path, FileWriteMode::kCreateOrFail, FilePermissions::kOwnerOnly); | |
521 if (report->handle == INVALID_HANDLE_VALUE) | |
522 return kFileSystemError; | |
523 | |
524 *out_report = report.release(); | |
525 return kNoError; | |
526 } | |
527 | |
528 OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( | |
529 NewReport* report, | |
530 UUID* uuid) { | |
531 // Take ownership of the report, and cast to our private version with UUID. | |
532 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | |
533 // Take ownership of the file handle. | |
534 ScopedFileHandle handle(report->handle); | |
535 | |
536 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
537 if (!metadata) | |
538 return kDatabaseError; | |
539 ReportDisk report_disk; | |
540 report_disk.uuid = scoped_report->uuid; | |
541 report_disk.file_path = scoped_report->path; | |
542 report_disk.creation_time = time(nullptr); | |
543 report_disk.state = ReportState::kPending; | |
544 metadata->AddNewRecord(report_disk); | |
545 *uuid = report_disk.uuid; | |
546 return kNoError; | |
547 } | |
548 | |
549 OperationStatus CrashReportDatabaseWin::ErrorWritingCrashReport( | |
550 NewReport* report) { | |
551 // Close the outstanding handle. | |
552 LoggingCloseFile(report->handle); | |
553 | |
554 // Take ownership of the report, and cast to our private version with UUID. | |
Mark Mentovai
2015/02/11 17:25:49
Do this first, to ensure that the resource is owne
scottmg
2015/02/11 19:24:55
Done.
| |
555 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | |
556 | |
557 // We failed to write, so remove the dump file. There's no entry in the | |
558 // metadata table yet. | |
559 if (!DeleteFile(scoped_report->path.value().c_str())) { | |
560 PLOG(ERROR) << "DeleteFile " << scoped_report->path.value().c_str(); | |
561 return CrashReportDatabase::kFileSystemError; | |
562 } | |
563 | |
564 return kNoError; | |
565 } | |
566 | |
567 OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, | |
568 Report* report) { | |
569 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
570 if (!metadata) | |
571 return kDatabaseError; | |
572 // Find and return a copy of the matching report. | |
573 const ReportDisk* report_disk; | |
574 OperationStatus os = metadata->FindSingleReport(uuid, &report_disk); | |
575 if (os != kNoError) | |
576 return os; | |
577 *report = *report_disk; | |
578 return kNoError; | |
579 } | |
580 | |
581 OperationStatus CrashReportDatabaseWin::GetPendingReports( | |
582 std::vector<const Report>* reports) { | |
583 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
584 if (!metadata) | |
585 return kDatabaseError; | |
586 return metadata->FindReports(ReportState::kPending, reports); | |
587 } | |
588 | |
589 OperationStatus CrashReportDatabaseWin::GetCompletedReports( | |
590 std::vector<const Report>* reports) { | |
591 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
592 if (!metadata) | |
593 return kDatabaseError; | |
594 return metadata->FindReports(ReportState::kCompleted, reports); | |
595 } | |
596 | |
597 OperationStatus CrashReportDatabaseWin::GetReportForUploading( | |
598 const UUID& uuid, | |
599 const Report** report) { | |
600 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
601 if (!metadata) | |
602 return kDatabaseError; | |
603 // TODO(scottmg): After returning this report to the client, there is no way | |
604 // to reap this report if the uploader fails to call RecordUploadAttempt() or | |
605 // SkipReportUpload() (if it crashed or was otherwise buggy). To resolve this, | |
606 // one possibility would be to change the interface to be FileHandle based, so | |
607 // that instead of giving the file_path back to the client and changing state | |
608 // to kUploading, we return an exclusive access handle, and use that as the | |
609 // signal that the upload is pending, rather than an update to state in the | |
610 // metadata. Alternatively, there could be a "garbage collection" at startup | |
611 // where any reports that are orphaned in the kUploading state are either | |
612 // reset to kPending to retry, or discarded. | |
613 return metadata->MutateSingleReport( | |
614 uuid, ReportState::kPending, [report](ReportDisk* report_disk) { | |
615 report_disk->state = ReportState::kUploading; | |
616 // Create a copy for passing back to client. This will be freed in | |
617 // RecordUploadAttempt. | |
618 *report = new Report(*report_disk); | |
619 }); | |
620 } | |
621 | |
622 OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( | |
623 const Report* report, | |
624 bool successful, | |
625 const std::string& id) { | |
626 // Take ownership, allocated in GetReportForUploading. | |
627 scoped_ptr<const Report> upload_report(report); | |
628 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
629 if (!metadata) | |
630 return kDatabaseError; | |
631 return metadata->MutateSingleReport( | |
632 report->uuid, | |
633 ReportState::kUploading, | |
634 [successful, id](ReportDisk* report_disk) { | |
635 report_disk->uploaded = successful; | |
636 report_disk->id = id; | |
637 report_disk->last_upload_attempt_time = time(nullptr); | |
638 report_disk->upload_attempts++; | |
639 report_disk->state = | |
640 successful ? ReportState::kCompleted : ReportState::kPending; | |
641 }); | |
642 } | |
643 | |
644 OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { | |
645 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
646 if (!metadata) | |
647 return kDatabaseError; | |
648 return metadata->MutateSingleReport( | |
649 uuid, ReportState::kPending, [](ReportDisk* report_disk) { | |
650 report_disk->state = ReportState::kCompleted; | |
651 }); | |
652 } | |
653 | |
654 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { | |
655 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); | |
656 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); | |
657 } | |
658 | |
659 } // namespace | |
660 | |
661 // static | |
662 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( | |
663 const base::FilePath& path) { | |
664 scoped_ptr<CrashReportDatabaseWin> database_win( | |
665 new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); | |
666 if (!database_win->Initialize()) | |
667 database_win.reset(); | |
668 | |
669 return scoped_ptr<CrashReportDatabase>(database_win.release()); | |
670 } | |
671 | |
672 } // namespace crashpad | |
OLD | NEW |