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