#include "client/settings.h"
#include <stdint.h>
#include <limits>
#include "base/logging.h"
#include "base/notreached.h"
#include "base/posix/eintr_wrapper.h"
#include "build/build_config.h"
#include "util/file/filesystem.h"
#include "util/numeric/in_range_cast.h"
namespace crashpad {
#if !CRASHPAD_FLOCK_ALWAYS_SUPPORTED
Settings::ScopedLockedFileHandle::ScopedLockedFileHandle()
: handle_(kInvalidFileHandle), lockfile_path_() {
}
Settings::ScopedLockedFileHandle::ScopedLockedFileHandle(
FileHandle handle,
const base::FilePath& lockfile_path)
: handle_(handle), lockfile_path_(lockfile_path) {
}
Settings::ScopedLockedFileHandle::ScopedLockedFileHandle(
ScopedLockedFileHandle&& other)
: handle_(other.handle_), lockfile_path_(other.lockfile_path_) {
other.handle_ = kInvalidFileHandle;
other.lockfile_path_ = base::FilePath();
}
Settings::ScopedLockedFileHandle& Settings::ScopedLockedFileHandle::operator=(
ScopedLockedFileHandle&& other) {
handle_ = other.handle_;
lockfile_path_ = other.lockfile_path_;
other.handle_ = kInvalidFileHandle;
other.lockfile_path_ = base::FilePath();
return *this;
}
Settings::ScopedLockedFileHandle::~ScopedLockedFileHandle() {
Destroy();
}
void Settings::ScopedLockedFileHandle::Destroy() {
if (handle_ != kInvalidFileHandle) {
CheckedCloseFile(handle_);
}
if (!lockfile_path_.empty()) {
const bool success = LoggingRemoveFile(lockfile_path_);
DCHECK(success);
}
}
#else
#if BUILDFLAG(IS_IOS)
Settings::ScopedLockedFileHandle::ScopedLockedFileHandle(
const FileHandle& value)
: ScopedGeneric(value) {
ios_background_task_ = std::make_unique<internal::ScopedBackgroundTask>(
"ScopedLockedFileHandle");
}
Settings::ScopedLockedFileHandle::ScopedLockedFileHandle(
Settings::ScopedLockedFileHandle&& rvalue) {
ios_background_task_.reset(rvalue.ios_background_task_.release());
reset(rvalue.release());
}
Settings::ScopedLockedFileHandle& Settings::ScopedLockedFileHandle::operator=(
Settings::ScopedLockedFileHandle&& rvalue) {
ios_background_task_.reset(rvalue.ios_background_task_.release());
reset(rvalue.release());
return *this;
}
Settings::ScopedLockedFileHandle::~ScopedLockedFileHandle() {
reset();
}
#endif
namespace internal {
void ScopedLockedFileHandleTraits::Free(FileHandle handle) { … }
}
#endif
struct Settings::Data { … };
Settings::Settings() = default;
Settings::~Settings() = default;
bool Settings::Initialize(const base::FilePath& file_path) { … }
bool Settings::GetClientID(UUID* client_id) { … }
bool Settings::GetUploadsEnabled(bool* enabled) { … }
bool Settings::SetUploadsEnabled(bool enabled) { … }
bool Settings::GetLastUploadAttemptTime(time_t* time) { … }
bool Settings::SetLastUploadAttemptTime(time_t time) { … }
#if !CRASHPAD_FLOCK_ALWAYS_SUPPORTED
bool Settings::IsLockExpired(const base::FilePath& file_path,
time_t lockfile_ttl) {
time_t now = time(nullptr);
base::FilePath lock_path(file_path.value() + Settings::kLockfileExtension);
ScopedFileHandle lock_fd(LoggingOpenFileForRead(lock_path));
time_t lock_timestamp;
if (!LoggingReadFileExactly(
lock_fd.get(), &lock_timestamp, sizeof(lock_timestamp))) {
return false;
}
return now >= lock_timestamp + lockfile_ttl;
}
#endif
Settings::ScopedLockedFileHandle Settings::MakeScopedLockedFileHandle(
const internal::MakeScopedLockedFileHandleOptions& options,
FileLocking locking,
const base::FilePath& file_path) { … }
FileHandle Settings::GetHandleFromOptions(
const base::FilePath& file_path,
const internal::MakeScopedLockedFileHandleOptions& options) { … }
Settings::ScopedLockedFileHandle Settings::OpenForReading() { … }
Settings::ScopedLockedFileHandle Settings::OpenForReadingAndWriting(
FileWriteMode mode, bool log_open_error) { … }
bool Settings::OpenAndReadSettings(Data* out_data) { … }
Settings::ScopedLockedFileHandle Settings::OpenForWritingAndReadSettings(
Data* out_data) { … }
bool Settings::ReadSettings(FileHandle handle,
Data* out_data,
bool log_read_error) { … }
bool Settings::WriteSettings(FileHandle handle, const Data& data) { … }
bool Settings::RecoverSettings(FileHandle handle, Data* out_data) { … }
bool Settings::InitializeSettings(FileHandle handle) { … }
}