| // Copyright 2020 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "src/storage/lib/vfs/cpp/connection/stream_file_connection.h" | |
| #include <fidl/fuchsia.io/cpp/natural_types.h> | |
| #include <fidl/fuchsia.io/cpp/wire.h> | |
| #include <lib/fidl/cpp/wire/vector_view.h> | |
| #include <lib/zx/result.h> | |
| #include <lib/zx/stream.h> | |
| #include <stdint.h> | |
| #include <stdlib.h> | |
| #include <zircon/assert.h> | |
| #include <zircon/errors.h> | |
| #include <zircon/status.h> | |
| #include <zircon/types.h> | |
| #include <utility> | |
| #include <fbl/ref_ptr.h> | |
| #include "src/storage/lib/vfs/cpp/connection/file_connection.h" | |
| #include "src/storage/lib/vfs/cpp/debug.h" | |
| #include "src/storage/lib/vfs/cpp/vnode.h" | |
| namespace fio = fuchsia_io; | |
| namespace fs::internal { | |
| StreamFileConnection::StreamFileConnection(fs::FuchsiaVfs* vfs, fbl::RefPtr<fs::Vnode> vnode, | |
| fuchsia_io::Rights rights, bool append, | |
| zx::stream stream, zx_koid_t koid) | |
| : FileConnection(vfs, std::move(vnode), rights, koid), | |
| stream_(std::move(stream)), | |
| append_(append) {} | |
| zx_status_t StreamFileConnection::ReadInternal(void* data, size_t len, size_t* out_actual) { | |
| FS_PRETTY_TRACE_DEBUG("[FileRead] rights: ", rights()); | |
| if (!(rights() & fuchsia_io::Rights::kReadBytes)) { | |
| return ZX_ERR_BAD_HANDLE; | |
| } | |
| if (len > fio::wire::kMaxBuf) { | |
| return ZX_ERR_OUT_OF_RANGE; | |
| } | |
| zx_iovec_t vector = { | |
| .buffer = data, | |
| .capacity = len, | |
| }; | |
| zx_status_t status = stream_.readv(0, &vector, 1, out_actual); | |
| if (status == ZX_OK) { | |
| ZX_DEBUG_ASSERT(*out_actual <= len); | |
| } | |
| return status; | |
| } | |
| void StreamFileConnection::Read(ReadRequestView request, ReadCompleter::Sync& completer) { | |
| uint8_t data[fio::wire::kMaxBuf]; | |
| size_t actual = 0; | |
| zx_status_t status = ReadInternal(data, request->count, &actual); | |
| if (status != ZX_OK) { | |
| completer.ReplyError(status); | |
| } else { | |
| completer.ReplySuccess(fidl::VectorView<uint8_t>::FromExternal(data, actual)); | |
| } | |
| } | |
| zx_status_t StreamFileConnection::ReadAtInternal(void* data, size_t len, size_t offset, | |
| size_t* out_actual) { | |
| FS_PRETTY_TRACE_DEBUG("[FileReadAt] rights: ", rights()); | |
| if (!(rights() & fuchsia_io::Rights::kReadBytes)) { | |
| return ZX_ERR_BAD_HANDLE; | |
| } | |
| if (len > fio::wire::kMaxBuf) { | |
| return ZX_ERR_OUT_OF_RANGE; | |
| } | |
| zx_iovec_t vector = { | |
| .buffer = data, | |
| .capacity = len, | |
| }; | |
| zx_status_t status = stream_.readv_at(0, offset, &vector, 1, out_actual); | |
| if (status == ZX_OK) { | |
| ZX_DEBUG_ASSERT(*out_actual <= len); | |
| } | |
| return status; | |
| } | |
| void StreamFileConnection::ReadAt(ReadAtRequestView request, ReadAtCompleter::Sync& completer) { | |
| uint8_t data[fio::wire::kMaxBuf]; | |
| size_t actual = 0; | |
| zx_status_t status = ReadAtInternal(data, request->count, request->offset, &actual); | |
| if (status != ZX_OK) { | |
| completer.ReplyError(status); | |
| } else { | |
| completer.ReplySuccess(fidl::VectorView<uint8_t>::FromExternal(data, actual)); | |
| } | |
| } | |
| zx_status_t StreamFileConnection::WriteInternal(const void* data, size_t len, size_t* out_actual) { | |
| FS_PRETTY_TRACE_DEBUG("[FileWrite] rights: ", rights()); | |
| if (!(rights() & fuchsia_io::Rights::kWriteBytes)) { | |
| return ZX_ERR_BAD_HANDLE; | |
| } | |
| zx_iovec_t vector = { | |
| .buffer = const_cast<void*>(data), | |
| .capacity = len, | |
| }; | |
| zx_status_t status = stream_.writev(0, &vector, 1, out_actual); | |
| if (status == ZX_OK) { | |
| ZX_DEBUG_ASSERT(*out_actual <= len); | |
| } | |
| return status; | |
| } | |
| void StreamFileConnection::Write(WriteRequestView request, WriteCompleter::Sync& completer) { | |
| size_t actual = 0u; | |
| zx_status_t status = WriteInternal(request->data.data(), request->data.size(), &actual); | |
| if (status != ZX_OK) { | |
| completer.ReplyError(status); | |
| } else { | |
| completer.ReplySuccess(actual); | |
| } | |
| } | |
| zx_status_t StreamFileConnection::WriteAtInternal(const void* data, size_t len, size_t offset, | |
| size_t* out_actual) { | |
| FS_PRETTY_TRACE_DEBUG("[FileWriteAt] rights: ", rights()); | |
| if (!(rights() & fuchsia_io::Rights::kWriteBytes)) { | |
| return ZX_ERR_BAD_HANDLE; | |
| } | |
| zx_iovec_t vector = { | |
| .buffer = const_cast<void*>(data), | |
| .capacity = len, | |
| }; | |
| zx_status_t status = stream_.writev_at(0, offset, &vector, 1, out_actual); | |
| if (status == ZX_OK) { | |
| ZX_DEBUG_ASSERT(*out_actual <= len); | |
| } | |
| return status; | |
| } | |
| void StreamFileConnection::WriteAt(WriteAtRequestView request, WriteAtCompleter::Sync& completer) { | |
| size_t actual = 0; | |
| zx_status_t status = | |
| WriteAtInternal(request->data.data(), request->data.size(), request->offset, &actual); | |
| if (status != ZX_OK) { | |
| completer.ReplyError(status); | |
| } else { | |
| completer.ReplySuccess(actual); | |
| } | |
| } | |
| void StreamFileConnection::Seek(SeekRequestView request, SeekCompleter::Sync& completer) { | |
| FS_PRETTY_TRACE_DEBUG("[FileSeek] rights: ", rights()); | |
| zx_off_t seek = 0u; | |
| zx_status_t status = | |
| stream_.seek(static_cast<zx_stream_seek_origin_t>(request->origin), request->offset, &seek); | |
| if (status != ZX_OK) { | |
| completer.ReplyError(status); | |
| } else { | |
| completer.ReplySuccess(seek); | |
| } | |
| } | |
| bool StreamFileConnection::GetAppend() const { | |
| if constexpr (ZX_DEBUG_ASSERT_IMPLEMENTED) { | |
| // Validate that the connection's append mode and the stream's append mode match. | |
| uint8_t mode_append; | |
| if (zx_status_t status = stream_.get_prop_mode_append(&mode_append); status != ZX_OK) { | |
| ZX_PANIC("failed to query stream property for append mode: %s", zx_status_get_string(status)); | |
| } | |
| bool stream_append = static_cast<bool>(mode_append); | |
| ZX_ASSERT_MSG(stream_append == append_, "stream append: %d flags append: %d", stream_append, | |
| append_); | |
| } | |
| return append_; | |
| } | |
| zx::result<> StreamFileConnection::SetAppend(bool append) { | |
| if (append != append_) { | |
| if (zx_status_t status = stream_.set_prop_mode_append(append); status != ZX_OK) { | |
| return zx::error(status); | |
| } | |
| append_ = append; | |
| } | |
| return zx::ok(); | |
| } | |
| } // namespace fs::internal |