From 94f2fca814dbe316e9ce735ef5aaf356103cf12a Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 11 Sep 2018 17:13:53 +0300 Subject: [PATCH] Unify FileFd::read implementation. GitOrigin-RevId: bfe86b448459ba7ef875256d99796122bb486e4d --- tdutils/td/utils/port/FileFd.cpp | 121 ++++++++----------------------- 1 file changed, 31 insertions(+), 90 deletions(-) diff --git a/tdutils/td/utils/port/FileFd.cpp b/tdutils/td/utils/port/FileFd.cpp index d44dac8a9..0f0e5332f 100644 --- a/tdutils/td/utils/port/FileFd.cpp +++ b/tdutils/td/utils/port/FileFd.cpp @@ -201,144 +201,85 @@ FileFd FileFd::from_native_fd(NativeFd native_fd) { } Result FileFd::write(Slice slice) { + auto native_fd = get_native_fd().fd(); #if TD_PORT_POSIX - auto native_fd = get_native_fd().fd(); - auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); - if (write_res >= 0) { - return narrow_cast(write_res); - } - - auto write_errno = errno; - auto error = Status::PosixError(write_errno, PSLICE() << "Write to [fd = " << native_fd << "] has failed"); - if (write_errno != EAGAIN -#if EAGAIN != EWOULDBLOCK - && write_errno != EWOULDBLOCK -#endif - && write_errno != EIO) { - LOG(ERROR) << error; - } - return std::move(error); + auto bytes_written = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); + bool success = bytes_written >= 0; #elif TD_PORT_WINDOWS - auto native_fd = get_native_fd().fd(); DWORD bytes_written = 0; - auto res = WriteFile(native_fd, slice.data(), narrow_cast(slice.size()), &bytes_written, nullptr); - if (res) { - return bytes_written; + BOOL success = WriteFile(native_fd, slice.data(), narrow_cast(slice.size()), &bytes_written, nullptr); +#endif + if (success) { + return narrow_cast(bytes_written); } return OS_ERROR(PSLICE() << "Write to [fd = " << native_fd << "] has failed"); -#endif } Result FileFd::read(MutableSlice slice) { + auto native_fd = get_native_fd().fd(); #if TD_PORT_POSIX - auto native_fd = get_native_fd().fd(); - auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); - auto read_errno = errno; - - if (read_res >= 0) { - if (narrow_cast(read_res) < slice.size()) { - get_poll_info().clear_flags(PollFlags::Read()); - } - return static_cast(read_res); - } - - auto error = Status::PosixError(read_errno, PSLICE() << "Read from [fd = " << native_fd << "] has failed"); - if (read_errno != EAGAIN -#if EAGAIN != EWOULDBLOCK - && read_errno != EWOULDBLOCK -#endif - && read_errno != EIO) { - LOG(ERROR) << error; - } - return std::move(error); + auto bytes_read = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); + bool success = bytes_read >= 0; + bool is_eof = narrow_cast(bytes_read) < slice.size(); #elif TD_PORT_WINDOWS - auto native_fd = get_native_fd().fd(); DWORD bytes_read = 0; - auto res = ReadFile(native_fd, slice.data(), narrow_cast(slice.size()), &bytes_read, nullptr); - if (res) { - if (bytes_read == 0) { + BOOL success = ReadFile(native_fd, slice.data(), narrow_cast(slice.size()), &bytes_read, nullptr); + bool is_eof = bytes_read == 0; +#endif + if (success) { + if (is_eof) { get_poll_info().clear_flags(PollFlags::Read()); } return static_cast(bytes_read); } return OS_ERROR(PSLICE() << "Read from [fd = " << native_fd << "] has failed"); -#endif } Result FileFd::pwrite(Slice slice, int64 offset) { if (offset < 0) { return Status::Error("Offset must be non-negative"); } + auto native_fd = get_native_fd().fd(); #if TD_PORT_POSIX - auto native_fd = get_native_fd().fd(); TRY_RESULT(offset_off_t, narrow_cast_safe(offset)); - auto pwrite_res = detail::skip_eintr([&] { return ::pwrite(native_fd, slice.begin(), slice.size(), offset_off_t); }); - if (pwrite_res >= 0) { - return narrow_cast(pwrite_res); - } - - auto pwrite_errno = errno; - auto error = Status::PosixError( - pwrite_errno, PSLICE() << "Pwrite to [fd = " << native_fd << "] at [offset = " << offset << "] has failed"); - if (pwrite_errno != EAGAIN -#if EAGAIN != EWOULDBLOCK - && pwrite_errno != EWOULDBLOCK -#endif - && pwrite_errno != EIO) { - LOG(ERROR) << error; - } - return std::move(error); + auto bytes_written = + detail::skip_eintr([&] { return ::pwrite(native_fd, slice.begin(), slice.size(), offset_off_t); }); + bool success = bytes_written >= 0; #elif TD_PORT_WINDOWS - auto native_fd = get_native_fd().fd(); DWORD bytes_written = 0; OVERLAPPED overlapped; std::memset(&overlapped, 0, sizeof(overlapped)); overlapped.Offset = static_cast(offset); overlapped.OffsetHigh = static_cast(offset >> 32); - auto res = WriteFile(native_fd, slice.data(), narrow_cast(slice.size()), &bytes_written, &overlapped); - if (res) { - return bytes_written; + BOOL success = WriteFile(native_fd, slice.data(), narrow_cast(slice.size()), &bytes_written, &overlapped); +#endif + if (success) { + return narrow_cast(bytes_written); } return OS_ERROR(PSLICE() << "Pwrite to [fd = " << native_fd << "] at [offset = " << offset << "] has failed"); -#endif } Result FileFd::pread(MutableSlice slice, int64 offset) { if (offset < 0) { return Status::Error("Offset must be non-negative"); } + auto native_fd = get_native_fd().fd(); #if TD_PORT_POSIX - auto native_fd = get_native_fd().fd(); TRY_RESULT(offset_off_t, narrow_cast_safe(offset)); - auto pread_res = detail::skip_eintr([&] { return ::pread(native_fd, slice.begin(), slice.size(), offset_off_t); }); - if (pread_res >= 0) { - return narrow_cast(pread_res); - } - - auto pread_errno = errno; - auto error = Status::PosixError( - pread_errno, PSLICE() << "Pread from [fd = " << native_fd << "] at [offset = " << offset << "] has failed"); - if (pread_errno != EAGAIN -#if EAGAIN != EWOULDBLOCK - && pread_errno != EWOULDBLOCK -#endif - && pread_errno != EIO) { - LOG(ERROR) << error; - } - return std::move(error); + auto bytes_read = detail::skip_eintr([&] { return ::pread(native_fd, slice.begin(), slice.size(), offset_off_t); }); + bool success = bytes_read >= 0; #elif TD_PORT_WINDOWS - auto native_fd = get_native_fd().fd(); DWORD bytes_read = 0; OVERLAPPED overlapped; std::memset(&overlapped, 0, sizeof(overlapped)); overlapped.Offset = static_cast(offset); overlapped.OffsetHigh = static_cast(offset >> 32); - auto res = ReadFile(native_fd, slice.data(), narrow_cast(slice.size()), &bytes_read, &overlapped); - if (res) { - return bytes_read; + BOOL success = ReadFile(native_fd, slice.data(), narrow_cast(slice.size()), &bytes_read, &overlapped); +#endif + if (success) { + return narrow_cast(bytes_read); } return OS_ERROR(PSLICE() << "Pread from [fd = " << native_fd << "] at [offset = " << offset << "] has failed"); -#endif } Status FileFd::lock(FileFd::LockFlags flags, int32 max_tries) {