Index: client/crash_report_database_win.cc |
diff --git a/client/crash_report_database_win.cc b/client/crash_report_database_win.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e4abf32ecfe5f6d21587d1915022991e9eeed183 |
--- /dev/null |
+++ b/client/crash_report_database_win.cc |
@@ -0,0 +1,682 @@ |
+// Copyright 2015 The Crashpad Authors. All rights reserved. |
+// |
+// Licensed under the Apache License, Version 2.0 (the "License"); |
+// you may not use this file except in compliance with the License. |
+// You may obtain a copy of the License at |
+// |
+// http://www.apache.org/licenses/LICENSE-2.0 |
+// |
+// Unless required by applicable law or agreed to in writing, software |
+// distributed under the License is distributed on an "AS IS" BASIS, |
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+// See the License for the specific language governing permissions and |
+// limitations under the License. |
+ |
+#include "client/crash_report_database.h" |
+ |
+#include <rpc.h> |
+#include <time.h> |
+#include <windows.h> |
+ |
+#include <limits> |
+ |
+#include "base/files/file_path.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/numerics/safe_math.h" |
+#include "base/strings/stringprintf.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "util/misc/uuid.h" |
+ |
+namespace crashpad { |
+ |
+namespace { |
+ |
+const wchar_t kDatabaseDirectoryName[] = L"Crashpad"; |
+ |
+const wchar_t kReportsDirectory[] = L"reports"; |
+const wchar_t kMetadataFileName[] = L"metadata"; |
+const wchar_t kMetadataTempFileName[] = L"metadata.tmp"; |
+const wchar_t kMetadataMutexName[] = L"crashpad.metadata.lock"; |
+ |
+const wchar_t kCrashReportFileExtension[] = L"dmp"; |
+ |
+enum class ReportState : int { |
+ //! \brief Just created, owned by caller and being written. |
+ kNew, |
+ //! \brief Fully filled out, owned by database. |
+ kPending, |
+ //! \brief In the process of uploading, owned by caller. |
+ kUploading, |
+ //! \brief Upload completed or skipped, owned by database. |
+ kCompleted, |
+ |
+ //! \brief Used during query to indicate a report in any state is acceptable. |
+ //! Not a valid state for a report to be in. |
+ kAny, |
+}; |
+ |
+using OperationStatus = CrashReportDatabase::OperationStatus; |
+ |
+//! \brief Ensures that the node at path is a directory, and creates it if it |
+//! does not exist. |
+//! |
+//! \return If the path points to a file, rather than a directory, or the |
+//! directory could not be created, returns false. Otherwise, returns true, |
+//! indicating that path already was or now is a directory. |
+bool CreateOrEnsureDirectoryExists(const base::FilePath& path) { |
+ if (CreateDirectory(path.value().c_str(), nullptr)) { |
+ return true; |
+ } else if (GetLastError() == ERROR_ALREADY_EXISTS) { |
+ DWORD fileattr = GetFileAttributes(path.value().c_str()); |
+ if (fileattr == INVALID_FILE_ATTRIBUTES) { |
+ PLOG(ERROR) << "GetFileAttributes"; |
+ return false; |
+ } |
+ if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) |
+ return true; |
+ LOG(ERROR) << "not a directory"; |
+ return false; |
+ } else { |
+ PLOG(ERROR) << "CreateDirectory"; |
+ return false; |
+ } |
+} |
+ |
+//! \brief Splits the given string on a the given character, returning a vector |
+//! of StringPieces. The lifetime of the input string must therefore outlive |
+//! the call to this function. |
+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.
|
+ std::vector<base::StringPiece> result; |
+ size_t last = 0; |
+ size_t end = str.size(); |
+ for (size_t i = 0; i <= end; ++i) { |
+ if (i == end || str[i] == on) { |
+ result.push_back(base::StringPiece(str.begin() + last, i - last)); |
+ last = i + 1; |
+ } |
+ } |
+ return result; |
+} |
+ |
+//! \brief A private extension of the Report class that includes additional data |
+//! that's stored on disk in the metadata file. |
+struct ReportDisk : public CrashReportDatabase::Report { |
+ //! \brief The current state of the report. |
+ ReportState state; |
+}; |
+ |
+//! \brief A private extension of the NewReport class to hold the UUID during |
+//! initial write. We don't store metadata in dump's file attributes, and |
+//! use the UUID to identify the dump on write completion. |
+struct NewReportDisk : public CrashReportDatabase::NewReport { |
+ //! \brief The UUID for this crash report. |
+ UUID uuid; |
+}; |
+ |
+//! \brief Manages the metadata for the set of reports, handling serialization |
+//! to disk, and queries. Instances of this class should be created by using |
+//! CrashReportDatabaseWin::AcquireMetadata(), rather than direct |
+//! instantiation. |
+class Metadata { |
+ public: |
+ //! \param[in] base_dir Root of storage for data files. |
+ //! \param[in] mutex_to_release An acquired mutex guarding access to the |
+ //! metadata file. The lock is owned by this object and will be released |
+ //! on destruction. The lifetime of the HANDLE is not owned. |
+ Metadata(const base::FilePath& base_dir, HANDLE mutex_to_release); |
+ ~Metadata(); |
+ |
+ //! \brief Reads the database from disk. May only be called once without an |
+ //! interceding call to Write(). |
Robert Sesek
2015/02/05 22:46:18
\return ?
scottmg
2015/02/05 23:43:11
Done.
|
+ bool Read(); |
+ |
+ //! \brief Flushes the database back to disk. It is not necessary to call this |
+ //! method if the reports were not mutated, however, Read() may not be |
+ //! called again until Write() is used. |
Robert Sesek
2015/02/05 22:46:18
\return ?
scottmg
2015/02/05 23:43:11
Done.
|
+ bool Write(); |
+ |
+ //! \brief Adds a new report to the set. Write() must be called to persist |
+ //! after adding new records. |
+ void AddNewRecord(const NewReportDisk& new_report); |
+ |
+ //! \brief Finds all reports in a given state. The reports vector is only |
+ //! valid when #kNoError is returned. |
+ OperationStatus FindReports( |
+ ReportState desired_state, |
+ std::vector<const CrashReportDatabase::Report>* reports); |
+ |
+ //! \brief Finds the report matching the given UUID and the desired state, |
+ //! returns a mutable pointer to it. Only valid if |
+ //! CrashReportDatabase::kNoError is returned. If the report is mutated by |
+ //! the caller, the caller is also responsible for calling Write() before |
+ //! destruction to persist the changes. |
+ //! |
+ //! \param[in] uuid The report identifier. |
+ //! \param[in] desired_state A specific state that the given report must be |
+ //! in. If the report is located, but is in a different state, |
+ //! CrashReportDatabase::kBusyError will be returned. If no filtering by |
+ //! state is required, ReportState::kAny can be used. |
+ //! \param[out] report_disk The found report, valid only if |
+ //! CrashReportDatabase::kNoError is returned. Ownership is not |
+ //! transferred to the caller. |
+ OperationStatus FindSingleReport(const UUID& uuid, |
+ ReportState desired_state, |
+ ReportDisk** report_disk); |
+ |
+ //! \brief Removes a single report identified by UUID. This removes both the |
+ //! entry in the metadata database, as well as the on-disk report, if any. |
+ //! Write() must be called to persist when CrashReportDatabase::kNoError |
+ //! is returned. |
+ //! |
+ //! \param[in] uuid The report identifier. |
+ OperationStatus RemoveByUUID(const UUID& uuid); |
+ |
+ private: |
+ //! \brief Confirms that the corresponding report actually exists on disk |
+ //! (that is, the dump file has not been removed), and if desired_state |
+ //! is not ReportState::kAny, confirms that the report is in the given |
+ //! state. |
+ static OperationStatus VerifyReport(const ReportDisk& report_win, |
+ ReportState desired_state); |
+ |
+ base::FilePath base_dir_; |
+ bool loaded_; |
+ HANDLE mutex_; |
+ std::vector<ReportDisk> reports_; |
+}; |
+ |
+Metadata::Metadata(const base::FilePath& base_dir, HANDLE mutex_to_release) |
+ : base_dir_(base_dir), |
+ loaded_(false), |
+ mutex_(mutex_to_release), |
+ reports_() { |
+} |
+ |
+Metadata::~Metadata() { |
+ ReleaseMutex(mutex_); |
+} |
+ |
+// The format of the metadata file is a MetadataFileHeader, followed by a |
+// number of fixed size records of MetadataFileReportRecord, followed by a |
+// string table in UTF8 format, where each string is \0 terminated. |
+ |
+#pragma pack(push, 1) |
+ |
+struct MetadataFileHeader { |
+ uint32_t magic; |
+ uint32_t version; |
+ uint32_t remaining_file_size; |
+ uint32_t num_records; |
+}; |
+ |
+struct MetadataFileReportRecord { |
+ UUID uuid; // UUID is a 16 byte, standard layout structure. |
+ uint32_t file_path_index; |
+ uint32_t id_index; |
+ int64_t creation_time; |
+ uint8_t uploaded; |
+ int64_t last_upload_attempt_time; |
+ int32_t upload_attempts; |
+ int32_t state; |
+}; |
+ |
+const uint32_t kMetadataFileHeaderMagic = 'CPAD'; |
+const uint32_t kMetadataFileVersion = 1; |
+ |
+#pragma pack(pop) |
+ |
+uint32_t AddStringToTable(std::string* string_table, const std::string& str) { |
+ uint32_t offset = base::checked_cast<uint32_t>(string_table->size()); |
+ *string_table += str; |
+ *string_table += '\0'; |
+ return offset; |
+} |
+ |
+uint32_t AddStringToTable(std::string* string_table, const std::wstring& str) { |
+ return AddStringToTable(string_table, base::UTF16ToUTF8(str)); |
+} |
+ |
+bool Metadata::Read() { |
+ DCHECK(!loaded_); |
+ base::FilePath path = base_dir_.Append(kMetadataFileName); |
+ auto input_file = ScopedFileHandle(LoggingOpenFileForRead(path)); |
+ // The file not existing is OK, we may not have created it yet. |
+ if (input_file.get() == INVALID_HANDLE_VALUE) { |
+ loaded_ = true; |
+ return true; |
+ } |
+ |
+ MetadataFileHeader header; |
+ if (!LoggingReadFile(input_file.get(), &header, sizeof(header))) |
+ return false; |
+ if (header.magic != kMetadataFileHeaderMagic || |
+ header.version != kMetadataFileVersion) { |
+ return false; |
+ } |
+ scoped_ptr<uint8_t> buffer(new uint8_t[header.remaining_file_size]); |
+ if (!LoggingReadFile( |
+ input_file.get(), buffer.get(), header.remaining_file_size)) { |
+ return false; |
+ } |
+ |
+ auto records_size = base::CheckedNumeric<uint32_t>(header.num_records) * |
+ sizeof(MetadataFileReportRecord); |
+ if (!records_size.IsValid()) |
+ return false; |
+ |
+ // Each record will have two NUL-terminated strings in the table. |
+ auto min_string_table_size = |
+ base::CheckedNumeric<uint32_t>(header.num_records) * 2; |
+ auto min_file_size = records_size + min_string_table_size; |
+ if (!min_file_size.IsValid()) |
+ return false; |
+ |
+ if (min_file_size.ValueOrDie() > header.remaining_file_size) |
+ return false; |
+ |
+ 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.
|
+ reinterpret_cast<const char*>(buffer.get() + records_size.ValueOrDie()); |
+ |
+ for (uint32_t i = 0; i < header.num_records; ++i) { |
+ ReportDisk r; |
+ const MetadataFileReportRecord* record = |
+ reinterpret_cast<const MetadataFileReportRecord*>( |
+ buffer.get() + i * sizeof(MetadataFileReportRecord)); |
+ r.uuid = record->uuid; |
+ r.file_path = base::FilePath( |
+ 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.
|
+ r.id = &string_table[record->id_index]; |
+ r.creation_time = record->creation_time; |
Robert Sesek
2015/02/05 22:46:18
> 0?
|
+ 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
|
+ r.last_upload_attempt_time = record->last_upload_attempt_time; |
+ r.upload_attempts = record->upload_attempts; |
Robert Sesek
2015/02/05 22:46:18
> 0 ?
|
+ r.state = static_cast<ReportState>(record->state); |
+ reports_.push_back(r); |
+ } |
+ loaded_ = true; |
+ return true; |
+} |
+ |
+bool Metadata::Write() { |
+ DCHECK(loaded_); |
+ base::FilePath path = base_dir_.Append(kMetadataTempFileName); |
+ |
+ // Write data to a temporary file. |
+ { |
+ auto output_file = ScopedFileHandle(LoggingOpenFileForWrite( |
+ path, FileWriteMode::kTruncateOrCreate, FilePermissions::kOwnerOnly)); |
+ |
+ // Build the records and string table we're going to write. |
+ size_t num_records = reports_.size(); |
+ std::string string_table; |
+ scoped_ptr<MetadataFileReportRecord[]> records( |
+ new MetadataFileReportRecord[num_records]); |
+ for (size_t i = 0; i < num_records; ++i) { |
+ const ReportDisk& report = reports_[i]; |
+ MetadataFileReportRecord& record = records[i]; |
+ record.uuid = report.uuid; |
+ record.file_path_index = |
+ AddStringToTable(&string_table, report.file_path.value()); |
+ record.id_index = AddStringToTable(&string_table, report.id); |
+ record.creation_time = report.creation_time; |
+ record.uploaded = report.uploaded; |
+ record.last_upload_attempt_time = report.last_upload_attempt_time; |
+ record.upload_attempts = report.upload_attempts; |
+ record.state = static_cast<uint32_t>(report.state); |
+ } |
+ |
+ // Now we have the size of the file, fill out the header. |
+ MetadataFileHeader header; |
+ header.magic = kMetadataFileHeaderMagic; |
+ header.version = kMetadataFileVersion; |
+ auto file_size = base::CheckedNumeric<uint32_t>(num_records) * |
+ sizeof(MetadataFileReportRecord) + |
+ string_table.size(); |
+ if (!file_size.IsValid()) |
+ return false; |
+ header.remaining_file_size = file_size.ValueOrDie(); |
+ header.num_records = base::checked_cast<uint32_t>(num_records); |
+ |
+ if (!LoggingWriteFile(output_file.get(), &header, sizeof(header))) |
+ return false; |
+ if (!LoggingWriteFile(output_file.get(), |
+ records.get(), |
+ num_records * sizeof(MetadataFileReportRecord))) { |
+ return false; |
+ } |
+ if (!LoggingWriteFile( |
+ output_file.get(), string_table.c_str(), string_table.size())) { |
+ return false; |
+ } |
+ } |
+ |
+ // While this is not guaranteed to be atomic, it is in practical cases. We |
+ // 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
|
+ // MoveFileEx only to avoid corrupting data in case of failure during write. |
+ bool result = MoveFileEx(path.value().c_str(), |
+ base_dir_.Append(kMetadataFileName).value().c_str(), |
+ MOVEFILE_REPLACE_EXISTING); |
+ loaded_ = !result; |
+ return result; |
+} |
+ |
+void Metadata::AddNewRecord(const NewReportDisk& new_report) { |
+ ReportDisk report; |
+ report.uuid = new_report.uuid; |
+ report.file_path = new_report.path; |
+ report.state = ReportState::kNew; |
+ reports_.push_back(report); |
+} |
+ |
+OperationStatus Metadata::FindReports( |
+ ReportState desired_state, |
+ std::vector<const CrashReportDatabase::Report>* reports) { |
+ DCHECK(reports->empty()); |
+ for (const auto& report : reports_) { |
+ if (report.state == desired_state) { |
+ if (VerifyReport(report, desired_state) != CrashReportDatabase::kNoError) |
+ continue; |
+ reports->push_back(report); |
+ } |
+ } |
+ return CrashReportDatabase::kNoError; |
+} |
+ |
+OperationStatus Metadata::FindSingleReport(const UUID& uuid, |
+ ReportState desired_state, |
+ ReportDisk** out_report) { |
+ for (size_t i = 0; i < reports_.size(); ++i) { |
+ if (reports_[i].uuid == uuid) { |
+ OperationStatus os = VerifyReport(reports_[i], desired_state); |
+ if (os != CrashReportDatabase::kNoError) |
+ return os; |
+ *out_report = &reports_[i]; |
+ return CrashReportDatabase::kNoError; |
+ } |
+ } |
+ return CrashReportDatabase::kReportNotFound; |
+} |
+ |
+OperationStatus Metadata::RemoveByUUID(const UUID& uuid) { |
+ for (size_t i = 0; i < reports_.size(); ++i) { |
+ if (reports_[i].uuid == uuid) { |
+ if (!DeleteFile(reports_[i].file_path.value().c_str())) { |
+ PLOG(ERROR) << "DeleteFile " << reports_[i].file_path.value().c_str(); |
+ return CrashReportDatabase::kFileSystemError; |
+ } |
+ reports_.erase(reports_.begin() + i); |
+ return CrashReportDatabase::kNoError; |
+ } |
+ } |
+ return CrashReportDatabase::kReportNotFound; |
+} |
+ |
+// static |
+OperationStatus Metadata::VerifyReport(const ReportDisk& report_win, |
+ ReportState desired_state) { |
+ if (desired_state != ReportState::kAny && report_win.state != desired_state) |
+ return CrashReportDatabase::kBusyError; |
+ if (GetFileAttributes(report_win.file_path.value().c_str()) == |
+ INVALID_FILE_ATTRIBUTES) { |
+ return CrashReportDatabase::kReportNotFound; |
+ } |
+ return CrashReportDatabase::kNoError; |
+} |
+ |
+class CrashReportDatabaseWin : public CrashReportDatabase { |
+ public: |
+ explicit CrashReportDatabaseWin(const base::FilePath& path); |
+ ~CrashReportDatabaseWin() override; |
+ |
+ bool Initialize(); |
+ |
+ // CrashReportDatabase:: |
+ OperationStatus PrepareNewCrashReport(NewReport** report) override; |
+ OperationStatus FinishedWritingCrashReport(NewReport* report, |
+ UUID* uuid) override; |
+ OperationStatus ErrorWritingCrashReport(NewReport* report) override; |
+ OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; |
+ OperationStatus GetPendingReports( |
+ std::vector<const Report>* reports) override; |
+ OperationStatus GetCompletedReports( |
+ std::vector<const Report>* reports) override; |
+ OperationStatus GetReportForUploading(const UUID& uuid, |
+ const Report** report) override; |
+ OperationStatus RecordUploadAttempt(const Report* report, |
+ bool successful, |
+ const std::string& id) override; |
+ OperationStatus SkipReportUpload(const UUID& uuid) override; |
+ |
+ private: |
+ scoped_ptr<Metadata> AcquireMetadata(); |
+ |
+ base::FilePath base_dir_; |
+ ScopedKernelHANDLE mutex_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CrashReportDatabaseWin); |
+}; |
+ |
+CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) |
+ : CrashReportDatabase(), base_dir_(path), mutex_() { |
+} |
+ |
+CrashReportDatabaseWin::~CrashReportDatabaseWin() { |
+} |
+ |
+bool CrashReportDatabaseWin::Initialize() { |
+ // Check if the database already exists. |
+ if (!CreateOrEnsureDirectoryExists(base_dir_)) |
+ return false; |
+ |
+ // Create our reports subdirectory. |
+ if (!CreateOrEnsureDirectoryExists(base_dir_.Append(kReportsDirectory))) |
+ return false; |
+ |
+ mutex_.reset(CreateMutex(nullptr, false, kMetadataMutexName)); |
+ |
+ // TODO(scottmg): When are completed reports pruned from disk? Delete here or |
+ // maybe on Read(). |
+ |
+ return true; |
+} |
+ |
+OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( |
+ NewReport** out_report) { |
+ scoped_ptr<Metadata> metadata(AcquireMetadata()); |
+ if (!metadata) |
+ return kFileSystemError; |
+ |
+ scoped_ptr<NewReportDisk> report(new NewReportDisk()); |
+ |
+ ::UUID system_uuid; |
+ if (UuidCreate(&system_uuid) != RPC_S_OK) { |
+ return kFileSystemError; |
+ } |
+ static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); |
+ static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); |
+ UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); |
+ |
+ report->uuid = uuid; |
+ report->path = |
+ base_dir_.Append(kReportsDirectory) |
+ .Append(uuid.ToWideString() + L"." + kCrashReportFileExtension); |
+ report->handle = LoggingOpenFileForWrite(report->path, |
+ FileWriteMode::kTruncateOrCreate, |
+ FilePermissions::kOwnerOnly); |
+ if (report->handle == INVALID_HANDLE_VALUE) |
+ return kFileSystemError; |
+ |
+ metadata->AddNewRecord(*report); |
+ if (!metadata->Write()) |
+ return kDatabaseError; |
+ *out_report = report.release(); |
+ return kNoError; |
+} |
+ |
+OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( |
+ NewReport* report, |
+ UUID* uuid) { |
+ // Take ownership of the report, and cast to our private version with UUID. |
+ scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); |
+ // Take ownership of the file handle. |
+ ScopedFileHandle handle(report->handle); |
+ |
+ scoped_ptr<Metadata> metadata(AcquireMetadata()); |
+ if (!metadata) |
+ return kFileSystemError; |
+ |
+ ReportDisk* report_disk; |
+ OperationStatus os = metadata->FindSingleReport( |
+ scoped_report->uuid, ReportState::kNew, &report_disk); |
+ if (os != kNoError) |
+ return os; |
+ report_disk->state = ReportState::kPending; |
+ report_disk->creation_time = time(nullptr); |
+ if (!metadata->Write()) |
+ return kDatabaseError; |
+ *uuid = report_disk->uuid; |
+ return kNoError; |
+} |
+ |
+OperationStatus CrashReportDatabaseWin::ErrorWritingCrashReport( |
+ NewReport* report) { |
+ // Close the outstanding handle. |
+ LoggingCloseFile(report->handle); |
+ |
+ // Take ownership of the report, and cast to our private version with UUID. |
+ scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); |
+ |
+ // Remove the report from the metadata and disk. |
+ scoped_ptr<Metadata> metadata(AcquireMetadata()); |
+ if (!metadata) |
+ return kFileSystemError; |
+ OperationStatus os = metadata->RemoveByUUID(scoped_report->uuid); |
+ if (os != kNoError) |
+ return os; |
+ if (!metadata->Write()) |
+ return kDatabaseError; |
+ |
+ return kNoError; |
+} |
+ |
+OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, |
+ Report* report) { |
+ scoped_ptr<Metadata> metadata(AcquireMetadata()); |
+ if (!metadata) |
+ return kFileSystemError; |
+ |
+ ReportDisk* report_disk; |
+ OperationStatus os = |
+ metadata->FindSingleReport(uuid, ReportState::kAny, &report_disk); |
+ if (os != kNoError) |
+ return os; |
+ *report = *report_disk; |
+ return kNoError; |
+} |
+ |
+OperationStatus CrashReportDatabaseWin::GetPendingReports( |
+ std::vector<const Report>* reports) { |
+ scoped_ptr<Metadata> metadata(AcquireMetadata()); |
+ if (!metadata) |
+ return kFileSystemError; |
+ return metadata->FindReports(ReportState::kPending, reports); |
+} |
+ |
+OperationStatus CrashReportDatabaseWin::GetCompletedReports( |
+ std::vector<const Report>* reports) { |
+ scoped_ptr<Metadata> metadata(AcquireMetadata()); |
+ if (!metadata) |
+ return kFileSystemError; |
+ return metadata->FindReports(ReportState::kCompleted, reports); |
+} |
+ |
+OperationStatus CrashReportDatabaseWin::GetReportForUploading( |
+ const UUID& uuid, |
+ const Report** report) { |
+ scoped_ptr<Metadata> metadata(AcquireMetadata()); |
+ if (!metadata) |
+ return kFileSystemError; |
+ |
+ ReportDisk* report_disk; |
+ OperationStatus os = |
+ metadata->FindSingleReport(uuid, ReportState::kPending, &report_disk); |
+ if (os != kNoError) |
+ return os; |
+ report_disk->state = ReportState::kUploading; |
+ // Create a copy for passing back to client. This will be freed in |
+ // RecordUploadAttempt. |
+ *report = new Report(*report_disk); |
+ if (!metadata->Write()) |
+ return kDatabaseError; |
+ return kNoError; |
+} |
+ |
+OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( |
+ const Report* report, |
+ bool successful, |
+ const std::string& id) { |
+ // Take ownership, allocated in GetReportForUploading. |
+ scoped_ptr<const Report> upload_report(report); |
+ |
+ scoped_ptr<Metadata> metadata(AcquireMetadata()); |
+ if (!metadata) |
+ return kFileSystemError; |
+ |
+ ReportDisk* report_disk; |
+ OperationStatus os = metadata->FindSingleReport( |
+ report->uuid, ReportState::kUploading, &report_disk); |
+ if (os != kNoError) |
+ return os; |
+ report_disk->uploaded = successful; |
+ report_disk->id = id; |
+ report_disk->last_upload_attempt_time = time(nullptr); |
+ report_disk->upload_attempts++; |
+ report_disk->state = |
+ successful ? ReportState::kCompleted : ReportState::kPending; |
+ if (!metadata->Write()) |
+ return kDatabaseError; |
+ return kNoError; |
+} |
+ |
+OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { |
+ scoped_ptr<Metadata> metadata(AcquireMetadata()); |
+ if (!metadata) |
+ return kFileSystemError; |
+ |
+ ReportDisk* report_disk; |
+ OperationStatus os = |
+ metadata->FindSingleReport(uuid, ReportState::kPending, &report_disk); |
+ if (os != kNoError) |
+ return os; |
+ report_disk->state = ReportState::kCompleted; |
+ if (!metadata->Write()) |
+ return kDatabaseError; |
+ return kNoError; |
+} |
+ |
+scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { |
+ if (WaitForSingleObject(mutex_.get(), INFINITE) != WAIT_OBJECT_0) |
+ return nullptr; |
+ scoped_ptr<Metadata> metadata(new Metadata(base_dir_, mutex_.get())); |
+ if (!metadata->Read()) { |
+ LOG(ERROR) << "metadata file could not be loaded"; |
+ return nullptr; |
+ } |
+ return metadata; |
+} |
+ |
+} // namespace |
+ |
+// static |
+scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( |
+ const base::FilePath& path) { |
+ scoped_ptr<CrashReportDatabaseWin> database_win( |
+ new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); |
+ if (!database_win->Initialize()) |
+ database_win.reset(); |
+ |
+ return scoped_ptr<CrashReportDatabase>(database_win.release()); |
+} |
+ |
+} // namespace crashpad |