OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 } | 476 } |
477 | 477 |
478 // static | 478 // static |
479 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, | 479 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, |
480 ReportState desired_state) { | 480 ReportState desired_state) { |
481 return (report_disk.state == desired_state) | 481 return (report_disk.state == desired_state) |
482 ? VerifyReportAnyState(report_disk) | 482 ? VerifyReportAnyState(report_disk) |
483 : CrashReportDatabase::kBusyError; | 483 : CrashReportDatabase::kBusyError; |
484 } | 484 } |
485 | 485 |
| 486 bool EnsureDirectory(const base::FilePath& path) { |
| 487 DWORD fileattr = GetFileAttributes(path.value().c_str()); |
| 488 if (fileattr == INVALID_FILE_ATTRIBUTES) { |
| 489 PLOG(ERROR) << "GetFileAttributes " << base::UTF16ToUTF8(path.value()); |
| 490 return false; |
| 491 } |
| 492 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) |
| 493 return true; |
| 494 LOG(ERROR) << "GetFileAttributes " |
| 495 << base::UTF16ToUTF8(path.value()) |
| 496 << ": not a directory"; |
| 497 return false; |
| 498 } |
| 499 |
486 //! \brief Ensures that the node at path is a directory, and creates it if it | 500 //! \brief Ensures that the node at path is a directory, and creates it if it |
487 //! does not exist. | 501 //! does not exist. |
488 //! | 502 //! |
489 //! \return If the path points to a file, rather than a directory, or the | 503 //! \return If the path points to a file, rather than a directory, or the |
490 //! directory could not be created, returns `false`. Otherwise, returns | 504 //! directory could not be created, returns `false`. Otherwise, returns |
491 //! `true`, indicating that path already was or now is a directory. | 505 //! `true`, indicating that path already was or now is a directory. |
492 bool CreateDirectoryIfNecessary(const base::FilePath& path) { | 506 bool CreateDirectoryIfNecessary(const base::FilePath& path) { |
493 if (CreateDirectory(path.value().c_str(), nullptr)) | 507 if (CreateDirectory(path.value().c_str(), nullptr)) |
494 return true; | 508 return true; |
495 if (GetLastError() != ERROR_ALREADY_EXISTS) { | 509 if (GetLastError() != ERROR_ALREADY_EXISTS) { |
496 PLOG(ERROR) << "CreateDirectory"; | 510 PLOG(ERROR) << "CreateDirectory " << base::UTF16ToUTF8(path.value()); |
497 return false; | 511 return false; |
498 } | 512 } |
499 DWORD fileattr = GetFileAttributes(path.value().c_str()); | 513 return EnsureDirectory(path); |
500 if (fileattr == INVALID_FILE_ATTRIBUTES) { | |
501 PLOG(ERROR) << "GetFileAttributes"; | |
502 return false; | |
503 } | |
504 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) | |
505 return true; | |
506 LOG(ERROR) << "not a directory"; | |
507 return false; | |
508 } | 514 } |
509 | 515 |
510 // CrashReportDatabaseWin ------------------------------------------------------ | 516 // CrashReportDatabaseWin ------------------------------------------------------ |
511 | 517 |
512 class CrashReportDatabaseWin : public CrashReportDatabase { | 518 class CrashReportDatabaseWin : public CrashReportDatabase { |
513 public: | 519 public: |
514 explicit CrashReportDatabaseWin(const base::FilePath& path); | 520 explicit CrashReportDatabaseWin(const base::FilePath& path); |
515 ~CrashReportDatabaseWin() override; | 521 ~CrashReportDatabaseWin() override; |
516 | 522 |
517 bool Initialize(); | 523 bool Initialize(bool create); |
518 | 524 |
519 // CrashReportDatabase: | 525 // CrashReportDatabase: |
520 Settings* GetSettings() override; | 526 Settings* GetSettings() override; |
521 OperationStatus PrepareNewCrashReport(NewReport** report) override; | 527 OperationStatus PrepareNewCrashReport(NewReport** report) override; |
522 OperationStatus FinishedWritingCrashReport(NewReport* report, | 528 OperationStatus FinishedWritingCrashReport(NewReport* report, |
523 UUID* uuid) override; | 529 UUID* uuid) override; |
524 OperationStatus ErrorWritingCrashReport(NewReport* report) override; | 530 OperationStatus ErrorWritingCrashReport(NewReport* report) override; |
525 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; | 531 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; |
526 OperationStatus GetPendingReports(std::vector<Report>* reports) override; | 532 OperationStatus GetPendingReports(std::vector<Report>* reports) override; |
527 OperationStatus GetCompletedReports(std::vector<Report>* reports) override; | 533 OperationStatus GetCompletedReports(std::vector<Report>* reports) override; |
(...skipping 17 matching lines...) Expand all Loading... |
545 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) | 551 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) |
546 : CrashReportDatabase(), | 552 : CrashReportDatabase(), |
547 base_dir_(path), | 553 base_dir_(path), |
548 settings_(base_dir_.Append(kSettings)), | 554 settings_(base_dir_.Append(kSettings)), |
549 initialized_() { | 555 initialized_() { |
550 } | 556 } |
551 | 557 |
552 CrashReportDatabaseWin::~CrashReportDatabaseWin() { | 558 CrashReportDatabaseWin::~CrashReportDatabaseWin() { |
553 } | 559 } |
554 | 560 |
555 bool CrashReportDatabaseWin::Initialize() { | 561 bool CrashReportDatabaseWin::Initialize(bool create) { |
556 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); | 562 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); |
557 | 563 |
558 // Ensure the database and report subdirectories exist. | 564 // Ensure the databse directory exists. |
559 if (!CreateDirectoryIfNecessary(base_dir_) || | 565 if (create) { |
560 !CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory))) | 566 if (!CreateDirectoryIfNecessary(base_dir_)) |
| 567 return false; |
| 568 } else if (!EnsureDirectory(base_dir_)) { |
| 569 return false; |
| 570 } |
| 571 |
| 572 // Ensure that the report subdirectory exists. |
| 573 if (!CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory))) |
561 return false; | 574 return false; |
562 | 575 |
563 // TODO(scottmg): When are completed reports pruned from disk? Delete here or | 576 // TODO(scottmg): When are completed reports pruned from disk? Delete here or |
564 // maybe on AcquireMetadata(). | 577 // maybe on AcquireMetadata(). |
565 | 578 |
566 if (!settings_.Initialize()) | 579 if (!settings_.Initialize()) |
567 return false; | 580 return false; |
568 | 581 |
569 INITIALIZATION_STATE_SET_VALID(initialized_); | 582 INITIALIZATION_STATE_SET_VALID(initialized_); |
570 return true; | 583 return true; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 | 761 |
749 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { | 762 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { |
750 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); | 763 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); |
751 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); | 764 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); |
752 } | 765 } |
753 | 766 |
754 } // namespace | 767 } // namespace |
755 | 768 |
756 // static | 769 // static |
757 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( | 770 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( |
758 const base::FilePath& path) { | 771 const base::FilePath& path, bool create) { |
759 scoped_ptr<CrashReportDatabaseWin> database_win( | 772 scoped_ptr<CrashReportDatabaseWin> database_win( |
760 new CrashReportDatabaseWin(path)); | 773 new CrashReportDatabaseWin(path)); |
761 return database_win->Initialize() ? database_win.Pass() | 774 return database_win->Initialize(create) |
762 : scoped_ptr<CrashReportDatabaseWin>(); | 775 ? database_win.Pass() : scoped_ptr<CrashReportDatabaseWin>(); |
763 } | 776 } |
764 | 777 |
765 } // namespace crashpad | 778 } // namespace crashpad |
OLD | NEW |