Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: client/crash_report_database_win.cc

Issue 1395653002: crashpad_database_util: Don’t create a database unless explicitly asked (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: For checkin Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « client/crash_report_database_test.cc ('k') | handler/main.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 } 503 }
504 504
505 // static 505 // static
506 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk, 506 OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk,
507 ReportState desired_state) { 507 ReportState desired_state) {
508 return (report_disk.state == desired_state) 508 return (report_disk.state == desired_state)
509 ? VerifyReportAnyState(report_disk) 509 ? VerifyReportAnyState(report_disk)
510 : CrashReportDatabase::kBusyError; 510 : CrashReportDatabase::kBusyError;
511 } 511 }
512 512
513 bool EnsureDirectory(const base::FilePath& path) {
514 DWORD fileattr = GetFileAttributes(path.value().c_str());
515 if (fileattr == INVALID_FILE_ATTRIBUTES) {
516 PLOG(ERROR) << "GetFileAttributes " << base::UTF16ToUTF8(path.value());
517 return false;
518 }
519 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
520 LOG(ERROR) << "GetFileAttributes "
521 << base::UTF16ToUTF8(path.value())
522 << ": not a directory";
523 return false;
524 }
525 return true;
526 }
527
513 //! \brief Ensures that the node at path is a directory, and creates it if it 528 //! \brief Ensures that the node at path is a directory, and creates it if it
514 //! does not exist. 529 //! does not exist.
515 //! 530 //!
516 //! \return If the path points to a file, rather than a directory, or the 531 //! \return If the path points to a file, rather than a directory, or the
517 //! directory could not be created, returns `false`. Otherwise, returns 532 //! directory could not be created, returns `false`. Otherwise, returns
518 //! `true`, indicating that path already was or now is a directory. 533 //! `true`, indicating that path already was or now is a directory.
519 bool CreateDirectoryIfNecessary(const base::FilePath& path) { 534 bool CreateDirectoryIfNecessary(const base::FilePath& path) {
520 if (CreateDirectory(path.value().c_str(), nullptr)) 535 if (CreateDirectory(path.value().c_str(), nullptr))
521 return true; 536 return true;
522 if (GetLastError() != ERROR_ALREADY_EXISTS) { 537 if (GetLastError() != ERROR_ALREADY_EXISTS) {
523 PLOG(ERROR) << "CreateDirectory"; 538 PLOG(ERROR) << "CreateDirectory " << base::UTF16ToUTF8(path.value());
524 return false; 539 return false;
525 } 540 }
526 DWORD fileattr = GetFileAttributes(path.value().c_str()); 541 return EnsureDirectory(path);
527 if (fileattr == INVALID_FILE_ATTRIBUTES) {
528 PLOG(ERROR) << "GetFileAttributes";
529 return false;
530 }
531 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0)
532 return true;
533 LOG(ERROR) << "not a directory";
534 return false;
535 } 542 }
536 543
537 // CrashReportDatabaseWin ------------------------------------------------------ 544 // CrashReportDatabaseWin ------------------------------------------------------
538 545
539 class CrashReportDatabaseWin : public CrashReportDatabase { 546 class CrashReportDatabaseWin : public CrashReportDatabase {
540 public: 547 public:
541 explicit CrashReportDatabaseWin(const base::FilePath& path); 548 explicit CrashReportDatabaseWin(const base::FilePath& path);
542 ~CrashReportDatabaseWin() override; 549 ~CrashReportDatabaseWin() override;
543 550
544 bool Initialize(); 551 bool Initialize(bool may_create);
545 552
546 // CrashReportDatabase: 553 // CrashReportDatabase:
547 Settings* GetSettings() override; 554 Settings* GetSettings() override;
548 OperationStatus PrepareNewCrashReport(NewReport** report) override; 555 OperationStatus PrepareNewCrashReport(NewReport** report) override;
549 OperationStatus FinishedWritingCrashReport(NewReport* report, 556 OperationStatus FinishedWritingCrashReport(NewReport* report,
550 UUID* uuid) override; 557 UUID* uuid) override;
551 OperationStatus ErrorWritingCrashReport(NewReport* report) override; 558 OperationStatus ErrorWritingCrashReport(NewReport* report) override;
552 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; 559 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override;
553 OperationStatus GetPendingReports(std::vector<Report>* reports) override; 560 OperationStatus GetPendingReports(std::vector<Report>* reports) override;
554 OperationStatus GetCompletedReports(std::vector<Report>* reports) override; 561 OperationStatus GetCompletedReports(std::vector<Report>* reports) override;
(...skipping 18 matching lines...) Expand all
573 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) 580 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path)
574 : CrashReportDatabase(), 581 : CrashReportDatabase(),
575 base_dir_(path), 582 base_dir_(path),
576 settings_(base_dir_.Append(kSettings)), 583 settings_(base_dir_.Append(kSettings)),
577 initialized_() { 584 initialized_() {
578 } 585 }
579 586
580 CrashReportDatabaseWin::~CrashReportDatabaseWin() { 587 CrashReportDatabaseWin::~CrashReportDatabaseWin() {
581 } 588 }
582 589
583 bool CrashReportDatabaseWin::Initialize() { 590 bool CrashReportDatabaseWin::Initialize(bool may_create) {
584 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); 591 INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
585 592
586 // Ensure the database and report subdirectories exist. 593 // Ensure the database directory exists.
587 if (!CreateDirectoryIfNecessary(base_dir_) || 594 if (may_create) {
588 !CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory))) 595 if (!CreateDirectoryIfNecessary(base_dir_))
596 return false;
597 } else if (!EnsureDirectory(base_dir_)) {
598 return false;
599 }
600
601 // Ensure that the report subdirectory exists.
602 if (!CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory)))
589 return false; 603 return false;
590 604
591 if (!settings_.Initialize()) 605 if (!settings_.Initialize())
592 return false; 606 return false;
593 607
594 INITIALIZATION_STATE_SET_VALID(initialized_); 608 INITIALIZATION_STATE_SET_VALID(initialized_);
595 return true; 609 return true;
596 } 610 }
597 611
598 Settings* CrashReportDatabaseWin::GetSettings() { 612 Settings* CrashReportDatabaseWin::GetSettings() {
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 if (os == kNoError) 803 if (os == kNoError)
790 report_disk->state = ReportState::kCompleted; 804 report_disk->state = ReportState::kCompleted;
791 return os; 805 return os;
792 } 806 }
793 807
794 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { 808 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() {
795 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); 809 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName);
796 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); 810 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory));
797 } 811 }
798 812
813 scoped_ptr<CrashReportDatabase> InitializeInternal(
814 const base::FilePath& path, bool may_create) {
815 scoped_ptr<CrashReportDatabaseWin> database_win(
816 new CrashReportDatabaseWin(path));
817 return database_win->Initialize(may_create)
818 ? database_win.Pass()
819 : scoped_ptr<CrashReportDatabaseWin>();
820 }
821
799 } // namespace 822 } // namespace
800 823
801 // static 824 // static
802 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( 825 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize(
803 const base::FilePath& path) { 826 const base::FilePath& path) {
804 scoped_ptr<CrashReportDatabaseWin> database_win( 827 return InitializeInternal(path, true);
805 new CrashReportDatabaseWin(path)); 828 }
806 return database_win->Initialize() ? database_win.Pass() 829
807 : scoped_ptr<CrashReportDatabaseWin>(); 830 // static
831 scoped_ptr<CrashReportDatabase> CrashReportDatabase::InitializeWithoutCreating(
832 const base::FilePath& path) {
833 return InitializeInternal(path, false);
808 } 834 }
809 835
810 } // namespace crashpad 836 } // namespace crashpad
OLDNEW
« no previous file with comments | « client/crash_report_database_test.cc ('k') | handler/main.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698