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

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: InitializeWithoutCreating() 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 465 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 LOG(ERROR) << "GetFileAttributes "
494 << base::UTF16ToUTF8(path.value())
495 << ": not a directory";
496 return false;
497 }
498 return true;
499 }
500
486 //! \brief Ensures that the node at path is a directory, and creates it if it 501 //! \brief Ensures that the node at path is a directory, and creates it if it
487 //! does not exist. 502 //! does not exist.
488 //! 503 //!
489 //! \return If the path points to a file, rather than a directory, or the 504 //! \return If the path points to a file, rather than a directory, or the
490 //! directory could not be created, returns `false`. Otherwise, returns 505 //! directory could not be created, returns `false`. Otherwise, returns
491 //! `true`, indicating that path already was or now is a directory. 506 //! `true`, indicating that path already was or now is a directory.
492 bool CreateDirectoryIfNecessary(const base::FilePath& path) { 507 bool CreateDirectoryIfNecessary(const base::FilePath& path) {
493 if (CreateDirectory(path.value().c_str(), nullptr)) 508 if (CreateDirectory(path.value().c_str(), nullptr))
494 return true; 509 return true;
495 if (GetLastError() != ERROR_ALREADY_EXISTS) { 510 if (GetLastError() != ERROR_ALREADY_EXISTS) {
496 PLOG(ERROR) << "CreateDirectory"; 511 PLOG(ERROR) << "CreateDirectory " << base::UTF16ToUTF8(path.value());
497 return false; 512 return false;
498 } 513 }
499 DWORD fileattr = GetFileAttributes(path.value().c_str()); 514 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 } 515 }
509 516
510 // CrashReportDatabaseWin ------------------------------------------------------ 517 // CrashReportDatabaseWin ------------------------------------------------------
511 518
512 class CrashReportDatabaseWin : public CrashReportDatabase { 519 class CrashReportDatabaseWin : public CrashReportDatabase {
513 public: 520 public:
514 explicit CrashReportDatabaseWin(const base::FilePath& path); 521 explicit CrashReportDatabaseWin(const base::FilePath& path);
515 ~CrashReportDatabaseWin() override; 522 ~CrashReportDatabaseWin() override;
516 523
517 bool Initialize(); 524 bool Initialize(bool may_create);
518 525
519 // CrashReportDatabase: 526 // CrashReportDatabase:
520 Settings* GetSettings() override; 527 Settings* GetSettings() override;
521 OperationStatus PrepareNewCrashReport(NewReport** report) override; 528 OperationStatus PrepareNewCrashReport(NewReport** report) override;
522 OperationStatus FinishedWritingCrashReport(NewReport* report, 529 OperationStatus FinishedWritingCrashReport(NewReport* report,
523 UUID* uuid) override; 530 UUID* uuid) override;
524 OperationStatus ErrorWritingCrashReport(NewReport* report) override; 531 OperationStatus ErrorWritingCrashReport(NewReport* report) override;
525 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override; 532 OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override;
526 OperationStatus GetPendingReports(std::vector<Report>* reports) override; 533 OperationStatus GetPendingReports(std::vector<Report>* reports) override;
527 OperationStatus GetCompletedReports(std::vector<Report>* reports) override; 534 OperationStatus GetCompletedReports(std::vector<Report>* reports) override;
(...skipping 17 matching lines...) Expand all
545 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path) 552 CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path)
546 : CrashReportDatabase(), 553 : CrashReportDatabase(),
547 base_dir_(path), 554 base_dir_(path),
548 settings_(base_dir_.Append(kSettings)), 555 settings_(base_dir_.Append(kSettings)),
549 initialized_() { 556 initialized_() {
550 } 557 }
551 558
552 CrashReportDatabaseWin::~CrashReportDatabaseWin() { 559 CrashReportDatabaseWin::~CrashReportDatabaseWin() {
553 } 560 }
554 561
555 bool CrashReportDatabaseWin::Initialize() { 562 bool CrashReportDatabaseWin::Initialize(bool may_create) {
556 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); 563 INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
557 564
558 // Ensure the database and report subdirectories exist. 565 // Ensure the databse directory exists.
scottmg 2015/10/07 20:58:18 'database' if you didn't already land.
Mark Mentovai 2015/10/07 21:03:34 scottmg wrote:
559 if (!CreateDirectoryIfNecessary(base_dir_) || 566 if (may_create) {
560 !CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory))) 567 if (!CreateDirectoryIfNecessary(base_dir_))
568 return false;
569 } else if (!EnsureDirectory(base_dir_)) {
570 return false;
571 }
572
573 // Ensure that the report subdirectory exists.
574 if (!CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory)))
561 return false; 575 return false;
562 576
563 // TODO(scottmg): When are completed reports pruned from disk? Delete here or 577 // TODO(scottmg): When are completed reports pruned from disk? Delete here or
564 // maybe on AcquireMetadata(). 578 // maybe on AcquireMetadata().
565 579
566 if (!settings_.Initialize()) 580 if (!settings_.Initialize())
567 return false; 581 return false;
568 582
569 INITIALIZATION_STATE_SET_VALID(initialized_); 583 INITIALIZATION_STATE_SET_VALID(initialized_);
570 return true; 584 return true;
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 if (os == kNoError) 758 if (os == kNoError)
745 report_disk->state = ReportState::kCompleted; 759 report_disk->state = ReportState::kCompleted;
746 return os; 760 return os;
747 } 761 }
748 762
749 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() { 763 scoped_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() {
750 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName); 764 base::FilePath metadata_file = base_dir_.Append(kMetadataFileName);
751 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory)); 765 return Metadata::Create(metadata_file, base_dir_.Append(kReportsDirectory));
752 } 766 }
753 767
768 scoped_ptr<CrashReportDatabase> InitializeInternal(
769 const base::FilePath& path, bool may_create) {
770 scoped_ptr<CrashReportDatabaseWin> database_win(
771 new CrashReportDatabaseWin(path));
772 return database_win->Initialize(may_create)
773 ? database_win.Pass() : scoped_ptr<CrashReportDatabaseWin>();
Robert Sesek 2015/10/07 21:04:33 nit: shouldn't ? go on the previous line?
Mark Mentovai 2015/10/07 21:07:09 Robert Sesek wrote:
774 }
775
754 } // namespace 776 } // namespace
755 777
756 // static 778 // static
757 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize( 779 scoped_ptr<CrashReportDatabase> CrashReportDatabase::Initialize(
758 const base::FilePath& path) { 780 const base::FilePath& path) {
759 scoped_ptr<CrashReportDatabaseWin> database_win( 781 return InitializeInternal(path, true);
760 new CrashReportDatabaseWin(path)); 782 }
761 return database_win->Initialize() ? database_win.Pass() 783
762 : scoped_ptr<CrashReportDatabaseWin>(); 784 // static
785 scoped_ptr<CrashReportDatabase> CrashReportDatabase::InitializeWithoutCreating(
786 const base::FilePath& path) {
787 return InitializeInternal(path, false);
763 } 788 }
764 789
765 } // namespace crashpad 790 } // 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