2018-10-10 11:53:07 +02:00
|
|
|
#include "SyntheticFileSystem.h"
|
2018-11-07 11:37:54 +01:00
|
|
|
#include "FileDescriptor.h"
|
2018-11-18 14:57:41 +01:00
|
|
|
#include <LibC/errno_numbers.h>
|
2018-10-14 23:13:45 +02:00
|
|
|
#include <AK/StdLib.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-29 21:54:11 +01:00
|
|
|
#ifndef SERENITY
|
|
|
|
typedef int InterruptDisabler;
|
|
|
|
#define ASSERT_INTERRUPTS_DISABLED()
|
|
|
|
#endif
|
|
|
|
|
2018-10-23 11:57:38 +02:00
|
|
|
//#define SYNTHFS_DEBUG
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
RetainPtr<SynthFS> SynthFS::create()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-11-15 17:13:10 +01:00
|
|
|
return adopt(*new SynthFS);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
SynthFS::SynthFS()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
SynthFS::~SynthFS()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
bool SynthFS::initialize()
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
// Add a File for the root directory.
|
|
|
|
// FIXME: This needs work.
|
2018-11-13 23:44:54 +01:00
|
|
|
auto root = adopt(*new SynthFSInode(*this, RootInodeIndex));
|
|
|
|
root->m_parent = { id(), RootInodeIndex };
|
|
|
|
root->m_metadata.mode = 0040555;
|
|
|
|
root->m_metadata.uid = 0;
|
|
|
|
root->m_metadata.gid = 0;
|
|
|
|
root->m_metadata.size = 0;
|
|
|
|
root->m_metadata.mtime = mepoch;
|
|
|
|
m_inodes.set(RootInodeIndex, move(root));
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-23 11:57:38 +02:00
|
|
|
#ifndef SERENITY
|
2018-11-07 12:05:51 +01:00
|
|
|
addFile(createTextFile("file", String("I'm a synthetic file!\n").toByteBuffer(), 0100644));
|
|
|
|
addFile(createTextFile("message", String("Hey! This isn't my bottle!\n").toByteBuffer(), 0100644));
|
2018-10-29 21:54:11 +01:00
|
|
|
addFile(createGeneratedFile("lunk", [] { return String("/home/andreas/file1").toByteBuffer(); }, 00120777));
|
2018-10-23 11:57:38 +02:00
|
|
|
#endif
|
2018-10-10 11:53:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
RetainPtr<SynthFSInode> SynthFS::create_directory(String&& name)
|
2018-10-26 17:42:12 +02:00
|
|
|
{
|
2018-12-03 00:39:25 +01:00
|
|
|
auto file = adopt(*new SynthFSInode(*this, generate_inode_index()));
|
2018-11-13 23:44:54 +01:00
|
|
|
file->m_name = move(name);
|
|
|
|
file->m_metadata.size = 0;
|
|
|
|
file->m_metadata.uid = 0;
|
|
|
|
file->m_metadata.gid = 0;
|
|
|
|
file->m_metadata.mode = 0040555;
|
|
|
|
file->m_metadata.mtime = mepoch;
|
2018-10-26 17:42:12 +02:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
RetainPtr<SynthFSInode> SynthFS::create_text_file(String&& name, ByteBuffer&& contents, Unix::mode_t mode)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-12-03 00:39:25 +01:00
|
|
|
auto file = adopt(*new SynthFSInode(*this, generate_inode_index()));
|
2018-11-13 23:44:54 +01:00
|
|
|
file->m_data = contents;
|
|
|
|
file->m_name = move(name);
|
|
|
|
file->m_metadata.size = file->m_data.size();
|
|
|
|
file->m_metadata.uid = 100;
|
|
|
|
file->m_metadata.gid = 200;
|
|
|
|
file->m_metadata.mode = mode;
|
|
|
|
file->m_metadata.mtime = mepoch;
|
2018-10-10 11:53:07 +02:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
RetainPtr<SynthFSInode> SynthFS::create_generated_file(String&& name, Function<ByteBuffer()>&& generator, Unix::mode_t mode)
|
2018-10-23 11:57:38 +02:00
|
|
|
{
|
2018-12-03 00:39:25 +01:00
|
|
|
auto file = adopt(*new SynthFSInode(*this, generate_inode_index()));
|
2018-11-13 23:44:54 +01:00
|
|
|
file->m_generator = move(generator);
|
|
|
|
file->m_name = move(name);
|
|
|
|
file->m_metadata.size = 0;
|
|
|
|
file->m_metadata.uid = 0;
|
|
|
|
file->m_metadata.gid = 0;
|
|
|
|
file->m_metadata.mode = mode;
|
|
|
|
file->m_metadata.mtime = mepoch;
|
2018-10-23 11:57:38 +02:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:39:25 +01:00
|
|
|
InodeIdentifier SynthFS::add_file(RetainPtr<SynthFSInode>&& file, InodeIndex parent)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-10-26 22:32:35 +02:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(file);
|
2018-10-26 17:42:12 +02:00
|
|
|
auto it = m_inodes.find(parent);
|
|
|
|
ASSERT(it != m_inodes.end());
|
2018-11-13 23:44:54 +01:00
|
|
|
auto new_inode_id = file->identifier();
|
|
|
|
file->m_metadata.inode = new_inode_id;
|
|
|
|
file->m_parent = { id(), parent };
|
|
|
|
(*it).value->m_children.append(file.ptr());
|
|
|
|
m_inodes.set(new_inode_id.index(), move(file));
|
|
|
|
return new_inode_id;
|
2018-10-26 17:42:12 +02:00
|
|
|
}
|
|
|
|
|
2018-12-03 00:39:25 +01:00
|
|
|
bool SynthFS::remove_file(InodeIndex inode)
|
2018-10-26 17:42:12 +02:00
|
|
|
{
|
2018-10-26 22:32:35 +02:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2018-10-26 17:42:12 +02:00
|
|
|
auto it = m_inodes.find(inode);
|
|
|
|
if (it == m_inodes.end())
|
|
|
|
return false;
|
|
|
|
auto& file = *(*it).value;
|
|
|
|
|
2018-11-13 23:44:54 +01:00
|
|
|
auto pit = m_inodes.find(file.m_parent.index());
|
2018-10-26 17:42:12 +02:00
|
|
|
if (pit == m_inodes.end())
|
|
|
|
return false;
|
|
|
|
auto& parent = *(*pit).value;
|
2018-11-13 23:44:54 +01:00
|
|
|
for (size_t i = 0; i < parent.m_children.size(); ++i) {
|
|
|
|
if (parent.m_children[i]->m_metadata.inode.index() != inode) {
|
2018-10-26 17:42:12 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-13 23:44:54 +01:00
|
|
|
parent.m_children.remove(i);
|
2018-10-26 17:42:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-13 23:44:54 +01:00
|
|
|
for (auto& child : file.m_children)
|
2018-12-03 00:39:25 +01:00
|
|
|
remove_file(child->m_metadata.inode.index());
|
2018-10-26 17:42:12 +02:00
|
|
|
m_inodes.remove(inode);
|
|
|
|
return true;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
const char* SynthFS::class_name() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
return "synthfs";
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
InodeIdentifier SynthFS::root_inode() const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
return { id(), 1 };
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
InodeMetadata SynthFS::inode_metadata(InodeIdentifier inode) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-10-26 22:32:35 +02:00
|
|
|
InterruptDisabler disabler;
|
2018-11-15 15:10:12 +01:00
|
|
|
ASSERT(inode.fsid() == id());
|
2018-10-10 11:53:07 +02:00
|
|
|
#ifdef SYNTHFS_DEBUG
|
2018-11-13 23:44:54 +01:00
|
|
|
kprintf("SynthFS: inodeMetadata(%u)\n", inode.index());
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
2018-10-26 17:42:12 +02:00
|
|
|
|
|
|
|
auto it = m_inodes.find(inode.index());
|
|
|
|
if (it == m_inodes.end())
|
|
|
|
return { };
|
2018-11-13 23:44:54 +01:00
|
|
|
return (*it).value->m_metadata;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
bool SynthFS::set_mtime(InodeIdentifier, dword timestamp)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
(void) timestamp;
|
2018-10-23 11:57:38 +02:00
|
|
|
kprintf("FIXME: Implement SyntheticFileSystem::setModificationTime().\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
InodeIdentifier SynthFS::create_inode(InodeIdentifier parentInode, const String& name, Unix::mode_t mode, unsigned size, int& error)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
(void) parentInode;
|
|
|
|
(void) name;
|
|
|
|
(void) mode;
|
2018-10-26 17:42:12 +02:00
|
|
|
(void) size;
|
2018-11-18 14:57:41 +01:00
|
|
|
(void) error;
|
|
|
|
kprintf("FIXME: Implement SyntheticFileSystem::create_inode().\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:20:00 +01:00
|
|
|
bool SynthFS::write_inode(InodeIdentifier, const ByteBuffer&)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-10-23 11:57:38 +02:00
|
|
|
kprintf("FIXME: Implement SyntheticFileSystem::writeInode().\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-14 21:19:27 +02:00
|
|
|
|
2018-12-02 23:34:50 +01:00
|
|
|
ssize_t SynthFS::read_inode_bytes(InodeIdentifier inode, Unix::off_t offset, size_t count, byte* buffer, FileDescriptor* handle) const
|
2018-10-14 21:19:27 +02:00
|
|
|
{
|
2018-11-15 15:10:12 +01:00
|
|
|
ASSERT(inode.fsid() == id());
|
2018-10-14 23:13:45 +02:00
|
|
|
#ifdef SYNTHFS_DEBUG
|
2018-11-13 23:44:54 +01:00
|
|
|
kprintf("SynthFS: readInode %u\n", inode.index());
|
2018-10-14 23:13:45 +02:00
|
|
|
#endif
|
|
|
|
ASSERT(offset >= 0);
|
|
|
|
ASSERT(buffer);
|
2018-10-28 12:20:25 +01:00
|
|
|
|
2018-11-13 23:44:54 +01:00
|
|
|
const SynthFSInode* found_file;
|
2018-11-08 16:07:45 +01:00
|
|
|
{
|
|
|
|
InterruptDisabler disabler;
|
|
|
|
auto it = m_inodes.find(inode.index());
|
|
|
|
if (it == m_inodes.end())
|
|
|
|
return false;
|
|
|
|
found_file = (*it).value.ptr();
|
|
|
|
}
|
2018-11-13 23:44:54 +01:00
|
|
|
const SynthFSInode& file = *found_file;
|
2018-10-23 11:57:38 +02:00
|
|
|
ByteBuffer generatedData;
|
2018-11-13 23:44:54 +01:00
|
|
|
if (file.m_generator) {
|
2018-10-27 00:14:24 +02:00
|
|
|
if (!handle) {
|
2018-11-13 23:44:54 +01:00
|
|
|
generatedData = file.m_generator();
|
2018-10-27 00:14:24 +02:00
|
|
|
} else {
|
2018-12-03 00:39:25 +01:00
|
|
|
if (!handle->generator_cache())
|
|
|
|
handle->generator_cache() = file.m_generator();
|
|
|
|
generatedData = handle->generator_cache();
|
2018-10-27 00:14:24 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 11:57:38 +02:00
|
|
|
|
2018-11-13 23:44:54 +01:00
|
|
|
auto* data = generatedData ? &generatedData : &file.m_data;
|
2018-12-02 23:34:50 +01:00
|
|
|
ssize_t nread = min(static_cast<Unix::off_t>(data->size() - offset), static_cast<Unix::off_t>(count));
|
2018-10-23 11:57:38 +02:00
|
|
|
memcpy(buffer, data->pointer() + offset, nread);
|
2018-12-03 00:39:25 +01:00
|
|
|
if (nread == 0 && handle && handle->generator_cache())
|
|
|
|
handle->generator_cache().clear();
|
2018-10-14 23:13:45 +02:00
|
|
|
return nread;
|
2018-10-14 21:19:27 +02:00
|
|
|
}
|
2018-10-16 00:35:03 +02:00
|
|
|
|
2018-11-18 14:57:41 +01:00
|
|
|
InodeIdentifier SynthFS::create_directory(InodeIdentifier, const String&, Unix::mode_t, int& error)
|
2018-10-16 00:35:03 +02:00
|
|
|
{
|
2018-11-18 14:57:41 +01:00
|
|
|
error = -EROFS;
|
2018-10-16 00:35:03 +02:00
|
|
|
return { };
|
|
|
|
}
|
2018-10-26 17:42:12 +02:00
|
|
|
|
2018-12-03 00:39:25 +01:00
|
|
|
auto SynthFS::generate_inode_index() -> InodeIndex
|
2018-10-26 17:42:12 +02:00
|
|
|
{
|
2018-12-03 00:39:25 +01:00
|
|
|
return m_next_inode_index++;
|
2018-10-26 17:42:12 +02:00
|
|
|
}
|
2018-10-28 12:20:25 +01:00
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
InodeIdentifier SynthFS::find_parent_of_inode(InodeIdentifier inode) const
|
2018-10-28 12:20:25 +01:00
|
|
|
{
|
|
|
|
auto it = m_inodes.find(inode.index());
|
|
|
|
if (it == m_inodes.end())
|
|
|
|
return { };
|
2018-11-13 23:44:54 +01:00
|
|
|
return (*it).value->m_parent;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
RetainPtr<CoreInode> SynthFS::get_inode(InodeIdentifier inode) const
|
2018-11-13 23:44:54 +01:00
|
|
|
{
|
|
|
|
auto it = m_inodes.find(inode.index());
|
|
|
|
if (it == m_inodes.end())
|
|
|
|
return { };
|
|
|
|
return (*it).value;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
SynthFSInode::SynthFSInode(SynthFS& fs, unsigned index)
|
2018-11-13 23:44:54 +01:00
|
|
|
: CoreInode(fs, index)
|
|
|
|
{
|
|
|
|
m_metadata.inode = { fs.id(), index };
|
|
|
|
}
|
|
|
|
|
|
|
|
SynthFSInode::~SynthFSInode()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SynthFSInode::populate_metadata() const
|
|
|
|
{
|
|
|
|
// Already done when SynthFS created the file.
|
|
|
|
}
|
|
|
|
|
2018-12-02 23:34:50 +01:00
|
|
|
ssize_t SynthFSInode::read_bytes(Unix::off_t offset, size_t count, byte* buffer, FileDescriptor* descriptor)
|
2018-11-13 23:44:54 +01:00
|
|
|
{
|
|
|
|
#ifdef SYNTHFS_DEBUG
|
|
|
|
kprintf("SynthFS: read_bytes %u\n", index());
|
|
|
|
#endif
|
|
|
|
ASSERT(offset >= 0);
|
|
|
|
ASSERT(buffer);
|
|
|
|
|
|
|
|
ByteBuffer generatedData;
|
|
|
|
if (m_generator) {
|
|
|
|
if (!descriptor) {
|
|
|
|
generatedData = m_generator();
|
|
|
|
} else {
|
2018-12-03 00:39:25 +01:00
|
|
|
if (!descriptor->generator_cache())
|
|
|
|
descriptor->generator_cache() = m_generator();
|
|
|
|
generatedData = descriptor->generator_cache();
|
2018-11-13 23:44:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* data = generatedData ? &generatedData : &m_data;
|
2018-12-02 23:34:50 +01:00
|
|
|
ssize_t nread = min(static_cast<Unix::off_t>(data->size() - offset), static_cast<Unix::off_t>(count));
|
2018-11-13 23:44:54 +01:00
|
|
|
memcpy(buffer, data->pointer() + offset, nread);
|
2018-12-03 00:39:25 +01:00
|
|
|
if (nread == 0 && descriptor && descriptor->generator_cache())
|
|
|
|
descriptor->generator_cache().clear();
|
2018-11-13 23:44:54 +01:00
|
|
|
return nread;
|
2018-10-28 12:20:25 +01:00
|
|
|
}
|
2018-11-13 13:02:39 +01:00
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
bool SynthFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback)
|
2018-11-13 13:02:39 +01:00
|
|
|
{
|
2018-11-13 23:44:54 +01:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
#ifdef SYNTHFS_DEBUG
|
|
|
|
kprintf("SynthFS: traverse_as_directory %u\n", index());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!m_metadata.isDirectory())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
callback({ ".", 1, m_metadata.inode, 2 });
|
|
|
|
callback({ "..", 2, m_parent, 2 });
|
|
|
|
|
|
|
|
for (auto& child : m_children)
|
|
|
|
callback({ child->m_name.characters(), child->m_name.length(), child->m_metadata.inode, child->m_metadata.isDirectory() ? (byte)2 : (byte)1 });
|
|
|
|
return true;
|
2018-11-13 13:02:39 +01:00
|
|
|
}
|
2018-11-15 16:34:36 +01:00
|
|
|
|
|
|
|
InodeIdentifier SynthFSInode::lookup(const String& name)
|
|
|
|
{
|
|
|
|
ASSERT(is_directory());
|
|
|
|
if (name == ".")
|
|
|
|
return identifier();
|
|
|
|
if (name == "..")
|
|
|
|
return m_parent;
|
|
|
|
for (auto& child : m_children) {
|
|
|
|
if (child->m_name == name)
|
|
|
|
return child->identifier();
|
|
|
|
}
|
|
|
|
return { };
|
|
|
|
}
|
2018-11-15 17:04:55 +01:00
|
|
|
|
|
|
|
String SynthFSInode::reverse_lookup(InodeIdentifier child_id)
|
|
|
|
{
|
|
|
|
ASSERT(is_directory());
|
|
|
|
for (auto& child : m_children) {
|
|
|
|
if (child->identifier() == child_id)
|
|
|
|
return child->m_name;
|
|
|
|
}
|
|
|
|
return { };
|
|
|
|
}
|