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
|
// Copyright 2024 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/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/ash/file_system_provider/content_cache/local_fd.h"
#include "base/files/file_error_or.h"
#include "base/logging.h"
namespace ash::file_system_provider {
namespace {
base::FileErrorOr<std::unique_ptr<base::File>> WriteBytesBlocking(
std::unique_ptr<base::File> file,
const base::FilePath& path,
scoped_refptr<net::IOBuffer> buffer,
int64_t offset,
int length) {
VLOG(2) << "WriteBytesBlocking: {path = '" << path.value() << "', offset = '"
<< offset << "', length = '" << length << "'}";
if (!file) {
file = std::make_unique<base::File>(
path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE);
}
if (file->Write(offset, buffer->data(), length) != length) {
PLOG(ERROR) << "Failed to write bytes to file";
return base::unexpected(base::File::FILE_ERROR_FAILED);
}
return file;
}
FileErrorOrFileAndBytesRead ReadBytesBlocking(
std::unique_ptr<base::File> file,
const base::FilePath& path,
scoped_refptr<net::IOBuffer> buffer,
int64_t offset,
int length) {
if (!file) {
file = std::make_unique<base::File>(
path, base::File::FLAG_OPEN | base::File::FLAG_READ);
}
int bytes_read = file->Read(offset, buffer->data(), length);
if (bytes_read < 0) {
PLOG(ERROR) << "Failed to read bytes from file";
return base::unexpected(base::File::FILE_ERROR_FAILED);
}
VLOG(2) << "ReadBytesBlocking: {bytes_read = '" << bytes_read
<< "', file.GetLength = '" << file->GetLength() << "', offset = '"
<< offset << "', length = '" << length << "'}";
return std::make_pair(std::move(file), bytes_read);
}
} // namespace
LocalFD::LocalFD(
const base::FilePath& file_path,
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
: file_path_(file_path), blocking_task_runner_(blocking_task_runner) {
VLOG(2) << "LocalFD instance created {file_path = '" << file_path_ << "'}";
}
LocalFD::~LocalFD() {
DCHECK(!in_progress_operation_)
<< "Operation still pending when FD destroyed";
VLOG(2) << "LocalFD instance destroyed {file_path = '" << file_path_
<< "'}";
if (file_) {
CloseFile();
}
}
void LocalFD::WriteBytes(
scoped_refptr<net::IOBuffer> buffer,
int64_t offset,
int length,
base::OnceCallback<void(base::File::Error)> callback) {
if (in_progress_operation_) {
std::move(callback).Run(base::File::FILE_ERROR_INVALID_OPERATION);
return;
}
in_progress_operation_ = true;
if (read_only_) {
// In the event we've only ever read from this file but we're now writing to
// the file, let's close the existing FD and instead start a new one that
// opens for write.
CloseFile();
}
read_only_ = false;
blocking_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&WriteBytesBlocking, std::move(file_), file_path_, buffer,
offset, length),
base::BindOnce(&LocalFD::OnBytesWritten, weak_ptr_factory_.GetWeakPtr(),
std::move(callback)));
}
void LocalFD::OnBytesWritten(
base::OnceCallback<void(base::File::Error)> callback,
FileErrorOrFile error_or_file) {
base::File::Error result = error_or_file.error_or(base::File::FILE_OK);
if (result == base::File::FILE_OK) {
CloseOrCacheFile(std::move(error_or_file.value()));
}
std::move(callback).Run(result);
}
void LocalFD::ReadBytes(scoped_refptr<net::IOBuffer> buffer,
int64_t offset,
int length,
FileErrorOrBytesReadCallback callback) {
if (in_progress_operation_) {
std::move(callback).Run(base::unexpected(base::File::FILE_ERROR_IN_USE));
return;
}
in_progress_operation_ = true;
blocking_task_runner_->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&ReadBytesBlocking, std::move(file_), file_path_, buffer,
offset, length),
base::BindOnce(&LocalFD::OnBytesRead, weak_ptr_factory_.GetWeakPtr(),
std::move(callback)));
}
void LocalFD::OnBytesRead(
FileErrorOrBytesReadCallback callback,
FileErrorOrFileAndBytesRead error_or_file_and_bytes_read) {
base::File::Error result =
error_or_file_and_bytes_read.error_or(base::File::FILE_OK);
if (result != base::File::FILE_OK) {
std::move(callback).Run(base::unexpected(result));
return;
}
auto [file, bytes_read] = std::move(error_or_file_and_bytes_read.value());
CloseOrCacheFile(std::move(file));
std::move(callback).Run(bytes_read);
}
void LocalFD::CloseOrCacheFile(std::unique_ptr<base::File> file) {
in_progress_operation_ = false;
if (!close_closure_.is_null()) {
VLOG(2) << "File closed whilst reading/writing, closing FD";
CloseFile();
} else {
VLOG(2) << "Caching FD for next operation";
file_ = std::move(file);
}
}
void LocalFD::Close(base::OnceClosure close_closure) {
DCHECK(close_closure_.is_null());
close_closure_ = std::move(close_closure);
if (in_progress_operation_) {
VLOG(2) << "FD is currently open, scheduling close";
return;
}
VLOG(2) << "Directly closing FD";
CloseFile();
}
void LocalFD::CloseFile() {
if (close_closure_) {
std::move(close_closure_).Run();
}
blocking_task_runner_->DeleteSoon(FROM_HERE, std::move(file_));
}
} // namespace ash::file_system_provider
|