2018-11-07 11:37:54 +01:00
|
|
|
#include "FileDescriptor.h"
|
2019-04-03 12:25:24 +02:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
2019-04-03 12:36:40 +02:00
|
|
|
#include <Kernel/Devices/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>
|
2019-04-06 20:29:48 +02:00
|
|
|
#include <Kernel/FileSystem/FIFO.h>
|
2019-04-03 12:28:45 +02:00
|
|
|
#include <Kernel/TTY/TTY.h>
|
|
|
|
#include <Kernel/TTY/MasterPTY.h>
|
2019-04-06 20:29:48 +02:00
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-02-16 09:57:42 +01:00
|
|
|
#include <Kernel/Process.h>
|
2019-04-03 12:36:40 +02:00
|
|
|
#include <Kernel/Devices/BlockDevice.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
2019-04-09 01:10:00 +02:00
|
|
|
#include <Kernel/SharedMemory.h>
|
2018-11-07 12:05:51 +01:00
|
|
|
|
2019-02-25 12:43:52 +01:00
|
|
|
Retained<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)));
|
|
|
|
}
|
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
Retained<FileDescriptor> FileDescriptor::create(RetainPtr<File>&& file)
|
2019-01-16 12:57:07 +01:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
return adopt(*new FileDescriptor(move(file)));
|
2019-04-22 18:44:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-25 12:43:52 +01:00
|
|
|
Retained<FileDescriptor> FileDescriptor::create(RetainPtr<Socket>&& socket, SocketRole role)
|
2019-02-14 14:17:38 +01:00
|
|
|
{
|
2019-02-14 15:40:04 +01:00
|
|
|
return adopt(*new FileDescriptor(move(socket), role));
|
2019-02-14 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
2019-01-16 12:57:07 +01:00
|
|
|
FileDescriptor::FileDescriptor(RetainPtr<Inode>&& inode)
|
|
|
|
: m_inode(move(inode))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
FileDescriptor::FileDescriptor(RetainPtr<File>&& file)
|
|
|
|
: m_file(move(file))
|
2019-04-22 18:44:45 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:40:04 +01:00
|
|
|
FileDescriptor::FileDescriptor(RetainPtr<Socket>&& socket, SocketRole role)
|
2019-02-14 14:17:38 +01:00
|
|
|
: m_socket(move(socket))
|
|
|
|
{
|
2019-02-17 11:00:35 +01:00
|
|
|
set_socket_role(role);
|
2019-02-14 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 11:37:54 +01:00
|
|
|
FileDescriptor::~FileDescriptor()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2019-02-17 00:13:47 +01:00
|
|
|
if (m_socket) {
|
2019-05-03 20:15:54 +02:00
|
|
|
m_socket->detach(*this);
|
2019-02-17 00:13:47 +01:00
|
|
|
m_socket = nullptr;
|
|
|
|
}
|
2019-04-29 04:55:54 +02:00
|
|
|
if (is_fifo())
|
|
|
|
static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file) {
|
|
|
|
m_file->close();
|
|
|
|
m_file = nullptr;
|
2019-01-30 18:26:19 +01:00
|
|
|
}
|
|
|
|
m_inode = nullptr;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2019-02-17 11:00:35 +01:00
|
|
|
void FileDescriptor::set_socket_role(SocketRole role)
|
|
|
|
{
|
|
|
|
if (role == m_socket_role)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ASSERT(m_socket);
|
2019-05-03 20:15:54 +02:00
|
|
|
if (m_socket_role != SocketRole::None)
|
|
|
|
m_socket->detach(*this);
|
2019-02-17 11:00:35 +01:00
|
|
|
m_socket_role = role;
|
2019-05-03 20:15:54 +02:00
|
|
|
m_socket->attach(*this);
|
2019-02-17 11:00:35 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 12:43:52 +01:00
|
|
|
Retained<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()) {
|
2019-04-29 04:55:54 +02:00
|
|
|
descriptor = fifo()->open_direction(m_fifo_direction);
|
2018-11-12 01:28:46 +01:00
|
|
|
} else {
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file) {
|
|
|
|
descriptor = FileDescriptor::create(m_file.copy_ref());
|
2019-01-31 17:31:23 +01:00
|
|
|
descriptor->m_inode = m_inode.copy_ref();
|
2019-02-17 11:00:35 +01:00
|
|
|
} else if (m_socket) {
|
|
|
|
descriptor = FileDescriptor::create(m_socket.copy_ref(), m_socket_role);
|
|
|
|
descriptor->m_inode = m_inode.copy_ref();
|
2019-01-31 05:05:57 +01:00
|
|
|
} else {
|
2019-01-31 17:31:23 +01:00
|
|
|
descriptor = FileDescriptor::create(m_inode.copy_ref());
|
2019-01-16 12:57:07 +01:00
|
|
|
}
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
2019-02-25 12:43:52 +01:00
|
|
|
ASSERT(descriptor);
|
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;
|
2019-02-25 12:43:52 +01:00
|
|
|
return *descriptor;
|
2018-11-02 20:41:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 17:31:23 +01:00
|
|
|
bool addition_would_overflow(off_t a, off_t b)
|
2018-10-14 21:19:27 +02:00
|
|
|
{
|
|
|
|
ASSERT(a > 0);
|
|
|
|
uint64_t ua = a;
|
2019-04-03 13:18:42 +02:00
|
|
|
return (ua + b) > OFF_T_MAX;
|
2018-10-14 21:19:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 00:11:08 +01:00
|
|
|
KResult FileDescriptor::fstat(stat& buffer)
|
2018-10-14 22:57:41 +02:00
|
|
|
{
|
2018-11-12 01:28:46 +01:00
|
|
|
ASSERT(!is_fifo());
|
2019-04-28 15:02:55 +02:00
|
|
|
if (!m_inode && !m_file)
|
2019-03-02 00:11:08 +01:00
|
|
|
return KResult(-EBADF);
|
2018-10-14 22:57:41 +02:00
|
|
|
|
2019-01-16 12:57:07 +01:00
|
|
|
auto metadata = this->metadata();
|
2019-01-31 17:31:23 +01:00
|
|
|
if (!metadata.is_valid())
|
2019-03-02 00:11:08 +01:00
|
|
|
return KResult(-EIO);
|
|
|
|
|
|
|
|
buffer.st_rdev = encoded_device(metadata.major_device, metadata.minor_device);
|
|
|
|
buffer.st_ino = metadata.inode.index();
|
|
|
|
buffer.st_mode = metadata.mode;
|
|
|
|
buffer.st_nlink = metadata.link_count;
|
|
|
|
buffer.st_uid = metadata.uid;
|
|
|
|
buffer.st_gid = metadata.gid;
|
|
|
|
buffer.st_dev = 0; // FIXME
|
|
|
|
buffer.st_size = metadata.size;
|
|
|
|
buffer.st_blksize = metadata.block_size;
|
|
|
|
buffer.st_blocks = metadata.block_count;
|
|
|
|
buffer.st_atime = metadata.atime;
|
|
|
|
buffer.st_mtime = metadata.mtime;
|
|
|
|
buffer.st_ctime = metadata.ctime;
|
|
|
|
return KSuccess;
|
2018-10-14 22:57:41 +02:00
|
|
|
}
|
|
|
|
|
2019-03-01 10:39:19 +01:00
|
|
|
KResult FileDescriptor::fchmod(mode_t mode)
|
|
|
|
{
|
|
|
|
if (!m_inode)
|
|
|
|
return KResult(-EBADF);
|
|
|
|
return VFS::the().chmod(*m_inode, mode);
|
|
|
|
}
|
|
|
|
|
2019-01-23 06:53:01 +01:00
|
|
|
off_t FileDescriptor::seek(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-04-28 15:02:55 +02:00
|
|
|
if (!m_inode && !m_file)
|
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();
|
2019-01-31 17:31:23 +01:00
|
|
|
if (!metadata.is_valid())
|
2018-10-14 22:57:41 +02:00
|
|
|
return -EIO;
|
|
|
|
|
2019-01-31 17:31:23 +01:00
|
|
|
if (metadata.is_socket() || metadata.is_fifo())
|
2018-10-14 21:19:27 +02:00
|
|
|
return -ESPIPE;
|
|
|
|
|
2019-01-23 06:53:01 +01:00
|
|
|
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-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-04-29 13:58:40 +02:00
|
|
|
ssize_t FileDescriptor::read(byte* buffer, ssize_t count)
|
2018-10-14 21:19:27 +02:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file) {
|
2019-04-29 13:58:40 +02:00
|
|
|
int nread = m_file->read(*this, buffer, count);
|
2019-04-28 15:02:55 +02:00
|
|
|
if (!m_file->is_seekable())
|
|
|
|
m_current_offset += nread;
|
|
|
|
return nread;
|
2018-10-14 21:19:27 +02:00
|
|
|
}
|
2019-02-14 16:01:08 +01:00
|
|
|
if (m_socket)
|
2019-05-03 20:15:54 +02:00
|
|
|
return m_socket->read(*this, buffer, count);
|
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-04-29 13:58:40 +02:00
|
|
|
ssize_t FileDescriptor::write(const byte* data, ssize_t size)
|
2018-10-30 15:33:37 +01:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file) {
|
2019-04-29 13:58:40 +02:00
|
|
|
int nwritten = m_file->write(*this, data, size);
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file->is_seekable())
|
|
|
|
m_current_offset += nwritten;
|
|
|
|
return nwritten;
|
2018-10-30 15:33:37 +01:00
|
|
|
}
|
2019-02-14 16:01:08 +01:00
|
|
|
if (m_socket)
|
2019-05-03 20:15:54 +02:00
|
|
|
return m_socket->write(*this, data, size);
|
2019-01-18 15:01:40 +01:00
|
|
|
ASSERT(m_inode);
|
2019-01-23 04:29:56 +01:00
|
|
|
ssize_t nwritten = m_inode->write_bytes(m_current_offset, size, data, this);
|
2019-01-18 15:01:40 +01:00
|
|
|
m_current_offset += nwritten;
|
|
|
|
return nwritten;
|
2018-10-30 15:33:37 +01:00
|
|
|
}
|
|
|
|
|
2019-04-29 13:58:40 +02:00
|
|
|
bool FileDescriptor::can_write()
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file)
|
2019-04-29 13:58:40 +02:00
|
|
|
return m_file->can_write(*this);
|
2019-02-14 16:01:08 +01:00
|
|
|
if (m_socket)
|
2019-05-03 20:15:54 +02:00
|
|
|
return m_socket->can_write(*this);
|
2018-11-12 01:28:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-29 13:58:40 +02:00
|
|
|
bool FileDescriptor::can_read()
|
2018-10-25 13:07:59 +02:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file)
|
2019-04-29 13:58:40 +02:00
|
|
|
return m_file->can_read(*this);
|
2019-02-14 16:01:08 +01:00
|
|
|
if (m_socket)
|
2019-05-03 20:15:54 +02:00
|
|
|
return m_socket->can_read(*this);
|
2018-10-25 13:07:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-29 13:58:40 +02:00
|
|
|
ByteBuffer FileDescriptor::read_entire_file()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
// HACK ALERT: (This entire function)
|
|
|
|
|
2018-11-12 01:28:46 +01:00
|
|
|
ASSERT(!is_fifo());
|
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file) {
|
2018-12-21 02:10:45 +01:00
|
|
|
auto buffer = ByteBuffer::create_uninitialized(1024);
|
2019-04-29 13:58:40 +02:00
|
|
|
ssize_t nread = m_file->read(*this, buffer.pointer(), buffer.size());
|
2019-02-25 21:19:57 +01:00
|
|
|
ASSERT(nread >= 0);
|
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-31 17:31:23 +01:00
|
|
|
return metadata().is_directory();
|
2018-10-26 14:24:11 +02:00
|
|
|
}
|
|
|
|
|
2019-02-25 21:19:57 +01:00
|
|
|
ssize_t FileDescriptor::get_dir_entries(byte* buffer, ssize_t size)
|
2018-10-24 12:43:52 +02:00
|
|
|
{
|
2019-01-16 12:57:07 +01:00
|
|
|
auto metadata = this->metadata();
|
2019-01-31 17:31:23 +01:00
|
|
|
if (!metadata.is_valid())
|
2018-10-24 12:43:52 +02:00
|
|
|
return -EIO;
|
2019-01-31 17:31:23 +01:00
|
|
|
if (!metadata.is_directory())
|
2018-10-24 12:43:52 +02:00
|
|
|
return -ENOTDIR;
|
|
|
|
|
2019-03-20 18:31:12 +01:00
|
|
|
int size_to_allocate = max(PAGE_SIZE, metadata.size);
|
|
|
|
|
|
|
|
auto temp_buffer = ByteBuffer::create_uninitialized(size_to_allocate);
|
2019-01-31 17:31:23 +01:00
|
|
|
BufferStream stream(temp_buffer);
|
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();
|
2019-01-31 17:31:23 +01:00
|
|
|
stream << (byte)entry.file_type;
|
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;
|
|
|
|
});
|
2019-03-20 18:31:12 +01:00
|
|
|
stream.snip();
|
2018-10-24 12:43:52 +02:00
|
|
|
|
2019-03-20 18:31:12 +01:00
|
|
|
if (size < temp_buffer.size())
|
2018-10-24 14:28:22 +02:00
|
|
|
return -1;
|
|
|
|
|
2019-03-20 18:31:12 +01:00
|
|
|
memcpy(buffer, temp_buffer.pointer(), temp_buffer.size());
|
2018-10-24 12:43:52 +02:00
|
|
|
return stream.offset();
|
|
|
|
}
|
2019-01-16 12:57:07 +01:00
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
bool FileDescriptor::is_device() const
|
|
|
|
{
|
|
|
|
return m_file && m_file->is_device();
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:39:25 +01:00
|
|
|
bool FileDescriptor::is_tty() const
|
2018-10-30 22:03:02 +01:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
return m_file && m_file->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-04-28 15:02:55 +02:00
|
|
|
return static_cast<const TTY*>(m_file.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-04-28 15:02:55 +02:00
|
|
|
return static_cast<TTY*>(m_file.ptr());
|
2018-11-02 13:14:25 +01:00
|
|
|
}
|
2019-01-15 06:30:19 +01:00
|
|
|
|
|
|
|
bool FileDescriptor::is_master_pty() const
|
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
return m_file && m_file->is_master_pty();
|
2019-01-15 06:30:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const MasterPTY* FileDescriptor::master_pty() const
|
|
|
|
{
|
|
|
|
if (!is_master_pty())
|
|
|
|
return nullptr;
|
2019-04-28 15:02:55 +02:00
|
|
|
return static_cast<const MasterPTY*>(m_file.ptr());
|
2019-01-15 06:30:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MasterPTY* FileDescriptor::master_pty()
|
|
|
|
{
|
|
|
|
if (!is_master_pty())
|
|
|
|
return nullptr;
|
2019-04-28 15:02:55 +02:00
|
|
|
return static_cast<MasterPTY*>(m_file.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;
|
|
|
|
}
|
|
|
|
|
2019-02-17 09:40:52 +01:00
|
|
|
const char* to_string(SocketRole role)
|
|
|
|
{
|
|
|
|
switch (role) {
|
|
|
|
case SocketRole::Listener:
|
|
|
|
return "Listener";
|
|
|
|
case SocketRole::Accepted:
|
|
|
|
return "Accepted";
|
|
|
|
case SocketRole::Connected:
|
|
|
|
return "Connected";
|
|
|
|
default:
|
|
|
|
return "None";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
bool FileDescriptor::is_fsfile() const
|
2019-04-09 01:10:00 +02:00
|
|
|
{
|
|
|
|
return !is_tty() && !is_fifo() && !is_device() && !is_socket() && !is_shared_memory();
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:30:13 +01:00
|
|
|
KResultOr<String> FileDescriptor::absolute_path()
|
2018-11-01 14:00:28 +01:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file)
|
|
|
|
return m_file->absolute_path();
|
2019-02-17 01:11:37 +01:00
|
|
|
if (is_socket())
|
2019-02-17 09:40:52 +01:00
|
|
|
return String::format("socket:%x (role: %s)", m_socket.ptr(), to_string(m_socket_role));
|
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
|
|
|
|
2019-01-16 12:57:07 +01:00
|
|
|
InodeMetadata FileDescriptor::metadata() const
|
|
|
|
{
|
|
|
|
if (m_inode)
|
|
|
|
return m_inode->metadata();
|
|
|
|
return { };
|
|
|
|
}
|
2019-02-16 09:57:42 +01:00
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
KResultOr<Region*> FileDescriptor::mmap(Process& process, LinearAddress laddr, size_t offset, size_t size, int prot)
|
2019-02-16 09:57:42 +01:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
if (m_file)
|
|
|
|
return m_file->mmap(process, laddr, offset, size);
|
2019-02-16 09:57:42 +01:00
|
|
|
|
2019-04-09 02:37:05 +02:00
|
|
|
if (is_shared_memory()) {
|
|
|
|
if (!shared_memory()->vmo())
|
2019-04-28 15:02:55 +02:00
|
|
|
return KResult(-ENODEV);
|
2019-04-09 02:37:05 +02:00
|
|
|
return process.allocate_region_with_vmo(laddr, size, *shared_memory()->vmo(), offset, shared_memory()->name(), true, true);
|
|
|
|
}
|
|
|
|
|
2019-04-28 15:02:55 +02:00
|
|
|
if (!is_fsfile())
|
|
|
|
return KResult(-ENODEV);
|
|
|
|
|
2019-02-16 09:57:42 +01:00
|
|
|
ASSERT(m_inode);
|
|
|
|
// FIXME: If PROT_EXEC, check that the underlying file system isn't mounted noexec.
|
2019-02-27 00:02:01 +01:00
|
|
|
String region_name;
|
|
|
|
#if 0
|
|
|
|
// FIXME: I would like to do this, but it would instantiate all the damn inodes.
|
|
|
|
region_name = absolute_path();
|
|
|
|
#else
|
|
|
|
region_name = "Memory-mapped file";
|
|
|
|
#endif
|
2019-02-16 09:57:42 +01:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
// FIXME: Implement mapping at a client-specified address. Most of the support is already in plcae.
|
|
|
|
ASSERT(laddr.as_ptr() == nullptr);
|
|
|
|
auto* region = process.allocate_file_backed_region(LinearAddress(), size, inode(), move(region_name), prot & PROT_READ, prot & PROT_WRITE);
|
2019-02-17 00:13:47 +01:00
|
|
|
region->page_in();
|
2019-02-16 09:57:42 +01:00
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2019-04-09 01:10:00 +02:00
|
|
|
KResult FileDescriptor::truncate(off_t length)
|
|
|
|
{
|
|
|
|
if (is_file()) {
|
|
|
|
return m_inode->truncate(length);
|
|
|
|
}
|
|
|
|
ASSERT(is_shared_memory());
|
|
|
|
return shared_memory()->truncate(length);
|
|
|
|
}
|
2019-04-28 22:31:31 +02:00
|
|
|
|
|
|
|
bool FileDescriptor::is_shared_memory() const
|
|
|
|
{
|
|
|
|
return m_file && m_file->is_shared_memory();
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedMemory* FileDescriptor::shared_memory()
|
|
|
|
{
|
|
|
|
if (!is_shared_memory())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<SharedMemory*>(m_file.ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
const SharedMemory* FileDescriptor::shared_memory() const
|
|
|
|
{
|
|
|
|
if (!is_shared_memory())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<const SharedMemory*>(m_file.ptr());
|
|
|
|
}
|
2019-04-29 04:55:54 +02:00
|
|
|
|
|
|
|
bool FileDescriptor::is_fifo() const
|
|
|
|
{
|
|
|
|
return m_file && m_file->is_fifo();
|
|
|
|
}
|
|
|
|
|
|
|
|
FIFO* FileDescriptor::fifo()
|
|
|
|
{
|
|
|
|
if (!is_fifo())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<FIFO*>(m_file.ptr());
|
|
|
|
}
|