1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260

base / files / file.cc [blame]

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

#include "base/files/file.h"

#include <utility>

#include "base/check_op.h"
#include "base/files/file_path.h"
#include "base/files/file_tracing.h"
#include "base/metrics/histogram.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/timer/elapsed_timer.h"
#include "base/trace_event/base_tracing.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include <errno.h>
#endif

namespace base {

File::Info::Info() = default;

File::Info::~Info() = default;

File::File() = default;

#if !BUILDFLAG(IS_NACL)
File::File(const FilePath& path, uint32_t flags) : error_details_(FILE_OK) {
  Initialize(path, flags);
}
#endif

File::File(ScopedPlatformFile platform_file)
    : File(std::move(platform_file), false) {}

File::File(PlatformFile platform_file) : File(platform_file, false) {}

File::File(ScopedPlatformFile platform_file, bool async)
    : file_(std::move(platform_file)), error_details_(FILE_OK), async_(async) {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  DCHECK_GE(file_.get(), -1);
#endif
}

File::File(PlatformFile platform_file, bool async)
    : file_(platform_file),
      error_details_(FILE_OK),
      async_(async) {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  DCHECK_GE(platform_file, -1);
#endif
}

File::File(Error error_details) : error_details_(error_details) {}

File::File(File&& other)
    : file_(other.TakePlatformFile()),
      path_(other.path_),
      error_details_(other.error_details()),
      created_(other.created()),
      async_(other.async_) {}

File::~File() {
  // Go through the AssertIOAllowed logic.
  Close();
}

File& File::operator=(File&& other) {
  Close();
  SetPlatformFile(other.TakePlatformFile());
  path_ = other.path_;
  error_details_ = other.error_details();
  created_ = other.created();
  async_ = other.async_;
  return *this;
}

#if !BUILDFLAG(IS_NACL)
void File::Initialize(const FilePath& path, uint32_t flags) {
  if (path.ReferencesParent()) {
#if BUILDFLAG(IS_WIN)
    ::SetLastError(ERROR_ACCESS_DENIED);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
    errno = EACCES;
#else
#error Unsupported platform
#endif
    error_details_ = FILE_ERROR_ACCESS_DENIED;
    return;
  }
  if (FileTracing::IsCategoryEnabled()
#if BUILDFLAG(IS_ANDROID)
      || path.IsContentUri()
#endif
  ) {
    path_ = path;
  }
  SCOPED_FILE_TRACE("Initialize");
  DoInitialize(path, flags);
}
#endif

std::optional<size_t> File::Read(int64_t offset, span<uint8_t> data) {
  span<char> chars = base::as_writable_chars(data);
  int size = checked_cast<int>(chars.size());
  // SAFETY: `chars.size()` describes valid portion of `chars.data()`.
  int result = UNSAFE_BUFFERS(Read(offset, chars.data(), size));
  if (result < 0) {
    return std::nullopt;
  }
  return checked_cast<size_t>(result);
}

bool File::ReadAndCheck(int64_t offset, span<uint8_t> data) {
  // Size checked in span form of Read() above.
  return Read(offset, data) == static_cast<int>(data.size());
}

std::optional<size_t> File::ReadAtCurrentPos(span<uint8_t> data) {
  span<char> chars = base::as_writable_chars(data);
  int size = checked_cast<int>(chars.size());
  // SAFETY: `chars.size()` describes valid portion of `chars.data()`.
  int result = UNSAFE_BUFFERS(ReadAtCurrentPos(chars.data(), size));
  if (result < 0) {
    return std::nullopt;
  }
  return checked_cast<size_t>(result);
}

bool File::ReadAtCurrentPosAndCheck(span<uint8_t> data) {
  // Size checked in span form of ReadAtCurrentPos() above.
  return ReadAtCurrentPos(data) == static_cast<int>(data.size());
}

std::optional<size_t> File::Write(int64_t offset, span<const uint8_t> data) {
  span<const char> chars = base::as_chars(data);
  int size = checked_cast<int>(chars.size());
  // SAFETY: `chars.size()` describes valid portion of `chars.data()`.
  int result = UNSAFE_BUFFERS(Write(offset, chars.data(), size));
  if (result < 0) {
    return std::nullopt;
  }
  return checked_cast<size_t>(result);
}

bool File::WriteAndCheck(int64_t offset, span<const uint8_t> data) {
  // Size checked in span form of Write() above.
  return Write(offset, data) == static_cast<int>(data.size());
}

std::optional<size_t> File::WriteAtCurrentPos(span<const uint8_t> data) {
  span<const char> chars = base::as_chars(data);
  int size = checked_cast<int>(chars.size());
  // SAFETY: `chars.size()` describes valid portion of `chars.data()`.
  int result = UNSAFE_BUFFERS(WriteAtCurrentPos(chars.data(), size));
  if (result < 0) {
    return std::nullopt;
  }
  return checked_cast<size_t>(result);
}

bool File::WriteAtCurrentPosAndCheck(span<const uint8_t> data) {
  // Size checked in span form of WriteAtCurrentPos() above.
  return WriteAtCurrentPos(data) == static_cast<int>(data.size());
}

std::optional<size_t> File::ReadNoBestEffort(int64_t offset,
                                             base::span<uint8_t> data) {
  span<char> chars = base::as_writable_chars(data);
  int size = checked_cast<int>(chars.size());
  // SAFETY: `chars.size()` describes valid portion of `chars.data()`.
  int result = UNSAFE_BUFFERS(ReadNoBestEffort(offset, chars.data(), size));
  if (result < 0) {
    return std::nullopt;
  }
  return checked_cast<size_t>(result);
}

std::optional<size_t> File::ReadAtCurrentPosNoBestEffort(
    base::span<uint8_t> data) {
  span<char> chars = base::as_writable_chars(data);
  int size = checked_cast<int>(chars.size());
  // SAFETY: `chars.size()` describes valid portion of `chars.data()`.
  int result = UNSAFE_BUFFERS(ReadAtCurrentPosNoBestEffort(chars.data(), size));
  if (result < 0) {
    return std::nullopt;
  }
  return checked_cast<size_t>(result);
}

std::optional<size_t> File::WriteAtCurrentPosNoBestEffort(
    base::span<const uint8_t> data) {
  span<const char> chars = base::as_chars(data);
  int size = checked_cast<int>(chars.size());
  // SAFETY: `chars.size()` describes valid portion of `chars.data()`.
  int result =
      UNSAFE_BUFFERS(WriteAtCurrentPosNoBestEffort(chars.data(), size));
  if (result < 0) {
    return std::nullopt;
  }
  return checked_cast<size_t>(result);
}

// static
std::string File::ErrorToString(Error error) {
  switch (error) {
    case FILE_OK:
      return "FILE_OK";
    case FILE_ERROR_FAILED:
      return "FILE_ERROR_FAILED";
    case FILE_ERROR_IN_USE:
      return "FILE_ERROR_IN_USE";
    case FILE_ERROR_EXISTS:
      return "FILE_ERROR_EXISTS";
    case FILE_ERROR_NOT_FOUND:
      return "FILE_ERROR_NOT_FOUND";
    case FILE_ERROR_ACCESS_DENIED:
      return "FILE_ERROR_ACCESS_DENIED";
    case FILE_ERROR_TOO_MANY_OPENED:
      return "FILE_ERROR_TOO_MANY_OPENED";
    case FILE_ERROR_NO_MEMORY:
      return "FILE_ERROR_NO_MEMORY";
    case FILE_ERROR_NO_SPACE:
      return "FILE_ERROR_NO_SPACE";
    case FILE_ERROR_NOT_A_DIRECTORY:
      return "FILE_ERROR_NOT_A_DIRECTORY";
    case FILE_ERROR_INVALID_OPERATION:
      return "FILE_ERROR_INVALID_OPERATION";
    case FILE_ERROR_SECURITY:
      return "FILE_ERROR_SECURITY";
    case FILE_ERROR_ABORT:
      return "FILE_ERROR_ABORT";
    case FILE_ERROR_NOT_A_FILE:
      return "FILE_ERROR_NOT_A_FILE";
    case FILE_ERROR_NOT_EMPTY:
      return "FILE_ERROR_NOT_EMPTY";
    case FILE_ERROR_INVALID_URL:
      return "FILE_ERROR_INVALID_URL";
    case FILE_ERROR_IO:
      return "FILE_ERROR_IO";
    case FILE_ERROR_MAX:
      break;
  }

  NOTREACHED();
}

void File::WriteIntoTrace(perfetto::TracedValue context) const {
  auto dict = std::move(context).WriteDictionary();
  dict.Add("is_valid", IsValid());
  dict.Add("created", created_);
  dict.Add("async", async_);
  dict.Add("error_details", ErrorToString(error_details_));
}

}  // namespace base