summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/FileSystem.h
blob: 8d5313eb1e20e78c84ce411cf4d00f1caf5e932a (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
#pragma once

#include "DiskDevice.h"
#include "InodeIdentifier.h"
#include "InodeMetadata.h"
#include "Limits.h"
#include "UnixTypes.h"
#include <AK/ByteBuffer.h>
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Retainable.h>
#include <AK/RetainPtr.h>
#include <AK/String.h>
#include <AK/Function.h>
#include <AK/kstdio.h>

static const dword mepoch = 476763780;

class FileDescriptor;

class FileSystem : public Retainable<FileSystem> {
public:
    static void initializeGlobals();
    virtual ~FileSystem();

    dword id() const { return m_id; }
    static FileSystem* fromID(dword);

    virtual bool initialize() = 0;
    virtual const char* className() const = 0;
    virtual InodeIdentifier rootInode() const = 0;
    virtual bool writeInode(InodeIdentifier, const ByteBuffer&) = 0;
    virtual InodeMetadata inodeMetadata(InodeIdentifier) const = 0;

    virtual Unix::ssize_t readInodeBytes(InodeIdentifier, Unix::off_t offset, Unix::size_t count, byte* buffer, FileDescriptor*) const = 0;

    struct DirectoryEntry {
        String name;
        InodeIdentifier inode;
        byte fileType { 0 };
    };
    virtual bool enumerateDirectoryInode(InodeIdentifier, Function<bool(const DirectoryEntry&)>) const = 0;

    virtual bool setModificationTime(InodeIdentifier, dword timestamp) = 0;
    virtual InodeIdentifier createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t, unsigned size) = 0;
    virtual InodeIdentifier makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t) = 0;

    virtual InodeIdentifier findParentOfInode(InodeIdentifier) const = 0;

    InodeIdentifier childOfDirectoryInodeWithName(InodeIdentifier, const String& name) const;
    ByteBuffer readEntireInode(InodeIdentifier, FileDescriptor* = nullptr) const;
    String nameOfChildInDirectory(InodeIdentifier parent, InodeIdentifier child) const;

protected:
    FileSystem();

private:
    dword m_id { 0 };
};

inline FileSystem* InodeIdentifier::fileSystem()
{
    return FileSystem::fromID(m_fileSystemID);
}

inline const FileSystem* InodeIdentifier::fileSystem() const
{
    return FileSystem::fromID(m_fileSystemID);
}

inline InodeMetadata InodeIdentifier::metadata() const
{
    if (!isValid())
        return InodeMetadata();
    return fileSystem()->inodeMetadata(*this);
}

inline bool InodeIdentifier::isRootInode() const
{
    return (*this) == fileSystem()->rootInode();
}

namespace AK {

template<>
struct Traits<InodeIdentifier> {
    // FIXME: This is a shitty hash.
    static unsigned hash(const InodeIdentifier& inode) { return Traits<unsigned>::hash(inode.fileSystemID()) + Traits<unsigned>::hash(inode.index()); }
    static void dump(const InodeIdentifier& inode) { kprintf("%02u:%08u", inode.fileSystemID(), inode.index()); }
};

}