Luna/kernel/src/fs/Pipe.cpp
apio efc6d03f23
All checks were successful
continuous-integration/drone/push Build is passing
kernel+libc: Add support for unnamed pipes
2023-05-10 22:48:31 +02:00

63 lines
1.3 KiB
C++

#include "fs/Pipe.h"
#include "thread/Scheduler.h"
Result<void> Pipe::create(SharedPtr<VFS::Inode>& rpipe, SharedPtr<VFS::Inode>& wpipe)
{
auto pipe = TRY(make_shared<Pipe>());
auto writer = TRY(make_shared<PipeWriter>());
auto reader = TRY(make_shared<PipeReader>());
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<usize> 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<usize> 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;
}