53 lines
1.3 KiB
C++
53 lines
1.3 KiB
C++
#include "fs/FileDescriptor.h"
|
|
#include "errno.h"
|
|
|
|
Descriptor::Descriptor() : m_is_open(false)
|
|
{
|
|
}
|
|
|
|
Descriptor::Descriptor(const Descriptor& other)
|
|
: m_is_open(other.m_is_open), m_can_read(other.m_can_read), m_can_write(other.m_can_write), m_node(other.m_node),
|
|
m_offset(other.m_offset)
|
|
{
|
|
}
|
|
|
|
void Descriptor::open(VFS::Node* node, bool can_read, bool can_write)
|
|
{
|
|
m_can_read = can_read;
|
|
m_can_write = can_write;
|
|
m_node = node;
|
|
m_offset = 0;
|
|
m_is_open = true;
|
|
}
|
|
|
|
ssize_t Descriptor::read(size_t size, char* buffer)
|
|
{
|
|
ssize_t result = VFS::read(m_node, m_offset, size, buffer);
|
|
m_offset += result;
|
|
return result;
|
|
}
|
|
|
|
ssize_t Descriptor::write(size_t size, const char* buffer)
|
|
{
|
|
ssize_t result = VFS::write(m_node, m_offset, size, buffer);
|
|
m_offset += result;
|
|
return result;
|
|
}
|
|
|
|
int Descriptor::seek(long offset)
|
|
{
|
|
if (m_node->type != VFS_DEVICE && (uint64_t)offset > m_node->length)
|
|
return -EINVAL; // FIXME: Support seeking beyond the current file's length.
|
|
m_offset = (uint64_t)offset;
|
|
return 0;
|
|
}
|
|
|
|
const Descriptor& Descriptor::operator=(const Descriptor& other)
|
|
{
|
|
m_is_open = other.m_is_open;
|
|
m_can_read = other.m_can_read;
|
|
m_can_write = other.m_can_write;
|
|
m_offset = other.m_offset;
|
|
m_node = other.m_node;
|
|
return other;
|
|
} |