2018-11-07 11:37:54 +01:00
|
|
|
#include "FileDescriptor.h"
|
2018-10-10 11:53:07 +02:00
|
|
|
#include "FileSystem.h"
|
2018-10-14 02:59:22 +02:00
|
|
|
#include "CharacterDevice.h"
|
2018-11-07 21:19:47 +01:00
|
|
|
#include <LibC/errno_numbers.h>
|
2018-10-14 22:57:41 +02:00
|
|
|
#include "UnixTypes.h"
|
2018-10-24 12:43:52 +02:00
|
|
|
#include <AK/BufferStream.h>
|
2018-11-12 01:28:46 +01:00
|
|
|
#include "FIFO.h"
|
2018-11-07 12:05:51 +01:00
|
|
|
#include "TTY.h"
|
2019-01-15 06:30:19 +01:00
|
|
|
#include "MasterPTY.h"
|
2018-11-07 12:05:51 +01:00
|
|
|
|
2019-01-16 12:57:07 +01:00
|
|
|
RetainPtr<FileDescriptor> FileDescriptor::create(RetainPtr<Inode>&& inode)
|
2018-11-05 19:01:22 +01:00
|
|
|
{
|
2019-01-16 12:57:07 +01:00
|
|
|
return adopt(*new FileDescriptor(move(inode)));
|
|
|
|
}
|
|
|
|
|
|
|
|
RetainPtr<FileDescriptor> FileDescriptor::create(RetainPtr<CharacterDevice>&& device)
|
|
|
|
{
|
|
|
|
return adopt(*new FileDescriptor(move(device)));
|
2018-11-05 19:01:22 +01:00
|
|
|
}
|
|
|
|
|
2018-11-12 01:28:46 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2019-01-16 12:57:07 +01:00
|
|
|
FileDescriptor::FileDescriptor(RetainPtr<Inode>&& inode)
|
|
|
|
: m_inode(move(inode))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileDescriptor::FileDescriptor(RetainPtr<CharacterDevice>&& device)
|
|
|
|
: m_device(move(device))
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-07 11:37:54 +01:00
|
|
|
FileDescriptor::~FileDescriptor()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
if (m_fifo)
|
|
|
|
m_fifo->close(fifo_direction());
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 11:37:54 +01:00
|
|
|
RetainPtr<FileDescriptor> FileDescriptor::clone()
|
2018-11-02 20:41:58 +01:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
RetainPtr<FileDescriptor> descriptor;
|
|
|
|
if (is_fifo()) {
|
|
|
|
descriptor = fifo_direction() == FIFO::Reader
|
|
|
|
? FileDescriptor::create_pipe_reader(*m_fifo)
|
|
|
|
: FileDescriptor::create_pipe_writer(*m_fifo);
|
|
|
|
} else {
|
2019-01-16 12:57:07 +01:00
|
|
|
if (m_inode)
|
|
|
|
descriptor = FileDescriptor::create(m_inode.copyRef());
|
|
|
|
else {
|
|
|
|
descriptor = FileDescriptor::create(m_device.copyRef());
|
|
|
|
}
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
2018-11-07 11:37:54 +01:00
|
|
|
if (!descriptor)
|
2018-11-02 20:41:58 +01:00
|
|
|
return nullptr;
|
2018-12-03 00:39:25 +01:00
|
|
|
descriptor->m_current_offset = m_current_offset;
|
|
|
|
descriptor->m_is_blocking = m_is_blocking;
|
2018-11-12 01:28:46 +01:00
|
|
|
descriptor->m_file_flags = m_file_flags;
|
2018-11-07 11:37:54 +01:00
|
|
|
return descriptor;
|
2018-11-02 20:41:58 +01:00
|
|
|
}
|
|
|
|
|
2018-10-14 22:57:41 +02:00
|
|
|
bool additionWouldOverflow(Unix::off_t a, Unix::off_t b)
|
2018-10-14 21:19:27 +02:00
|
|
|
{
|
|
|
|
ASSERT(a > 0);
|
|
|
|
uint64_t ua = a;
|
|
|
|
return (ua + b) > maxFileOffset;
|
|
|
|
}
|
|
|
|
|
2018-11-07 11:37:54 +01:00
|
|
|
int FileDescriptor::stat(Unix::stat* buffer)
|
2018-10-14 22:57:41 +02:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
ASSERT(!is_fifo());
|
2019-01-16 12:57:07 +01:00
|
|
|
if (!m_inode && !m_device)
|
2018-10-14 22:57:41 +02:00
|
|
|
return -EBADF;
|
|
|
|
|
2019-01-16 12:57:07 +01:00
|
|
|
auto metadata = this->metadata();
|
2018-10-14 22:57:41 +02:00
|
|
|
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
|
2018-10-14 23:39:11 +02:00
|
|
|
buffer->st_size = metadata.size;
|
|
|
|
buffer->st_blksize = metadata.blockSize;
|
|
|
|
buffer->st_blocks = metadata.blockCount;
|
2018-10-14 22:57:41 +02:00
|
|
|
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)
|
2018-10-14 21:19:27 +02:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
ASSERT(!is_fifo());
|
2019-01-16 12:57:07 +01:00
|
|
|
if (!m_inode && !m_device)
|
2018-10-14 21:19:27 +02:00
|
|
|
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.
|
2019-01-16 12:57:07 +01:00
|
|
|
auto metadata = this->metadata();
|
2018-10-14 22:57:41 +02:00
|
|
|
if (!metadata.isValid())
|
|
|
|
return -EIO;
|
|
|
|
|
2018-10-14 21:19:27 +02:00
|
|
|
if (metadata.isSocket() || metadata.isFIFO())
|
|
|
|
return -ESPIPE;
|
|
|
|
|
2018-10-14 22:57:41 +02:00
|
|
|
Unix::off_t newOffset;
|
2018-10-14 21:19:27 +02:00
|
|
|
|
2018-10-14 22:57:41 +02:00
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET:
|
2018-10-14 21:19:27 +02:00
|
|
|
newOffset = offset;
|
|
|
|
break;
|
2018-10-14 22:57:41 +02:00
|
|
|
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
|
2018-10-14 21:19:27 +02:00
|
|
|
if (additionWouldOverflow(m_currentOffset, offset))
|
|
|
|
return -EOVERFLOW;
|
2018-10-17 12:07:39 +02:00
|
|
|
#endif
|
2018-10-14 21:19:27 +02:00
|
|
|
if (newOffset < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
break;
|
2018-10-14 22:57:41 +02:00
|
|
|
case SEEK_END:
|
2018-10-31 17:50:43 +01:00
|
|
|
ASSERT(metadata.size); // FIXME: What do I do?
|
|
|
|
newOffset = metadata.size;
|
2018-10-14 21:19:27 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:39:25 +01:00
|
|
|
m_current_offset = newOffset;
|
|
|
|
return m_current_offset;
|
2018-10-14 21:19:27 +02:00
|
|
|
}
|
|
|
|
|
2019-01-16 00:20:38 +01:00
|
|
|
ssize_t FileDescriptor::read(Process& process, byte* buffer, size_t count)
|
2018-10-14 21:19:27 +02:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
if (is_fifo()) {
|
|
|
|
ASSERT(fifo_direction() == FIFO::Reader);
|
|
|
|
return m_fifo->read(buffer, count);
|
|
|
|
}
|
2019-01-16 12:57:07 +01:00
|
|
|
if (m_device) {
|
2018-10-14 21:19:27 +02:00
|
|
|
// FIXME: What should happen to m_currentOffset?
|
2019-01-16 12:57:07 +01:00
|
|
|
return m_device->read(process, buffer, count);
|
2018-10-14 21:19:27 +02:00
|
|
|
}
|
2018-12-21 17:28:16 +01:00
|
|
|
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;
|
2018-10-14 21:19:27 +02:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2019-01-16 00:20:38 +01:00
|
|
|
ssize_t FileDescriptor::write(Process& process, const byte* data, size_t size)
|
2018-10-30 15:33:37 +01:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
if (is_fifo()) {
|
|
|
|
ASSERT(fifo_direction() == FIFO::Writer);
|
|
|
|
return m_fifo->write(data, size);
|
|
|
|
}
|
2019-01-16 12:57:07 +01:00
|
|
|
if (m_device) {
|
2018-10-30 15:33:37 +01:00
|
|
|
// FIXME: What should happen to m_currentOffset?
|
2019-01-16 12:57:07 +01:00
|
|
|
return m_device->write(process, data, size);
|
2018-10-30 15:33:37 +01:00
|
|
|
}
|
|
|
|
// FIXME: Implement non-device writes.
|
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-30 22:03:02 +01:00
|
|
|
return -1;
|
2018-10-30 15:33:37 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 09:17:22 +01:00
|
|
|
bool FileDescriptor::can_write(Process& process)
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
|
|
|
if (is_fifo()) {
|
|
|
|
ASSERT(fifo_direction() == FIFO::Writer);
|
|
|
|
return m_fifo->can_write();
|
|
|
|
}
|
2019-01-16 12:57:07 +01:00
|
|
|
if (m_device)
|
|
|
|
return m_device->can_write(process);
|
2018-11-12 01:28:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-16 00:47:00 +01:00
|
|
|
bool FileDescriptor::can_read(Process& process)
|
2018-10-25 13:07:59 +02:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
if (is_fifo()) {
|
|
|
|
ASSERT(fifo_direction() == FIFO::Reader);
|
|
|
|
return m_fifo->can_read();
|
|
|
|
}
|
2019-01-16 12:57:07 +01:00
|
|
|
if (m_device)
|
|
|
|
return m_device->can_read(process);
|
2018-10-25 13:07:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-16 00:20:38 +01:00
|
|
|
ByteBuffer FileDescriptor::read_entire_file(Process& process)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
ASSERT(!is_fifo());
|
|
|
|
|
2019-01-16 12:57:07 +01:00
|
|
|
if (m_device) {
|
2018-12-21 02:10:45 +01:00
|
|
|
auto buffer = ByteBuffer::create_uninitialized(1024);
|
2019-01-16 12:57:07 +01:00
|
|
|
ssize_t nread = m_device->read(process, buffer.pointer(), buffer.size());
|
2018-10-14 02:59:22 +02:00
|
|
|
buffer.trim(nread);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2019-01-16 12:57:07 +01:00
|
|
|
ASSERT(m_inode);
|
|
|
|
return m_inode->read_entire(this);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:39:25 +01:00
|
|
|
bool FileDescriptor::is_directory() const
|
2018-10-26 14:24:11 +02:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
ASSERT(!is_fifo());
|
2019-01-16 12:57:07 +01:00
|
|
|
return metadata().isDirectory();
|
2018-10-26 14:24:11 +02:00
|
|
|
}
|
|
|
|
|
2018-12-02 23:34:50 +01:00
|
|
|
ssize_t FileDescriptor::get_dir_entries(byte* buffer, size_t size)
|
2018-10-24 12:43:52 +02:00
|
|
|
{
|
2019-01-16 12:57:07 +01:00
|
|
|
auto metadata = this->metadata();
|
2018-10-24 12:43:52 +02:00
|
|
|
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);
|
2018-10-24 12:43:52 +02:00
|
|
|
BufferStream stream(tempBuffer);
|
2019-01-16 12:57:07 +01:00
|
|
|
VFS::the().traverse_directory_inode(*m_inode, [&stream] (auto& entry) {
|
2018-10-24 12:43:52 +02:00
|
|
|
stream << (dword)entry.inode.index();
|
|
|
|
stream << (byte)entry.fileType;
|
2018-11-13 00:17:30 +01:00
|
|
|
stream << (dword)entry.name_length;
|
2018-10-24 12:43:52 +02:00
|
|
|
stream << entry.name;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-10-24 14:28:22 +02:00
|
|
|
if (size < stream.offset())
|
|
|
|
return -1;
|
|
|
|
|
2018-10-24 12:43:52 +02:00
|
|
|
memcpy(buffer, tempBuffer.pointer(), stream.offset());
|
|
|
|
return stream.offset();
|
|
|
|
}
|
2019-01-16 12:57:07 +01:00
|
|
|
|
2018-12-03 00:39:25 +01:00
|
|
|
bool FileDescriptor::is_tty() const
|
2018-10-30 22:03:02 +01:00
|
|
|
{
|
2019-01-16 12:57:07 +01:00
|
|
|
return m_device && m_device->is_tty();
|
2018-10-30 22:03:02 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 11:37:54 +01:00
|
|
|
const TTY* FileDescriptor::tty() const
|
2018-10-30 22:03:02 +01:00
|
|
|
{
|
2019-01-16 12:57:07 +01:00
|
|
|
if (!is_tty())
|
2018-11-12 01:28:46 +01:00
|
|
|
return nullptr;
|
2019-01-16 12:57:07 +01:00
|
|
|
return static_cast<const TTY*>(m_device.ptr());
|
2018-10-30 22:03:02 +01:00
|
|
|
}
|
2018-11-01 14:00:28 +01:00
|
|
|
|
2018-11-07 11:37:54 +01:00
|
|
|
TTY* FileDescriptor::tty()
|
2018-11-02 13:14:25 +01:00
|
|
|
{
|
2019-01-16 12:57:07 +01:00
|
|
|
if (!is_tty())
|
2018-11-12 01:28:46 +01:00
|
|
|
return nullptr;
|
2019-01-16 12:57:07 +01:00
|
|
|
return static_cast<TTY*>(m_device.ptr());
|
2018-11-02 13:14:25 +01:00
|
|
|
}
|
2019-01-15 06:30:19 +01:00
|
|
|
|
|
|
|
bool FileDescriptor::is_master_pty() const
|
|
|
|
{
|
2019-01-16 12:57:07 +01:00
|
|
|
if (m_device)
|
|
|
|
return m_device->is_master_pty();
|
2019-01-15 06:30:19 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MasterPTY* FileDescriptor::master_pty() const
|
|
|
|
{
|
|
|
|
if (!is_master_pty())
|
|
|
|
return nullptr;
|
2019-01-16 12:57:07 +01:00
|
|
|
return static_cast<const MasterPTY*>(m_device.ptr());
|
2019-01-15 06:30:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MasterPTY* FileDescriptor::master_pty()
|
|
|
|
{
|
|
|
|
if (!is_master_pty())
|
|
|
|
return nullptr;
|
2019-01-16 12:57:07 +01:00
|
|
|
return static_cast<MasterPTY*>(m_device.ptr());
|
2019-01-15 06:30:19 +01:00
|
|
|
}
|
2018-11-02 13:14:25 +01:00
|
|
|
|
2018-11-07 11:37:54 +01:00
|
|
|
int FileDescriptor::close()
|
2018-11-01 14:00:28 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:44:54 +01:00
|
|
|
String FileDescriptor::absolute_path()
|
2018-11-01 14:00:28 +01:00
|
|
|
{
|
2018-11-13 23:44:54 +01:00
|
|
|
Stopwatch sw("absolute_path");
|
2018-12-03 00:39:25 +01:00
|
|
|
if (is_tty())
|
|
|
|
return tty()->tty_name();
|
2018-11-12 01:28:46 +01:00
|
|
|
if (is_fifo()) {
|
|
|
|
char buf[32];
|
|
|
|
ksprintf(buf, "fifo:%x", m_fifo.ptr());
|
|
|
|
return buf;
|
|
|
|
}
|
2019-01-16 12:57:07 +01:00
|
|
|
ASSERT(m_inode);
|
|
|
|
return VFS::the().absolute_path(*m_inode);
|
2018-11-01 14:00:28 +01:00
|
|
|
}
|
2018-11-12 01:28:46 +01:00
|
|
|
|
|
|
|
FileDescriptor::FileDescriptor(FIFO& fifo, FIFO::Direction direction)
|
2018-12-03 00:39:25 +01:00
|
|
|
: m_is_blocking(true)
|
2018-11-12 01:28:46 +01:00
|
|
|
, m_fifo(fifo)
|
|
|
|
, m_fifo_direction(direction)
|
|
|
|
{
|
|
|
|
m_fifo->open(direction);
|
|
|
|
}
|
2019-01-16 12:57:07 +01:00
|
|
|
|
|
|
|
InodeMetadata FileDescriptor::metadata() const
|
|
|
|
{
|
|
|
|
if (m_inode)
|
|
|
|
return m_inode->metadata();
|
|
|
|
return { };
|
|
|
|
}
|