chromium/base/files/file_posix.cc

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif

#include "base/files/file.h"

// The only 32-bit platform that uses this file is Android. On Android APIs
// >= 21, this standard define is the right way to express that you want a
// 64-bit offset in struct stat, and the stat64 struct and functions aren't
// useful.
#define _FILE_OFFSET_BITS

#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/stat.h>
#include <unistd.h>

static_assert;

#include <atomic>
#include <optional>

#include "base/check_op.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/scoped_blocking_call.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/os_compat_android.h"
#endif

namespace base {

// Make sure our Whence mappings match the system headers.
static_assert;

namespace {

#if BUILDFLAG(IS_APPLE)
// When enabled, `F_FULLFSYNC` is not used in `File::Flush`. Instead,
// `F_BARRIERFSYNC` or `flush()` is used (depending on the
// "MacEfficientFileFlushUseBarrier" param). The feature exists to measure the
// cost of `F_FULLFSYNC` compared to other solutions (not ready to enable by
// default as-is). See
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html
BASE_FEATURE(kMacEfficientFileFlush,
             "MacEfficientFileFlush",
             base::FEATURE_DISABLED_BY_DEFAULT);

const FeatureParam<bool> kMacEfficientFileFlushUseBarrier{
    &kMacEfficientFileFlush, "MacEfficientFileFlushUseBarrier", true};

enum class MacFileFlushMechanism {
  kFlush,
  kFullFsync,
  kBarrierFsync,
};

std::atomic<MacFileFlushMechanism> g_mac_file_flush_mechanism{
    MacFileFlushMechanism::kFullFsync};
#endif  // BUILDFLAG(IS_APPLE)

// NaCl doesn't provide the following system calls, so either simulate them or
// wrap them in order to minimize the number of #ifdef's in this file.
#if !BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_AIX)
bool IsOpenAppend(PlatformFile file) {}

int CallFtruncate(PlatformFile file, int64_t length) {}

int CallFutimes(PlatformFile file, const struct timeval times[2]) {}

#if !BUILDFLAG(IS_FUCHSIA)
short FcntlFlockType(std::optional<File::LockMode> mode) {}

File::Error CallFcntlFlock(PlatformFile file,
                           std::optional<File::LockMode> mode) {}
#endif

#else   // BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_AIX)

bool IsOpenAppend(PlatformFile file) {
  // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX
  // standard and always appends if the file is opened with O_APPEND, just
  // return false here.
  return false;
}

int CallFtruncate(PlatformFile file, int64_t length) {
  NOTIMPLEMENTED();  // NaCl doesn't implement ftruncate.
  return 0;
}

int CallFutimes(PlatformFile file, const struct timeval times[2]) {
  NOTIMPLEMENTED();  // NaCl doesn't implement futimes.
  return 0;
}

File::Error CallFcntlFlock(PlatformFile file,
                           std::optional<File::LockMode> mode) {
  NOTIMPLEMENTED();  // NaCl doesn't implement flock struct.
  return File::FILE_ERROR_INVALID_OPERATION;
}
#endif  // BUILDFLAG(IS_NACL)

}  // namespace

void File::Info::FromStat(const stat_wrapper_t& stat_info) {}

bool File::IsValid() const {}

PlatformFile File::GetPlatformFile() const {}

PlatformFile File::TakePlatformFile() {}

void File::Close() {}

int64_t File::Seek(Whence whence, int64_t offset) {}

int File::Read(int64_t offset, char* data, int size) {}

int File::ReadAtCurrentPos(char* data, int size) {}

int File::ReadNoBestEffort(int64_t offset, char* data, int size) {}

int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {}

int File::Write(int64_t offset, const char* data, int size) {}

int File::WriteAtCurrentPos(const char* data, int size) {}

int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) {}

int64_t File::GetLength() const {}

bool File::SetLength(int64_t length) {}

bool File::SetTimes(Time last_access_time, Time last_modified_time) {}

bool File::GetInfo(Info* info) {}

#if !BUILDFLAG(IS_FUCHSIA)
File::Error File::Lock(File::LockMode mode) {}

File::Error File::Unlock() {}
#endif

File File::Duplicate() const {}

#if BUILDFLAG(IS_APPLE)
void File::InitializeFeatures() {
  if (FeatureList::IsEnabled(kMacEfficientFileFlush)) {
    // "relaxed" because there is no dependency between these memory operations
    // and other memory operations.
    if (kMacEfficientFileFlushUseBarrier.Get()) {
      g_mac_file_flush_mechanism.store(MacFileFlushMechanism::kBarrierFsync,
                                       std::memory_order_relaxed);
    } else {
      g_mac_file_flush_mechanism.store(MacFileFlushMechanism::kFlush,
                                       std::memory_order_relaxed);
    }
  }
}
#endif  // BUILDFLAG(IS_APPLE)

// Static.
File::Error File::OSErrorToFileError(int saved_errno) {}

// NaCl doesn't implement system calls to open files directly.
#if !BUILDFLAG(IS_NACL)
// TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
void File::DoInitialize(const FilePath& path, uint32_t flags) {}
#endif  // !BUILDFLAG(IS_NACL)

bool File::Flush() {}

void File::SetPlatformFile(PlatformFile file) {}

// static
File::Error File::GetLastFileError() {}

int File::Stat(const FilePath& path, stat_wrapper_t* sb) {}
int File::Fstat(int fd, stat_wrapper_t* sb) {}
int File::Lstat(const FilePath& path, stat_wrapper_t* sb) {}

}  // namespace base