From: Mikko Rasa Date: Tue, 15 Oct 2013 11:53:04 +0000 (+0300) Subject: Allow setting a pipe read-only or write-only after creation X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9a4c531eb1aa68595cd3ab074b31bfa565aaefe2;p=libs%2Fcore.git Allow setting a pipe read-only or write-only after creation This is useful for handing the pipe to a child process and then closing the unused end so an end-of-file event can be properly received. --- diff --git a/source/io/pipe.cpp b/source/io/pipe.cpp index 831076f..0a72a80 100644 --- a/source/io/pipe.cpp +++ b/source/io/pipe.cpp @@ -24,6 +24,22 @@ Pipe::~Pipe() sys_close(write_handle); } +void Pipe::set_mode(Mode m) +{ + m = m&M_RDWR; + if(!m) + throw invalid_argument("Pipe::set_mode"); + + check_access(m); + + Mode close = mode&M_RDWR&~m; + if(close&M_READ) + sys_close(read_handle); + if(close&M_WRITE) + sys_close(write_handle); + mode = (mode&~M_RDWR)|m; +} + void Pipe::set_block(bool b) { mode = (mode&~M_NONBLOCK); diff --git a/source/io/pipe.h b/source/io/pipe.h index 50da5e5..3f964f4 100644 --- a/source/io/pipe.h +++ b/source/io/pipe.h @@ -22,6 +22,7 @@ private: public: ~Pipe(); + void set_mode(Mode); void set_block(bool); protected: