diff --git a/base/cvd/cuttlefish/common/libs/fs/file_instance.cc b/base/cvd/cuttlefish/common/libs/fs/file_instance.cc index 010908fb1f3..900e4887638 100644 --- a/base/cvd/cuttlefish/common/libs/fs/file_instance.cc +++ b/base/cvd/cuttlefish/common/libs/fs/file_instance.cc @@ -77,6 +77,22 @@ constexpr size_t kPreferredBufferSize = 8192; } // namespace +FileInstance::FileInstance() : FileInstance(-1, 0) {} + +FileInstance::FileInstance(FileInstance&& other) : FileInstance() { + std::swap(fd_, other.fd_); + std::swap(errno_, other.errno_); +} + +FileInstance::~FileInstance() { Close(); } + +FileInstance& FileInstance::operator=(FileInstance&& other) { + Close(); + std::swap(fd_, other.fd_); + std::swap(errno_, other.errno_); + return *this; +} + bool FileInstance::CopyFrom(FileInstance& in, size_t length, FileInstance* stop) { LocalErrno record_errno(errno_); @@ -282,10 +298,6 @@ void FileInstance::Set(fd_set* dest, int* max_index) const { FD_SET(fd_, dest); } -/* static */ std::unique_ptr FileInstance::ClosedInstance() { - return std::unique_ptr(new FileInstance(-1, EBADF)); -} - int FileInstance::Bind(const struct sockaddr* addr, socklen_t addrlen) { LocalErrno record_errno(errno_); diff --git a/base/cvd/cuttlefish/common/libs/fs/file_instance.h b/base/cvd/cuttlefish/common/libs/fs/file_instance.h index 067f54e8e2f..8d2df9e57c5 100644 --- a/base/cvd/cuttlefish/common/libs/fs/file_instance.h +++ b/base/cvd/cuttlefish/common/libs/fs/file_instance.h @@ -78,12 +78,10 @@ namespace cuttlefish { * number. * * FileInstances have two states: Open and Closed. They may start in either - * state. However, once a FileIntance enters the Closed state it cannot be - * reopened. + * state. * * Construction of FileInstances is limited to select classes to avoid - * escaping file descriptors. At this point SharedFD is the only class - * that has access. We may eventually have ScopedFD and WeakFD. + * escaping file descriptors. */ class FileInstance : public ReaderSeeker { // Give SharedFD access to the aliasing constructor. @@ -92,10 +90,12 @@ class FileInstance : public ReaderSeeker { friend class Epoll; public: - virtual ~FileInstance() { Close(); } - - // This can't be a singleton because our shared_ptr's aren't thread safe. - static std::unique_ptr ClosedInstance(); + FileInstance(); + FileInstance(FileInstance&) = delete; + FileInstance(FileInstance&&); + ~FileInstance(); + FileInstance& operator=(FileInstance&) = delete; + FileInstance& operator=(FileInstance&&); int Bind(const struct sockaddr* addr, socklen_t addrlen); int Connect(const struct sockaddr* addr, socklen_t addrlen); diff --git a/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp b/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp index 49aab5885f4..572d03e7fa3 100644 --- a/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp +++ b/base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp @@ -117,6 +117,8 @@ int Select(SharedFDSet* read_set, SharedFDSet* write_set, return rval; } +SharedFD::SharedFD() : value_(std::make_shared()) {} + SharedFD::SharedFD(SharedFD&& other) { value_ = std::move(other.value_); other.value_.reset(new FileInstance(-1, EBADF)); diff --git a/base/cvd/cuttlefish/common/libs/fs/shared_fd.h b/base/cvd/cuttlefish/common/libs/fs/shared_fd.h index 3872aed6954..d37cce9dacf 100644 --- a/base/cvd/cuttlefish/common/libs/fs/shared_fd.h +++ b/base/cvd/cuttlefish/common/libs/fs/shared_fd.h @@ -127,7 +127,7 @@ class SharedFD { friend class WeakFD; public: - inline SharedFD(); + SharedFD(); SharedFD(const std::shared_ptr& in) : value_(in) {} SharedFD(SharedFD const&) = default; SharedFD(SharedFD&& other); @@ -242,11 +242,6 @@ struct PollSharedFd { short revents; }; -/* Methods that need both a fully defined SharedFD and a fully defined - FileInstance. */ - -SharedFD::SharedFD() : value_(FileInstance::ClosedInstance()) {} - } // namespace cuttlefish #endif // CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_ diff --git a/base/cvd/cuttlefish/common/libs/fs/unique_fd.cc b/base/cvd/cuttlefish/common/libs/fs/unique_fd.cc index 32f03a4e9fa..d19b3ead861 100644 --- a/base/cvd/cuttlefish/common/libs/fs/unique_fd.cc +++ b/base/cvd/cuttlefish/common/libs/fs/unique_fd.cc @@ -113,6 +113,8 @@ static void MakeAddress(const char* name, bool abstract, *len = namelen + offsetof(struct sockaddr_un, sun_path) + 1; } +UniqueFd::UniqueFd() : value_(std::make_unique()) {} + UniqueFd UniqueFd::Accept(const FileInstance& listener, struct sockaddr* addr, socklen_t* addrlen) { return UniqueFd( diff --git a/base/cvd/cuttlefish/common/libs/fs/unique_fd.h b/base/cvd/cuttlefish/common/libs/fs/unique_fd.h index be58fb1e675..03c00fd0096 100644 --- a/base/cvd/cuttlefish/common/libs/fs/unique_fd.h +++ b/base/cvd/cuttlefish/common/libs/fs/unique_fd.h @@ -116,7 +116,7 @@ class UniqueFd { friend class SharedFD; public: - inline UniqueFd(); + UniqueFd(); UniqueFd(std::unique_ptr in) : value_(std::move(in)) {} UniqueFd(UniqueFd&& other); UniqueFd& operator=(UniqueFd&& other); @@ -203,8 +203,6 @@ class UniqueFd { std::unique_ptr value_; }; -UniqueFd::UniqueFd() : value_(FileInstance::ClosedInstance()) {} - } // namespace cuttlefish #endif // CUTTLEFISH_COMMON_COMMON_LIBS_FS_UNIQUE_FD_H_