diff --git a/tdutils/td/utils/port/Fd.cpp b/tdutils/td/utils/port/Fd.cpp index 6b04e1b2..9c60c6d9 100644 --- a/tdutils/td/utils/port/Fd.cpp +++ b/tdutils/td/utils/port/Fd.cpp @@ -316,7 +316,7 @@ Status Fd::get_pending_error() { Result Fd::write_unsafe(Slice slice) { int native_fd = get_native_fd(); - auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); + auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); auto write_errno = errno; if (write_res >= 0) { return narrow_cast(write_res); @@ -326,7 +326,7 @@ Result Fd::write_unsafe(Slice slice) { Result Fd::write(Slice slice) { int native_fd = get_native_fd(); - auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); + auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); auto write_errno = errno; if (write_res >= 0) { return narrow_cast(write_res); @@ -372,7 +372,7 @@ Result Fd::read(MutableSlice slice) { } int native_fd = get_native_fd(); CHECK(slice.size() > 0); - auto read_res = skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); + auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); auto read_errno = errno; if (read_res >= 0) { if (read_res == 0) { diff --git a/tdutils/td/utils/port/Fd.h b/tdutils/td/utils/port/Fd.h index 06bc1ee1..5aac934e 100644 --- a/tdutils/td/utils/port/Fd.h +++ b/tdutils/td/utils/port/Fd.h @@ -195,28 +195,6 @@ class Fd { #endif }; -#if TD_PORT_POSIX -template -auto skip_eintr(F &&f) { - decltype(f()) res; - static_assert(std::is_integral::value, "integral type expected"); - do { - errno = 0; // just in case - res = f(); - } while (res < 0 && errno == EINTR); - return res; -} -template -auto skip_eintr_cstr(F &&f) { - char *res; - do { - errno = 0; // just in case - res = f(); - } while (res == nullptr && errno == EINTR); - return res; -} -#endif - template bool can_read(const FdT &fd) { return (fd.get_flags() & (Fd::Read | Fd::Error)) != 0; diff --git a/tdutils/td/utils/port/FileFd.cpp b/tdutils/td/utils/port/FileFd.cpp index 715d9c99..b0c4042e 100644 --- a/tdutils/td/utils/port/FileFd.cpp +++ b/tdutils/td/utils/port/FileFd.cpp @@ -129,7 +129,7 @@ Result FileFd::open(CSlice filepath, int32 flags, int32 mode) { native_flags |= O_APPEND; } - int native_fd = skip_eintr([&] { return ::open(filepath.c_str(), native_flags, static_cast(mode)); }); + int native_fd = detail::skip_eintr([&] { return ::open(filepath.c_str(), native_flags, static_cast(mode)); }); if (native_fd < 0) { return OS_ERROR(PSLICE() << "File \"" << filepath << "\" can't be " << PrintFlags{flags}); } @@ -202,7 +202,7 @@ FileFd FileFd::from_native_fd(NativeFd native_fd) { Result FileFd::write(Slice slice) { #if TD_PORT_POSIX auto native_fd = get_native_fd().fd(); - auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); + auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); if (write_res >= 0) { return narrow_cast(write_res); } @@ -231,7 +231,7 @@ Result FileFd::write(Slice slice) { Result FileFd::read(MutableSlice slice) { #if TD_PORT_POSIX auto native_fd = get_native_fd().fd(); - auto read_res = skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); + auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); auto read_errno = errno; if (read_res >= 0) { @@ -271,7 +271,7 @@ Result FileFd::pwrite(Slice slice, int64 offset) { #if TD_PORT_POSIX auto native_fd = get_native_fd().fd(); TRY_RESULT(offset_off_t, narrow_cast_safe(offset)); - auto pwrite_res = skip_eintr([&] { return ::pwrite(native_fd, slice.begin(), slice.size(), offset_off_t); }); + 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); } @@ -309,7 +309,7 @@ Result FileFd::pread(MutableSlice slice, int64 offset) { #if TD_PORT_POSIX auto native_fd = get_native_fd().fd(); TRY_RESULT(offset_off_t, narrow_cast_safe(offset)); - auto pread_res = skip_eintr([&] { return ::pread(native_fd, slice.begin(), slice.size(), offset_off_t); }); + 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); } @@ -481,7 +481,7 @@ Status FileFd::seek(int64 position) { CHECK(!empty()); #if TD_PORT_POSIX TRY_RESULT(position_off_t, narrow_cast_safe(position)); - if (skip_eintr([&] { return ::lseek(get_native_fd().fd(), position_off_t, SEEK_SET); }) < 0) { + if (detail::skip_eintr([&] { return ::lseek(get_native_fd().fd(), position_off_t, SEEK_SET); }) < 0) { #elif TD_PORT_WINDOWS LARGE_INTEGER offset; offset.QuadPart = position; @@ -496,7 +496,7 @@ Status FileFd::truncate_to_current_position(int64 current_position) { CHECK(!empty()); #if TD_PORT_POSIX TRY_RESULT(current_position_off_t, narrow_cast_safe(current_position)); - if (skip_eintr([&] { return ::ftruncate(get_native_fd().fd(), current_position_off_t); }) < 0) { + if (detail::skip_eintr([&] { return ::ftruncate(get_native_fd().fd(), current_position_off_t); }) < 0) { #elif TD_PORT_WINDOWS if (SetEndOfFile(get_native_fd().io_handle()) == 0) { #endif diff --git a/tdutils/td/utils/port/ServerSocketFd.cpp b/tdutils/td/utils/port/ServerSocketFd.cpp index 4c1cb44c..fd107db0 100644 --- a/tdutils/td/utils/port/ServerSocketFd.cpp +++ b/tdutils/td/utils/port/ServerSocketFd.cpp @@ -215,7 +215,7 @@ class ServerSocketFdImpl { sockaddr_storage addr; socklen_t addr_len = sizeof(addr); int native_fd = get_native_fd().fd(); - int r_fd = skip_eintr([&] { return ::accept(native_fd, reinterpret_cast(&addr), &addr_len); }); + int r_fd = detail::skip_eintr([&] { return ::accept(native_fd, reinterpret_cast(&addr), &addr_len); }); auto accept_errno = errno; if (r_fd >= 0) { return SocketFd::from_native_fd(NativeFd(r_fd)); diff --git a/tdutils/td/utils/port/SocketFd.cpp b/tdutils/td/utils/port/SocketFd.cpp index 52f9496a..446775a0 100644 --- a/tdutils/td/utils/port/SocketFd.cpp +++ b/tdutils/td/utils/port/SocketFd.cpp @@ -331,7 +331,7 @@ class SocketFdImpl { } Result write(Slice slice) { int native_fd = get_native_fd().fd(); - auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); + auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); auto write_errno = errno; if (write_res >= 0) { return narrow_cast(write_res); @@ -376,7 +376,7 @@ class SocketFdImpl { } int native_fd = get_native_fd().fd(); CHECK(slice.size() > 0); - auto read_res = skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); + auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); auto read_errno = errno; if (read_res >= 0) { if (read_res == 0) { diff --git a/tdutils/td/utils/port/UdpSocketFd.cpp b/tdutils/td/utils/port/UdpSocketFd.cpp index 0d6e686b..591c8bc8 100644 --- a/tdutils/td/utils/port/UdpSocketFd.cpp +++ b/tdutils/td/utils/port/UdpSocketFd.cpp @@ -503,7 +503,7 @@ class UdpSocketFdImpl { helper.to_native(message, message_header); auto native_fd = get_native_fd().fd(); - auto recvmsg_res = skip_eintr([&] { return recvmsg(native_fd, &message_header, flags); }); + auto recvmsg_res = detail::skip_eintr([&] { return recvmsg(native_fd, &message_header, flags); }); auto recvmsg_errno = errno; if (recvmsg_res >= 0) { helper.from_native(message_header, recvmsg_res, message); @@ -558,7 +558,7 @@ class UdpSocketFdImpl { helper.to_native(message, message_header); auto native_fd = get_native_fd().fd(); - auto sendmsg_res = skip_eintr([&] { return sendmsg(native_fd, &message_header, 0); }); + auto sendmsg_res = detail::skip_eintr([&] { return sendmsg(native_fd, &message_header, 0); }); auto sendmsg_errno = errno; if (sendmsg_res >= 0) { is_sent = true; @@ -668,7 +668,7 @@ class UdpSocketFdImpl { auto native_fd = get_native_fd().fd(); auto sendmmsg_res = - skip_eintr([&] { return sendmmsg(native_fd, headers.data(), narrow_cast(to_send), 0); }); + detail::skip_eintr([&] { return sendmmsg(native_fd, headers.data(), narrow_cast(to_send), 0); }); auto sendmmsg_errno = errno; if (sendmmsg_res >= 0) { cnt = sendmmsg_res; @@ -718,7 +718,7 @@ class UdpSocketFdImpl { } auto native_fd = get_native_fd().fd(); - auto recvmmsg_res = skip_eintr( + auto recvmmsg_res = detail::skip_eintr( [&] { return recvmmsg(native_fd, headers.data(), narrow_cast(to_receive), flags, nullptr); }); auto recvmmsg_errno = errno; if (recvmmsg_res >= 0) { diff --git a/tdutils/td/utils/port/detail/EventFdLinux.cpp b/tdutils/td/utils/port/detail/EventFdLinux.cpp index af43ed12..89e2ecfe 100644 --- a/tdutils/td/utils/port/detail/EventFdLinux.cpp +++ b/tdutils/td/utils/port/detail/EventFdLinux.cpp @@ -61,7 +61,7 @@ void EventFdLinux::release() { auto native_fd = impl_->info.native_fd().fd(); auto result = [&]() -> Result { - auto write_res = skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); + auto write_res = detail::skip_eintr([&] { return ::write(native_fd, slice.begin(), slice.size()); }); auto write_errno = errno; if (write_res >= 0) { return narrow_cast(write_res); @@ -84,7 +84,7 @@ void EventFdLinux::acquire() { auto native_fd = impl_->info.native_fd().fd(); auto result = [&]() -> Result { CHECK(slice.size() > 0); - auto read_res = skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); + auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); }); auto read_errno = errno; if (read_res >= 0) { CHECK(read_res != 0); diff --git a/tdutils/td/utils/port/detail/PollableFd.h b/tdutils/td/utils/port/detail/PollableFd.h index 0392f128..3982667c 100644 --- a/tdutils/td/utils/port/detail/PollableFd.h +++ b/tdutils/td/utils/port/detail/PollableFd.h @@ -324,6 +324,7 @@ inline const NativeFd &PollableFd::native_fd() const { } #if TD_PORT_POSIX +namespace detail { template auto skip_eintr(F &&f) { decltype(f()) res; @@ -343,6 +344,7 @@ auto skip_eintr_cstr(F &&f) { } while (res == nullptr && errno == EINTR); return res; } +} // namespace detail #endif template diff --git a/tdutils/td/utils/port/path.cpp b/tdutils/td/utils/port/path.cpp index 60be4382..c1583d2c 100644 --- a/tdutils/td/utils/port/path.cpp +++ b/tdutils/td/utils/port/path.cpp @@ -77,7 +77,7 @@ Status rmrf(CSlice path) { #if TD_PORT_POSIX Status mkdir(CSlice dir, int32 mode) { - int mkdir_res = skip_eintr([&] { return ::mkdir(dir.c_str(), static_cast(mode)); }); + int mkdir_res = detail::skip_eintr([&] { return ::mkdir(dir.c_str(), static_cast(mode)); }); if (mkdir_res == 0) { return Status::OK(); } @@ -90,7 +90,7 @@ Status mkdir(CSlice dir, int32 mode) { } Status rename(CSlice from, CSlice to) { - int rename_res = skip_eintr([&] { return ::rename(from.c_str(), to.c_str()); }); + int rename_res = detail::skip_eintr([&] { return ::rename(from.c_str(), to.c_str()); }); if (rename_res < 0) { return OS_ERROR(PSLICE() << "Can't rename \"" << from << "\" to \"" << to << '\"'); } @@ -100,7 +100,7 @@ Status rename(CSlice from, CSlice to) { Result realpath(CSlice slice, bool ignore_access_denied) { char full_path[PATH_MAX + 1]; string res; - char *err = skip_eintr_cstr([&] { return ::realpath(slice.c_str(), full_path); }); + char *err = detail::skip_eintr_cstr([&] { return ::realpath(slice.c_str(), full_path); }); if (err != full_path) { if (ignore_access_denied && (errno == EACCES || errno == EPERM)) { res = slice.str(); @@ -122,7 +122,7 @@ Result realpath(CSlice slice, bool ignore_access_denied) { } Status chdir(CSlice dir) { - int chdir_res = skip_eintr([&] { return ::chdir(dir.c_str()); }); + int chdir_res = detail::skip_eintr([&] { return ::chdir(dir.c_str()); }); if (chdir_res) { return OS_ERROR(PSLICE() << "Can't change directory to \"" << dir << '"'); } @@ -130,7 +130,7 @@ Status chdir(CSlice dir) { } Status rmdir(CSlice dir) { - int rmdir_res = skip_eintr([&] { return ::rmdir(dir.c_str()); }); + int rmdir_res = detail::skip_eintr([&] { return ::rmdir(dir.c_str()); }); if (rmdir_res) { return OS_ERROR(PSLICE() << "Can't delete directory \"" << dir << '"'); } @@ -138,7 +138,7 @@ Status rmdir(CSlice dir) { } Status unlink(CSlice path) { - int unlink_res = skip_eintr([&] { return ::unlink(path.c_str()); }); + int unlink_res = detail::skip_eintr([&] { return ::unlink(path.c_str()); }); if (unlink_res) { return OS_ERROR(PSLICE() << "Can't unlink \"" << path << '"'); } @@ -185,7 +185,7 @@ Result> mkstemp(CSlice dir) { } file_pattern += "tmpXXXXXXXXXX"; - int fd = skip_eintr([&] { return ::mkstemp(&file_pattern[0]); }); + int fd = detail::skip_eintr([&] { return ::mkstemp(&file_pattern[0]); }); if (fd == -1) { return OS_ERROR(PSLICE() << "Can't create temporary file \"" << file_pattern << '"'); } @@ -217,7 +217,7 @@ Result mkdtemp(CSlice dir, Slice prefix) { dir_pattern.append(prefix.begin(), prefix.size()); dir_pattern += "XXXXXX"; - char *result = skip_eintr_cstr([&] { return ::mkdtemp(&dir_pattern[0]); }); + char *result = detail::skip_eintr_cstr([&] { return ::mkdtemp(&dir_pattern[0]); }); if (result == nullptr) { return OS_ERROR(PSLICE() << "Can't create temporary directory \"" << dir_pattern << '"'); }