Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,12 @@ ssize_t FileInstance::Read(void* buf, size_t count) {
return TEMP_FAILURE_RETRY(read(fd_, buf, count));
}

ssize_t FileInstance::PRead(void* buf, size_t count, size_t offset) {
LocalErrno record_errno(errno_);

return TEMP_FAILURE_RETRY(pread(fd_, buf, count, offset));
}

#ifdef __linux__
int FileInstance::EventfdRead(eventfd_t* value) {
LocalErrno record_errno(errno_);
Expand Down Expand Up @@ -1028,6 +1034,12 @@ ssize_t FileInstance::Write(const void* buf, size_t count) {
return TEMP_FAILURE_RETRY(write(fd_, buf, count));
}

ssize_t FileInstance::PWrite(const void* buf, size_t count, size_t offset) {
LocalErrno record_errno(errno_);

return TEMP_FAILURE_RETRY(pwrite(fd_, buf, count, offset));
}

#ifdef __linux__
int FileInstance::EventfdWrite(eventfd_t value) {
LocalErrno record_errno(errno_);
Expand Down
2 changes: 2 additions & 0 deletions base/cvd/cuttlefish/common/libs/fs/shared_fd.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ class FileInstance {
ssize_t Recv(void* buf, size_t len, int flags);
ssize_t RecvMsg(struct msghdr* msg, int flags);
ssize_t Read(void* buf, size_t count);
ssize_t PRead(void* buf, size_t count, size_t offset);
#ifdef __linux__
int EventfdRead(eventfd_t* value);
#endif
Expand Down Expand Up @@ -391,6 +392,7 @@ class FileInstance {
*
*/
ssize_t Write(const void* buf, size_t count);
ssize_t PWrite(const void* buf, size_t count, size_t offset);
#ifdef __linux__
int EventfdWrite(eventfd_t value);
#endif
Expand Down
25 changes: 25 additions & 0 deletions base/cvd/cuttlefish/io/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("//cuttlefish/bazel:rules.bzl", "cf_cc_library")

package(
default_visibility = ["//:android_cuttlefish"],
)

cf_cc_library(
name = "io",
hdrs = ["io.h"],
deps = [
"//cuttlefish/result:result_type",
],
)

cf_cc_library(
name = "shared_fd",
srcs = ["shared_fd.cc"],
hdrs = ["shared_fd.h"],
deps = [
"//cuttlefish/common/libs/fs",
"//cuttlefish/io",
"//cuttlefish/result:expect",
"//cuttlefish/result:result_type",
],
)
58 changes: 58 additions & 0 deletions base/cvd/cuttlefish/io/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Copyright (C) 2026 The Android Open Source Project
//
// 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.

#pragma once

#include <stdint.h>

#include "cuttlefish/result/result_type.h"

namespace cuttlefish {

class Reader {
public:
virtual Result<size_t> Read(void* buf, size_t count) = 0;
};

class Writer {
public:
virtual Result<size_t> Write(const void* buf, size_t count) = 0;
};

class Seeker {
public:
virtual Result<size_t> SeekSet(size_t offset) = 0;
virtual Result<size_t> SeekCur(ssize_t offset) = 0;
virtual Result<size_t> SeekEnd(ssize_t offset) = 0;
};

class ReaderSeeker : virtual public Reader, virtual public Seeker {
public:
virtual Result<size_t> PRead(void* buf, size_t count,
size_t offset) const = 0;
};

class SeekerWriter : virtual public Seeker, virtual public Writer {
public:
virtual Result<size_t> PWrite(const void* buf, size_t count,
size_t offset) const = 0;
};

class ReaderWriter : virtual public Reader, virtual public Writer {};

class ReaderSeekerWriter : virtual public ReaderSeeker,
virtual public SeekerWriter {};

} // namespace cuttlefish
73 changes: 73 additions & 0 deletions base/cvd/cuttlefish/io/shared_fd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// Copyright (C) 2026 The Android Open Source Project
//
// 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 "cuttlefish/io/shared_fd.h"

#include <stdint.h>
#include <unistd.h>

#include <utility>

#include "cuttlefish/common/libs/fs/shared_fd.h"
#include "cuttlefish/result/expect.h"
#include "cuttlefish/result/result_type.h"

namespace cuttlefish {

SharedFdIo::SharedFdIo(SharedFD fd) : fd_(std::move(fd)) {}

Result<size_t> SharedFdIo::Read(void* buf, size_t count) {
ssize_t data_read = fd_->Read(buf, count);
CF_EXPECT_GE(data_read, 0, fd_->StrError());
return data_read;
}

Result<size_t> SharedFdIo::Write(const void* buf, size_t count) {
ssize_t data_written = fd_->Write(buf, count);
CF_EXPECT_GE(data_written, 0, fd_->StrError());
return data_written;
}

Result<size_t> SharedFdIo::SeekSet(size_t offset) {
CF_EXPECT_EQ(fd_->LSeek(offset, SEEK_SET), offset, fd_->StrError());
return offset;
}

Result<size_t> SharedFdIo::SeekCur(ssize_t offset) {
ssize_t new_offset = fd_->LSeek(offset, SEEK_CUR);
CF_EXPECT_GE(new_offset, 0, fd_->StrError());
return new_offset;
}

Result<size_t> SharedFdIo::SeekEnd(ssize_t offset) {
ssize_t new_offset = fd_->LSeek(offset, SEEK_END);
CF_EXPECT_GE(new_offset, 0, fd_->StrError());
return new_offset;
}

Result<size_t> SharedFdIo::PRead(void* buf, size_t count, size_t offset) const {
ssize_t data_read = fd_->PRead(buf, count, offset);
CF_EXPECT_GE(data_read, 0, fd_->StrError());
return data_read;
}

Result<size_t> SharedFdIo::PWrite(const void* buf, size_t count,
size_t offset) const {
ssize_t data_written = fd_->PWrite(buf, count, offset);
CF_EXPECT_GE(data_written, 0, fd_->StrError());
return data_written;
}

} // namespace cuttlefish
43 changes: 43 additions & 0 deletions base/cvd/cuttlefish/io/shared_fd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Copyright (C) 2026 The Android Open Source Project
//
// 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.

#pragma once

#include <stdint.h>

#include "cuttlefish/common/libs/fs/shared_fd.h"
#include "cuttlefish/io/io.h"
#include "cuttlefish/result/result_type.h"

namespace cuttlefish {

class SharedFdIo : public ReaderSeekerWriter {
public:
explicit SharedFdIo(SharedFD);

Result<size_t> Read(void* buf, size_t count) override;
Result<size_t> Write(const void* buf, size_t count) override;
Result<size_t> SeekSet(size_t offset) override;
Result<size_t> SeekCur(ssize_t offset) override;
Result<size_t> SeekEnd(ssize_t offset) override;
Result<size_t> PRead(void* buf, size_t count, size_t offset) const override;
Result<size_t> PWrite(const void* buf, size_t count,
size_t offset) const override;

private:
SharedFD fd_;
};

} // namespace cuttlefish
Loading