2018-10-24 12:43:52 +02:00
|
|
|
#include <AK/BufferStream.h>
|
2019-05-30 13:39:17 +02:00
|
|
|
#include <Kernel/Devices/BlockDevice.h>
|
|
|
|
#include <Kernel/Devices/CharacterDevice.h>
|
2019-05-30 18:58:59 +02:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2019-04-06 20:29:48 +02:00
|
|
|
#include <Kernel/FileSystem/FIFO.h>
|
2019-06-07 09:36:51 +02:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-05-30 13:39:17 +02:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
|
|
|
#include <Kernel/FileSystem/InodeFile.h>
|
2019-07-09 14:50:01 +02:00
|
|
|
#include <Kernel/FileSystem/SharedMemory.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-05-30 13:39:17 +02:00
|
|
|
#include <Kernel/TTY/MasterPTY.h>
|
|
|
|
#include <Kernel/TTY/TTY.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
#include <LibC/errno_numbers.h>
|
2018-11-07 12:05:51 +01:00
|
|
|
|
2019-08-11 09:32:21 +02:00
|
|
|
NonnullRefPtr<FileDescription> FileDescription::create(Custody& custody)
|
2018-11-05 19:01:22 +01:00
|
|
|
{
|
2019-08-11 09:32:21 +02:00
|
|
|
auto description = adopt(*new FileDescription(InodeFile::create(custody.inode())));
|
|
|
|
description->m_custody = custody;
|
2019-06-13 22:03:04 +02:00
|
|
|
return description;
|
2019-01-16 12:57:07 +01:00
|
|
|
}
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
NonnullRefPtr<FileDescription> FileDescription::create(File& file)
|
2019-01-16 12:57:07 +01:00
|
|
|
{
|
2019-08-11 16:38:20 +03:00
|
|
|
return adopt(*new FileDescription(file));
|
2019-02-14 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
FileDescription::FileDescription(File& file)
|
2019-08-11 09:32:21 +02:00
|
|
|
: m_file(file)
|
2019-02-14 14:17:38 +01:00
|
|
|
{
|
2019-08-11 09:32:21 +02:00
|
|
|
if (file.is_inode())
|
|
|
|
m_inode = static_cast<InodeFile&>(file).inode();
|
2019-08-11 16:28:18 +03:00
|
|
|
if (is_socket())
|
|
|
|
socket()->attach(*this);
|
2019-10-25 09:22:22 +02:00
|
|
|
m_is_directory = metadata().is_directory();
|
2019-02-14 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
FileDescription::~FileDescription()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2019-05-03 20:42:43 +02:00
|
|
|
if (is_socket())
|
|
|
|
socket()->detach(*this);
|
2019-04-29 04:55:54 +02:00
|
|
|
if (is_fifo())
|
|
|
|
static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
|
2019-05-30 13:39:17 +02:00
|
|
|
m_file->close();
|
2019-01-30 18:26:19 +01:00
|
|
|
m_inode = nullptr;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
KResult FileDescription::fstat(stat& buffer)
|
2018-10-14 22:57:41 +02:00
|
|
|
{
|
2019-09-17 21:39:44 +03:00
|
|
|
if (is_fifo()) {
|
|
|
|
memset(&buffer, 0, sizeof(buffer));
|
|
|
|
buffer.st_mode = 001000;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:39:17 +02:00
|
|
|
if (!m_inode)
|
2019-03-02 00:11:08 +01:00
|
|
|
return KResult(-EBADF);
|
2019-06-01 18:46:10 +02:00
|
|
|
return metadata().stat(buffer);
|
2018-10-14 22:57:41 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
KResult FileDescription::fchmod(mode_t mode)
|
2019-03-01 10:39:19 +01:00
|
|
|
{
|
|
|
|
if (!m_inode)
|
|
|
|
return KResult(-EBADF);
|
2019-06-02 12:36:38 +02:00
|
|
|
return VFS::the().chmod(*m_inode, mode);
|
2019-03-01 10:39:19 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
off_t FileDescription::seek(off_t offset, int whence)
|
2018-10-14 21:19:27 +02:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
if (!m_file->is_seekable())
|
|
|
|
return -EINVAL;
|
2018-10-14 21:19:27 +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-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-05-30 13:39:17 +02:00
|
|
|
off_t new_offset;
|
2018-10-14 21:19:27 +02:00
|
|
|
|
2018-10-14 22:57:41 +02:00
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET:
|
2019-05-30 13:39:17 +02:00
|
|
|
new_offset = offset;
|
2018-10-14 21:19:27 +02:00
|
|
|
break;
|
2018-10-14 22:57:41 +02:00
|
|
|
case SEEK_CUR:
|
2019-05-30 13:39:17 +02:00
|
|
|
new_offset = m_current_offset + offset;
|
2018-10-14 21:19:27 +02:00
|
|
|
break;
|
2018-10-14 22:57:41 +02:00
|
|
|
case SEEK_END:
|
2019-05-30 13:39:17 +02:00
|
|
|
new_offset = metadata.size;
|
2018-10-14 21:19:27 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:39:17 +02:00
|
|
|
if (new_offset < 0)
|
2019-05-16 15:44:01 +02:00
|
|
|
return -EINVAL;
|
2019-05-18 21:54:31 +02:00
|
|
|
// FIXME: Return -EINVAL if attempting to seek past the end of a seekable device.
|
2019-05-16 15:44:01 +02:00
|
|
|
|
2019-05-30 13:39:17 +02:00
|
|
|
m_current_offset = new_offset;
|
2018-12-03 00:39:25 +01:00
|
|
|
return m_current_offset;
|
2018-10-14 21:19:27 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t FileDescription::read(u8* buffer, ssize_t count)
|
2018-10-14 21:19:27 +02:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
int nread = m_file->read(*this, buffer, count);
|
2019-11-04 13:57:04 +01:00
|
|
|
if (nread > 0 && m_file->is_seekable())
|
2019-05-30 13:39:17 +02:00
|
|
|
m_current_offset += nread;
|
2018-10-14 21:19:27 +02:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t FileDescription::write(const u8* data, ssize_t size)
|
2018-10-30 15:33:37 +01:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
int nwritten = m_file->write(*this, data, size);
|
2019-11-04 13:57:04 +01:00
|
|
|
if (nwritten > 0 && m_file->is_seekable())
|
2019-05-30 13:39:17 +02:00
|
|
|
m_current_offset += nwritten;
|
2019-01-18 15:01:40 +01:00
|
|
|
return nwritten;
|
2018-10-30 15:33:37 +01:00
|
|
|
}
|
|
|
|
|
2019-07-19 13:19:47 +02:00
|
|
|
bool FileDescription::can_write() const
|
2018-11-12 01:28:46 +01:00
|
|
|
{
|
2019-11-04 14:03:14 +01:00
|
|
|
return m_file->can_write(*this);
|
2018-11-12 01:28:46 +01:00
|
|
|
}
|
|
|
|
|
2019-07-19 13:19:47 +02:00
|
|
|
bool FileDescription::can_read() const
|
2018-10-25 13:07:59 +02:00
|
|
|
{
|
2019-11-04 14:03:14 +01:00
|
|
|
return m_file->can_read(*this);
|
2018-10-25 13:07:59 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
ByteBuffer FileDescription::read_entire_file()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2019-04-28 15:02:55 +02:00
|
|
|
// HACK ALERT: (This entire function)
|
2019-05-30 13:39:17 +02:00
|
|
|
ASSERT(m_file->is_inode());
|
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-10-26 14:24:11 +02:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size)
|
2018-10-24 12:43:52 +02:00
|
|
|
{
|
2019-10-25 09:22:22 +02:00
|
|
|
if (!is_directory())
|
|
|
|
return -ENOTDIR;
|
|
|
|
|
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-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-06-07 11:43:58 +02:00
|
|
|
VFS::the().traverse_directory_inode(*m_inode, [&stream](auto& entry) {
|
2019-07-03 21:17:35 +02:00
|
|
|
stream << (u32)entry.inode.index();
|
|
|
|
stream << (u8)entry.file_type;
|
|
|
|
stream << (u32)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-09-30 08:57:01 +02:00
|
|
|
memcpy(buffer, temp_buffer.data(), temp_buffer.size());
|
2018-10-24 12:43:52 +02:00
|
|
|
return stream.offset();
|
|
|
|
}
|
2019-01-16 12:57:07 +01:00
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
bool FileDescription::is_device() const
|
2019-04-28 15:02:55 +02:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
return m_file->is_device();
|
2019-04-28 15:02:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
bool FileDescription::is_tty() const
|
2018-10-30 22:03:02 +01:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
return m_file->is_tty();
|
2018-10-30 22:03:02 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
const TTY* FileDescription::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
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
TTY* FileDescription::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
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
bool FileDescription::is_master_pty() const
|
2019-01-15 06:30:19 +01:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
return m_file->is_master_pty();
|
2019-01-15 06:30:19 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
const MasterPTY* FileDescription::master_pty() const
|
2019-01-15 06:30:19 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
MasterPTY* FileDescription::master_pty()
|
2019-01-15 06:30:19 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
int FileDescription::close()
|
2018-11-01 14:00:28 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
String FileDescription::absolute_path() const
|
2018-11-01 14:00:28 +01:00
|
|
|
{
|
2019-05-30 18:58:59 +02:00
|
|
|
if (m_custody)
|
|
|
|
return m_custody->absolute_path();
|
2019-05-30 13:39:17 +02:00
|
|
|
return m_file->absolute_path(*this);
|
2018-11-01 14:00:28 +01:00
|
|
|
}
|
2018-11-12 01:28:46 +01:00
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
InodeMetadata FileDescription::metadata() const
|
2019-01-16 12:57:07 +01:00
|
|
|
{
|
|
|
|
if (m_inode)
|
|
|
|
return m_inode->metadata();
|
2019-06-07 11:43:58 +02:00
|
|
|
return {};
|
2019-01-16 12:57:07 +01:00
|
|
|
}
|
2019-02-16 09:57:42 +01:00
|
|
|
|
2019-06-07 12:56:50 +02:00
|
|
|
KResultOr<Region*> FileDescription::mmap(Process& process, VirtualAddress vaddr, size_t offset, size_t size, int prot)
|
2019-02-16 09:57:42 +01:00
|
|
|
{
|
2019-06-07 12:56:50 +02:00
|
|
|
return m_file->mmap(process, *this, vaddr, offset, size, prot);
|
2019-02-16 09:57:42 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
KResult FileDescription::truncate(off_t length)
|
2019-04-09 01:10:00 +02:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
return m_file->truncate(length);
|
2019-04-09 01:10:00 +02:00
|
|
|
}
|
2019-04-28 22:31:31 +02:00
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
bool FileDescription::is_shared_memory() const
|
2019-04-28 22:31:31 +02:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
return m_file->is_shared_memory();
|
2019-04-28 22:31:31 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
SharedMemory* FileDescription::shared_memory()
|
2019-04-28 22:31:31 +02:00
|
|
|
{
|
|
|
|
if (!is_shared_memory())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<SharedMemory*>(m_file.ptr());
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
const SharedMemory* FileDescription::shared_memory() const
|
2019-04-28 22:31:31 +02:00
|
|
|
{
|
|
|
|
if (!is_shared_memory())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<const SharedMemory*>(m_file.ptr());
|
|
|
|
}
|
2019-04-29 04:55:54 +02:00
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
bool FileDescription::is_fifo() const
|
2019-04-29 04:55:54 +02:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
return m_file->is_fifo();
|
2019-04-29 04:55:54 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
FIFO* FileDescription::fifo()
|
2019-04-29 04:55:54 +02:00
|
|
|
{
|
|
|
|
if (!is_fifo())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<FIFO*>(m_file.ptr());
|
|
|
|
}
|
2019-05-03 20:42:43 +02:00
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
bool FileDescription::is_socket() const
|
2019-05-03 20:42:43 +02:00
|
|
|
{
|
2019-05-30 13:39:17 +02:00
|
|
|
return m_file->is_socket();
|
2019-05-03 20:42:43 +02:00
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
Socket* FileDescription::socket()
|
2019-05-03 20:42:43 +02:00
|
|
|
{
|
|
|
|
if (!is_socket())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<Socket*>(m_file.ptr());
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
const Socket* FileDescription::socket() const
|
2019-05-03 20:42:43 +02:00
|
|
|
{
|
|
|
|
if (!is_socket())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<const Socket*>(m_file.ptr());
|
|
|
|
}
|
2019-05-30 15:37:51 +02:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
void FileDescription::set_file_flags(u32 flags)
|
2019-05-30 15:37:51 +02:00
|
|
|
{
|
|
|
|
m_is_blocking = !(flags & O_NONBLOCK);
|
|
|
|
m_should_append = flags & O_APPEND;
|
|
|
|
m_file_flags = flags;
|
|
|
|
}
|
2019-06-01 20:31:36 +02:00
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
KResult FileDescription::chown(uid_t uid, gid_t gid)
|
2019-06-01 20:31:36 +02:00
|
|
|
{
|
|
|
|
if (!m_inode)
|
|
|
|
return KResult(-EINVAL);
|
2019-06-02 12:30:24 +02:00
|
|
|
return VFS::the().chown(*m_inode, uid, gid);
|
2019-06-01 20:31:36 +02:00
|
|
|
}
|