serenity/VirtualFileSystem/FileDescriptor.cpp

317 lines
7.8 KiB
C++
Raw Normal View History

2018-11-07 11:37:54 +01:00
#include "FileDescriptor.h"
#include "FileSystem.h"
#include "CharacterDevice.h"
#include <LibC/errno_numbers.h>
#include "UnixTypes.h"
#include <AK/BufferStream.h>
#include "FIFO.h"
2018-11-07 12:05:51 +01:00
#ifdef SERENITY
#include "TTY.h"
#include "MasterPTY.h"
2018-11-07 12:05:51 +01:00
#endif
RetainPtr<FileDescriptor> FileDescriptor::create(RetainPtr<Vnode>&& vnode)
{
2018-11-07 11:37:54 +01:00
return adopt(*new FileDescriptor(move(vnode)));
}
RetainPtr<FileDescriptor> FileDescriptor::create_pipe_writer(FIFO& fifo)
{
return adopt(*new FileDescriptor(fifo, FIFO::Writer));
}
RetainPtr<FileDescriptor> FileDescriptor::create_pipe_reader(FIFO& fifo)
{
return adopt(*new FileDescriptor(fifo, FIFO::Reader));
}
FileDescriptor::FileDescriptor(RetainPtr<Vnode>&& vnode)
: m_vnode(move(vnode))
{
}
2018-11-07 11:37:54 +01:00
FileDescriptor::~FileDescriptor()
{
if (m_fifo)
m_fifo->close(fifo_direction());
}
2018-11-07 11:37:54 +01:00
RetainPtr<FileDescriptor> FileDescriptor::clone()
{
RetainPtr<FileDescriptor> descriptor;
if (is_fifo()) {
descriptor = fifo_direction() == FIFO::Reader
? FileDescriptor::create_pipe_reader(*m_fifo)
: FileDescriptor::create_pipe_writer(*m_fifo);
} else {
descriptor = FileDescriptor::create(m_vnode.copyRef());
}
2018-11-07 11:37:54 +01:00
if (!descriptor)
return nullptr;
2018-12-03 00:39:25 +01:00
descriptor->m_current_offset = m_current_offset;
#ifdef SERENITY
2018-12-03 00:39:25 +01:00
descriptor->m_is_blocking = m_is_blocking;
descriptor->m_file_flags = m_file_flags;
#endif
2018-11-07 11:37:54 +01:00
return descriptor;
}
2018-10-22 14:06:22 +02:00
#ifndef SERENITY
bool additionWouldOverflow(Unix::off_t a, Unix::off_t b)
{
ASSERT(a > 0);
uint64_t ua = a;
return (ua + b) > maxFileOffset;
}
#endif
2018-11-07 11:37:54 +01:00
int FileDescriptor::stat(Unix::stat* buffer)
{
ASSERT(!is_fifo());
if (!m_vnode)
return -EBADF;
auto metadata = m_vnode->metadata();
if (!metadata.isValid())
return -EIO;
buffer->st_dev = 0; // FIXME
buffer->st_ino = metadata.inode.index();
buffer->st_mode = metadata.mode;
buffer->st_nlink = metadata.linkCount;
buffer->st_uid = metadata.uid;
buffer->st_gid = metadata.gid;
buffer->st_rdev = 0; // FIXME
buffer->st_size = metadata.size;
buffer->st_blksize = metadata.blockSize;
buffer->st_blocks = metadata.blockCount;
buffer->st_atime = metadata.atime;
buffer->st_mtime = metadata.mtime;
buffer->st_ctime = metadata.ctime;
return 0;
}
2018-11-07 11:37:54 +01:00
Unix::off_t FileDescriptor::seek(Unix::off_t offset, int whence)
{
ASSERT(!is_fifo());
if (!m_vnode)
return -EBADF;
// FIXME: The file type should be cached on the vnode.
// It's silly that we have to do a full metadata lookup here.
auto metadata = m_vnode->metadata();
if (!metadata.isValid())
return -EIO;
if (metadata.isSocket() || metadata.isFIFO())
return -ESPIPE;
Unix::off_t newOffset;
switch (whence) {
case SEEK_SET:
newOffset = offset;
break;
case SEEK_CUR:
2018-12-03 00:39:25 +01:00
newOffset = m_current_offset + offset;
2018-10-22 14:06:22 +02:00
#ifndef SERENITY
if (additionWouldOverflow(m_currentOffset, offset))
return -EOVERFLOW;
#endif
if (newOffset < 0)
return -EINVAL;
break;
case SEEK_END:
ASSERT(metadata.size); // FIXME: What do I do?
newOffset = metadata.size;
break;
default:
return -EINVAL;
}
2018-12-03 00:39:25 +01:00
m_current_offset = newOffset;
return m_current_offset;
}
ssize_t FileDescriptor::read(Process& process, byte* buffer, size_t count)
{
if (is_fifo()) {
ASSERT(fifo_direction() == FIFO::Reader);
return m_fifo->read(buffer, count);
}
if (m_vnode->isCharacterDevice()) {
// FIXME: What should happen to m_currentOffset?
return m_vnode->characterDevice()->read(process, buffer, count);
}
ASSERT(inode());
ssize_t nread = inode()->read_bytes(m_current_offset, count, buffer, this);
2018-12-03 00:39:25 +01:00
m_current_offset += nread;
return nread;
}
ssize_t FileDescriptor::write(Process& process, const byte* data, size_t size)
{
if (is_fifo()) {
ASSERT(fifo_direction() == FIFO::Writer);
return m_fifo->write(data, size);
}
if (m_vnode->isCharacterDevice()) {
// FIXME: What should happen to m_currentOffset?
return m_vnode->characterDevice()->write(process, data, size);
}
// FIXME: Implement non-device writes.
ASSERT_NOT_REACHED();
return -1;
}
bool FileDescriptor::can_write(Process& process)
{
if (is_fifo()) {
ASSERT(fifo_direction() == FIFO::Writer);
return m_fifo->can_write();
}
if (m_vnode->isCharacterDevice())
return m_vnode->characterDevice()->can_write(process);
return true;
}
bool FileDescriptor::can_read(Process& process)
{
if (is_fifo()) {
ASSERT(fifo_direction() == FIFO::Reader);
return m_fifo->can_read();
}
if (m_vnode->isCharacterDevice())
return m_vnode->characterDevice()->can_read(process);
return true;
}
ByteBuffer FileDescriptor::read_entire_file(Process& process)
{
ASSERT(!is_fifo());
if (m_vnode->isCharacterDevice()) {
2018-12-21 02:10:45 +01:00
auto buffer = ByteBuffer::create_uninitialized(1024);
ssize_t nread = m_vnode->characterDevice()->read(process, buffer.pointer(), buffer.size());
buffer.trim(nread);
return buffer;
}
ASSERT(inode());
return inode()->read_entire(this);
}
2018-12-03 00:39:25 +01:00
bool FileDescriptor::is_directory() const
{
ASSERT(!is_fifo());
return m_vnode->metadata().isDirectory();
}
ssize_t FileDescriptor::get_dir_entries(byte* buffer, size_t size)
{
auto metadata = m_vnode->metadata();
if (!metadata.isValid())
return -EIO;
if (!metadata.isDirectory())
return -ENOTDIR;
// FIXME: Compute the actual size needed.
2018-12-21 02:10:45 +01:00
auto tempBuffer = ByteBuffer::create_uninitialized(2048);
BufferStream stream(tempBuffer);
2018-11-15 15:36:35 +01:00
m_vnode->vfs()->traverse_directory_inode(*m_vnode->core_inode(), [&stream] (auto& entry) {
stream << (dword)entry.inode.index();
stream << (byte)entry.fileType;
stream << (dword)entry.name_length;
stream << entry.name;
return true;
});
if (size < stream.offset())
return -1;
memcpy(buffer, tempBuffer.pointer(), stream.offset());
return stream.offset();
}
2018-11-07 12:05:51 +01:00
\
#ifdef SERENITY
2018-12-03 00:39:25 +01:00
bool FileDescriptor::is_tty() const
{
if (is_fifo())
return false;
if (auto* device = m_vnode->characterDevice())
2018-12-03 00:39:25 +01:00
return device->is_tty();
return false;
}
2018-11-07 11:37:54 +01:00
const TTY* FileDescriptor::tty() const
{
if (is_fifo())
return nullptr;
if (auto* device = m_vnode->characterDevice())
return static_cast<const TTY*>(device);
return nullptr;
}
2018-11-07 11:37:54 +01:00
TTY* FileDescriptor::tty()
{
if (is_fifo())
return nullptr;
if (auto* device = m_vnode->characterDevice())
return static_cast<TTY*>(device);
return nullptr;
}
bool FileDescriptor::is_master_pty() const
{
if (is_fifo())
return false;
if (auto* device = m_vnode->characterDevice())
return device->is_master_pty();
return false;
}
const MasterPTY* FileDescriptor::master_pty() const
{
if (!is_master_pty())
return nullptr;
return static_cast<const MasterPTY*>(m_vnode->characterDevice());
}
MasterPTY* FileDescriptor::master_pty()
{
if (!is_master_pty())
return nullptr;
return static_cast<MasterPTY*>(m_vnode->characterDevice());
}
2018-11-07 12:05:51 +01:00
#endif
2018-11-07 11:37:54 +01:00
int FileDescriptor::close()
{
return 0;
}
2018-11-13 23:44:54 +01:00
String FileDescriptor::absolute_path()
{
2018-11-13 23:44:54 +01:00
Stopwatch sw("absolute_path");
2018-11-07 12:05:51 +01:00
#ifdef SERENITY
2018-12-03 00:39:25 +01:00
if (is_tty())
return tty()->tty_name();
2018-11-07 12:05:51 +01:00
#endif
if (is_fifo()) {
char buf[32];
ksprintf(buf, "fifo:%x", m_fifo.ptr());
return buf;
}
2018-11-13 23:44:54 +01:00
ASSERT(m_vnode->core_inode());
return VFS::the().absolute_path(*m_vnode->core_inode());
}
FileDescriptor::FileDescriptor(FIFO& fifo, FIFO::Direction direction)
2018-12-03 00:39:25 +01:00
: m_is_blocking(true)
, m_fifo(fifo)
, m_fifo_direction(direction)
{
m_fifo->open(direction);
}