diff options
author | 2021-08-22 09:06:15 +1000 | |
---|---|---|
committer | 2021-08-22 09:06:15 +1000 | |
commit | e8bbea1fc6e94e14768509d322f6c624403ffb36 (patch) | |
tree | 74dd1606a900d83de8aecff17a6737af4113308d /src/osd/modules/file/posixptty.cpp | |
parent | e319bde5fc3696d7f48f62b15b6366c4377fe5d1 (diff) |
formats, osd, util: Started refactoring file I/O stuff. (#8456)
Added more modern generic I/O interfaces with implementation backed by stdio, osd_file and core_file, replacing io_generic. Also replaced core_file's build-in zlib compression with a filter.
unzip.cpp, un7z.cpp: Added option to supply abstract I/O interface rather than filename.
Converted osd_file, core_file, archive_file, chd_file and device_image_interface to use std::error_condition rather than their own error enums.
Allow mounting TI-99 RPK from inside archives.
Diffstat (limited to 'src/osd/modules/file/posixptty.cpp')
-rw-r--r-- | src/osd/modules/file/posixptty.cpp | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/src/osd/modules/file/posixptty.cpp b/src/osd/modules/file/posixptty.cpp index 5ecb37057ad..57ff069a2ce 100644 --- a/src/osd/modules/file/posixptty.cpp +++ b/src/osd/modules/file/posixptty.cpp @@ -52,7 +52,7 @@ public: posix_osd_ptty& operator=(posix_osd_ptty const &) = delete; posix_osd_ptty& operator=(posix_osd_ptty &&) = delete; - posix_osd_ptty(int fd) : m_fd(fd) + posix_osd_ptty(int fd) noexcept : m_fd(fd) { assert(m_fd >= 0); } @@ -62,36 +62,36 @@ public: ::close(m_fd); } - virtual error read(void *buffer, std::uint64_t offset, std::uint32_t count, std::uint32_t &actual) override + virtual std::error_condition read(void *buffer, std::uint64_t offset, std::uint32_t count, std::uint32_t &actual) noexcept override { ssize_t const result = ::read(m_fd, buffer, count); if (result < 0) - return errno_to_file_error(errno); + return std::error_condition(errno, std::generic_category()); actual = std::uint32_t(size_t(result)); - return error::NONE; + return std::error_condition(); } - virtual error write(void const *buffer, std::uint64_t offset, std::uint32_t count, std::uint32_t &actual) override + virtual std::error_condition write(void const *buffer, std::uint64_t offset, std::uint32_t count, std::uint32_t &actual) noexcept override { ssize_t const result = ::write(m_fd, buffer, count); if (result < 0) - return errno_to_file_error(errno); + return std::error_condition(errno, std::generic_category()); actual = std::uint32_t(size_t(result)); - return error::NONE; + return std::error_condition(); } - virtual error truncate(std::uint64_t offset) override + virtual std::error_condition truncate(std::uint64_t offset) noexcept override { // doesn't make sense on ptty - return error::INVALID_ACCESS; + return std::errc::bad_file_descriptor; } - virtual error flush() override + virtual std::error_condition flush() noexcept override { // no userspace buffers on read/write - return error::NONE; + return std::error_condition(); } private: @@ -101,17 +101,18 @@ private: } // anonymous namespace -bool posix_check_ptty_path(std::string const &path) +bool posix_check_ptty_path(std::string const &path) noexcept { return strncmp(path.c_str(), posix_ptty_identifier, strlen(posix_ptty_identifier)) == 0; } -osd_file::error posix_open_ptty(std::uint32_t openflags, osd_file::ptr &file, std::uint64_t &filesize, std::string &name) +std::error_condition posix_open_ptty(std::uint32_t openflags, osd_file::ptr &file, std::uint64_t &filesize, std::string &name) noexcept { #if defined(__ANDROID__) - return osd_file::error::FAILURE; + return std::errc::not_supported; // TODO: revisit this error code #else // defined(__ANDROID__) + // TODO: handling of the slave path is insecure - should use ptsname_r/ttyname_r in a loop #if (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__)) int access = O_NOCTTY; if (openflags & OPEN_FLAG_WRITE) @@ -119,11 +120,11 @@ osd_file::error posix_open_ptty(std::uint32_t openflags, osd_file::ptr &file, st else if (openflags & OPEN_FLAG_READ) access |= O_RDONLY; else - return osd_file::error::INVALID_ACCESS; + return std::errc::invalid_argument; int const masterfd = ::posix_openpt(access); if (masterfd < 0) - return errno_to_file_error(errno); + return std::error_condition(errno, std::generic_category()); // grant access to slave device and check that it can be opened char const *slavepath; @@ -133,9 +134,9 @@ osd_file::error posix_open_ptty(std::uint32_t openflags, osd_file::ptr &file, st ((slavepath = ::ptsname(masterfd)) == nullptr) || ((slavefd = ::open(slavepath, O_RDWR | O_NOCTTY)) < 0)) { - int const err = errno; + std::error_condition err(errno, std::generic_category()); ::close(masterfd); - return errno_to_file_error(err); + return err; } // check that it's possible to stack BSD-compatibility STREAMS modules @@ -143,20 +144,20 @@ osd_file::error posix_open_ptty(std::uint32_t openflags, osd_file::ptr &file, st (::ioctl(slavefd, I_PUSH, "ldterm") < 0) || (::ioctl(slavefd, I_PUSH, "ttcompat") < 0)) { - int const err = errno; + std::error_condition err(errno, std::generic_category()); ::close(slavefd); ::close(masterfd); - return errno_to_file_error(err); + return err; } #else struct termios tios; std::memset(&tios, 0, sizeof(tios)); - ::cfmakeraw(&tios); + ::cfmakeraw(&tios); // TODO: this is a non-standard BSDism - should set flags some other way int masterfd = -1, slavefd = -1; char slavepath[PATH_MAX]; if (::openpty(&masterfd, &slavefd, slavepath, &tios, nullptr) < 0) - return errno_to_file_error(errno); + return std::error_condition(errno, std::generic_category()); #endif ::close(slavefd); @@ -164,16 +165,16 @@ osd_file::error posix_open_ptty(std::uint32_t openflags, osd_file::ptr &file, st int const oldflags = ::fcntl(masterfd, F_GETFL, 0); if (oldflags < 0) { - int const err = errno; + std::error_condition err(errno, std::generic_category()); ::close(masterfd); - return errno_to_file_error(err); + return err; } if (::fcntl(masterfd, F_SETFL, oldflags | O_NONBLOCK) < 0) { - int const err = errno; + std::error_condition err(errno, std::generic_category()); ::close(masterfd); - return errno_to_file_error(err); + return err; } try @@ -181,12 +182,12 @@ osd_file::error posix_open_ptty(std::uint32_t openflags, osd_file::ptr &file, st name = slavepath; file = std::make_unique<posix_osd_ptty>(masterfd); filesize = 0; - return osd_file::error::NONE; + return std::error_condition(); } catch (...) { ::close(masterfd); - return osd_file::error::OUT_OF_MEMORY; + return std::errc::not_enough_memory; } #endif // defined(__ANDROID__) } |