summaryrefslogtreecommitdiff
path: root/VirtualFileSystem/SyntheticFileSystem.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2018-10-10 11:53:07 +0200
committerAndreas Kling <awesomekling@gmail.com>2018-10-10 11:53:07 +0200
commit5a300551574451fbf509685d11095bda4fcb20be (patch)
tree7bd68b5b0bc9daab6899be52dc694b7162dc6b89 /VirtualFileSystem/SyntheticFileSystem.cpp
downloadserenity-5a300551574451fbf509685d11095bda4fcb20be.zip
Import all this stuff into a single repo called Serenity.
Diffstat (limited to 'VirtualFileSystem/SyntheticFileSystem.cpp')
-rw-r--r--VirtualFileSystem/SyntheticFileSystem.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/VirtualFileSystem/SyntheticFileSystem.cpp b/VirtualFileSystem/SyntheticFileSystem.cpp
new file mode 100644
index 0000000000..2d12eae04b
--- /dev/null
+++ b/VirtualFileSystem/SyntheticFileSystem.cpp
@@ -0,0 +1,123 @@
+#include "SyntheticFileSystem.h"
+
+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(), 1 };
+ rootDir->metadata.mode = 0040555;
+ rootDir->metadata.uid = 0;
+ rootDir->metadata.gid = 0;
+ rootDir->metadata.size = 0;
+ rootDir->metadata.mtime = mepoch;
+ m_files.append(std::move(rootDir));
+
+ addFile(createTextFile("file", "I'm a synthetic file!\n"));
+ addFile(createTextFile("message", "Hey! This isn't my bottle!\n"));
+ return true;
+}
+
+auto SyntheticFileSystem::createTextFile(String&& name, String&& text) -> OwnPtr<File>
+{
+ auto file = make<File>();
+ file->data = text.toByteBuffer();
+ file->name = std::move(name);
+ file->metadata.size = file->data.size();
+ file->metadata.uid = 100;
+ file->metadata.gid = 200;
+ file->metadata.mode = 0100644;
+ file->metadata.mtime = mepoch;
+ return file;
+}
+
+void SyntheticFileSystem::addFile(OwnPtr<File>&& file)
+{
+ ASSERT(file);
+ file->metadata.inode = { id(), m_files.size() + 1 };
+ m_files.append(std::move(file));
+}
+
+const char* SyntheticFileSystem::className() const
+{
+ return "synthfs";
+}
+
+InodeIdentifier SyntheticFileSystem::rootInode() const
+{
+ return { id(), 1 };
+}
+
+ByteBuffer SyntheticFileSystem::readInode(InodeIdentifier inode) const
+{
+ ASSERT(inode.fileSystemID() == id());
+#ifdef SYNTHFS_DEBUG
+ printf("[synthfs] readInode %u\n", inode.index());
+#endif
+ ASSERT(inode.index() != 1);
+ ASSERT(inode.index() <= m_files.size());
+ return m_files[inode.index() - 1]->data;
+}
+
+bool SyntheticFileSystem::enumerateDirectoryInode(InodeIdentifier inode, std::function<bool(const DirectoryEntry&)> callback) const
+{
+ ASSERT(inode.fileSystemID() == id());
+#ifdef SYNTHFS_DEBUG
+ printf("[synthfs] enumerateDirectoryInode %u\n", inode.index());
+#endif
+ if (inode.index() != 1)
+ return false;
+
+ callback({ ".", m_files[0]->metadata.inode });
+ callback({ "..", m_files[0]->metadata.inode });
+
+ for (unsigned i = 1; i < m_files.size(); ++i)
+ callback({ m_files[i]->name, m_files[i]->metadata.inode });
+ return true;
+}
+
+InodeMetadata SyntheticFileSystem::inodeMetadata(InodeIdentifier inode) const
+{
+ ASSERT(inode.fileSystemID() == id());
+#ifdef SYNTHFS_DEBUG
+ printf("[synthfs] inodeMetadata(%u)\n", inode.index);
+#endif
+ if (inode.index() == 0 || inode.index() > m_files.size())
+ return InodeMetadata();
+ return m_files[inode.index() - 1]->metadata;
+}
+
+bool SyntheticFileSystem::setModificationTime(InodeIdentifier, dword timestamp)
+{
+ (void) timestamp;
+ printf("FIXME: Implement SyntheticFileSystem::setModificationTime().\n");
+ return false;
+}
+
+InodeIdentifier SyntheticFileSystem::createInode(InodeIdentifier parentInode, const String& name, word mode)
+{
+ (void) parentInode;
+ (void) name;
+ (void) mode;
+ printf("FIXME: Implement SyntheticFileSystem::createDirectoryInode().\n");
+ return { };
+}
+
+bool SyntheticFileSystem::writeInode(InodeIdentifier, const ByteBuffer&)
+{
+ printf("FIXME: Implement SyntheticFileSystem::writeInode().\n");
+ return false;
+}