diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/system/pipe.cpp | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/system/pipe.cpp')
-rw-r--r-- | util/system/pipe.cpp | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/util/system/pipe.cpp b/util/system/pipe.cpp index a543bd7472..012ee2c30e 100644 --- a/util/system/pipe.cpp +++ b/util/system/pipe.cpp @@ -1,50 +1,50 @@ -#include "pipe.h" - +#include "pipe.h" + #include <util/stream/output.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> ssize_t TPipeHandle::Read(void* buffer, size_t byteCount) const noexcept { #ifdef _win_ - return recv(Fd_, (char*)buffer, byteCount, 0); + return recv(Fd_, (char*)buffer, byteCount, 0); #else - return read(Fd_, buffer, byteCount); + return read(Fd_, buffer, byteCount); #endif } ssize_t TPipeHandle::Write(const void* buffer, size_t byteCount) const noexcept { #ifdef _win_ - return send(Fd_, (const char*)buffer, byteCount, 0); + return send(Fd_, (const char*)buffer, byteCount, 0); #else - return write(Fd_, buffer, byteCount); + return write(Fd_, buffer, byteCount); #endif } bool TPipeHandle::Close() noexcept { bool ok = true; - if (Fd_ != INVALID_PIPEHANDLE) { + if (Fd_ != INVALID_PIPEHANDLE) { #ifdef _win_ - ok = closesocket(Fd_) == 0; + ok = closesocket(Fd_) == 0; #else - ok = close(Fd_) == 0; + ok = close(Fd_) == 0; #endif } - Fd_ = INVALID_PIPEHANDLE; + Fd_ = INVALID_PIPEHANDLE; return ok; } void TPipeHandle::Pipe(TPipeHandle& reader, TPipeHandle& writer, EOpenMode mode) { - PIPEHANDLE fds[2]; -#ifdef _win_ + PIPEHANDLE fds[2]; +#ifdef _win_ int r = SocketPair(fds, false /* non-overlapped */, mode & CloseOnExec /* cloexec */); #elif defined(_linux_) int r = pipe2(fds, mode & CloseOnExec ? O_CLOEXEC : 0); -#else - int r = pipe(fds); -#endif - if (r < 0) { - ythrow TFileError() << "failed to create a pipe"; - } - +#else + int r = pipe(fds); +#endif + if (r < 0) { + ythrow TFileError() << "failed to create a pipe"; + } + #if !defined(_win_) && !defined(_linux_) // Non-atomic wrt exec if (mode & CloseOnExec) { @@ -61,19 +61,19 @@ void TPipeHandle::Pipe(TPipeHandle& reader, TPipeHandle& writer, EOpenMode mode) } #endif - TPipeHandle(fds[0]).Swap(reader); - TPipeHandle(fds[1]).Swap(writer); -} - -class TPipe::TImpl: public TAtomicRefCount<TImpl> { + TPipeHandle(fds[0]).Swap(reader); + TPipeHandle(fds[1]).Swap(writer); +} + +class TPipe::TImpl: public TAtomicRefCount<TImpl> { public: TImpl() - : Handle_(INVALID_PIPEHANDLE) + : Handle_(INVALID_PIPEHANDLE) { } - + TImpl(PIPEHANDLE fd) - : Handle_(fd) + : Handle_(fd) { } @@ -82,80 +82,80 @@ public: } bool IsOpen() { - return Handle_.IsOpen(); + return Handle_.IsOpen(); } inline void Close() { - if (!Handle_.IsOpen()) { + if (!Handle_.IsOpen()) { return; - } - if (!Handle_.Close()) { - ythrow TFileError() << "failed to close pipe"; - } + } + if (!Handle_.Close()) { + ythrow TFileError() << "failed to close pipe"; + } } TPipeHandle& GetHandle() noexcept { - return Handle_; + return Handle_; } size_t Read(void* buffer, size_t count) const { - ssize_t r = Handle_.Read(buffer, count); - if (r < 0) { - ythrow TFileError() << "failed to read from pipe"; - } + ssize_t r = Handle_.Read(buffer, count); + if (r < 0) { + ythrow TFileError() << "failed to read from pipe"; + } return r; } size_t Write(const void* buffer, size_t count) const { - ssize_t r = Handle_.Write(buffer, count); - if (r < 0) { - ythrow TFileError() << "failed to write to pipe"; - } + ssize_t r = Handle_.Write(buffer, count); + if (r < 0) { + ythrow TFileError() << "failed to write to pipe"; + } return r; } - + private: - TPipeHandle Handle_; + TPipeHandle Handle_; }; TPipe::TPipe() - : Impl_(new TImpl) + : Impl_(new TImpl) { } TPipe::TPipe(PIPEHANDLE fd) - : Impl_(new TImpl(fd)) + : Impl_(new TImpl(fd)) { } TPipe::~TPipe() = default; void TPipe::Close() { - Impl_->Close(); + Impl_->Close(); } PIPEHANDLE TPipe::GetHandle() const noexcept { - return Impl_->GetHandle(); + return Impl_->GetHandle(); } bool TPipe::IsOpen() const noexcept { - return Impl_->IsOpen(); + return Impl_->IsOpen(); } size_t TPipe::Read(void* buf, size_t len) const { - return Impl_->Read(buf, len); + return Impl_->Read(buf, len); } size_t TPipe::Write(const void* buf, size_t len) const { - return Impl_->Write(buf, len); + return Impl_->Write(buf, len); } void TPipe::Pipe(TPipe& reader, TPipe& writer, EOpenMode mode) { - TImplRef r(new TImpl()); - TImplRef w(new TImpl()); - + TImplRef r(new TImpl()); + TImplRef w(new TImpl()); + TPipeHandle::Pipe(r->GetHandle(), w->GetHandle(), mode); - - r.Swap(reader.Impl_); - w.Swap(writer.Impl_); + + r.Swap(reader.Impl_); + w.Swap(writer.Impl_); } |