chromium/third_party/crashpad/crashpad/client/settings.cc

// Copyright 2015 The Crashpad Authors
//
// 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/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  // !CRASHPAD_FLOCK_ALWAYS_SUPPORTED

#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() {
  // Call reset() to ensure the lock is released before the ios_background_task.
  reset();
}

#endif  // BUILDFLAG(IS_IOS)

namespace internal {

// static
void ScopedLockedFileHandleTraits::Free(FileHandle handle) {}

}  // namespace internal

#endif  // BUILDFLAG(IS_FUCHSIA)

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
// static
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  // !CRASHPAD_FLOCK_ALWAYS_SUPPORTED

// static
Settings::ScopedLockedFileHandle Settings::MakeScopedLockedFileHandle(
    const internal::MakeScopedLockedFileHandleOptions& options,
    FileLocking locking,
    const base::FilePath& file_path) {}

// static
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) {}

}  // namespace crashpad