From c6afabd633ab41894ab7b815da8b51a2ffcef737 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 10 Sep 2018 17:47:28 +0300 Subject: [PATCH] Reimplement NativeFd::duplicate. GitOrigin-RevId: 7f2dcff066d8f944514cb0bbc48991bbe3706820 --- tdutils/td/utils/FileLog.cpp | 6 ++---- tdutils/td/utils/port/Fd.cpp | 17 ----------------- tdutils/td/utils/port/Fd.h | 13 ------------- tdutils/td/utils/port/detail/NativeFd.cpp | 13 +++++++++++++ tdutils/td/utils/port/detail/NativeFd.h | 2 ++ 5 files changed, 17 insertions(+), 34 deletions(-) diff --git a/tdutils/td/utils/FileLog.cpp b/tdutils/td/utils/FileLog.cpp index 2cd0b63f..9bb07697 100644 --- a/tdutils/td/utils/FileLog.cpp +++ b/tdutils/td/utils/FileLog.cpp @@ -31,8 +31,7 @@ bool FileLog::init(string path, int64 rotate_threshold) { fd_.close(); fd_ = r_fd.move_as_ok(); - // FIXME - //Fd::duplicate(fd_.get_fd(), Fd::Stderr()).ignore(); + fd_.get_native_fd().duplicate(Stderr().get_native_fd()).ignore(); path_ = std::move(path); size_ = fd_.get_size(); @@ -85,8 +84,7 @@ void FileLog::do_rotate() { process_fatal_error(r_fd.error().message()); } fd_ = r_fd.move_as_ok(); - // FIXME - //Fd::duplicate(fd_.get_fd(), Fd::Stderr()).ignore(); + fd_.get_native_fd().duplicate(Stderr().get_native_fd()).ignore(); size_ = 0; SET_VERBOSITY_LEVEL(current_verbosity_level); } diff --git a/tdutils/td/utils/port/Fd.cpp b/tdutils/td/utils/port/Fd.cpp index 4e4f32d2..4afd2120 100644 --- a/tdutils/td/utils/port/Fd.cpp +++ b/tdutils/td/utils/port/Fd.cpp @@ -8,19 +8,6 @@ namespace td { -#if TD_PORT_POSIX - -Status Fd::duplicate(const Fd &from, Fd &to) { - CHECK(!from.empty()); - CHECK(!to.empty()); - if (dup2(from.get_native_fd(), to.get_native_fd()) == -1) { - return OS_ERROR("Failed to duplicate file descriptor"); - } - return Status::OK(); -} - -#endif - #if TD_PORT_WINDOWS #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) @@ -60,10 +47,6 @@ Fd &Fd::Stdout() { } #endif -Status Fd::duplicate(const Fd &from, Fd &to) { - return Status::Error("Not supported"); -} - #endif } // namespace td diff --git a/tdutils/td/utils/port/Fd.h b/tdutils/td/utils/port/Fd.h index 6700fa09..78c0d7bd 100644 --- a/tdutils/td/utils/port/Fd.h +++ b/tdutils/td/utils/port/Fd.h @@ -7,17 +7,4 @@ #pragma once #if 0 -#include "td/utils/port/config.h" - -#include "td/utils/common.h" -#include "td/utils/Status.h" - -namespace td { - -class Fd { - public: - static Status duplicate(const Fd &from, Fd &to); -}; - -} // namespace td #endif diff --git a/tdutils/td/utils/port/detail/NativeFd.cpp b/tdutils/td/utils/port/detail/NativeFd.cpp index 11421374..21b54065 100644 --- a/tdutils/td/utils/port/detail/NativeFd.cpp +++ b/tdutils/td/utils/port/detail/NativeFd.cpp @@ -96,6 +96,19 @@ Status NativeFd::set_is_blocking_unsafe(bool is_blocking) const { return Status::OK(); } +Status NativeFd::duplicate(const NativeFd &to) const { +#if TD_PORT_POSIX + CHECK(*this); + CHECK(to); + if (dup2(fd(), to.fd()) == -1) { + return OS_ERROR("Failed to duplicate file descriptor"); + } + return Status::OK(); +#elif TD_PORT_WINDOWS + return Status::Error("Not supported"); +#endif +} + void NativeFd::close() { if (!*this) { return; diff --git a/tdutils/td/utils/port/detail/NativeFd.h b/tdutils/td/utils/port/detail/NativeFd.h index d01de1f5..431db89b 100644 --- a/tdutils/td/utils/port/detail/NativeFd.h +++ b/tdutils/td/utils/port/detail/NativeFd.h @@ -50,6 +50,8 @@ class NativeFd { Status set_is_blocking_unsafe(bool is_blocking) const; // may drop other Fd flags on non-Windows + Status duplicate(const NativeFd &to) const; + void close(); Raw release();