summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/SyntheticFileSystem.h
blob: 2831d6d2393dc6873aef24d8f0b84ff0240b7eaa (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
#pragma once

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

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, FileHandle*) const override;
    virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) override;

protected:
    typedef unsigned InodeIndex;

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

    SyntheticFileSystem();

    struct File {
        String name;
        InodeMetadata metadata;
        InodeIdentifier parent;
        ByteBuffer data;
        Function<ByteBuffer()> generator;
        Vector<File*> children;
    };

    OwnPtr<File> createDirectory(String&& name);
    OwnPtr<File> createTextFile(String&& name, String&& text);
    OwnPtr<File> createGeneratedFile(String&& name, Function<ByteBuffer()>&&);

    InodeIdentifier addFile(OwnPtr<File>&&, InodeIndex parent = RootInodeIndex);
    bool removeFile(InodeIndex);

private:
    InodeIndex m_nextInodeIndex { 2 };
    HashMap<InodeIndex, OwnPtr<File>> m_inodes;
};