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