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 <time.h> | |
| 18 #include <windows.h> | |
| 19 | |
| 20 #include <limits> | |
| 21 | |
| 22 #include "base/files/file_path.h" | |
| 23 #include "base/logging.h" | |
| 24 #include "base/memory/scoped_ptr.h" | |
| 25 #include "base/strings/stringprintf.h" | |
| 26 #include "base/strings/utf_string_conversions.h" | |
| 27 #include "util/misc/uuid.h" | |
| 28 | |
| 29 namespace crashpad { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 const wchar_t kDatabaseDirectoryName[] = L"Crashpad"; | |
| 34 | |
| 35 const wchar_t kReportsDirectory[] = L"reports"; | |
| 36 const wchar_t kMetadataFileName[] = L"metadata"; | |
| 37 const wchar_t kMetadataTempFileName[] = L"metadata.tmp"; | |
| 38 const wchar_t kMetadataMutexName[] = L"crashpad.metadata.lock"; | |
| 39 | |
| 40 const wchar_t kCrashReportFileExtension[] = L"dmp"; | |
| 41 | |
| 42 enum class ReportState : int { | |
| 43 //! \brief Just created, owned by caller and being written. | |
| 44 kNew, | |
| 45 //! \brief Fully filled out, owned by database. | |
| 46 kPending, | |
| 47 //! \brief In the process of uploading, owned by caller. | |
| 48 kUploading, | |
| 49 //! \brief Upload completed or skipped, owned by database. | |
| 50 kCompleted, | |
| 51 | |
| 52 //! \brief Used during query to indicate a report in any state is acceptable. | |
| 53 //! Not a valid state for a report to be in. | |
| 54 kAny, | |
| 55 }; | |
| 56 | |
| 57 using OperationStatus = CrashReportDatabase::OperationStatus; | |
| 58 | |
| 59 //! \brief Ensures that the node at path is a directory, and creates it if it | |
| 60 //! does not exist. | |
| 61 //! | |
| 62 //! If the path points to a file, rather than a directory, or the directory | |
|
Robert Sesek
2015/02/02 23:06:08
\return ?
scottmg
2015/02/03 18:56:39
Done.
| |
| 63 //! could not be created, returns false. Otherwise, returns true, indicating | |
| 64 //! that path already was or now is a directory. | |
| 65 bool CreateOrEnsureDirectoryExists(const base::FilePath& path) { | |
| 66 if (CreateDirectory(path.value().c_str(), nullptr)) { | |
| 67 return true; | |
| 68 } else if (GetLastError() == ERROR_ALREADY_EXISTS) { | |
| 69 DWORD fileattr = GetFileAttributes(path.value().c_str()); | |
| 70 if (fileattr == INVALID_FILE_ATTRIBUTES) { | |
| 71 PLOG(ERROR) << "GetFileAttributes"; | |
| 72 return false; | |
| 73 } | |
| 74 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | |
| 75 return true; | |
| 76 LOG(ERROR) << "not a directory"; | |
| 77 return false; | |
| 78 } else { | |
| 79 PLOG(ERROR) << "CreateDirectory"; | |
| 80 return false; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 //! \brief Attempts to convert the given string to an int64. Only succeeds if | |
| 85 //! the entire string is consumed. | |
| 86 bool GetInt64(base::StringPiece str, int64_t* as_int) { | |
| 87 char* endptr; | |
| 88 std::string as_string = str.as_string(); | |
| 89 *as_int = _strtoi64(as_string.c_str(), &endptr, 10); | |
| 90 return endptr == &as_string.end()[0]; | |
| 91 } | |
| 92 | |
| 93 //! \brief Splits the given string on a the given character, returning a vector | |
| 94 //! of StringPieces. The lifetime of the input string must therefore outlive | |
| 95 //! the call to this function. | |
| 96 std::vector<base::StringPiece> SplitString(base::StringPiece str, char on) { | |
| 97 std::vector<base::StringPiece> result; | |
| 98 size_t last = 0; | |
| 99 size_t end = str.size(); | |
| 100 for (size_t i = 0; i <= end; ++i) { | |
| 101 if (i == end || str[i] == on) { | |
| 102 result.push_back(base::StringPiece(str.begin() + last, i - last)); | |
| 103 last = i + 1; | |
| 104 } | |
| 105 } | |
| 106 return result; | |
| 107 } | |
| 108 | |
| 109 //! \brief A private extension of the Report class that includes additional data | |
| 110 //! that's stored on disk in the metadata file. | |
| 111 struct ReportDisk : public CrashReportDatabase::Report { | |
| 112 //! \brief The current state of the report. | |
| 113 ReportState state; | |
| 114 }; | |
| 115 | |
| 116 //! \brief A private extension of the NewReport class to hold the UUID during | |
| 117 //! initial write. We don't store metadata in dump's file attributes, and | |
| 118 //! use the UUID to identify the dump on write completion. | |
| 119 struct NewReportDisk : public CrashReportDatabase::NewReport { | |
| 120 //! \brief The UUID for this crash report. | |
| 121 UUID uuid; | |
| 122 }; | |
| 123 | |
| 124 //! \brief Manages the metadata for the set of reports, handling serialization | |
| 125 //! to disk, and queries. | |
|
Robert Sesek
2015/02/02 23:06:08
Mention that this class shouldn't be instantiated
scottmg
2015/02/03 18:56:40
Done.
| |
| 126 class Metadata { | |
| 127 public: | |
| 128 //! \brief mutex_to_release must already be acquired. The lock is passed to | |
|
Robert Sesek
2015/02/02 23:06:08
Use \param instead?
scottmg
2015/02/03 18:56:39
Done.
| |
| 129 //! this object and will be released on destruction. | |
| 130 Metadata(const base::FilePath& base_dir, HANDLE mutex_to_release); | |
| 131 ~Metadata(); | |
| 132 | |
| 133 //! \brief Reads the database from disk. May only be called once without an | |
| 134 //! interceding call to Write(). | |
| 135 bool Read(); | |
| 136 | |
| 137 //! \brief Flushes the database back to disk. It is not necessary to call this | |
| 138 //! method the reports were not mutated, however, Read() may not be called | |
|
Robert Sesek
2015/02/02 23:06:08
Missing a word in this sentence.
scottmg
2015/02/03 18:56:39
Done.
| |
| 139 //! again until Write() is used. | |
| 140 bool Write(); | |
| 141 | |
| 142 //! \brief Adds a new report to the set. | |
|
Robert Sesek
2015/02/02 23:06:08
"Must call Write() to persist" ?
scottmg
2015/02/03 18:56:39
Done.
| |
| 143 void AddNewRecord(const NewReportDisk& new_report); | |
| 144 | |
| 145 //! \brief Finds all reports in a given state. The reports vector is only | |
| 146 //! valid when #kNoError is returned. | |
| 147 OperationStatus FindReports( | |
| 148 ReportState desired_state, | |
| 149 std::vector<const CrashReportDatabase::Report>* reports); | |
| 150 | |
| 151 //! \brief Finds the report matching the given UUID and the desired state, | |
| 152 //! returns a mutable pointer to it. Only valid if #kNoError is returned. | |
| 153 //! If the report is mutated by the caller, the caller is also responsible | |
| 154 //! for calling Write() before destruction. | |
|
Robert Sesek
2015/02/02 23:06:08
Is the caller also responsible for destruction? \p
scottmg
2015/02/03 18:56:39
Done.
| |
| 155 OperationStatus Metadata::FindSingleReport(const UUID& uuid, | |
| 156 ReportState desired_state, | |
| 157 ReportDisk** report_disk); | |
| 158 | |
| 159 private: | |
| 160 //! \brief Confirms that the corresponding report actually exists on disk | |
| 161 //! (that is, the dump file has not been removed), and if desired_state | |
| 162 //! is not ReportState::kAny, confirms that the report is in the given | |
| 163 //! state. | |
| 164 static OperationStatus VerifyReport(const ReportDisk& report_win, | |
| 165 ReportState desired_state); | |
| 166 | |
| 167 base::FilePath base_dir_; | |
| 168 bool loaded_; | |
| 169 HANDLE mutex_; | |
| 170 std::vector<ReportDisk> reports_; | |
| 171 }; | |
| 172 | |
| 173 Metadata::Metadata(const base::FilePath& base_dir, HANDLE mutex_to_release) | |
| 174 : base_dir_(base_dir), loaded_(false), mutex_(mutex_to_release) { | |
|
Robert Sesek
2015/02/02 23:06:08
, reports_() for consistency with full member init
scottmg
2015/02/03 18:56:39
Done.
| |
| 175 } | |
| 176 | |
| 177 Metadata::~Metadata() { | |
| 178 ReleaseMutex(mutex_); | |
| 179 } | |
| 180 | |
| 181 // The format of the metadata file is: | |
| 182 // | |
| 183 // uint32_t num_bytes_in_file | |
| 184 // "crashpad metadata\n" | |
| 185 // decimal integer "\n" -- file format version, currently "1". | |
| 186 // | |
| 187 // The remainder of the file is a variable number of lines, one for each report. | |
| 188 // Each line is terminated by a single \n. Elements in each line are separated | |
| 189 // by \t, and each element corresponds to the fields of `ReportDisk`. There is | |
| 190 // no terminator field, as the initial file length is sufficient to determine | |
| 191 // the number of records. | |
| 192 | |
| 193 bool Metadata::Read() { | |
| 194 DCHECK(!loaded_); | |
| 195 base::FilePath path = base_dir_.Append(kMetadataFileName); | |
| 196 ScopedFileHandle input_file(ScopedFileHandle(LoggingOpenFileForRead(path))); | |
|
Robert Sesek
2015/02/02 23:06:08
Why do you need to invoke ScopedFileHandle()'s cop
scottmg
2015/02/03 18:56:39
Do you mean why is it not written as ScopedFileHan
Robert Sesek
2015/02/05 18:43:09
Ah, I missed that the ctor was explicit. You can u
scottmg
2015/02/05 19:27:55
Done.
| |
| 197 // The file not existing is OK, we may not have created it yet. | |
| 198 if (input_file.get() == INVALID_HANDLE_VALUE) { | |
| 199 loaded_ = true; | |
| 200 return true; | |
| 201 } | |
| 202 | |
| 203 uint32_t num_bytes; | |
| 204 if (!LoggingReadFile(input_file.get(), &num_bytes, sizeof(num_bytes))) | |
| 205 return false; | |
| 206 scoped_ptr<char[]> buffer(new char[num_bytes]); | |
| 207 if (!LoggingReadFile(input_file.get(), buffer.get(), num_bytes)) | |
| 208 return false; | |
| 209 | |
| 210 base::StringPiece contents(buffer.get(), num_bytes); | |
| 211 std::vector<base::StringPiece> lines(SplitString(contents, '\n')); | |
| 212 // TODO(scottmg): Add StringPiece(string), and ==, != to avoid some of the | |
| 213 // as_string() calls. | |
| 214 if (lines[0].as_string() != "crashpad metadata") | |
|
Robert Sesek
2015/02/03 16:29:29
I thought about this a little more. It's a bit odd
scottmg
2015/02/03 18:56:39
I don't think there's a risk of corruption given t
| |
| 215 return false; | |
| 216 int64_t version; | |
|
Robert Sesek
2015/02/02 23:06:08
int32_t isn't big enough for the version, but uint
scottmg
2015/02/03 18:56:39
Yeah, was just to avoid another string->int functi
| |
| 217 if (!GetInt64(lines[1], &version) || version != 1) | |
| 218 return false; | |
| 219 // To lines - 1 because there'll be an empty line due to trailing \n. | |
| 220 for (size_t i = 2; i < lines.size() - 1; ++i) { | |
| 221 ReportDisk r; | |
| 222 std::vector<base::StringPiece> parts(SplitString(lines[i], '\t')); | |
| 223 if (parts.size() != 8) | |
| 224 return false; | |
|
Robert Sesek
2015/02/02 23:06:08
Would it be useful to LOG(ERROR) in these conditio
| |
| 225 if (!r.uuid.InitializeFromString(parts[0])) | |
| 226 return false; | |
| 227 r.file_path = base::FilePath(base::UTF8ToUTF16(parts[1].as_string())); | |
| 228 r.id = parts[2].as_string(); | |
| 229 if (!GetInt64(parts[3], &r.creation_time)) | |
| 230 return false; | |
| 231 int64_t uploaded; | |
| 232 if (!GetInt64(parts[4], &uploaded) || (uploaded != 0 && uploaded != 1)) | |
| 233 return false; | |
| 234 r.uploaded = static_cast<bool>(uploaded); | |
| 235 if (!GetInt64(parts[5], &r.last_upload_attempt_time)) | |
| 236 return false; | |
| 237 int64_t upload_attempts; | |
| 238 if (!GetInt64(parts[6], &upload_attempts) || | |
| 239 upload_attempts > std::numeric_limits<int>::max()) { | |
|
Robert Sesek
2015/02/02 23:06:08
|| upload_attempts < 0 , too?
scottmg
2015/02/03 18:56:39
Good catch, done.
| |
| 240 return false; | |
| 241 } | |
| 242 r.upload_attempts = static_cast<int>(upload_attempts); | |
| 243 int64_t state; | |
| 244 if (!GetInt64(parts[7], &state)) | |
| 245 return false; | |
| 246 r.state = static_cast<ReportState>(state); | |
| 247 CHECK(r.state != ReportState::kAny); | |
|
Robert Sesek
2015/02/02 23:06:08
This CHECKs but the rest return false.
scottmg
2015/02/03 18:56:39
Done.
| |
| 248 reports_.push_back(r); | |
| 249 } | |
| 250 loaded_ = true; | |
| 251 return true; | |
| 252 } | |
| 253 | |
| 254 bool Metadata::Write() { | |
| 255 DCHECK(loaded_); | |
| 256 base::FilePath path = base_dir_.Append(kMetadataTempFileName); | |
| 257 | |
| 258 // Write data to a temporary file. | |
| 259 { | |
| 260 ScopedFileHandle output_file(ScopedFileHandle(LoggingOpenFileForWrite( | |
| 261 path, FileWriteMode::kTruncateOrCreate, FilePermissions::kOwnerOnly))); | |
| 262 std::string to_output = "crashpad metadata\n1\n"; | |
|
Robert Sesek
2015/02/02 23:06:08
Move "crashpad metadata" and the version number to
| |
| 263 for (const auto& report : reports_) { | |
| 264 // All the other fields are internal, but make sure the client does not | |
| 265 // use our separators in |id|. | |
| 266 DCHECK(report.id.find_first_of("\t\n") == std::string::npos) | |
| 267 << "id may not contain \\t or \\n"; | |
| 268 to_output += base::StringPrintf( | |
| 269 "%s\t" | |
| 270 "%s\t" | |
| 271 "%s\t" | |
| 272 "%I64d\t" | |
| 273 "%d\t" | |
| 274 "%I64d\t" | |
| 275 "%d\t" | |
| 276 "%d\n", | |
| 277 report.uuid.ToString().c_str(), | |
| 278 base::UTF16ToUTF8(report.file_path.value()).c_str(), | |
|
Robert Sesek
2015/02/02 23:06:08
Is it legal for Windows filepaths/names to contain
scottmg
2015/02/03 18:56:40
No, nothing less than integer value 31 is valid. h
| |
| 279 report.id.c_str(), | |
| 280 report.creation_time, | |
| 281 report.uploaded, | |
| 282 report.last_upload_attempt_time, | |
| 283 report.upload_attempts, | |
| 284 report.state); | |
| 285 } | |
| 286 uint32_t num_bytes = static_cast<uint32_t>(to_output.size()); | |
| 287 if (!LoggingWriteFile(output_file.get(), &num_bytes, sizeof(num_bytes))) | |
| 288 return false; | |
| 289 if (!LoggingWriteFile(output_file.get(), to_output.c_str(), num_bytes)) | |
| 290 return false; | |
| 291 } | |
| 292 | |
| 293 // While this is not guaranteed to be atomic, it is in practical cases. We | |
| 294 // aren't racing ourself here as we're holding the mutex here, we use | |
| 295 // MoveFileEx only to avoid corrupting data in case of failure during write. | |
| 296 bool result = MoveFileEx(path.value().c_str(), | |
| 297 base_dir_.Append(kMetadataFileName).value().c_str(), | |
| 298 MOVEFILE_REPLACE_EXISTING); | |
| 299 loaded_ = !result; | |
| 300 return result; | |
| 301 } | |
| 302 | |
| 303 void Metadata::AddNewRecord(const NewReportDisk& new_report) { | |
| 304 ReportDisk report; | |
| 305 report.uuid = new_report.uuid; | |
| 306 report.file_path = new_report.path; | |
| 307 report.state = ReportState::kNew; | |
| 308 reports_.push_back(report); | |
| 309 } | |
| 310 | |
| 311 // static | |
| 312 OperationStatus Metadata::VerifyReport(const ReportDisk& report_win, | |
|
Robert Sesek
2015/02/02 23:06:08
This should be below FindSingleReport, based on th
scottmg
2015/02/03 18:56:39
Done.
| |
| 313 ReportState desired_state) { | |
| 314 if (desired_state != ReportState::kAny && report_win.state != desired_state) | |
| 315 return CrashReportDatabase::kBusyError; | |
| 316 if (GetFileAttributes(report_win.file_path.value().c_str()) == | |
| 317 INVALID_FILE_ATTRIBUTES) { | |
| 318 return CrashReportDatabase::kReportNotFound; | |
| 319 } | |
| 320 return CrashReportDatabase::kNoError; | |
| 321 } | |
| 322 | |
| 323 OperationStatus Metadata::FindReports( | |
| 324 ReportState desired_state, | |
| 325 std::vector<const CrashReportDatabase::Report>* reports) { | |
| 326 DCHECK(reports->empty()); | |
| 327 for (const auto& report : reports_) { | |
| 328 if (report.state == desired_state) { | |
| 329 if (VerifyReport(report, desired_state) != CrashReportDatabase::kNoError) | |
| 330 continue; | |
| 331 reports->push_back(report); | |
| 332 } | |
| 333 } | |
| 334 return CrashReportDatabase::kNoError; | |
| 335 } | |
| 336 | |
| 337 OperationStatus Metadata::FindSingleReport(const UUID& uuid, | |
| 338 ReportState desired_state, | |
| 339 ReportDisk** out_report) { | |
| 340 for (size_t i = 0; i < reports_.size(); ++i) { | |
| 341 if (reports_[i].uuid == uuid) { | |
| 342 OperationStatus os = VerifyReport(reports_[i], desired_state); | |
| 343 if (os != CrashReportDatabase::kNoError) | |
| 344 return os; | |
| 345 *out_report = &reports_[i]; | |
| 346 return CrashReportDatabase::kNoError; | |
| 347 } | |
| 348 } | |
| 349 return CrashReportDatabase::kReportNotFound; | |
| 350 } | |
| 351 | |
| 352 class CrashReportDatabaseWin : public CrashReportDatabase { | |
| 353 public: | |
| 354 explicit CrashReportDatabaseWin(const base::FilePath& path); | |
| 355 virtual ~CrashReportDatabaseWin(); | |
|
Robert Sesek
2015/02/02 23:06:07
virtual -> override
scottmg
2015/02/03 18:56:40
Done.
| |
| 356 | |
| 357 bool Initialize(); | |
| 358 | |
| 359 // CrashReportDatabase:: | |
| 360 OperationStatus PrepareNewCrashReport(NewReport** report) override; | |
| 361 OperationStatus FinishedWritingCrashReport(NewReport* report, | |
| 362 UUID* uuid) override; | |
| 363 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; | |
| 364 OperationStatus GetPendingReports( | |
| 365 std::vector<const Report>* reports) override; | |
| 366 OperationStatus GetCompletedReports( | |
| 367 std::vector<const Report>* reports) override; | |
| 368 OperationStatus GetReportForUploading(const UUID& uuid, | |
| 369 const Report** report) override; | |
| 370 OperationStatus RecordUploadAttempt(const Report* report, | |
| 371 bool successful, | |
| 372 const std::string& id) override; | |
| 373 OperationStatus SkipReportUpload(const UUID& uuid) override; | |
| 374 | |
| 375 private: | |
| 376 scoped_ptr<Metadata> AcquireMetadata(); | |
| 377 | |
| 378 base::FilePath base_dir_; | |
| 379 ScopedKernelHANDLE mutex_; | |
| 380 | |
| 381 DISALLOW_COPY_AND_ASSIGN(CrashReportDatabaseWin); | |
| 382 }; | |
| 383 | |
| 384 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) | |
| 385 : CrashReportDatabase(), base_dir_(path) { | |
|
Robert Sesek
2015/02/02 23:06:08
, mutex_()
scottmg
2015/02/03 18:56:39
Done.
| |
| 386 } | |
| 387 | |
| 388 CrashReportDatabaseWin::~CrashReportDatabaseWin() { | |
| 389 } | |
| 390 | |
| 391 bool CrashReportDatabaseWin::Initialize() { | |
| 392 // Check if the database already exists. | |
| 393 if (!CreateOrEnsureDirectoryExists(base_dir_)) | |
| 394 return false; | |
| 395 | |
| 396 // Create our reports subdirectory. | |
| 397 if (!CreateOrEnsureDirectoryExists(base_dir_.Append(kReportsDirectory))) | |
| 398 return false; | |
| 399 | |
| 400 mutex_.reset(CreateMutex(nullptr, false, kMetadataMutexName)); | |
|
Robert Sesek
2015/02/02 23:06:08
What will happen if multiple, different Crashpad-b
scottmg
2015/02/03 18:56:40
This call will succeed and behaviour will be corre
| |
| 401 | |
| 402 // TODO(scottmg): When are completed reports pruned from disk? Delete here or | |
| 403 // maybe on Read(). | |
| 404 | |
| 405 return true; | |
| 406 } | |
| 407 | |
| 408 OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport( | |
| 409 NewReport** out_report) { | |
| 410 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
| 411 if (!metadata) | |
| 412 return kFileSystemError; | |
|
Robert Sesek
2015/02/02 23:06:08
The documentation for kFileSystemError says that a
| |
| 413 | |
| 414 scoped_ptr<NewReportDisk> report(new NewReportDisk()); | |
| 415 | |
| 416 ::UUID system_uuid; | |
| 417 if (UuidCreate(&system_uuid) != RPC_S_OK) { | |
| 418 return kFileSystemError; | |
| 419 } | |
| 420 static_assert(sizeof(system_uuid) == 16, "unexpected system uuid size"); | |
|
Robert Sesek
2015/02/02 23:06:08
Would these be better in uuid.cc ?
scottmg
2015/02/03 18:56:39
It's not strictly required by uuid.cc I guess (i.e
| |
| 421 static_assert(offsetof(::UUID, Data1) == 0, "unexpected uuid layout"); | |
| 422 UUID uuid(reinterpret_cast<const uint8_t*>(&system_uuid.Data1)); | |
| 423 | |
| 424 report->uuid = uuid; | |
| 425 report->path = | |
| 426 base_dir_.Append(kReportsDirectory) | |
| 427 .Append(uuid.ToWideString() + L"." + kCrashReportFileExtension); | |
| 428 report->handle = LoggingOpenFileForWrite(report->path, | |
| 429 FileWriteMode::kTruncateOrCreate, | |
| 430 FilePermissions::kOwnerOnly); | |
| 431 if (report->handle == INVALID_HANDLE_VALUE) | |
| 432 return kFileSystemError; | |
| 433 | |
| 434 metadata->AddNewRecord(*report); | |
| 435 if (!metadata->Write()) | |
| 436 return kDatabaseError; | |
| 437 *out_report = report.release(); | |
| 438 return kNoError; | |
| 439 } | |
| 440 | |
| 441 OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport( | |
| 442 NewReport* report, | |
| 443 UUID* uuid) { | |
| 444 // Take ownership of the report, and cast to our private version with UUID. | |
| 445 scoped_ptr<NewReportDisk> scoped_report(static_cast<NewReportDisk*>(report)); | |
| 446 // Take ownership of the file handle. | |
| 447 ScopedFileHandle handle(report->handle); | |
| 448 | |
| 449 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
| 450 if (!metadata) | |
| 451 return kFileSystemError; | |
| 452 | |
| 453 ReportDisk* report_disk; | |
| 454 OperationStatus os = metadata->FindSingleReport( | |
| 455 scoped_report->uuid, ReportState::kNew, &report_disk); | |
| 456 if (os != kNoError) | |
| 457 return os; | |
| 458 report_disk->state = ReportState::kPending; | |
| 459 report_disk->creation_time = time(nullptr); | |
| 460 if (!metadata->Write()) | |
| 461 return kDatabaseError; | |
| 462 *uuid = report_disk->uuid; | |
| 463 return kNoError; | |
| 464 } | |
| 465 | |
| 466 OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid, | |
| 467 Report* report) { | |
| 468 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
| 469 if (!metadata) | |
| 470 return kFileSystemError; | |
| 471 | |
| 472 ReportDisk* report_disk; | |
| 473 OperationStatus os = | |
| 474 metadata->FindSingleReport(uuid, ReportState::kAny, &report_disk); | |
| 475 if (os != kNoError) | |
| 476 return os; | |
| 477 *report = *report_disk; | |
| 478 return kNoError; | |
| 479 } | |
| 480 | |
| 481 OperationStatus CrashReportDatabaseWin::GetPendingReports( | |
| 482 std::vector<const Report>* reports) { | |
| 483 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
| 484 if (!metadata) | |
| 485 return kFileSystemError; | |
| 486 return metadata->FindReports(ReportState::kPending, reports); | |
| 487 } | |
| 488 | |
| 489 OperationStatus CrashReportDatabaseWin::GetCompletedReports( | |
| 490 std::vector<const Report>* reports) { | |
| 491 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
| 492 if (!metadata) | |
| 493 return kFileSystemError; | |
| 494 return metadata->FindReports(ReportState::kCompleted, reports); | |
| 495 } | |
| 496 | |
| 497 OperationStatus CrashReportDatabaseWin::GetReportForUploading( | |
| 498 const UUID& uuid, | |
| 499 const Report** report) { | |
| 500 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
| 501 if (!metadata) | |
| 502 return kFileSystemError; | |
| 503 | |
| 504 ReportDisk* report_disk; | |
| 505 OperationStatus os = | |
| 506 metadata->FindSingleReport(uuid, ReportState::kPending, &report_disk); | |
| 507 if (os != kNoError) | |
| 508 return os; | |
| 509 report_disk->state = ReportState::kUploading; | |
| 510 // Create a copy for passing back to client. This will be freed in | |
| 511 // RecordUploadAttempt. | |
| 512 *report = new Report(*report_disk); | |
| 513 if (!metadata->Write()) | |
| 514 return kDatabaseError; | |
| 515 return kNoError; | |
| 516 } | |
| 517 | |
| 518 OperationStatus CrashReportDatabaseWin::RecordUploadAttempt( | |
| 519 const Report* report, | |
| 520 bool successful, | |
| 521 const std::string& id) { | |
| 522 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
| 523 if (!metadata) | |
| 524 return kFileSystemError; | |
| 525 | |
| 526 // Take ownership, allocated in GetReportForUploading. | |
|
Robert Sesek
2015/02/02 23:06:08
Should this happen before the potential early retu
scottmg
2015/02/03 18:56:39
Indeed! Done.
| |
| 527 scoped_ptr<const Report> upload_report(report); | |
| 528 | |
| 529 ReportDisk* report_disk; | |
| 530 OperationStatus os = metadata->FindSingleReport( | |
| 531 report->uuid, ReportState::kUploading, &report_disk); | |
| 532 if (os != kNoError) | |
| 533 return os; | |
| 534 report_disk->uploaded = successful; | |
| 535 report_disk->id = id; | |
| 536 report_disk->last_upload_attempt_time = time(nullptr); | |
| 537 report_disk->upload_attempts++; | |
| 538 report_disk->state = | |
| 539 successful ? ReportState::kCompleted : ReportState::kPending; | |
| 540 if (!metadata->Write()) | |
| 541 return kDatabaseError; | |
| 542 return kNoError; | |
| 543 } | |
| 544 | |
| 545 OperationStatus CrashReportDatabaseWin::SkipReportUpload(const UUID& uuid) { | |
| 546 scoped_ptr<Metadata> metadata(AcquireMetadata()); | |
| 547 if (!metadata) | |
| 548 return kFileSystemError; | |
| 549 | |
| 550 ReportDisk* report_disk; | |
| 551 OperationStatus os = | |
| 552 metadata->FindSingleReport(uuid, ReportState::kPending, &report_disk); | |
| 553 if (os != kNoError) | |
| 554 return os; | |
| 555 report_disk->state = ReportState::kCompleted; | |
| 556 if (!metadata->Write()) | |
| 557 return kDatabaseError; | |
| 558 return kNoError; | |
| 559 } | |
| 560 | |
| 561 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { | |
| 562 if (WaitForSingleObject(mutex_.get(), INFINITE) != WAIT_OBJECT_0) | |
| 563 return nullptr; | |
| 564 scoped_ptr<Metadata> metadata(new Metadata(base_dir_, mutex_.get())); | |
| 565 if (!metadata->Read()) | |
| 566 return nullptr; | |
| 567 return metadata; | |
| 568 } | |
| 569 | |
| 570 } // namespace | |
| 571 | |
| 572 // static | |
| 573 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( | |
| 574 const base::FilePath& path) { | |
| 575 scoped_ptr<CrashReportDatabaseWin> database_win( | |
| 576 new CrashReportDatabaseWin(path.Append(kDatabaseDirectoryName))); | |
| 577 if (!database_win->Initialize()) | |
| 578 database_win.reset(); | |
| 579 | |
| 580 return scoped_ptr<CrashReportDatabase>(database_win.release()); | |
| 581 } | |
| 582 | |
| 583 } // namespace crashpad | |
| OLD | NEW |