summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/SyntheticFileSystem.h
blob: f21707f5f261859094700c6e336d372beecb4997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once

#include "FileSystem.h"
#include "UnixTypes.h"
#include <AK/HashMap.h>

class SynthFSInode;

class SyntheticFileSystem : public FileSystem {
public:
    virtual ~SyntheticFileSystem() override;
    static RetainPtr<SyntheticFileSystem> create();

    virtual bool initialize() override;
    virtual const char* className() const override;
    virtual InodeIdentifier rootInode() const override;
    virtual bool writeInode(InodeIdentifier, const ByteBuffer&) override;
    virtual bool enumerateDirectoryInode(InodeIdentifier, Function<bool(const DirectoryEntry&)>) const override;
    virtual InodeMetadata inodeMetadata(InodeIdentifier) const override;
    virtual bool setModificationTime(InodeIdentifier, dword timestamp) override;
    virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) override;
    virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileDescriptor*) const override;
    virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override;
    virtual InodeIdentifier find_parent_of_inode(InodeIdentifier) const override;
    virtual RetainPtr<CoreInode> get_inode(InodeIdentifier) const override;

protected:
    typedef unsigned InodeIndex;

    InodeIndex generateInodeIndex();
    static constexpr InodeIndex RootInodeIndex = 1;

    SyntheticFileSystem();

    RetainPtr<SynthFSInode> create_directory(String&& name);
    RetainPtr<SynthFSInode> create_text_file(String&& name, ByteBuffer&&, Unix::mode_t = 0010644);
    RetainPtr<SynthFSInode> create_generated_file(String&& name, Function<ByteBuffer()>&&, Unix::mode_t = 0100644);

    InodeIdentifier addFile(RetainPtr<SynthFSInode>&&, InodeIndex parent = RootInodeIndex);
    bool removeFile(InodeIndex);

private:
    InodeIndex m_nextInodeIndex { 2 };
    HashMap<InodeIndex, RetainPtr<SynthFSInode>> m_inodes;
};

class SynthFSInode final : public CoreInode {
    friend class SyntheticFileSystem;
public:
    virtual ~SynthFSInode() override;

private:
    // ^CoreInode
    virtual Unix::ssize_t read_bytes(Unix::off_t, Unix::size_t, byte* buffer, FileDescriptor*) override;
    virtual void populate_metadata() const override;
    virtual bool traverse_as_directory(Function<bool(const FileSystem::DirectoryEntry&)>) override;

    SyntheticFileSystem& fs();
    const SyntheticFileSystem& fs() const;
    SynthFSInode(SyntheticFileSystem&, unsigned index);

    String m_name;
    InodeIdentifier m_parent;
    ByteBuffer m_data;
    Function<ByteBuffer()> m_generator;
    Vector<SynthFSInode*> m_children;
};