2022-09-06 19:14:07 -04:00
|
|
|
// Copyright 2015 The Crashpad Authors
|
2015-02-11 12:17:05 -08:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2016-01-06 12:22:50 -05:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdint.h>
|
2015-02-11 12:17:05 -08:00
|
|
|
#include <string.h>
|
2016-01-06 12:22:50 -05:00
|
|
|
#include <sys/types.h>
|
2015-02-11 12:17:05 -08:00
|
|
|
#include <time.h>
|
2016-01-06 12:22:50 -05:00
|
|
|
#include <wchar.h>
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2022-06-22 12:22:26 -04:00
|
|
|
#include <mutex>
|
2022-01-07 17:19:37 -05:00
|
|
|
#include <tuple>
|
2015-12-09 17:36:32 -05:00
|
|
|
#include <utility>
|
|
|
|
|
2023-10-18 12:08:10 -07:00
|
|
|
#include "base/check_op.h"
|
2015-02-11 12:17:05 -08:00
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/numerics/safe_math.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2015-05-01 15:09:38 -07:00
|
|
|
#include "client/settings.h"
|
2020-06-24 09:03:47 +02:00
|
|
|
#include "util/file/directory_reader.h"
|
|
|
|
#include "util/file/filesystem.h"
|
2018-02-15 08:17:12 -08:00
|
|
|
#include "util/misc/implicit_cast.h"
|
2015-03-10 13:24:44 -04:00
|
|
|
#include "util/misc/initialization_state_dcheck.h"
|
2016-09-02 12:10:57 -07:00
|
|
|
#include "util/misc/metrics.h"
|
2015-02-11 12:17:05 -08:00
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-07-25 13:34:04 -04:00
|
|
|
constexpr wchar_t kReportsDirectory[] = L"reports";
|
|
|
|
constexpr wchar_t kMetadataFileName[] = L"metadata";
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2017-07-25 13:34:04 -04:00
|
|
|
constexpr wchar_t kSettings[] = L"settings.dat";
|
2015-05-01 15:09:38 -07:00
|
|
|
|
2017-07-25 13:34:04 -04:00
|
|
|
constexpr wchar_t kCrashReportFileExtension[] = L"dmp";
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2017-07-25 14:31:27 -04:00
|
|
|
constexpr uint32_t kMetadataFileHeaderMagic = 'CPAD';
|
|
|
|
constexpr uint32_t kMetadataFileVersion = 1;
|
2015-02-17 12:05:29 -08:00
|
|
|
|
|
|
|
using OperationStatus = CrashReportDatabase::OperationStatus;
|
|
|
|
|
|
|
|
// Helpers ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Adds a string to the string table and returns the byte index where it was
|
|
|
|
// added.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts |str| to UTF8, adds the result to the string table and returns the
|
|
|
|
// byte index where it was added.
|
2020-09-12 09:20:14 +02:00
|
|
|
uint32_t AddStringToTable(std::string* string_table, const std::wstring& str) {
|
|
|
|
return AddStringToTable(string_table, base::WideToUTF8(str));
|
2015-02-17 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reads from the current file position to EOF and returns as a string of bytes.
|
|
|
|
std::string ReadRestOfFileAsString(FileHandle file) {
|
|
|
|
FileOffset read_from = LoggingSeekFile(file, 0, SEEK_CUR);
|
|
|
|
FileOffset end = LoggingSeekFile(file, 0, SEEK_END);
|
|
|
|
FileOffset original = LoggingSeekFile(file, read_from, SEEK_SET);
|
|
|
|
if (read_from == -1 || end == -1 || original == -1 || read_from == end)
|
|
|
|
return std::string();
|
|
|
|
DCHECK_EQ(read_from, original);
|
|
|
|
DCHECK_GT(end, read_from);
|
|
|
|
size_t data_length = static_cast<size_t>(end - read_from);
|
|
|
|
std::string buffer(data_length, '\0');
|
Make file_io reads more rational and predictable
ReadFile() attempted to continue reading after a short read. In most
cases, this is fine. However, ReadFile() would keep trying to fill a
partially-filled buffer until experiencing a 0-length read(), signaling
end-of-file. For certain weird file descriptors like terminal input, EOF
is an ephemeral condition, and attempting to read beyond EOF doesn’t
actually return 0 (EOF) provided that they remain open, it will block
waiting for more input. Consequently, ReadFile() and anything based on
ReadFile() had an undocumented and quirky interface, which was that any
short read that it returned (not an underlying short read) actually
indicated EOF.
This facet of ReadFile() was unexpected, so it’s being removed. The new
behavior is that ReadFile() will return an underlying short read. The
behavior of FileReaderInterface::Read() is updated in accordance with
this change.
Upon experiencing a short read, the caller can determine the best
action. Most callers were already prepared for this behavior. Outside of
util/file, only crashpad_database_util properly implemented EOF
detection according to previous semantics, and adapting it to new
semantics is trivial.
Callers who require an exact-length read can use the new
ReadFileExactly(), or the newly renamed LoggingReadFileExactly() or
CheckedReadFileExactly(). These functions will retry following a short
read. The renamed functions were previously called LoggingReadFile() and
CheckedReadFile(), but those names implied that they were simply
wrapping ReadFile(), which is not the case. They wrapped ReadFile() and
further, insisted on a full read. Since ReadFile()’s semantics are now
changing but these functions’ are not, they’re now even more distinct
from ReadFile(), and must be renamed to avoid confusion.
Test: *
Change-Id: I06b77e0d6ad8719bd2eb67dab93a8740542dd908
Reviewed-on: https://chromium-review.googlesource.com/456676
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2017-03-16 13:36:38 -04:00
|
|
|
return LoggingReadFileExactly(file, &buffer[0], data_length) ? buffer
|
|
|
|
: std::string();
|
2015-02-17 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
2020-06-24 09:03:47 +02:00
|
|
|
bool UUIDFromReportPath(const base::FilePath& path, UUID* uuid) {
|
|
|
|
return uuid->InitializeFromString(
|
|
|
|
path.RemoveFinalExtension().BaseName().value());
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
// Helper structures, and conversions ------------------------------------------
|
|
|
|
|
|
|
|
// The format of the on disk 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.
|
|
|
|
struct MetadataFileHeader {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t num_records;
|
|
|
|
uint32_t padding;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ReportDisk;
|
|
|
|
|
|
|
|
enum class ReportState {
|
2015-02-11 12:17:05 -08:00
|
|
|
//! \brief Created and filled out by caller, owned by database.
|
|
|
|
kPending,
|
|
|
|
//! \brief In the process of uploading, owned by caller.
|
|
|
|
kUploading,
|
|
|
|
//! \brief Upload completed or skipped, owned by database.
|
|
|
|
kCompleted,
|
|
|
|
};
|
|
|
|
|
2016-08-24 15:37:46 -04:00
|
|
|
enum {
|
|
|
|
//! \brief Corresponds to uploaded bit of the report state.
|
|
|
|
kAttributeUploaded = 1 << 0,
|
|
|
|
|
|
|
|
//! \brief Corresponds to upload_explicity_requested bit of the report state.
|
|
|
|
kAttributeUploadExplicitlyRequested = 1 << 1,
|
|
|
|
};
|
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
struct MetadataFileReportRecord {
|
|
|
|
// Note that this default constructor does no initialization. It is used only
|
|
|
|
// to create an array of records that are immediately initialized by reading
|
|
|
|
// from disk in Metadata::Read().
|
|
|
|
MetadataFileReportRecord() {}
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
// Constructs from a ReportDisk, adding to |string_table| and storing indices
|
|
|
|
// as strings into that table.
|
|
|
|
MetadataFileReportRecord(const ReportDisk& report, std::string* string_table);
|
|
|
|
|
|
|
|
UUID uuid; // UUID is a 16 byte, standard layout structure.
|
|
|
|
uint32_t file_path_index; // Index into string table. File name is relative
|
|
|
|
// to the reports directory when on disk.
|
|
|
|
uint32_t id_index; // Index into string table.
|
|
|
|
int64_t creation_time; // Holds a time_t.
|
|
|
|
int64_t last_upload_attempt_time; // Holds a time_t.
|
|
|
|
int32_t upload_attempts;
|
|
|
|
int32_t state; // A ReportState.
|
2016-08-24 15:37:46 -04:00
|
|
|
uint8_t attributes; // Bitfield of kAttribute*.
|
2015-02-17 12:05:29 -08:00
|
|
|
uint8_t padding[7];
|
|
|
|
};
|
2015-02-11 12:17:05 -08:00
|
|
|
|
|
|
|
//! \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 {
|
2015-02-17 12:05:29 -08:00
|
|
|
ReportDisk(const MetadataFileReportRecord& record,
|
|
|
|
const base::FilePath& report_dir,
|
|
|
|
const std::string& string_table);
|
|
|
|
|
|
|
|
ReportDisk(const UUID& uuid,
|
|
|
|
const base::FilePath& path,
|
|
|
|
time_t creation_tim,
|
|
|
|
ReportState state);
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
//! \brief The current state of the report.
|
|
|
|
ReportState state;
|
|
|
|
};
|
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
MetadataFileReportRecord::MetadataFileReportRecord(const ReportDisk& report,
|
|
|
|
std::string* string_table)
|
|
|
|
: uuid(report.uuid),
|
|
|
|
file_path_index(
|
|
|
|
AddStringToTable(string_table, report.file_path.BaseName().value())),
|
|
|
|
id_index(AddStringToTable(string_table, report.id)),
|
|
|
|
creation_time(report.creation_time),
|
|
|
|
last_upload_attempt_time(report.last_upload_attempt_time),
|
|
|
|
upload_attempts(report.upload_attempts),
|
|
|
|
state(static_cast<uint32_t>(report.state)),
|
2016-08-24 15:37:46 -04:00
|
|
|
attributes((report.uploaded ? kAttributeUploaded : 0) |
|
|
|
|
(report.upload_explicitly_requested
|
|
|
|
? kAttributeUploadExplicitlyRequested
|
|
|
|
: 0)) {
|
2015-02-17 12:05:29 -08:00
|
|
|
memset(&padding, 0, sizeof(padding));
|
|
|
|
}
|
|
|
|
|
|
|
|
ReportDisk::ReportDisk(const MetadataFileReportRecord& record,
|
|
|
|
const base::FilePath& report_dir,
|
|
|
|
const std::string& string_table)
|
|
|
|
: Report() {
|
|
|
|
uuid = record.uuid;
|
|
|
|
file_path = report_dir.Append(
|
2020-09-12 09:20:14 +02:00
|
|
|
base::UTF8ToWide(&string_table[record.file_path_index]));
|
2015-02-17 12:05:29 -08:00
|
|
|
id = &string_table[record.id_index];
|
|
|
|
creation_time = record.creation_time;
|
|
|
|
last_upload_attempt_time = record.last_upload_attempt_time;
|
|
|
|
upload_attempts = record.upload_attempts;
|
|
|
|
state = static_cast<ReportState>(record.state);
|
2016-08-24 15:37:46 -04:00
|
|
|
uploaded = (record.attributes & kAttributeUploaded) != 0;
|
|
|
|
upload_explicitly_requested =
|
|
|
|
(record.attributes & kAttributeUploadExplicitlyRequested) != 0;
|
2015-02-17 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ReportDisk::ReportDisk(const UUID& uuid,
|
|
|
|
const base::FilePath& path,
|
|
|
|
time_t creation_time,
|
|
|
|
ReportState state)
|
|
|
|
: Report() {
|
|
|
|
this->uuid = uuid;
|
|
|
|
this->file_path = path;
|
|
|
|
this->creation_time = creation_time;
|
|
|
|
this->state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata --------------------------------------------------------------------
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
//! \brief Manages the metadata for the set of reports, handling serialization
|
2015-02-17 12:05:29 -08:00
|
|
|
//! to disk, and queries.
|
2015-02-11 12:17:05 -08:00
|
|
|
class Metadata {
|
|
|
|
public:
|
2021-09-20 12:55:12 -07:00
|
|
|
Metadata(const Metadata&) = delete;
|
|
|
|
Metadata& operator=(const Metadata&) = delete;
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
//! \brief Writes any changes if necessary, unlocks and closes the file
|
|
|
|
//! handle.
|
|
|
|
~Metadata();
|
|
|
|
|
2021-11-02 10:50:42 +01:00
|
|
|
static std::unique_ptr<Metadata> Create(
|
|
|
|
const base::FilePath& metadata_file,
|
|
|
|
const base::FilePath& report_dir,
|
|
|
|
const base::FilePath& attachments_dir);
|
2015-02-17 12:05:29 -08:00
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
//! \brief Adds a new report to the set.
|
|
|
|
//!
|
|
|
|
//! \param[in] new_report_disk The record to add. The #state field must be set
|
|
|
|
//! to kPending.
|
|
|
|
void AddNewRecord(const ReportDisk& new_report_disk);
|
|
|
|
|
|
|
|
//! \brief Finds all reports in a given state. The \a reports vector is only
|
|
|
|
//! valid when CrashReportDatabase::kNoError is returned.
|
|
|
|
//!
|
|
|
|
//! \param[in] desired_state The state to match.
|
|
|
|
//! \param[out] reports Matching reports, must be empty on entry.
|
|
|
|
OperationStatus FindReports(
|
|
|
|
ReportState desired_state,
|
2015-03-06 18:43:28 -05:00
|
|
|
std::vector<CrashReportDatabase::Report>* reports) const;
|
2015-02-11 12:17:05 -08:00
|
|
|
|
|
|
|
//! \brief Finds the report matching the given UUID.
|
|
|
|
//!
|
|
|
|
//! The returned report is only valid if CrashReportDatabase::kNoError is
|
|
|
|
//! returned.
|
|
|
|
//!
|
|
|
|
//! \param[in] uuid The report identifier.
|
|
|
|
//! \param[out] report_disk The found report, valid only if
|
|
|
|
//! CrashReportDatabase::kNoError is returned. Ownership is not
|
|
|
|
//! transferred to the caller, and the report may not be modified.
|
|
|
|
OperationStatus FindSingleReport(const UUID& uuid,
|
2015-02-17 12:05:29 -08:00
|
|
|
const ReportDisk** report_disk) const;
|
2015-02-11 12:17:05 -08:00
|
|
|
|
|
|
|
//! \brief Finds a single report matching the given UUID and in the desired
|
2015-02-17 12:05:29 -08:00
|
|
|
//! state, and returns a mutable ReportDisk* if found.
|
2015-02-11 12:17:05 -08:00
|
|
|
//!
|
2015-02-17 12:05:29 -08:00
|
|
|
//! This marks the metadata as dirty, and on destruction, changes will be
|
|
|
|
//! written to disk via Write().
|
2015-02-11 12:17:05 -08:00
|
|
|
//!
|
|
|
|
//! \return #kNoError on success. #kReportNotFound if there was no report with
|
2017-04-13 10:12:28 -04:00
|
|
|
//! the specified UUID, or if the report was not in the specified state
|
|
|
|
//! and was not uploading. #kBusyError if the report was not in the
|
|
|
|
//! specified state and was uploading.
|
2015-02-17 12:05:29 -08:00
|
|
|
OperationStatus FindSingleReportAndMarkDirty(const UUID& uuid,
|
|
|
|
ReportState desired_state,
|
|
|
|
ReportDisk** report_disk);
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2015-10-07 17:01:47 -04:00
|
|
|
//! \brief Removes a report from the metadata database, without touching the
|
|
|
|
//! on-disk file.
|
|
|
|
//!
|
|
|
|
//! The returned report is only valid if CrashReportDatabase::kNoError is
|
|
|
|
//! returned. This will mark the database as dirty. Future metadata
|
|
|
|
//! operations for this report will not succeed.
|
|
|
|
//!
|
|
|
|
//! \param[in] uuid The report identifier to remove.
|
|
|
|
//! \param[out] report_path The found report's file_path, valid only if
|
|
|
|
//! CrashReportDatabase::kNoError is returned.
|
|
|
|
OperationStatus DeleteReport(const UUID& uuid,
|
|
|
|
base::FilePath* report_path);
|
|
|
|
|
2020-06-24 09:03:47 +02:00
|
|
|
//! \brief Removes reports from the metadata database, that don't have
|
|
|
|
//! corresponding report files.
|
|
|
|
//!
|
|
|
|
//! \return number of metadata entries removed
|
|
|
|
int CleanDatabase();
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
private:
|
2021-11-02 10:50:42 +01:00
|
|
|
Metadata(FileHandle handle,
|
|
|
|
const base::FilePath& report_dir,
|
|
|
|
const base::FilePath& attachments_dir);
|
2015-02-11 12:17:05 -08:00
|
|
|
|
|
|
|
bool Rewind();
|
|
|
|
|
|
|
|
void Read();
|
|
|
|
void Write();
|
|
|
|
|
|
|
|
//! \brief Confirms that the corresponding report actually exists on disk
|
2015-02-17 12:05:29 -08:00
|
|
|
//! (that is, the dump file has not been removed), and that the report is
|
|
|
|
//! in the given state.
|
2015-02-11 12:17:05 -08:00
|
|
|
static OperationStatus VerifyReport(const ReportDisk& report_disk,
|
|
|
|
ReportState desired_state);
|
|
|
|
//! \brief Confirms that the corresponding report actually exists on disk
|
|
|
|
//! (that is, the dump file has not been removed).
|
|
|
|
static OperationStatus VerifyReportAnyState(const ReportDisk& report_disk);
|
|
|
|
|
|
|
|
ScopedFileHandle handle_;
|
|
|
|
const base::FilePath report_dir_;
|
2021-11-02 10:50:42 +01:00
|
|
|
const base::FilePath attachments_dir_;
|
2015-02-17 12:05:29 -08:00
|
|
|
bool dirty_; //! \brief `true` when a Write() is required on destruction.
|
2015-02-11 12:17:05 -08:00
|
|
|
std::vector<ReportDisk> reports_;
|
|
|
|
};
|
|
|
|
|
|
|
|
Metadata::~Metadata() {
|
|
|
|
if (dirty_)
|
|
|
|
Write();
|
|
|
|
// Not actually async, UnlockFileEx requires the Offset fields.
|
|
|
|
OVERLAPPED overlapped = {0};
|
|
|
|
if (!UnlockFileEx(handle_.get(), 0, MAXDWORD, MAXDWORD, &overlapped))
|
|
|
|
PLOG(ERROR) << "UnlockFileEx";
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2021-11-02 10:50:42 +01:00
|
|
|
std::unique_ptr<Metadata> Metadata::Create(
|
|
|
|
const base::FilePath& metadata_file,
|
|
|
|
const base::FilePath& report_dir,
|
|
|
|
const base::FilePath& attachments_dir) {
|
2015-02-11 12:17:05 -08:00
|
|
|
// It is important that dwShareMode be non-zero so that concurrent access to
|
|
|
|
// this file results in a successful open. This allows us to get to LockFileEx
|
|
|
|
// which then blocks to guard access.
|
|
|
|
FileHandle handle = CreateFile(metadata_file.value().c_str(),
|
|
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
nullptr,
|
|
|
|
OPEN_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
nullptr);
|
|
|
|
if (handle == kInvalidFileHandle)
|
2016-04-25 12:13:07 -07:00
|
|
|
return std::unique_ptr<Metadata>();
|
2015-02-11 12:17:05 -08:00
|
|
|
// Not actually async, LockFileEx requires the Offset fields.
|
|
|
|
OVERLAPPED overlapped = {0};
|
|
|
|
if (!LockFileEx(handle,
|
|
|
|
LOCKFILE_EXCLUSIVE_LOCK,
|
|
|
|
0,
|
|
|
|
MAXDWORD,
|
|
|
|
MAXDWORD,
|
|
|
|
&overlapped)) {
|
|
|
|
PLOG(ERROR) << "LockFileEx";
|
2016-04-25 12:13:07 -07:00
|
|
|
return std::unique_ptr<Metadata>();
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
2021-11-02 10:50:42 +01:00
|
|
|
std::unique_ptr<Metadata> metadata(
|
|
|
|
new Metadata(handle, report_dir, attachments_dir));
|
2015-02-11 12:17:05 -08:00
|
|
|
// If Read() fails, for whatever reason (corruption, etc.) metadata will not
|
|
|
|
// have been modified and will be in a clean empty state. We continue on and
|
|
|
|
// return an empty database to hopefully recover. This means that existing
|
|
|
|
// crash reports have been orphaned.
|
|
|
|
metadata->Read();
|
|
|
|
return metadata;
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
void Metadata::AddNewRecord(const ReportDisk& new_report_disk) {
|
|
|
|
DCHECK(new_report_disk.state == ReportState::kPending);
|
|
|
|
reports_.push_back(new_report_disk);
|
|
|
|
dirty_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
OperationStatus Metadata::FindReports(
|
|
|
|
ReportState desired_state,
|
2015-03-06 18:43:28 -05:00
|
|
|
std::vector<CrashReportDatabase::Report>* reports) const {
|
2015-02-17 12:05:29 -08:00
|
|
|
DCHECK(reports->empty());
|
|
|
|
for (const auto& report : reports_) {
|
|
|
|
if (report.state == desired_state &&
|
|
|
|
VerifyReport(report, desired_state) == CrashReportDatabase::kNoError) {
|
|
|
|
reports->push_back(report);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CrashReportDatabase::kNoError;
|
|
|
|
}
|
|
|
|
|
|
|
|
OperationStatus Metadata::FindSingleReport(
|
|
|
|
const UUID& uuid,
|
|
|
|
const ReportDisk** out_report) const {
|
|
|
|
auto report_iter = std::find_if(
|
|
|
|
reports_.begin(), reports_.end(), [uuid](const ReportDisk& report) {
|
|
|
|
return report.uuid == uuid;
|
|
|
|
});
|
|
|
|
if (report_iter == reports_.end())
|
|
|
|
return CrashReportDatabase::kReportNotFound;
|
|
|
|
OperationStatus os = VerifyReportAnyState(*report_iter);
|
|
|
|
if (os == CrashReportDatabase::kNoError)
|
|
|
|
*out_report = &*report_iter;
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
OperationStatus Metadata::FindSingleReportAndMarkDirty(
|
|
|
|
const UUID& uuid,
|
|
|
|
ReportState desired_state,
|
|
|
|
ReportDisk** report_disk) {
|
|
|
|
auto report_iter = std::find_if(
|
|
|
|
reports_.begin(), reports_.end(), [uuid](const ReportDisk& report) {
|
|
|
|
return report.uuid == uuid;
|
|
|
|
});
|
|
|
|
if (report_iter == reports_.end())
|
|
|
|
return CrashReportDatabase::kReportNotFound;
|
|
|
|
OperationStatus os = VerifyReport(*report_iter, desired_state);
|
|
|
|
if (os == CrashReportDatabase::kNoError) {
|
|
|
|
dirty_ = true;
|
|
|
|
*report_disk = &*report_iter;
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2015-10-07 17:01:47 -04:00
|
|
|
OperationStatus Metadata::DeleteReport(const UUID& uuid,
|
|
|
|
base::FilePath* report_path) {
|
|
|
|
auto report_iter = std::find_if(
|
|
|
|
reports_.begin(), reports_.end(), [uuid](const ReportDisk& report) {
|
|
|
|
return report.uuid == uuid;
|
|
|
|
});
|
|
|
|
if (report_iter == reports_.end())
|
|
|
|
return CrashReportDatabase::kReportNotFound;
|
|
|
|
*report_path = report_iter->file_path;
|
|
|
|
reports_.erase(report_iter);
|
|
|
|
dirty_ = true;
|
|
|
|
return CrashReportDatabase::kNoError;
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:03:47 +02:00
|
|
|
int Metadata::CleanDatabase() {
|
|
|
|
int removed = 0;
|
|
|
|
for (auto report_iter = reports_.begin(); report_iter != reports_.end();) {
|
|
|
|
if (!IsRegularFile(report_iter->file_path)) {
|
|
|
|
report_iter = reports_.erase(report_iter);
|
|
|
|
++removed;
|
|
|
|
dirty_ = true;
|
|
|
|
} else {
|
|
|
|
++report_iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2021-11-02 10:50:42 +01:00
|
|
|
Metadata::Metadata(FileHandle handle,
|
|
|
|
const base::FilePath& report_dir,
|
|
|
|
const base::FilePath& attachments_dir)
|
|
|
|
: handle_(handle),
|
|
|
|
report_dir_(report_dir),
|
|
|
|
attachments_dir_(attachments_dir),
|
|
|
|
dirty_(false),
|
|
|
|
reports_() {}
|
2015-02-17 12:05:29 -08:00
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
bool Metadata::Rewind() {
|
|
|
|
FileOffset result = LoggingSeekFile(handle_.get(), 0, SEEK_SET);
|
|
|
|
DCHECK_EQ(result, 0);
|
|
|
|
return result == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Metadata::Read() {
|
|
|
|
FileOffset length = LoggingSeekFile(handle_.get(), 0, SEEK_END);
|
|
|
|
if (length <= 0) // Failed, or empty: Abort.
|
|
|
|
return;
|
|
|
|
if (!Rewind()) {
|
|
|
|
LOG(ERROR) << "failed to rewind to read";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MetadataFileHeader header;
|
Make file_io reads more rational and predictable
ReadFile() attempted to continue reading after a short read. In most
cases, this is fine. However, ReadFile() would keep trying to fill a
partially-filled buffer until experiencing a 0-length read(), signaling
end-of-file. For certain weird file descriptors like terminal input, EOF
is an ephemeral condition, and attempting to read beyond EOF doesn’t
actually return 0 (EOF) provided that they remain open, it will block
waiting for more input. Consequently, ReadFile() and anything based on
ReadFile() had an undocumented and quirky interface, which was that any
short read that it returned (not an underlying short read) actually
indicated EOF.
This facet of ReadFile() was unexpected, so it’s being removed. The new
behavior is that ReadFile() will return an underlying short read. The
behavior of FileReaderInterface::Read() is updated in accordance with
this change.
Upon experiencing a short read, the caller can determine the best
action. Most callers were already prepared for this behavior. Outside of
util/file, only crashpad_database_util properly implemented EOF
detection according to previous semantics, and adapting it to new
semantics is trivial.
Callers who require an exact-length read can use the new
ReadFileExactly(), or the newly renamed LoggingReadFileExactly() or
CheckedReadFileExactly(). These functions will retry following a short
read. The renamed functions were previously called LoggingReadFile() and
CheckedReadFile(), but those names implied that they were simply
wrapping ReadFile(), which is not the case. They wrapped ReadFile() and
further, insisted on a full read. Since ReadFile()’s semantics are now
changing but these functions’ are not, they’re now even more distinct
from ReadFile(), and must be renamed to avoid confusion.
Test: *
Change-Id: I06b77e0d6ad8719bd2eb67dab93a8740542dd908
Reviewed-on: https://chromium-review.googlesource.com/456676
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2017-03-16 13:36:38 -04:00
|
|
|
if (!LoggingReadFileExactly(handle_.get(), &header, sizeof(header))) {
|
2015-02-11 12:17:05 -08:00
|
|
|
LOG(ERROR) << "failed to read header";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (header.magic != kMetadataFileHeaderMagic ||
|
|
|
|
header.version != kMetadataFileVersion) {
|
|
|
|
LOG(ERROR) << "unexpected header";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
base::CheckedNumeric<uint32_t> records_size =
|
|
|
|
base::CheckedNumeric<uint32_t>(header.num_records) *
|
2015-03-06 16:05:34 -08:00
|
|
|
static_cast<uint32_t>(sizeof(MetadataFileReportRecord));
|
2015-02-11 12:17:05 -08:00
|
|
|
if (!records_size.IsValid()) {
|
|
|
|
LOG(ERROR) << "record size out of range";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
std::vector<ReportDisk> reports;
|
2015-12-01 10:33:24 -08:00
|
|
|
if (header.num_records > 0) {
|
|
|
|
std::vector<MetadataFileReportRecord> records(header.num_records);
|
Make file_io reads more rational and predictable
ReadFile() attempted to continue reading after a short read. In most
cases, this is fine. However, ReadFile() would keep trying to fill a
partially-filled buffer until experiencing a 0-length read(), signaling
end-of-file. For certain weird file descriptors like terminal input, EOF
is an ephemeral condition, and attempting to read beyond EOF doesn’t
actually return 0 (EOF) provided that they remain open, it will block
waiting for more input. Consequently, ReadFile() and anything based on
ReadFile() had an undocumented and quirky interface, which was that any
short read that it returned (not an underlying short read) actually
indicated EOF.
This facet of ReadFile() was unexpected, so it’s being removed. The new
behavior is that ReadFile() will return an underlying short read. The
behavior of FileReaderInterface::Read() is updated in accordance with
this change.
Upon experiencing a short read, the caller can determine the best
action. Most callers were already prepared for this behavior. Outside of
util/file, only crashpad_database_util properly implemented EOF
detection according to previous semantics, and adapting it to new
semantics is trivial.
Callers who require an exact-length read can use the new
ReadFileExactly(), or the newly renamed LoggingReadFileExactly() or
CheckedReadFileExactly(). These functions will retry following a short
read. The renamed functions were previously called LoggingReadFile() and
CheckedReadFile(), but those names implied that they were simply
wrapping ReadFile(), which is not the case. They wrapped ReadFile() and
further, insisted on a full read. Since ReadFile()’s semantics are now
changing but these functions’ are not, they’re now even more distinct
from ReadFile(), and must be renamed to avoid confusion.
Test: *
Change-Id: I06b77e0d6ad8719bd2eb67dab93a8740542dd908
Reviewed-on: https://chromium-review.googlesource.com/456676
Reviewed-by: Robert Sesek <rsesek@chromium.org>
2017-03-16 13:36:38 -04:00
|
|
|
if (!LoggingReadFileExactly(
|
2015-12-01 10:33:24 -08:00
|
|
|
handle_.get(), &records[0], records_size.ValueOrDie())) {
|
|
|
|
LOG(ERROR) << "failed to read records";
|
2015-02-11 12:17:05 -08:00
|
|
|
return;
|
|
|
|
}
|
2015-12-01 10:33:24 -08:00
|
|
|
|
|
|
|
std::string string_table = ReadRestOfFileAsString(handle_.get());
|
|
|
|
if (string_table.empty() || string_table.back() != '\0') {
|
|
|
|
LOG(ERROR) << "bad string table";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& record : records) {
|
|
|
|
if (record.file_path_index >= string_table.size() ||
|
|
|
|
record.id_index >= string_table.size()) {
|
|
|
|
LOG(ERROR) << "invalid string table index";
|
|
|
|
return;
|
|
|
|
}
|
2019-03-13 14:53:16 -07:00
|
|
|
ReportDisk report_disk(record, report_dir_, string_table);
|
|
|
|
|
2021-11-02 10:50:42 +01:00
|
|
|
report_disk.total_size = GetFileSize(report_disk.file_path);
|
|
|
|
base::FilePath report_attachment_dir =
|
|
|
|
attachments_dir_.Append(report_disk.uuid.ToWString());
|
|
|
|
report_disk.total_size += GetDirectorySize(report_attachment_dir);
|
2019-03-13 14:53:16 -07:00
|
|
|
reports.push_back(report_disk);
|
2015-12-01 10:33:24 -08:00
|
|
|
}
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
2015-02-17 12:05:29 -08:00
|
|
|
reports_.swap(reports);
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Metadata::Write() {
|
|
|
|
if (!Rewind()) {
|
|
|
|
LOG(ERROR) << "failed to rewind to write";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate to ensure that a partial write doesn't cause a mix of old and new
|
|
|
|
// data causing an incorrect interpretation on read.
|
|
|
|
if (!SetEndOfFile(handle_.get())) {
|
|
|
|
PLOG(ERROR) << "failed to truncate";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t num_records = reports_.size();
|
|
|
|
|
|
|
|
// Fill and write out the header.
|
|
|
|
MetadataFileHeader header = {0};
|
|
|
|
header.magic = kMetadataFileHeaderMagic;
|
|
|
|
header.version = kMetadataFileVersion;
|
|
|
|
header.num_records = base::checked_cast<uint32_t>(num_records);
|
|
|
|
if (!LoggingWriteFile(handle_.get(), &header, sizeof(header))) {
|
|
|
|
LOG(ERROR) << "failed to write header";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-01 10:33:24 -08:00
|
|
|
if (num_records == 0)
|
|
|
|
return;
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
// Build the records and string table we're going to write.
|
|
|
|
std::string string_table;
|
2015-02-17 12:05:29 -08:00
|
|
|
std::vector<MetadataFileReportRecord> records;
|
|
|
|
records.reserve(num_records);
|
|
|
|
for (const auto& report : reports_) {
|
2015-02-11 12:17:05 -08:00
|
|
|
const base::FilePath& path = report.file_path;
|
|
|
|
if (path.DirName() != report_dir_) {
|
2023-10-26 17:51:24 +00:00
|
|
|
LOG(ERROR) << path << " expected to start with " << report_dir_;
|
2015-02-11 12:17:05 -08:00
|
|
|
return;
|
|
|
|
}
|
2015-02-17 12:05:29 -08:00
|
|
|
records.push_back(MetadataFileReportRecord(report, &string_table));
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!LoggingWriteFile(handle_.get(),
|
2015-02-17 12:05:29 -08:00
|
|
|
&records[0],
|
|
|
|
records.size() * sizeof(MetadataFileReportRecord))) {
|
2015-02-11 12:17:05 -08:00
|
|
|
LOG(ERROR) << "failed to write records";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!LoggingWriteFile(
|
|
|
|
handle_.get(), string_table.c_str(), string_table.size())) {
|
|
|
|
LOG(ERROR) << "failed to write string table";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
OperationStatus Metadata::VerifyReportAnyState(const ReportDisk& report_disk) {
|
|
|
|
DWORD fileattr = GetFileAttributes(report_disk.file_path.value().c_str());
|
|
|
|
if (fileattr == INVALID_FILE_ATTRIBUTES)
|
|
|
|
return CrashReportDatabase::kReportNotFound;
|
2015-02-17 12:05:29 -08:00
|
|
|
return (fileattr & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
? CrashReportDatabase::kFileSystemError
|
|
|
|
: CrashReportDatabase::kNoError;
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
OperationStatus Metadata::VerifyReport(const ReportDisk& report_disk,
|
|
|
|
ReportState desired_state) {
|
2017-04-13 10:12:28 -04:00
|
|
|
if (report_disk.state == desired_state) {
|
|
|
|
return VerifyReportAnyState(report_disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
return report_disk.state == ReportState::kUploading
|
|
|
|
? CrashReportDatabase::kBusyError
|
|
|
|
: CrashReportDatabase::kReportNotFound;
|
2015-02-17 12:05:29 -08:00
|
|
|
}
|
|
|
|
|
2015-10-08 13:10:02 -04:00
|
|
|
bool EnsureDirectory(const base::FilePath& path) {
|
|
|
|
DWORD fileattr = GetFileAttributes(path.value().c_str());
|
|
|
|
if (fileattr == INVALID_FILE_ATTRIBUTES) {
|
2023-10-26 17:51:24 +00:00
|
|
|
PLOG(ERROR) << "GetFileAttributes " << path;
|
2015-10-08 13:10:02 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
|
2023-10-26 17:51:24 +00:00
|
|
|
LOG(ERROR) << "GetFileAttributes " << path << ": not a directory";
|
2015-10-08 13:10:02 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
//! \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 CreateDirectoryIfNecessary(const base::FilePath& path) {
|
|
|
|
if (CreateDirectory(path.value().c_str(), nullptr))
|
|
|
|
return true;
|
|
|
|
if (GetLastError() != ERROR_ALREADY_EXISTS) {
|
2020-09-12 09:20:14 +02:00
|
|
|
PLOG(ERROR) << "CreateDirectory " << base::WideToUTF8(path.value());
|
2015-02-17 12:05:29 -08:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-08 13:10:02 -04:00
|
|
|
return EnsureDirectory(path);
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
2018-02-13 17:28:11 -08:00
|
|
|
} // namespace
|
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
// CrashReportDatabaseWin ------------------------------------------------------
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
class CrashReportDatabaseWin : public CrashReportDatabase {
|
|
|
|
public:
|
|
|
|
explicit CrashReportDatabaseWin(const base::FilePath& path);
|
2021-09-20 12:55:12 -07:00
|
|
|
|
|
|
|
CrashReportDatabaseWin(const CrashReportDatabaseWin&) = delete;
|
|
|
|
CrashReportDatabaseWin& operator=(const CrashReportDatabaseWin&) = delete;
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
~CrashReportDatabaseWin() override;
|
|
|
|
|
2015-10-08 13:10:02 -04:00
|
|
|
bool Initialize(bool may_create);
|
2015-02-11 12:17:05 -08:00
|
|
|
|
|
|
|
// CrashReportDatabase:
|
2015-03-09 18:47:52 -04:00
|
|
|
Settings* GetSettings() override;
|
2018-02-13 17:28:11 -08:00
|
|
|
OperationStatus PrepareNewCrashReport(
|
|
|
|
std::unique_ptr<NewReport>* report) override;
|
|
|
|
OperationStatus FinishedWritingCrashReport(std::unique_ptr<NewReport> report,
|
2015-02-11 12:17:05 -08:00
|
|
|
UUID* uuid) override;
|
|
|
|
OperationStatus LookUpCrashReport(const UUID& uuid, Report* report) override;
|
2015-03-06 18:43:28 -05:00
|
|
|
OperationStatus GetPendingReports(std::vector<Report>* reports) override;
|
|
|
|
OperationStatus GetCompletedReports(std::vector<Report>* reports) override;
|
2018-02-15 08:17:12 -08:00
|
|
|
OperationStatus GetReportForUploading(
|
|
|
|
const UUID& uuid,
|
2018-07-24 09:13:49 -07:00
|
|
|
std::unique_ptr<const UploadReport>* report,
|
|
|
|
bool report_metrics) override;
|
2016-09-26 15:06:19 -07:00
|
|
|
OperationStatus SkipReportUpload(const UUID& uuid,
|
|
|
|
Metrics::CrashSkippedReason reason) override;
|
2015-10-07 17:01:47 -04:00
|
|
|
OperationStatus DeleteReport(const UUID& uuid) override;
|
2016-08-24 15:37:46 -04:00
|
|
|
OperationStatus RequestUpload(const UUID& uuid) override;
|
2020-06-24 09:03:47 +02:00
|
|
|
int CleanDatabase(time_t lockfile_ttl) override;
|
2021-11-02 10:50:42 +01:00
|
|
|
base::FilePath DatabasePath() override;
|
2015-02-11 12:17:05 -08:00
|
|
|
|
|
|
|
private:
|
2018-02-15 08:17:12 -08:00
|
|
|
// CrashReportDatabase:
|
|
|
|
OperationStatus RecordUploadAttempt(UploadReport* report,
|
|
|
|
bool successful,
|
|
|
|
const std::string& id) override;
|
|
|
|
|
2020-06-24 09:03:47 +02:00
|
|
|
// Cleans any attachments that have no associated report.
|
|
|
|
void CleanOrphanedAttachments();
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> AcquireMetadata();
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2022-06-08 19:45:54 -04:00
|
|
|
Settings& SettingsInternal() {
|
2022-06-22 12:22:26 -04:00
|
|
|
std::call_once(settings_init_, [this]() {
|
2022-06-08 19:45:54 -04:00
|
|
|
settings_.Initialize(base_dir_.Append(kSettings));
|
2022-06-22 12:22:26 -04:00
|
|
|
});
|
2022-06-08 19:45:54 -04:00
|
|
|
return settings_;
|
|
|
|
}
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
base::FilePath base_dir_;
|
2015-05-01 15:09:38 -07:00
|
|
|
Settings settings_;
|
2022-06-22 12:22:26 -04:00
|
|
|
std::once_flag settings_init_;
|
2015-03-10 13:24:44 -04:00
|
|
|
InitializationStateDcheck initialized_;
|
2015-02-11 12:17:05 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
CrashReportDatabaseWin::CrashReportDatabaseWin(const base::FilePath& path)
|
2022-06-08 19:45:54 -04:00
|
|
|
: CrashReportDatabase(),
|
|
|
|
base_dir_(path),
|
|
|
|
settings_(),
|
2022-06-22 12:22:26 -04:00
|
|
|
settings_init_(),
|
2022-06-08 19:45:54 -04:00
|
|
|
initialized_() {}
|
2015-02-11 12:17:05 -08:00
|
|
|
|
|
|
|
CrashReportDatabaseWin::~CrashReportDatabaseWin() {
|
|
|
|
}
|
|
|
|
|
2015-10-08 13:10:02 -04:00
|
|
|
bool CrashReportDatabaseWin::Initialize(bool may_create) {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
|
|
|
|
|
2015-10-08 13:10:02 -04:00
|
|
|
// Ensure the database directory exists.
|
|
|
|
if (may_create) {
|
|
|
|
if (!CreateDirectoryIfNecessary(base_dir_))
|
|
|
|
return false;
|
|
|
|
} else if (!EnsureDirectory(base_dir_)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the report subdirectory exists.
|
|
|
|
if (!CreateDirectoryIfNecessary(base_dir_.Append(kReportsDirectory)))
|
2015-02-11 12:17:05 -08:00
|
|
|
return false;
|
|
|
|
|
2021-11-02 10:50:42 +01:00
|
|
|
if (!CreateDirectoryIfNecessary(AttachmentsRootPath()))
|
|
|
|
return false;
|
|
|
|
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_SET_VALID(initialized_);
|
2015-02-11 12:17:05 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-11-02 10:50:42 +01:00
|
|
|
base::FilePath CrashReportDatabaseWin::DatabasePath() {
|
|
|
|
return base_dir_;
|
|
|
|
}
|
|
|
|
|
2015-03-09 18:47:52 -04:00
|
|
|
Settings* CrashReportDatabaseWin::GetSettings() {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
2022-06-08 19:45:54 -04:00
|
|
|
return &SettingsInternal();
|
2015-03-09 18:47:52 -04:00
|
|
|
}
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
OperationStatus CrashReportDatabaseWin::PrepareNewCrashReport(
|
2018-02-13 17:28:11 -08:00
|
|
|
std::unique_ptr<NewReport>* report) {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<NewReport> new_report(new NewReport());
|
2018-05-18 15:53:54 -07:00
|
|
|
if (!new_report->Initialize(this,
|
|
|
|
base_dir_.Append(kReportsDirectory),
|
2018-02-13 17:28:11 -08:00
|
|
|
std::wstring(L".") + kCrashReportFileExtension)) {
|
2015-02-11 12:17:05 -08:00
|
|
|
return kFileSystemError;
|
2018-02-13 17:28:11 -08:00
|
|
|
}
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2018-02-13 17:28:11 -08:00
|
|
|
report->reset(new_report.release());
|
2015-02-11 12:17:05 -08:00
|
|
|
return kNoError;
|
|
|
|
}
|
|
|
|
|
|
|
|
OperationStatus CrashReportDatabaseWin::FinishedWritingCrashReport(
|
2018-02-13 17:28:11 -08:00
|
|
|
std::unique_ptr<NewReport> report,
|
2015-02-11 12:17:05 -08:00
|
|
|
UUID* uuid) {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
2015-02-11 12:17:05 -08:00
|
|
|
if (!metadata)
|
|
|
|
return kDatabaseError;
|
2018-02-13 17:28:11 -08:00
|
|
|
metadata->AddNewRecord(ReportDisk(report->ReportID(),
|
|
|
|
report->file_remover_.get(),
|
2015-02-17 12:05:29 -08:00
|
|
|
time(nullptr),
|
|
|
|
ReportState::kPending));
|
2015-03-10 13:24:44 -04:00
|
|
|
|
2022-01-07 17:19:37 -05:00
|
|
|
std::ignore = report->file_remover_.release();
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2020-06-24 09:03:47 +02:00
|
|
|
// Close all the attachments and disarm their removers too.
|
|
|
|
for (auto& writer : report->attachment_writers_) {
|
|
|
|
writer->Close();
|
|
|
|
}
|
|
|
|
for (auto& remover : report->attachment_removers_) {
|
2022-01-07 17:19:37 -05:00
|
|
|
std::ignore = remover.release();
|
2020-06-24 09:03:47 +02:00
|
|
|
}
|
|
|
|
|
2018-02-13 17:28:11 -08:00
|
|
|
*uuid = report->ReportID();
|
2015-02-11 12:17:05 -08:00
|
|
|
|
2018-02-13 17:28:11 -08:00
|
|
|
Metrics::CrashReportPending(Metrics::PendingReportReason::kNewlyCreated);
|
|
|
|
Metrics::CrashReportSize(report->Writer()->Seek(0, SEEK_END));
|
2015-02-11 12:17:05 -08:00
|
|
|
|
|
|
|
return kNoError;
|
|
|
|
}
|
|
|
|
|
|
|
|
OperationStatus CrashReportDatabaseWin::LookUpCrashReport(const UUID& uuid,
|
|
|
|
Report* report) {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
2015-02-11 12:17:05 -08:00
|
|
|
if (!metadata)
|
|
|
|
return kDatabaseError;
|
|
|
|
// Find and return a copy of the matching report.
|
|
|
|
const ReportDisk* report_disk;
|
|
|
|
OperationStatus os = metadata->FindSingleReport(uuid, &report_disk);
|
2015-02-17 12:05:29 -08:00
|
|
|
if (os == kNoError)
|
|
|
|
*report = *report_disk;
|
|
|
|
return os;
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
OperationStatus CrashReportDatabaseWin::GetPendingReports(
|
2015-03-06 18:43:28 -05:00
|
|
|
std::vector<Report>* reports) {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
2015-02-17 12:05:29 -08:00
|
|
|
return metadata ? metadata->FindReports(ReportState::kPending, reports)
|
|
|
|
: kDatabaseError;
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
OperationStatus CrashReportDatabaseWin::GetCompletedReports(
|
2015-03-06 18:43:28 -05:00
|
|
|
std::vector<Report>* reports) {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
2015-02-17 12:05:29 -08:00
|
|
|
return metadata ? metadata->FindReports(ReportState::kCompleted, reports)
|
|
|
|
: kDatabaseError;
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
OperationStatus CrashReportDatabaseWin::GetReportForUploading(
|
|
|
|
const UUID& uuid,
|
2018-07-24 09:13:49 -07:00
|
|
|
std::unique_ptr<const UploadReport>* report,
|
|
|
|
bool report_metrics) {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
2015-02-11 12:17:05 -08:00
|
|
|
if (!metadata)
|
|
|
|
return kDatabaseError;
|
2018-02-15 08:17:12 -08:00
|
|
|
|
2015-02-17 12:05:29 -08:00
|
|
|
ReportDisk* report_disk;
|
|
|
|
OperationStatus os = metadata->FindSingleReportAndMarkDirty(
|
|
|
|
uuid, ReportState::kPending, &report_disk);
|
2015-10-07 14:00:42 -04:00
|
|
|
if (os == kNoError) {
|
2015-02-17 12:05:29 -08:00
|
|
|
report_disk->state = ReportState::kUploading;
|
2018-02-15 08:17:12 -08:00
|
|
|
auto upload_report = std::make_unique<UploadReport>();
|
|
|
|
*implicit_cast<Report*>(upload_report.get()) = *report_disk;
|
|
|
|
|
|
|
|
if (!upload_report->Initialize(upload_report->file_path, this)) {
|
|
|
|
return kFileSystemError;
|
|
|
|
}
|
2018-07-24 09:13:49 -07:00
|
|
|
upload_report->report_metrics_ = report_metrics;
|
2018-02-15 08:17:12 -08:00
|
|
|
|
|
|
|
report->reset(upload_report.release());
|
2015-02-17 12:05:29 -08:00
|
|
|
}
|
|
|
|
return os;
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
OperationStatus CrashReportDatabaseWin::RecordUploadAttempt(
|
2018-02-15 08:17:12 -08:00
|
|
|
UploadReport* report,
|
2015-02-11 12:17:05 -08:00
|
|
|
bool successful,
|
|
|
|
const std::string& id) {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
2018-07-24 09:13:49 -07:00
|
|
|
if (report->report_metrics_) {
|
|
|
|
Metrics::CrashUploadAttempted(successful);
|
|
|
|
}
|
2016-09-26 15:06:19 -07:00
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
2015-02-11 12:17:05 -08:00
|
|
|
if (!metadata)
|
|
|
|
return kDatabaseError;
|
2015-02-17 12:05:29 -08:00
|
|
|
ReportDisk* report_disk;
|
|
|
|
OperationStatus os = metadata->FindSingleReportAndMarkDirty(
|
|
|
|
report->uuid, ReportState::kUploading, &report_disk);
|
2015-10-07 14:00:42 -04:00
|
|
|
if (os != kNoError)
|
|
|
|
return os;
|
2015-03-10 12:22:31 -04:00
|
|
|
|
2015-10-07 14:00:42 -04:00
|
|
|
time_t now = time(nullptr);
|
2015-03-10 12:22:31 -04:00
|
|
|
|
2015-10-07 14:00:42 -04:00
|
|
|
report_disk->uploaded = successful;
|
|
|
|
report_disk->id = id;
|
|
|
|
report_disk->last_upload_attempt_time = now;
|
|
|
|
report_disk->upload_attempts++;
|
2016-08-24 15:37:46 -04:00
|
|
|
if (successful) {
|
|
|
|
report_disk->state = ReportState::kCompleted;
|
|
|
|
report_disk->upload_explicitly_requested = false;
|
|
|
|
} else {
|
|
|
|
report_disk->state = ReportState::kPending;
|
|
|
|
report_disk->upload_explicitly_requested =
|
|
|
|
report->upload_explicitly_requested;
|
|
|
|
}
|
2015-10-07 14:00:42 -04:00
|
|
|
|
2022-06-08 19:45:54 -04:00
|
|
|
if (!SettingsInternal().SetLastUploadAttemptTime(now))
|
2015-10-07 14:00:42 -04:00
|
|
|
return kDatabaseError;
|
|
|
|
|
|
|
|
return kNoError;
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
2015-10-07 17:01:47 -04:00
|
|
|
OperationStatus CrashReportDatabaseWin::DeleteReport(const UUID& uuid) {
|
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
2015-10-07 17:01:47 -04:00
|
|
|
if (!metadata)
|
|
|
|
return kDatabaseError;
|
|
|
|
|
|
|
|
base::FilePath report_path;
|
|
|
|
OperationStatus os = metadata->DeleteReport(uuid, &report_path);
|
|
|
|
if (os != kNoError)
|
|
|
|
return os;
|
|
|
|
|
|
|
|
if (!DeleteFile(report_path.value().c_str())) {
|
2023-10-26 17:51:24 +00:00
|
|
|
PLOG(ERROR) << "DeleteFile " << report_path;
|
2015-10-07 17:01:47 -04:00
|
|
|
return kFileSystemError;
|
|
|
|
}
|
2020-06-24 09:03:47 +02:00
|
|
|
|
|
|
|
RemoveAttachmentsByUUID(uuid);
|
|
|
|
|
2015-10-07 17:01:47 -04:00
|
|
|
return kNoError;
|
|
|
|
}
|
|
|
|
|
2016-09-26 15:06:19 -07:00
|
|
|
OperationStatus CrashReportDatabaseWin::SkipReportUpload(
|
|
|
|
const UUID& uuid,
|
|
|
|
Metrics::CrashSkippedReason reason) {
|
2015-03-10 13:24:44 -04:00
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
2016-09-26 15:06:19 -07:00
|
|
|
Metrics::CrashUploadSkipped(reason);
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
2015-02-11 12:17:05 -08:00
|
|
|
if (!metadata)
|
|
|
|
return kDatabaseError;
|
2015-02-17 12:05:29 -08:00
|
|
|
ReportDisk* report_disk;
|
|
|
|
OperationStatus os = metadata->FindSingleReportAndMarkDirty(
|
|
|
|
uuid, ReportState::kPending, &report_disk);
|
2016-08-24 15:37:46 -04:00
|
|
|
if (os == kNoError) {
|
2015-02-17 12:05:29 -08:00
|
|
|
report_disk->state = ReportState::kCompleted;
|
2016-08-24 15:37:46 -04:00
|
|
|
report_disk->upload_explicitly_requested = false;
|
|
|
|
}
|
2015-02-17 12:05:29 -08:00
|
|
|
return os;
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<Metadata> CrashReportDatabaseWin::AcquireMetadata() {
|
2015-02-11 12:17:05 -08:00
|
|
|
base::FilePath metadata_file = base_dir_.Append(kMetadataFileName);
|
2021-11-02 10:50:42 +01:00
|
|
|
return Metadata::Create(metadata_file,
|
|
|
|
base_dir_.Append(kReportsDirectory),
|
|
|
|
AttachmentsRootPath());
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<CrashReportDatabase> InitializeInternal(
|
|
|
|
const base::FilePath& path,
|
|
|
|
bool may_create) {
|
|
|
|
std::unique_ptr<CrashReportDatabaseWin> database_win(
|
2015-10-08 13:10:02 -04:00
|
|
|
new CrashReportDatabaseWin(path));
|
|
|
|
return database_win->Initialize(may_create)
|
2015-12-09 17:36:32 -05:00
|
|
|
? std::move(database_win)
|
2016-04-25 12:13:07 -07:00
|
|
|
: std::unique_ptr<CrashReportDatabaseWin>();
|
2015-10-08 13:10:02 -04:00
|
|
|
}
|
|
|
|
|
2016-08-24 15:37:46 -04:00
|
|
|
OperationStatus CrashReportDatabaseWin::RequestUpload(const UUID& uuid) {
|
|
|
|
INITIALIZATION_STATE_DCHECK_VALID(initialized_);
|
|
|
|
|
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
|
|
|
if (!metadata)
|
|
|
|
return kDatabaseError;
|
|
|
|
|
|
|
|
ReportDisk* report_disk;
|
|
|
|
// TODO(gayane): Search for the report only once regardless of its state.
|
|
|
|
OperationStatus os = metadata->FindSingleReportAndMarkDirty(
|
|
|
|
uuid, ReportState::kCompleted, &report_disk);
|
2017-04-13 10:12:28 -04:00
|
|
|
if (os == kReportNotFound) {
|
2016-08-24 15:37:46 -04:00
|
|
|
os = metadata->FindSingleReportAndMarkDirty(
|
|
|
|
uuid, ReportState::kPending, &report_disk);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (os != kNoError)
|
|
|
|
return os;
|
|
|
|
|
|
|
|
// If the crash report has already been uploaded, don't request new upload.
|
|
|
|
if (report_disk->uploaded)
|
|
|
|
return kCannotRequestUpload;
|
|
|
|
|
|
|
|
// Mark the crash report as having upload explicitly requested by the user,
|
|
|
|
// and move it to the pending state.
|
|
|
|
report_disk->upload_explicitly_requested = true;
|
|
|
|
report_disk->state = ReportState::kPending;
|
|
|
|
|
2016-09-26 15:06:19 -07:00
|
|
|
Metrics::CrashReportPending(Metrics::PendingReportReason::kUserInitiated);
|
|
|
|
|
2016-08-24 15:37:46 -04:00
|
|
|
return kNoError;
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:03:47 +02:00
|
|
|
int CrashReportDatabaseWin::CleanDatabase(time_t lockfile_ttl) {
|
|
|
|
int removed = 0;
|
|
|
|
const base::FilePath dir_path(base_dir_.Append(kReportsDirectory));
|
|
|
|
DirectoryReader reader;
|
|
|
|
if (!reader.Open(dir_path)) {
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::FilePath filename;
|
|
|
|
DirectoryReader::Result result;
|
|
|
|
time_t now = time(nullptr);
|
|
|
|
|
|
|
|
std::unique_ptr<Metadata> metadata(AcquireMetadata());
|
|
|
|
|
|
|
|
// Remove old reports without metadata.
|
|
|
|
while ((result = reader.NextFile(&filename)) ==
|
|
|
|
DirectoryReader::Result::kSuccess) {
|
|
|
|
timespec filetime;
|
|
|
|
const base::FilePath report_path(dir_path.Append(filename));
|
|
|
|
if (!FileModificationTime(report_path, &filetime) ||
|
|
|
|
filetime.tv_sec > now - lockfile_ttl) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ReportDisk* report_disk;
|
|
|
|
UUID uuid;
|
|
|
|
bool is_uuid = UUIDFromReportPath(report_path, &uuid);
|
|
|
|
// ignore files whose base name is not uuid
|
|
|
|
if (!is_uuid) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
OperationStatus os = metadata->FindSingleReport(uuid, &report_disk);
|
|
|
|
|
|
|
|
if (os == OperationStatus::kReportNotFound) {
|
|
|
|
if (LoggingRemoveFile(report_path)) {
|
|
|
|
++removed;
|
|
|
|
RemoveAttachmentsByUUID(uuid);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any metadata records without report files.
|
|
|
|
removed += metadata->CleanDatabase();
|
|
|
|
|
|
|
|
CleanOrphanedAttachments();
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CrashReportDatabaseWin::CleanOrphanedAttachments() {
|
2021-11-02 10:50:42 +01:00
|
|
|
base::FilePath root_attachments_dir = AttachmentsRootPath();
|
2020-06-24 09:03:47 +02:00
|
|
|
DirectoryReader reader;
|
|
|
|
if (!reader.Open(root_attachments_dir)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::FilePath filename;
|
|
|
|
DirectoryReader::Result result;
|
|
|
|
base::FilePath reports_dir = base_dir_.Append(kReportsDirectory);
|
|
|
|
while ((result = reader.NextFile(&filename)) ==
|
|
|
|
DirectoryReader::Result::kSuccess) {
|
|
|
|
const base::FilePath path(root_attachments_dir.Append(filename));
|
|
|
|
if (IsDirectory(path, false)) {
|
|
|
|
UUID uuid;
|
|
|
|
if (!uuid.InitializeFromString(filename.value())) {
|
2023-10-26 17:51:24 +00:00
|
|
|
LOG(ERROR) << "unexpected attachment dir name " << filename;
|
2020-06-24 09:03:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove attachments if corresponding report doen't exist.
|
|
|
|
base::FilePath report_path =
|
2020-09-12 09:20:14 +02:00
|
|
|
reports_dir.Append(uuid.ToWString() + kCrashReportFileExtension);
|
2020-06-24 09:03:47 +02:00
|
|
|
if (!IsRegularFile(report_path)) {
|
|
|
|
RemoveAttachmentsByUUID(uuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
// static
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<CrashReportDatabase> CrashReportDatabase::Initialize(
|
2015-02-11 12:17:05 -08:00
|
|
|
const base::FilePath& path) {
|
2015-10-08 13:10:02 -04:00
|
|
|
return InitializeInternal(path, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-04-25 12:13:07 -07:00
|
|
|
std::unique_ptr<CrashReportDatabase>
|
|
|
|
CrashReportDatabase::InitializeWithoutCreating(const base::FilePath& path) {
|
2015-10-08 13:10:02 -04:00
|
|
|
return InitializeInternal(path, false);
|
2015-02-11 12:17:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace crashpad
|