summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/SyntheticFileSystem.cpp
blob: 676aff6b8025d87978d722307db174e755cdf92f (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "SyntheticFileSystem.h"
#include "FileDescriptor.h"
#include <AK/StdLib.h>

#ifndef SERENITY
typedef int InterruptDisabler;
#define ASSERT_INTERRUPTS_DISABLED()
#endif

//#define SYNTHFS_DEBUG

RetainPtr<SyntheticFileSystem> SyntheticFileSystem::create()
{
    return adopt(*new SyntheticFileSystem);
}

SyntheticFileSystem::SyntheticFileSystem()
{
}

SyntheticFileSystem::~SyntheticFileSystem()
{
}

bool SyntheticFileSystem::initialize()
{
    // Add a File for the root directory.
    // FIXME: This needs work.
    auto rootDir = make<File>();
    rootDir->metadata.inode = { id(), RootInodeIndex };
    rootDir->parent = { id(), RootInodeIndex };
    rootDir->metadata.mode = 0040555;
    rootDir->metadata.uid = 0;
    rootDir->metadata.gid = 0;
    rootDir->metadata.size = 0;
    rootDir->metadata.mtime = mepoch;
    m_inodes.set(RootInodeIndex, move(rootDir));

#ifndef SERENITY
    addFile(createTextFile("file", String("I'm a synthetic file!\n").toByteBuffer(), 0100644));
    addFile(createTextFile("message", String("Hey! This isn't my bottle!\n").toByteBuffer(), 0100644));
    addFile(createGeneratedFile("lunk", [] { return String("/home/andreas/file1").toByteBuffer(); }, 00120777));
#endif
    return true;
}

auto SyntheticFileSystem::createDirectory(String&& name) -> OwnPtr<File>
{
    auto file = make<File>();
    file->name = move(name);
    file->metadata.size = 0;
    file->metadata.uid = 0;
    file->metadata.gid = 0;
    file->metadata.mode = 0040555;
    file->metadata.mtime = mepoch;
    return file;
}

auto SyntheticFileSystem::createTextFile(String&& name, ByteBuffer&& contents, Unix::mode_t mode) -> OwnPtr<File>
{
    auto file = make<File>();
    file->data = contents;
    file->name = move(name);
    file->metadata.size = file->data.size();
    file->metadata.uid = 100;
    file->metadata.gid = 200;
    file->metadata.mode = mode;
    file->metadata.mtime = mepoch;
    return file;
}

auto SyntheticFileSystem::createGeneratedFile(String&& name, Function<ByteBuffer()>&& generator, Unix::mode_t mode) -> OwnPtr<File>
{
    auto file = make<File>();
    file->generator = move(generator);
    file->name = move(name);
    file->metadata.size = 0;
    file->metadata.uid = 0;
    file->metadata.gid = 0;
    file->metadata.mode = mode;
    file->metadata.mtime = mepoch;
    return file;
}

InodeIdentifier SyntheticFileSystem::addFile(OwnPtr<File>&& file, InodeIndex parent)
{
    ASSERT_INTERRUPTS_DISABLED();
    ASSERT(file);
    auto it = m_inodes.find(parent);
    ASSERT(it != m_inodes.end());
    InodeIdentifier newInode { id(), generateInodeIndex() };
    file->metadata.inode = newInode;
    file->parent = { id(), parent };
    (*it).value->children.append(file.ptr());
    m_inodes.set(newInode.index(), move(file));
    return newInode;
}

bool SyntheticFileSystem::removeFile(InodeIndex inode)
{
    ASSERT_INTERRUPTS_DISABLED();
    auto it = m_inodes.find(inode);
    if (it == m_inodes.end())
        return false;
    auto& file = *(*it).value;

    auto pit = m_inodes.find(file.parent.index());
    if (pit == m_inodes.end())
        return false;
    auto& parent = *(*pit).value;
    for (size_t i = 0; i < parent.children.size(); ++i) {
        if (parent.children[i]->metadata.inode.index() != inode) {
            continue;
        }
        parent.children.remove(i);
        break;
    }

    for (auto& child : file.children)
        removeFile(child->metadata.inode.index());
    m_inodes.remove(inode);
    return true;
}

const char* SyntheticFileSystem::className() const
{
    return "synthfs";
}

InodeIdentifier SyntheticFileSystem::rootInode() const
{
    return { id(), 1 };
}

bool SyntheticFileSystem::enumerateDirectoryInode(InodeIdentifier inode, Function<bool(const DirectoryEntry&)> callback) const
{
    InterruptDisabler disabler;
    ASSERT(inode.fileSystemID() == id());
#ifdef SYNTHFS_DEBUG
    kprintf("synthfs: enumerateDirectoryInode %u\n", inode.index());
#endif

    auto it = m_inodes.find(inode.index());
    if (it == m_inodes.end())
        return false;
    const File& synInode = *(*it).value;
    if (!synInode.metadata.isDirectory())
        return false;

    callback({ ".", 1, synInode.metadata.inode, 2 });
    callback({ "..", 2, synInode.parent, 2 });

    for (auto& child : synInode.children)
        callback({ child->name.characters(), child->name.length(), child->metadata.inode, child->metadata.isDirectory() ? (byte)2 : (byte)1 });
    return true;
}

InodeMetadata SyntheticFileSystem::inodeMetadata(InodeIdentifier inode) const
{
    InterruptDisabler disabler;
    ASSERT(inode.fileSystemID() == id());
#ifdef SYNTHFS_DEBUG
    kprintf("synthfs: inodeMetadata(%u)\n", inode.index());
#endif

    auto it = m_inodes.find(inode.index());
    if (it == m_inodes.end())
        return { };
    return (*it).value->metadata;
}

bool SyntheticFileSystem::setModificationTime(InodeIdentifier, dword timestamp)
{
    (void) timestamp;
    kprintf("FIXME: Implement SyntheticFileSystem::setModificationTime().\n");
    return false;
}

InodeIdentifier SyntheticFileSystem::createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t mode, unsigned size)
{
    (void) parentInode;
    (void) name;
    (void) mode;
    (void) size;
    kprintf("FIXME: Implement SyntheticFileSystem::createDirectoryInode().\n");
    return { };
}

