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