2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-05-16 03:02:37 +02:00
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
|
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
|
|
|
|
#include <Kernel/UnixTypes.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
|
|
2018-11-13 23:44:54 +01:00
|
|
|
class SynthFSInode;
|
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
class SynthFS : public FS {
|
2018-10-10 11:53:07 +02:00
|
|
|
public:
|
2018-11-15 17:13:10 +01:00
|
|
|
virtual ~SynthFS() override;
|
2019-02-25 12:43:52 +01:00
|
|
|
static Retained<SynthFS> create();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
|
virtual bool initialize() override;
|
2018-11-15 15:36:35 +01:00
|
|
|
virtual const char* class_name() const override;
|
2018-12-03 00:20:00 +01:00
|
|
|
virtual InodeIdentifier root_inode() const override;
|
2019-05-03 22:59:58 +02:00
|
|
|
virtual RetainPtr<Inode> create_inode(InodeIdentifier parentInode, const String& name, mode_t, off_t size, dev_t, int& error) override;
|
2019-01-23 06:53:01 +01:00
|
|
|
virtual RetainPtr<Inode> create_directory(InodeIdentifier parentInode, const String& name, mode_t, int& error) override;
|
2018-12-19 21:18:28 +01:00
|
|
|
virtual RetainPtr<Inode> get_inode(InodeIdentifier) const override;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-23 11:57:38 +02:00
|
|
|
protected:
|
2018-10-26 17:42:12 +02:00
|
|
|
typedef unsigned InodeIndex;
|
|
|
|
|
|
2018-12-03 00:39:25 +01:00
|
|
|
InodeIndex generate_inode_index();
|
2018-10-26 17:42:12 +02:00
|
|
|
static constexpr InodeIndex RootInodeIndex = 1;
|
|
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
SynthFS();
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2019-02-25 12:43:52 +01:00
|
|
|
Retained<SynthFSInode> create_directory(String&& name);
|
|
|
|
|
Retained<SynthFSInode> create_text_file(String&& name, ByteBuffer&&, mode_t = 0010644);
|
|
|
|
|
Retained<SynthFSInode> create_generated_file(String&& name, Function<ByteBuffer(SynthFSInode&)>&&, mode_t = 0100644);
|
|
|
|
|
Retained<SynthFSInode> create_generated_file(String&& name, Function<ByteBuffer(SynthFSInode&)>&&, Function<ssize_t(SynthFSInode&, const ByteBuffer&)>&&, mode_t = 0100644);
|
2018-10-23 11:57:38 +02:00
|
|
|
|
2018-12-03 00:39:25 +01:00
|
|
|
InodeIdentifier add_file(RetainPtr<SynthFSInode>&&, InodeIndex parent = RootInodeIndex);
|
|
|
|
|
bool remove_file(InodeIndex);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-23 11:57:38 +02:00
|
|
|
private:
|
2018-12-03 00:39:25 +01:00
|
|
|
InodeIndex m_next_inode_index { 2 };
|
2018-11-13 23:44:54 +01:00
|
|
|
HashMap<InodeIndex, RetainPtr<SynthFSInode>> m_inodes;
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|
|
2019-01-18 15:01:40 +01:00
|
|
|
struct SynthFSInodeCustomData {
|
|
|
|
|
virtual ~SynthFSInodeCustomData();
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-19 21:18:28 +01:00
|
|
|
class SynthFSInode final : public Inode {
|
2018-11-15 17:13:10 +01:00
|
|
|
friend class SynthFS;
|
2019-01-30 00:49:20 +01:00
|
|
|
friend class DevPtsFS;
|
2018-11-13 23:44:54 +01:00
|
|
|
public:
|
|
|
|
|
virtual ~SynthFSInode() override;
|
|
|
|
|
|
2019-01-18 15:01:40 +01:00
|
|
|
void set_custom_data(OwnPtr<SynthFSInodeCustomData>&& 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(); }
|
|
|
|
|
|
2018-11-13 23:44:54 +01:00
|
|
|
private:
|
2018-12-19 21:56:45 +01:00
|
|
|
// ^Inode
|
2019-02-25 22:06:55 +01:00
|
|
|
virtual ssize_t read_bytes(off_t, ssize_t, byte* buffer, FileDescriptor*) const override;
|
2019-01-01 03:16:36 +01:00
|
|
|
virtual InodeMetadata metadata() const override;
|
2019-01-28 04:16:01 +01:00
|
|
|
virtual bool traverse_as_directory(Function<bool(const FS::DirectoryEntry&)>) const override;
|
2018-11-15 16:34:36 +01:00
|
|
|
virtual InodeIdentifier lookup(const String& name) override;
|
2018-11-15 17:04:55 +01:00
|
|
|
virtual String reverse_lookup(InodeIdentifier) override;
|
2018-12-19 21:56:45 +01:00
|
|
|
virtual void flush_metadata() override;
|
2019-02-25 22:06:55 +01:00
|
|
|
virtual ssize_t write_bytes(off_t, ssize_t, const byte* buffer, FileDescriptor*) override;
|
2019-02-27 15:31:26 +01:00
|
|
|
virtual KResult add_child(InodeIdentifier child_id, const String& name, byte file_type) override;
|
2019-02-27 14:11:25 +01:00
|
|
|
virtual KResult remove_child(const String& name) override;
|
2019-01-04 18:37:58 +01:00
|
|
|
virtual RetainPtr<Inode> parent() const override;
|
2019-01-28 04:16:01 +01:00
|
|
|
virtual size_t directory_entry_count() const override;
|
2019-02-25 20:47:56 +01:00
|
|
|
virtual KResult chmod(mode_t) override;
|
2019-02-27 12:32:53 +01:00
|
|
|
virtual KResult chown(uid_t, gid_t) override;
|
2018-11-13 23:44:54 +01:00
|
|
|
|
2018-11-15 17:13:10 +01:00
|
|
|
SynthFS& fs();
|
|
|
|
|
const SynthFS& fs() const;
|
|
|
|
|
SynthFSInode(SynthFS&, unsigned index);
|
2018-11-13 23:44:54 +01:00
|
|
|
|
|
|
|
|
String m_name;
|
|
|
|
|
InodeIdentifier m_parent;
|
|
|
|
|
ByteBuffer m_data;
|
2019-01-18 15:01:40 +01:00
|
|
|
Function<ByteBuffer(SynthFSInode&)> m_generator;
|
|
|
|
|
Function<ssize_t(SynthFSInode&, const ByteBuffer&)> m_write_callback;
|
2018-11-13 23:44:54 +01:00
|
|
|
Vector<SynthFSInode*> m_children;
|
2019-01-01 03:16:36 +01:00
|
|
|
InodeMetadata m_metadata;
|
2019-01-18 15:01:40 +01:00
|
|
|
OwnPtr<SynthFSInodeCustomData> m_custom_data;
|
2018-11-13 23:44:54 +01:00
|
|
|
};
|
2019-01-04 18:37:58 +01:00
|
|
|
|
|
|
|
|
inline SynthFS& SynthFSInode::fs()
|
|
|
|
|
{
|
|
|
|
|
return static_cast<SynthFS&>(Inode::fs());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline const SynthFS& SynthFSInode::fs() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<const SynthFS&>(Inode::fs());
|
|
|
|
|
}
|