2018-10-10 11:53:07 +02:00
|
|
|
#include "SyntheticFileSystem.h"
|
2018-10-27 00:14:24 +02:00
|
|
|
#include "FileHandle.h"
|
2018-10-14 23:13:45 +02:00
|
|
|
#include <AK/StdLib.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-29 21:54:11 +01:00
|
|
|
#ifndef SERENITY
|
|
|
|
typedef int InterruptDisabler;
|
|
|
|
#define ASSERT_INTERRUPTS_DISABLED()
|
|
|
|
#endif
|
|
|
|
|
2018-10-23 11:57:38 +02:00
|
|
|
//#define SYNTHFS_DEBUG
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
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>();
|
2018-10-26 17:42:12 +02:00
|
|
|
rootDir->metadata.inode = { id(), RootInodeIndex };
|
|
|
|
rootDir->parent = { id(), RootInodeIndex };
|
2018-10-10 11:53:07 +02:00
|
|
|
rootDir->metadata.mode = 0040555;
|
|
|
|
rootDir->metadata.uid = 0;
|
|
|
|
rootDir->metadata.gid = 0;
|
|
|
|
rootDir->metadata.size = 0;
|
|
|
|
rootDir->metadata.mtime = mepoch;
|
2018-10-26 17:42:12 +02:00
|
|
|
m_inodes.set(RootInodeIndex, move(rootDir));
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-23 11:57:38 +02:00
|
|
|
#ifndef SERENITY
|
2018-10-10 11:53:07 +02:00
|
|
|
addFile(createTextFile("file", "I'm a synthetic file!\n"));
|
|
|
|
addFile(createTextFile("message", "Hey! This isn't my bottle!\n"));
|
2018-10-29 21:54:11 +01:00
|
|
|
addFile(createGeneratedFile("lunk", [] { return String("/home/andreas/file1").toByteBuffer(); }, 00120777));
|
2018-10-23 11:57:38 +02:00
|
|
|
#endif
|
2018-10-10 11:53:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:42:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-29 22:43:39 +01:00
|
|
|
auto SyntheticFileSystem::createTextFile(String&& name, ByteBuffer&& contents, Unix::mode_t mode) -> OwnPtr<File>
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
auto file = make<File>();
|
2018-10-29 22:43:39 +01:00
|
|
|
file->data = contents;
|
2018-10-23 11:57:38 +02:00
|
|
|
file->name = move(name);
|
2018-10-10 11:53:07 +02:00
|
|
|
file->metadata.size = file->data.size();
|
|
|
|
file->metadata.uid = 100;
|
|
|
|
file->metadata.gid = 200;
|
2018-10-29 22:43:39 +01:00
|
|
|
file->metadata.mode = mode;
|
2018-10-10 11:53:07 +02:00
|
|
|
file->metadata.mtime = mepoch;
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:11:51 +01:00
|
|
|
auto SyntheticFileSystem::createGeneratedFile(String&& name, Function<ByteBuffer()>&& generator, Unix::mode_t mode) -> OwnPtr<File>
|
2018-10-23 11:57:38 +02:00
|
|
|
{
|
|
|
|
auto file = make<File>();
|
|
|
|
file->generator = move(generator);
|
|
|
|
file->name = move(name);
|
|
|
|
file->metadata.size = 0;
|
|
|
|
file->metadata.uid = 0;
|
|
|
|
file->metadata.gid = 0;
|
2018-10-28 14:11:51 +01:00
|
|
|
file->metadata.mode = mode;
|
2018-10-23 11:57:38 +02:00
|
|
|
file->metadata.mtime = mepoch;
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2018-10-26 17:42:12 +02:00
|
|
|
InodeIdentifier SyntheticFileSystem::addFile(OwnPtr<File>&& file, InodeIndex parent)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-10-26 22:32:35 +02:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(file);
|
2018-10-26 17:42:12 +02:00
|
|
|
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)
|
|
|
|
{
|
2018-10-26 22:32:35 +02:00
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
2018-10-26 17:42:12 +02:00
|
|
|
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;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* SyntheticFileSystem::className() const
|
|
|
|
{
|
|
|
|
return "synthfs";
|
|
|
|
}
|
|
|
|
|
|
|
|
InodeIdentifier SyntheticFileSystem::rootInode() const
|
|
|
|
{
|
|
|
|
return { id(), 1 };
|
|
|
|
}
|
|
|
|
|
2018-10-17 10:55:43 +02:00
|
|
|
bool SyntheticFileSystem::enumerateDirectoryInode(InodeIdentifier inode, Function<bool(const DirectoryEntry&)> callback) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
2018-10-26 22:32:35 +02:00
|
|
|
InterruptDisabler disabler;
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(inode.fileSystemID() == id());
|
|
|
|
#ifdef SYNTHFS_DEBUG
|
2018-10-23 11:57:38 +02:00
|
|
|
kprintf("[synthfs] enumerateDirectoryInode %u\n", inode.index());
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
2018-10-26 17:42:12 +02:00
|
|
|
|
|
|
|
auto it = m_inodes.find(inode.index());
|
|
|
|
if (it == m_inodes.end())
|
|
|
|
return false;
|
|
|
|
const File& synInode = *(*it).value;
|
|
|
|
if (!synInode.metadata.isDirectory())
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
|
2018-10-26 17:42:12 +02:00
|
|
|
callback({ ".", synInode.metadata.inode });
|
|
|
|
callback({ "..", synInode.parent });
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2018-10-28 14:11:51 +01:00
|
|
|
for (auto& child : synInode.children)
|
2018-10-26 17:42:12 +02:00
|
|
|
callback({ child->name, child->metadata.inode });
|
2018-10-10 11:53:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
InodeMetadata SyntheticFileSystem::inodeMetadata(InodeIdentifier inode) const
|
|
|
|
{
|
2018-10-26 22:32:35 +02:00
|
|
|
InterruptDisabler disabler;
|
2018-10-10 11:53:07 +02:00
|
|
|
ASSERT(inode.fileSystemID() == id());
|
|
|
|
#ifdef SYNTHFS_DEBUG
|
2018-10-23 11:57:38 +02:00
|
|
|
kprintf("[synthfs] inodeMetadata(%u)\n", inode.index());
|
2018-10-10 11:53:07 +02:00
|
|
|
#endif
|
2018-10-26 17:42:12 +02:00
|
|
|
|
|
|
|
auto it = m_inodes.find(inode.index());
|
|
|
|
if (it == m_inodes.end())
|
|
|
|
return { };
|
|
|
|
return (*it).value->metadata;
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SyntheticFileSystem::setModificationTime(InodeIdentifier, dword timestamp)
|
|
|
|
{
|
|
|
|
(void) timestamp;
|
2018-10-23 11:57:38 +02:00
|
|
|
kprintf("FIXME: Implement SyntheticFileSystem::setModificationTime().\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:35:03 +02:00
|
|
|
InodeIdentifier SyntheticFileSystem::createInode(InodeIdentifier parentInode, const String& name, Unix::mode_t mode, unsigned size)
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
(void) parentInode;
|
|
|
|
(void) name;
|
|
|
|
(void) mode;
|
2018-10-26 17:42:12 +02:00
|
|
|
(void) size;
|
2018-10-23 11:57:38 +02:00
|
|
|
kprintf("FIXME: Implement SyntheticFileSystem::createDirectoryInode().\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return { };
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SyntheticFileSystem::writeInode(InodeIdentifier, const ByteBuffer&)
|
|
|
|
{
|
2018-10-23 11:57:38 +02:00
|
|
|
kprintf("FIXME: Implement SyntheticFileSystem::writeInode().\n");
|
2018-10-10 11:53:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-14 21:19:27 +02:00
|
|
|
|
2018-10-27 00:14:24 +02:00
|
|
|
Unix::ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer, FileHandle* handle) const
|
2018-10-14 21:19:27 +02:00
|
|
|
{
|
2018-10-26 22:32:35 +02:00
|
|
|
InterruptDisabler disabler;
|
|
|
|
|
2018-10-14 23:13:45 +02:00
|
|
|
ASSERT(inode.fileSystemID() == id());
|
|
|
|
#ifdef SYNTHFS_DEBUG
|
2018-10-23 11:57:38 +02:00
|
|
|
kprintf("[synthfs] readInode %u\n", inode.index());
|
2018-10-14 23:13:45 +02:00
|
|
|
#endif
|
|
|
|
ASSERT(offset >= 0);
|
|
|
|
ASSERT(buffer);
|
2018-10-28 12:20:25 +01:00
|
|
|
|
2018-10-26 17:42:12 +02:00
|
|
|
auto it = m_inodes.find(inode.index());
|
|
|
|
if (it == m_inodes.end())
|
|
|
|
return false;
|
|
|
|
const File& file = *(*it).value;
|
2018-10-23 11:57:38 +02:00
|
|
|
ByteBuffer generatedData;
|
2018-10-27 00:14:24 +02:00
|
|
|
if (file.generator) {
|
|
|
|
if (!handle) {
|
|
|
|
generatedData = file.generator();
|
|
|
|
} else {
|
|
|
|
if (!handle->generatorCache())
|
|
|
|
handle->generatorCache() = file.generator();
|
|
|
|
generatedData = handle->generatorCache();
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 11:57:38 +02:00
|
|
|
|
2018-10-28 14:11:51 +01:00
|
|
|
auto* data = generatedData ? &generatedData : &file.data;
|
2018-10-23 11:57:38 +02:00
|
|
|
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);
|
2018-10-27 00:14:24 +02:00
|
|
|
if (nread == 0 && handle && handle->generatorCache())
|
|
|
|
handle->generatorCache().clear();
|
2018-10-14 23:13:45 +02:00
|
|
|
return nread;
|
2018-10-14 21:19:27 +02:00
|
|
|
}
|
2018-10-16 00:35:03 +02:00
|
|
|
|
|
|
|
InodeIdentifier SyntheticFileSystem::makeDirectory(InodeIdentifier parentInode, const String& name, Unix::mode_t)
|
|
|
|
{
|
2018-10-26 17:42:12 +02:00
|
|
|
(void) parentInode;
|
|
|
|
(void) name;
|
2018-10-23 11:57:38 +02:00
|
|
|
kprintf("FIXME: Implement SyntheticFileSystem::makeDirectory().\n");
|
2018-10-16 00:35:03 +02:00
|
|
|
return { };
|
|
|
|
}
|
2018-10-26 17:42:12 +02:00
|
|
|
|
|
|
|
auto SyntheticFileSystem::generateInodeIndex() -> InodeIndex
|
|
|
|
{
|
|
|
|
return m_nextInodeIndex++;
|
|
|
|
}
|
2018-10-28 12:20:25 +01:00
|
|
|
|
|
|
|
InodeIdentifier SyntheticFileSystem::findParentOfInode(InodeIdentifier inode) const
|
|
|
|
{
|
|
|
|
auto it = m_inodes.find(inode.index());
|
|
|
|
if (it == m_inodes.end())
|
|
|
|
return { };
|
|
|
|
return (*it).value->parent;
|
|
|
|
}
|