2019-06-07 09:36:51 +02:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
|
|
|
#include <Kernel/FileSystem/InodeFile.h>
|
2019-05-30 13:39:17 +02:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
InodeFile::InodeFile(NonnullRefPtr<Inode>&& inode)
|
2019-05-30 13:39:17 +02:00
|
|
|
: m_inode(move(inode))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InodeFile::~InodeFile()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t InodeFile::read(FileDescription& description, u8* buffer, ssize_t count)
|
2019-05-30 13:39:17 +02:00
|
|
|
{
|
2019-12-01 17:36:06 +01:00
|
|
|
ssize_t nread = m_inode->read_bytes(description.offset(), count, buffer, &description);
|
|
|
|
if (nread > 0)
|
|
|
|
current->did_file_read(nread);
|
|
|
|
return nread;
|
2019-05-30 13:39:17 +02:00
|
|
|
}
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
ssize_t InodeFile::write(FileDescription& description, const u8* data, ssize_t count)
|
2019-05-30 13:39:17 +02:00
|
|
|
{
|
2019-12-01 17:36:06 +01:00
|
|
|
ssize_t nwritten = m_inode->write_bytes(description.offset(), count, data, &description);
|
|
|
|
if (nwritten > 0) {
|
2019-10-22 22:23:58 +02:00
|
|
|
m_inode->set_mtime(kgettimeofday().tv_sec);
|
2019-12-01 17:36:06 +01:00
|
|
|
current->did_file_write(nwritten);
|
|
|
|
}
|
|
|
|
return nwritten;
|
2019-05-30 13:39:17 +02:00
|
|
|
}
|
|
|
|
|
2019-06-13 22:03:04 +02:00
|
|
|
KResultOr<Region*> InodeFile::mmap(Process& process, FileDescription& description, VirtualAddress preferred_vaddr, size_t offset, size_t size, int prot)
|
2019-05-30 13:39:17 +02:00
|
|
|
{
|
|
|
|
ASSERT(offset == 0);
|
|
|
|
// FIXME: If PROT_EXEC, check that the underlying file system isn't mounted noexec.
|
2019-06-13 22:03:04 +02:00
|
|
|
auto* region = process.allocate_file_backed_region(preferred_vaddr, size, inode(), description.absolute_path(), prot);
|
2019-05-30 13:39:17 +02:00
|
|
|
if (!region)
|
|
|
|
return KResult(-ENOMEM);
|
|
|
|
return region;
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:03:04 +02:00
|
|
|
String InodeFile::absolute_path(const FileDescription& description) const
|
2019-05-30 13:39:17 +02:00
|
|
|
{
|
2019-05-30 21:29:26 +02:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-06-13 22:03:04 +02:00
|
|
|
ASSERT(description.custody());
|
|
|
|
return description.absolute_path();
|
2019-05-30 13:39:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
KResult InodeFile::truncate(off_t size)
|
|
|
|
{
|
2020-01-08 13:56:49 +01:00
|
|
|
auto truncate_result = m_inode->truncate(size);
|
|
|
|
if (truncate_result.is_error())
|
|
|
|
return truncate_result;
|
|
|
|
int mtime_result = m_inode->set_mtime(kgettimeofday().tv_sec);
|
|
|
|
if (mtime_result != 0)
|
|
|
|
return KResult(mtime_result);
|
|
|
|
return KSuccess;
|
2019-05-30 13:39:17 +02:00
|
|
|
}
|
2020-01-03 20:14:56 +01:00
|
|
|
|
|
|
|
KResult InodeFile::chown(uid_t uid, gid_t gid)
|
|
|
|
{
|
|
|
|
return VFS::the().chown(*m_inode, uid, gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
KResult InodeFile::chmod(mode_t mode)
|
|
|
|
{
|
|
|
|
return VFS::the().chmod(*m_inode, mode);
|
|
|
|
}
|