summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/FileSystem.cpp
blob: b718fe5c35b29e949cd96f699c714b6effa7689f (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
#include <AK/Assertions.h>
#include <AK/HashMap.h>
#include "FileSystem.h"

static dword s_lastFileSystemID;
static HashMap<dword, FileSystem*>* map;

static HashMap<dword, FileSystem*>& fileSystems()
{
    if (!map)
        map = new HashMap<dword, FileSystem*>();
    return *map;
}

void FileSystem::initializeGlobals()
{
    s_lastFileSystemID = 0;
    map = 0;
}

FileSystem::FileSystem()
    : m_id(++s_lastFileSystemID)
{
    fileSystems().set(m_id, this);
}

FileSystem::~FileSystem()
{
    fileSystems().remove(m_id);
}

FileSystem* FileSystem::fromID(dword id)
{
    auto it = fileSystems().find(id);
    if (it != fileSystems().end())
        return (*it).value;
    return nullptr;
}

InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode, const String& name) const
{
    InodeIdentifier foundInode;
    enumerateDirectoryInode(inode, [&] (const DirectoryEntry& entry) {
        if (entry.name == name) {
            foundInode = entry.inode;
            return false;
        }
        return true;
    });
    return foundInode;
}

String FileSystem::nameOfChildInDirectory(InodeIdentifier parent, InodeIdentifier child) const
{
    String name;
    bool success = enumerateDirectoryInode(parent, [&] (auto& entry) {
        if (entry.inode == child) {
            name = entry.name;
            return false;
        }
        return true;
    });
    ASSERT(success);
    return name;
}

ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode, FileHandle* handle) const
{
    ASSERT(inode.fileSystemID() == id());

    auto metadata = inodeMetadata(inode);
    if (!metadata.isValid()) {
        kprintf("[fs] readInode: metadata lookup for inode %u failed\n", inode.index());
        return nullptr;
    }

    auto contents = ByteBuffer::createUninitialized(metadata.size);

    Unix::ssize_t nread;
    byte buffer[512];
    byte* out = contents.pointer();
    Unix::off_t offset = 0;
    for (;;) {
        nread = readInodeBytes(inode, offset, sizeof(buffer), buffer, handle);
        if (nread <= 0)
            break;
        memcpy(out, buffer, nread);
        out += nread;
        offset += nread;
    }
    if (nread < 0) {
        kprintf("[fs] readInode: ERROR: %d\n", nread);
        return nullptr;
    }

    return contents;
}