bool SyntheticFileSystem::writeInode(InodeIdentifier, const ByteBuffer&)
{
    kprintf("FIXME: Implement SyntheticFileSystem::writeInode().\n");
    return false;
}

Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer, FileDescriptor* handle) const
{
    ASSERT(inode.fileSystemID() == id());
#ifdef SYNTHFS_DEBUG
    kprintf("synthfs: readInode %u\n", inode.index());
#endif
    ASSERT(offset >= 0);
    ASSERT(buffer);

    const File* found_file;
    {
        InterruptDisabler disabler;
        auto it = m_inodes.find(inode.index());
        if (it == m_inodes.end())
            return false;
        found_file = (*it).value.ptr();
    }
    const File& file = *found_file;
    ByteBuffer generatedData;
    if (file.generator) {
        if (!handle) {
            generatedData = file.generator();
        } else {
            if (!handle->generatorCache())
                handle->generatorCache() = file.generator();
            generatedData = handle->generatorCache();
        }
    }

    auto* data = generatedData ? &generatedData : &file.data;
    Unix::ssize_t nread = min(static_cast<Unix::off_t>(data->size() - offset), static_cast<Unix::off_t>(count));
    memcpy(buffer, data->pointer() + offset, nread);
    if (nread == 0 && handle && handle->generatorCache())
        handle->generatorCache().clear();
    return nread;
}

InodeIdentifier SyntheticFileSystem::makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t)
{
    (void) parentInode;
    (void) name;
    kprintf("FIXME: Implement SyntheticFileSystem::makeDirectory().\n");
    return { };
}

auto SyntheticFileSystem::generateInodeIndex() -> InodeIndex
{
    return m_nextInodeIndex++;
}

InodeIdentifier SyntheticFileSystem::findParentOfInode(InodeIdentifier inode) const
{
    auto it = m_inodes.find(inode.index());
    if (it == m_inodes.end())
        return { };
    return (*it).value->parent;
}