From 754037874c692769c703f86bfa7af641e1346139 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 23 Jan 2019 05:13:17 +0100 Subject: Move VFS sources into Kernel/. --- Kernel/SyntheticFileSystem.h | 90 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Kernel/SyntheticFileSystem.h (limited to 'Kernel/SyntheticFileSystem.h') diff --git a/Kernel/SyntheticFileSystem.h b/Kernel/SyntheticFileSystem.h new file mode 100644 index 0000000000..a0ad26e87b --- /dev/null +++ b/Kernel/SyntheticFileSystem.h @@ -0,0 +1,90 @@ +#pragma once + +#include "FileSystem.h" +#include "UnixTypes.h" +#include + +class SynthFSInode; + +class SynthFS : public FS { +public: + virtual ~SynthFS() override; + static RetainPtr create(); + + virtual bool initialize() override; + virtual const char* class_name() const override; + virtual InodeIdentifier root_inode() const override; + virtual RetainPtr create_inode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size, int& error) override; + virtual RetainPtr create_directory(InodeIdentifier parentInode, const String& name, Unix::mode_t, int& error) override; + virtual RetainPtr get_inode(InodeIdentifier) const override; + +protected: + typedef unsigned InodeIndex; + + InodeIndex generate_inode_index(); + static constexpr InodeIndex RootInodeIndex = 1; + + SynthFS(); + + RetainPtr create_directory(String&& name); + RetainPtr create_text_file(String&& name, ByteBuffer&&, Unix::mode_t = 0010644); + RetainPtr create_generated_file(String&& name, Function&&, Unix::mode_t = 0100644); + RetainPtr create_generated_file(String&& name, Function&&, Function&&, Unix::mode_t = 0100644); + + InodeIdentifier add_file(RetainPtr&&, InodeIndex parent = RootInodeIndex); + bool remove_file(InodeIndex); + +private: + InodeIndex m_next_inode_index { 2 }; + HashMap> m_inodes; +}; + +struct SynthFSInodeCustomData { + virtual ~SynthFSInodeCustomData(); +}; + +class SynthFSInode final : public Inode { + friend class SynthFS; +public: + virtual ~SynthFSInode() override; + + void set_custom_data(OwnPtr&& custom_data) { m_custom_data = move(custom_data); } + SynthFSInodeCustomData* custom_data() { return m_custom_data.ptr(); } + const SynthFSInodeCustomData* custom_data() const { return m_custom_data.ptr(); } + +private: + // ^Inode + virtual ssize_t read_bytes(Unix::off_t, size_t, byte* buffer, FileDescriptor*) override; + virtual InodeMetadata metadata() const override; + virtual bool traverse_as_directory(Function) override; + virtual InodeIdentifier lookup(const String& name) override; + virtual String reverse_lookup(InodeIdentifier) override; + virtual void flush_metadata() override; + virtual ssize_t write_bytes(Unix::off_t, size_t, const byte* buffer, FileDescriptor*) override; + virtual bool add_child(InodeIdentifier child_id, const String& name, byte file_type, int& error) override; + virtual bool remove_child(const String& name, int& error) override; + virtual RetainPtr parent() const override; + + SynthFS& fs(); + const SynthFS& fs() const; + SynthFSInode(SynthFS&, unsigned index); + + String m_name; + InodeIdentifier m_parent; + ByteBuffer m_data; + Function m_generator; + Function m_write_callback; + Vector m_children; + InodeMetadata m_metadata; + OwnPtr m_custom_data; +}; + +inline SynthFS& SynthFSInode::fs() +{ + return static_cast(Inode::fs()); +} + +inline const SynthFS& SynthFSInode::fs() const +{ + return static_cast(Inode::fs()); +} -- cgit v1.2.3