summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/VirtualFileSystem.h
blob: bafb791d9f75e78d9f4b7028fd0dbbdc72531842 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#pragma once

#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/RetainPtr.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <AK/Lock.h>
#include <AK/Function.h>
#include "InodeIdentifier.h"
#include "InodeMetadata.h"
#include "Limits.h"
#include "FileSystem.h"

class CharacterDevice;
class FileHandle;

class VirtualFileSystem {
public:
    static void initializeGlobals();
    static SpinLock& lock();

    struct Node {
        InodeIdentifier inode;
        const InodeMetadata& metadata() const;

        bool inUse() const { return inode.isValid(); }

        bool isCharacterDevice() const { return m_characterDevice; }
        CharacterDevice* characterDevice() { return m_characterDevice; }

        void retain();
        void release();

        FileSystem* fileSystem() { return inode.fileSystem(); }
        const FileSystem* fileSystem() const { return inode.fileSystem(); }

        VirtualFileSystem* vfs() { return m_vfs; }
        const VirtualFileSystem* vfs() const { return m_vfs; }

    private:
        friend class VirtualFileSystem;
        VirtualFileSystem* m_vfs { nullptr };
        unsigned retainCount { 0 };
        CharacterDevice* m_characterDevice { nullptr };
        mutable InodeMetadata m_cachedMetadata;
    };

    static VirtualFileSystem& the();

    VirtualFileSystem();
    ~VirtualFileSystem();

    bool isDirectory(const String& path);
    void listDirectory(const String& path);
    void listDirectoryRecursively(const String& path);

    unsigned maxNodeCount() const { return m_maxNodeCount; }
    unsigned allocatedNodeCount() const { return m_maxNodeCount - m_nodeFreeList.size(); }

    Node* root() { return m_rootNode.ptr(); }
    const Node* root() const { return m_rootNode.ptr(); }

    bool mountRoot(RetainPtr<FileSystem>&&);
    bool mount(RetainPtr<FileSystem>&&, const String& path);

    OwnPtr<FileHandle> open(const String& path);
    OwnPtr<FileHandle> create(const String& path);
    OwnPtr<FileHandle> mkdir(const String& path);

    bool isRoot(InodeIdentifier) const;

    bool touch(const String&path);

    void registerCharacterDevice(unsigned major, unsigned minor, CharacterDevice&);

private:
    friend class FileHandle;

    void enumerateDirectoryInode(InodeIdentifier, Function<bool(const FileSystem::DirectoryEntry&)>);
    InodeIdentifier resolvePath(const String& path);
    InodeIdentifier resolveSymbolicLink(const String& basePath, InodeIdentifier symlinkInode);

    RetainPtr<Node> allocateNode();
    void freeNode(Node*);

    RetainPtr<Node> makeNode(InodeIdentifier);
    RetainPtr<Node> getOrCreateNode(InodeIdentifier);

    class Mount {
    public:
        Mount(InodeIdentifier host, RetainPtr<FileSystem>&&);

        InodeIdentifier host() const { return m_host; }
        InodeIdentifier guest() const { return m_guest; }

    private:
        InodeIdentifier m_host;
        InodeIdentifier m_guest;
        RetainPtr<FileSystem> m_fileSystem;
    };

    Mount* findMountForHost(InodeIdentifier);
    Mount* findMountForGuest(InodeIdentifier);

    HashMap<InodeIdentifier, Node*> m_inode2vnode;
    HashMap<dword, Node*> m_device2vnode;

    Vector<OwnPtr<Mount>> m_mounts;

    unsigned m_maxNodeCount { 0 };
    Node* m_nodes { nullptr };

    Vector<Node*> m_nodeFreeList;

    RetainPtr<Node> m_rootNode;

    HashMap<dword, CharacterDevice*> m_characterDevices;
};