#include "fs/Pipe.h" #include "thread/Scheduler.h" Result Pipe::create(SharedPtr& rpipe, SharedPtr& wpipe) { auto pipe = TRY(make_shared()); auto writer = TRY(make_shared()); auto reader = TRY(make_shared()); auto auth = Scheduler::current()->auth; pipe->m_writer = writer.ptr(); pipe->m_reader = reader.ptr(); writer->m_pipe = reader->m_pipe = pipe; writer->chown(auth.euid, auth.egid); reader->chown(auth.euid, auth.egid); rpipe = reader; wpipe = writer; return {}; } Result Pipe::read(u8* buf, usize, usize length) { if (length > m_data_buffer.size()) length = m_data_buffer.size(); memcpy(buf, m_data_buffer.data(), length); memmove(m_data_buffer.data(), m_data_buffer.data() + length, m_data_buffer.size() - length); TRY(m_data_buffer.try_resize(m_data_buffer.size() - length)); return length; } Result Pipe::write(const u8* buf, usize, usize length) { if (!m_reader) return length; u8* slice = TRY(m_data_buffer.slice_at_end(length)); memcpy(slice, buf, length); return length; } bool Pipe::blocking() const { return !m_data_buffer.size() && m_writer; } PipeWriter::~PipeWriter() { m_pipe->m_writer = nullptr; } PipeReader::~PipeReader() { m_pipe->m_reader = nullptr; }