2018-10-24 13:43:52 +03:00
|
|
|
#include <AK/BufferStream.h>
|
2019-05-30 14:39:17 +03:00
|
|
|
#include <Kernel/Devices/BlockDevice.h>
|
|
|
|
#include <Kernel/Devices/CharacterDevice.h>
|
2019-04-06 21:29:48 +03:00
|
|
|
#include <Kernel/FileSystem/FIFO.h>
|
2019-05-30 14:39:17 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescriptor.h>
|
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
|
|
|
#include <Kernel/FileSystem/InodeFile.h>
|
2019-04-06 21:29:48 +03:00
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-02-16 11:57:42 +03:00
|
|
|
#include <Kernel/Process.h>
|
2019-04-09 02:10:00 +03:00
|
|
|
#include <Kernel/SharedMemory.h>
|
2019-05-30 14:39:17 +03: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 14:05:51 +03:00
|
|
|
|
2019-02-25 14:43:52 +03:00
|
|
|
Retained<FileDescriptor> FileDescriptor::create(RetainPtr<Inode>&& inode)
|
2018-11-05 21:01:22 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return adopt(*new FileDescriptor(InodeFile::create(*inode)));
|
2019-01-16 14:57:07 +03:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:42:43 +03:00
|
|
|
Retained<FileDescriptor> FileDescriptor::create(RetainPtr<File>&& file, SocketRole role)
|
2019-01-16 14:57:07 +03:00
|
|
|
{
|
2019-05-03 21:42:43 +03:00
|
|
|
return adopt(*new FileDescriptor(move(file), role));
|
2019-02-14 16:17:38 +03:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:42:43 +03:00
|
|
|
FileDescriptor::FileDescriptor(RetainPtr<File>&& file, SocketRole role)
|
2019-04-28 16:02:55 +03:00
|
|
|
: m_file(move(file))
|
2019-02-14 16:17:38 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
if (m_file->is_inode())
|
|
|
|
m_inode = static_cast<InodeFile&>(*m_file).inode();
|
2019-02-17 13:00:35 +03:00
|
|
|
set_socket_role(role);
|
2019-02-14 16:17:38 +03:00
|
|
|
}
|
|
|
|
|
2018-11-07 13:37:54 +03:00
|
|
|
FileDescriptor::~FileDescriptor()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-05-03 21:42:43 +03:00
|
|
|
if (is_socket())
|
|
|
|
socket()->detach(*this);
|
2019-04-29 05:55:54 +03:00
|
|
|
if (is_fifo())
|
|
|
|
static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
|
2019-05-30 14:39:17 +03:00
|
|
|
m_file->close();
|
|
|
|
m_file = nullptr;
|
2019-01-30 20:26:19 +03:00
|
|
|
m_inode = nullptr;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:00:35 +03:00
|
|
|
void FileDescriptor::set_socket_role(SocketRole role)
|
|
|
|
{
|
|
|
|
if (role == m_socket_role)
|
|
|
|
return;
|
|
|
|
|
2019-05-03 21:42:43 +03:00
|
|
|
ASSERT(is_socket());
|
2019-05-03 21:15:54 +03:00
|
|
|
if (m_socket_role != SocketRole::None)
|
2019-05-03 21:42:43 +03:00
|
|
|
socket()->detach(*this);
|
2019-02-17 13:00:35 +03:00
|
|
|
m_socket_role = role;
|
2019-05-03 21:42:43 +03:00
|
|
|
socket()->attach(*this);
|
2019-02-17 13:00:35 +03:00
|
|
|
}
|
|
|
|
|
2019-02-25 14:43:52 +03:00
|
|
|
Retained<FileDescriptor> FileDescriptor::clone()
|
2018-11-02 22:41:58 +03:00
|
|
|
{
|
2018-11-12 03:28:46 +03:00
|
|
|
RetainPtr<FileDescriptor> descriptor;
|
|
|
|
if (is_fifo()) {
|
2019-04-29 05:55:54 +03:00
|
|
|
descriptor = fifo()->open_direction(m_fifo_direction);
|
2018-11-12 03:28:46 +03:00
|
|
|
} else {
|
2019-05-30 14:39:17 +03:00
|
|
|
descriptor = FileDescriptor::create(m_file.copy_ref(), m_socket_role);
|
|
|
|
descriptor->m_inode = m_inode.copy_ref();
|
2018-11-12 03:28:46 +03:00
|
|
|
}
|
2019-02-25 14:43:52 +03:00
|
|
|
ASSERT(descriptor);
|
2018-12-03 02:39:25 +03:00
|
|
|
descriptor->m_current_offset = m_current_offset;
|
|
|
|
descriptor->m_is_blocking = m_is_blocking;
|
2019-05-26 02:18:04 +03:00
|
|
|
descriptor->m_should_append = m_should_append;
|
2018-11-12 03:28:46 +03:00
|
|
|
descriptor->m_file_flags = m_file_flags;
|
2019-02-25 14:43:52 +03:00
|
|
|
return *descriptor;
|
2018-11-02 22:41:58 +03:00
|
|
|
}
|
|
|
|
|
2019-03-02 02:11:08 +03:00
|
|
|
KResult FileDescriptor::fstat(stat& buffer)
|
2018-10-14 23:57:41 +03:00
|
|
|
{
|
2018-11-12 03:28:46 +03:00
|
|
|
ASSERT(!is_fifo());
|
2019-05-30 14:39:17 +03:00
|
|
|
if (!m_inode)
|
2019-03-02 02:11:08 +03:00
|
|
|
return KResult(-EBADF);
|
2018-10-14 23:57:41 +03:00
|
|
|
|
2019-01-16 14:57:07 +03:00
|
|
|
auto metadata = this->metadata();
|
2019-01-31 19:31:23 +03:00
|
|
|
if (!metadata.is_valid())
|
2019-03-02 02:11:08 +03: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 23:57:41 +03:00
|
|
|
}
|
|
|
|
|
2019-03-01 12:39:19 +03:00
|
|
|
KResult FileDescriptor::fchmod(mode_t mode)
|
|
|
|
{
|
|
|
|
if (!m_inode)
|
|
|
|
return KResult(-EBADF);
|
|
|
|
return VFS::the().chmod(*m_inode, mode);
|
|
|
|
}
|
|
|
|
|
2019-01-23 08:53:01 +03:00
|
|
|
off_t FileDescriptor::seek(off_t offset, int whence)
|
2018-10-14 22:19:27 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
if (!m_file->is_seekable())
|
|
|
|
return -EINVAL;
|
2018-10-14 22:19:27 +03:00
|
|
|
|
2019-01-16 14:57:07 +03:00
|
|
|
auto metadata = this->metadata();
|
2019-01-31 19:31:23 +03:00
|
|
|
if (!metadata.is_valid())
|
2018-10-14 23:57:41 +03:00
|
|
|
return -EIO;
|
|
|
|
|
2019-01-31 19:31:23 +03:00
|
|
|
if (metadata.is_socket() || metadata.is_fifo())
|
2018-10-14 22:19:27 +03:00
|
|
|
return -ESPIPE;
|
|
|
|
|
2019-05-30 14:39:17 +03:00
|
|
|
off_t new_offset;
|
2018-10-14 22:19:27 +03:00
|
|
|
|
2018-10-14 23:57:41 +03:00
|
|
|
switch (whence) {
|
|
|
|
case SEEK_SET:
|
2019-05-30 14:39:17 +03:00
|
|
|
new_offset = offset;
|
2018-10-14 22:19:27 +03:00
|
|
|
break;
|
2018-10-14 23:57:41 +03:00
|
|
|
case SEEK_CUR:
|
2019-05-30 14:39:17 +03:00
|
|
|
new_offset = m_current_offset + offset;
|
2018-10-14 22:19:27 +03:00
|
|
|
break;
|
2018-10-14 23:57:41 +03:00
|
|
|
case SEEK_END:
|
2019-05-30 14:39:17 +03:00
|
|
|
new_offset = metadata.size;
|
2018-10-14 22:19:27 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-05-30 14:39:17 +03:00
|
|
|
if (new_offset < 0)
|
2019-05-16 16:44:01 +03:00
|
|
|
return -EINVAL;
|
2019-05-18 22:54:31 +03:00
|
|
|
// FIXME: Return -EINVAL if attempting to seek past the end of a seekable device.
|
2019-05-16 16:44:01 +03:00
|
|
|
|
2019-05-30 14:39:17 +03:00
|
|
|
m_current_offset = new_offset;
|
2018-12-03 02:39:25 +03:00
|
|
|
return m_current_offset;
|
2018-10-14 22:19:27 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 14:58:40 +03:00
|
|
|
ssize_t FileDescriptor::read(byte* buffer, ssize_t count)
|
2018-10-14 22:19:27 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
int nread = m_file->read(*this, buffer, count);
|
|
|
|
if (m_file->is_seekable())
|
|
|
|
m_current_offset += nread;
|
2018-10-14 22:19:27 +03:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2019-04-29 14:58:40 +03:00
|
|
|
ssize_t FileDescriptor::write(const byte* data, ssize_t size)
|
2018-10-30 17:33:37 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
int nwritten = m_file->write(*this, data, size);
|
|
|
|
if (m_file->is_seekable())
|
|
|
|
m_current_offset += nwritten;
|
2019-01-18 17:01:40 +03:00
|
|
|
return nwritten;
|
2018-10-30 17:33:37 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 14:58:40 +03:00
|
|
|
bool FileDescriptor::can_write()
|
2018-11-12 03:28:46 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->can_write(*this);
|
2018-11-12 03:28:46 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 14:58:40 +03:00
|
|
|
bool FileDescriptor::can_read()
|
2018-10-25 14:07:59 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->can_read(*this);
|
2018-10-25 14:07:59 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 14:58:40 +03:00
|
|
|
ByteBuffer FileDescriptor::read_entire_file()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-04-28 16:02:55 +03:00
|
|
|
// HACK ALERT: (This entire function)
|
2019-05-30 14:39:17 +03:00
|
|
|
ASSERT(m_file->is_inode());
|
2019-01-16 14:57:07 +03:00
|
|
|
ASSERT(m_inode);
|
|
|
|
return m_inode->read_entire(this);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 02:39:25 +03:00
|
|
|
bool FileDescriptor::is_directory() const
|
2018-10-26 15:24:11 +03:00
|
|
|
{
|
2018-11-12 03:28:46 +03:00
|
|
|
ASSERT(!is_fifo());
|
2019-01-31 19:31:23 +03:00
|
|
|
return metadata().is_directory();
|
2018-10-26 15:24:11 +03:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:19:57 +03:00
|
|
|
ssize_t FileDescriptor::get_dir_entries(byte* buffer, ssize_t size)
|
2018-10-24 13:43:52 +03:00
|
|
|
{
|
2019-01-16 14:57:07 +03:00
|
|
|
auto metadata = this->metadata();
|
2019-01-31 19:31:23 +03:00
|
|
|
if (!metadata.is_valid())
|
2018-10-24 13:43:52 +03:00
|
|
|
return -EIO;
|
2019-01-31 19:31:23 +03:00
|
|
|
if (!metadata.is_directory())
|
2018-10-24 13:43:52 +03:00
|
|
|
return -ENOTDIR;
|
|
|
|
|
2019-03-20 20:31:12 +03:00
|
|
|
int size_to_allocate = max(PAGE_SIZE, metadata.size);
|
|
|
|
|
|
|
|
auto temp_buffer = ByteBuffer::create_uninitialized(size_to_allocate);
|
2019-01-31 19:31:23 +03:00
|
|
|
BufferStream stream(temp_buffer);
|
2019-01-16 14:57:07 +03:00
|
|
|
VFS::the().traverse_directory_inode(*m_inode, [&stream] (auto& entry) {
|
2018-10-24 13:43:52 +03:00
|
|
|
stream << (dword)entry.inode.index();
|
2019-01-31 19:31:23 +03:00
|
|
|
stream << (byte)entry.file_type;
|
2018-11-13 02:17:30 +03:00
|
|
|
stream << (dword)entry.name_length;
|
2018-10-24 13:43:52 +03:00
|
|
|
stream << entry.name;
|
|
|
|
return true;
|
|
|
|
});
|
2019-03-20 20:31:12 +03:00
|
|
|
stream.snip();
|
2018-10-24 13:43:52 +03:00
|
|
|
|
2019-03-20 20:31:12 +03:00
|
|
|
if (size < temp_buffer.size())
|
2018-10-24 15:28:22 +03:00
|
|
|
return -1;
|
|
|
|
|
2019-03-20 20:31:12 +03:00
|
|
|
memcpy(buffer, temp_buffer.pointer(), temp_buffer.size());
|
2018-10-24 13:43:52 +03:00
|
|
|
return stream.offset();
|
|
|
|
}
|
2019-01-16 14:57:07 +03:00
|
|
|
|
2019-04-28 16:02:55 +03:00
|
|
|
bool FileDescriptor::is_device() const
|
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_device();
|
2019-04-28 16:02:55 +03:00
|
|
|
}
|
|
|
|
|
2018-12-03 02:39:25 +03:00
|
|
|
bool FileDescriptor::is_tty() const
|
2018-10-31 00:03:02 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_tty();
|
2018-10-31 00:03:02 +03:00
|
|
|
}
|
|
|
|
|
2018-11-07 13:37:54 +03:00
|
|
|
const TTY* FileDescriptor::tty() const
|
2018-10-31 00:03:02 +03:00
|
|
|
{
|
2019-01-16 14:57:07 +03:00
|
|
|
if (!is_tty())
|
2018-11-12 03:28:46 +03:00
|
|
|
return nullptr;
|
2019-04-28 16:02:55 +03:00
|
|
|
return static_cast<const TTY*>(m_file.ptr());
|
2018-10-31 00:03:02 +03:00
|
|
|
}
|
2018-11-01 16:00:28 +03:00
|
|
|
|
2018-11-07 13:37:54 +03:00
|
|
|
TTY* FileDescriptor::tty()
|
2018-11-02 15:14:25 +03:00
|
|
|
{
|
2019-01-16 14:57:07 +03:00
|
|
|
if (!is_tty())
|
2018-11-12 03:28:46 +03:00
|
|
|
return nullptr;
|
2019-04-28 16:02:55 +03:00
|
|
|
return static_cast<TTY*>(m_file.ptr());
|
2018-11-02 15:14:25 +03:00
|
|
|
}
|
2019-01-15 08:30:19 +03:00
|
|
|
|
|
|
|
bool FileDescriptor::is_master_pty() const
|
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_master_pty();
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const MasterPTY* FileDescriptor::master_pty() const
|
|
|
|
{
|
|
|
|
if (!is_master_pty())
|
|
|
|
return nullptr;
|
2019-04-28 16:02:55 +03:00
|
|
|
return static_cast<const MasterPTY*>(m_file.ptr());
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MasterPTY* FileDescriptor::master_pty()
|
|
|
|
{
|
|
|
|
if (!is_master_pty())
|
|
|
|
return nullptr;
|
2019-04-28 16:02:55 +03:00
|
|
|
return static_cast<MasterPTY*>(m_file.ptr());
|
2019-01-15 08:30:19 +03:00
|
|
|
}
|
2018-11-02 15:14:25 +03:00
|
|
|
|
2018-11-07 13:37:54 +03:00
|
|
|
int FileDescriptor::close()
|
2018-11-01 16:00:28 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:30:13 +03:00
|
|
|
KResultOr<String> FileDescriptor::absolute_path()
|
2018-11-01 16:00:28 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->absolute_path(*this);
|
2018-11-01 16:00:28 +03:00
|
|
|
}
|
2018-11-12 03:28:46 +03:00
|
|
|
|
2019-01-16 14:57:07 +03:00
|
|
|
InodeMetadata FileDescriptor::metadata() const
|
|
|
|
{
|
|
|
|
if (m_inode)
|
|
|
|
return m_inode->metadata();
|
|
|
|
return { };
|
|
|
|
}
|
2019-02-16 11:57:42 +03:00
|
|
|
|
2019-04-28 16:02:55 +03:00
|
|
|
KResultOr<Region*> FileDescriptor::mmap(Process& process, LinearAddress laddr, size_t offset, size_t size, int prot)
|
2019-02-16 11:57:42 +03:00
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->mmap(process, laddr, offset, size, prot);
|
2019-02-16 11:57:42 +03:00
|
|
|
}
|
|
|
|
|
2019-04-09 02:10:00 +03:00
|
|
|
KResult FileDescriptor::truncate(off_t length)
|
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->truncate(length);
|
2019-04-09 02:10:00 +03:00
|
|
|
}
|
2019-04-28 23:31:31 +03:00
|
|
|
|
|
|
|
bool FileDescriptor::is_shared_memory() const
|
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_shared_memory();
|
2019-04-28 23:31:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 05:55:54 +03:00
|
|
|
|
|
|
|
bool FileDescriptor::is_fifo() const
|
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_fifo();
|
2019-04-29 05:55:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FIFO* FileDescriptor::fifo()
|
|
|
|
{
|
|
|
|
if (!is_fifo())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<FIFO*>(m_file.ptr());
|
|
|
|
}
|
2019-05-03 21:42:43 +03:00
|
|
|
|
|
|
|
bool FileDescriptor::is_socket() const
|
|
|
|
{
|
2019-05-30 14:39:17 +03:00
|
|
|
return m_file->is_socket();
|
2019-05-03 21:42:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Socket* FileDescriptor::socket()
|
|
|
|
{
|
|
|
|
if (!is_socket())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<Socket*>(m_file.ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
const Socket* FileDescriptor::socket() const
|
|
|
|
{
|
|
|
|
if (!is_socket())
|
|
|
|
return nullptr;
|
|
|
|
return static_cast<const Socket*>(m_file.ptr());
|
|
|
|
}
|