Reimplement NativeFd::duplicate.

GitOrigin-RevId: 7f2dcff066d8f944514cb0bbc48991bbe3706820
This commit is contained in:
levlam 2018-09-10 17:47:28 +03:00
parent 0f4343d542
commit c6afabd633
5 changed files with 17 additions and 34 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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();