2018-10-10 11:53:07 +02:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include "FileSystem.h"
|
|
|
|
|
2018-10-19 11:20:49 +02:00
|
|
|
static dword s_lastFileSystemID;
|
|
|
|
static HashMap<dword, FileSystem*>* map;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
static HashMap<dword, FileSystem*>& fileSystems()
|
|
|
|
{
|
2018-10-17 10:55:43 +02:00
|
|
|
if (!map)
|
|
|
|
map = new HashMap<dword, FileSystem*>();
|
2018-10-10 11:53:07 +02:00
|
|
|
return *map;
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:20:49 +02:00
|
|
|
void FileSystem::initializeGlobals()
|
|
|
|
{
|
|
|
|
s_lastFileSystemID = 0;
|
|
|
|
map = 0;
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
FileSystem::FileSystem()
|
|
|
|
: m_id(++s_lastFileSystemID)
|
|
|
|
{
|
|
|
|
fileSystems().set(m_id, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileSystem::~FileSystem()
|
|
|
|
{
|
2018-10-13 14:26:37 +02:00
|
|
|
fileSystems().remove(m_id);
|
2018-10-10 11:53:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FileSystem* FileSystem::fromID(dword id)
|
|
|
|
{
|
|
|
|
auto it = fileSystems().find(id);
|
|
|
|
if (it != fileSystems().end())
|
|
|
|
return (*it).value;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:16:14 +02:00
|
|
|
InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode, const String& name) const
|
2018-10-10 11:53:07 +02:00
|
|
|
{
|
|
|
|
InodeIdentifier foundInode;
|
|
|
|
enumerateDirectoryInode(inode, [&] (const DirectoryEntry& entry) {
|
|
|
|
if (entry.name == name) {
|
|
|
|
foundInode = entry.inode;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
return foundInode;
|
|
|
|
}
|
|
|
|
|
2018-10-15 00:16:14 +02:00
|
|
|
ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const
|
|
|
|
{
|
|
|
|
ASSERT(inode.fileSystemID() == id());
|
|
|
|
|
|
|
|
auto metadata = inodeMetadata(inode);
|
|
|
|
if (!metadata.isValid()) {
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("[fs] readInode: metadata lookup for inode %u failed\n", inode.index());
|
2018-10-15 00:16:14 +02:00
|
|
|
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);
|
|
|
|
if (nread <= 0)
|
|
|
|
break;
|
|
|
|
memcpy(out, buffer, nread);
|
|
|
|
out += nread;
|
|
|
|
offset += nread;
|
|
|
|
}
|
|
|
|
if (nread < 0) {
|
2018-10-17 10:55:43 +02:00
|
|
|
kprintf("[fs] readInode: ERROR: %d\n", nread);
|
2018-10-15 00:16:14 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|