mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 11:55:08 +00:00
Kernel: Add InodeFile, a File subclass for regular files.
Finally everything that can be held by a FileDescriptor actually inherits from the File class.
This commit is contained in:
parent
66c1a9be3b
commit
08926e59b3
9 changed files with 151 additions and 133 deletions
55
Kernel/FileSystem/InodeFile.cpp
Normal file
55
Kernel/FileSystem/InodeFile.cpp
Normal file
|
@ -0,0 +1,55 @@
|
|||
#include <Kernel/FileSystem/InodeFile.h>
|
||||
#include <Kernel/FileSystem/Inode.h>
|
||||
#include <Kernel/FileSystem/FileDescriptor.h>
|
||||
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
||||
#include <Kernel/Process.h>
|
||||
|
||||
InodeFile::InodeFile(Retained<Inode>&& inode)
|
||||
: m_inode(move(inode))
|
||||
{
|
||||
}
|
||||
|
||||
InodeFile::~InodeFile()
|
||||
{
|
||||
}
|
||||
|
||||
ssize_t InodeFile::read(FileDescriptor& descriptor, byte* buffer, ssize_t count)
|
||||
{
|
||||
return m_inode->read_bytes(descriptor.offset(), count, buffer, &descriptor);
|
||||
}
|
||||
|
||||
ssize_t InodeFile::write(FileDescriptor& descriptor, const byte* data, ssize_t count)
|
||||
{
|
||||
return m_inode->write_bytes(descriptor.offset(), count, data, &descriptor);
|
||||
}
|
||||
|
||||
KResultOr<Region*> InodeFile::mmap(Process& process, LinearAddress preferred_laddr, size_t offset, size_t size, int prot)
|
||||
{
|
||||
ASSERT(offset == 0);
|
||||
// FIXME: If PROT_EXEC, check that the underlying file system isn't mounted noexec.
|
||||
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
|
||||
InterruptDisabler disabler;
|
||||
auto* region = process.allocate_file_backed_region(preferred_laddr, size, inode(), move(region_name), prot & PROT_READ, prot & PROT_WRITE);
|
||||
if (!region)
|
||||
return KResult(-ENOMEM);
|
||||
return region;
|
||||
}
|
||||
|
||||
String InodeFile::absolute_path(FileDescriptor&) const
|
||||
{
|
||||
auto path_or_error = VFS::the().absolute_path(const_cast<Inode&>(inode()));
|
||||
if (path_or_error.is_error())
|
||||
return { };
|
||||
return path_or_error.value();
|
||||
}
|
||||
|
||||
KResult InodeFile::truncate(off_t size)
|
||||
{
|
||||
return m_inode->truncate(size);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue