ladybird/VirtualFileSystem/FileSystem.cpp

130 lines
3.2 KiB
C++
Raw Normal View History

#include <AK/Assertions.h>
#include <AK/HashMap.h>
#include "FileSystem.h"
static dword s_lastFileSystemID;
static HashMap<dword, FS*>* map;
static HashMap<dword, FS*>& fileSystems()
{
2018-10-17 11:55:43 +03:00
if (!map)
map = new HashMap<dword, FS*>();
return *map;
}
void FS::initializeGlobals()
{
s_lastFileSystemID = 0;
map = 0;
}
FS::FS()
: m_id(++s_lastFileSystemID)
{
fileSystems().set(m_id, this);
}
FS::~FS()
{
fileSystems().remove(m_id);
}
FS* FS::fromID(dword id)
{
auto it = fileSystems().find(id);
if (it != fileSystems().end())
return (*it).value;
return nullptr;
}
2018-11-14 01:44:54 +03:00
ByteBuffer CoreInode::read_entire(FileDescriptor* descriptor)
{
return fs().readEntireInode(identifier(), descriptor);
/*
size_t initial_size = metadata().size ? metadata().size : 4096;
auto contents = ByteBuffer::createUninitialized(initial_size);
Unix::ssize_t nread;
byte buffer[4096];
byte* out = contents.pointer();
Unix::off_t offset = 0;
for (;;) {
nread = read_bytes(offset, sizeof(buffer), buffer, descriptor);
//kprintf("nread: %u, bufsiz: %u, initial_size: %u\n", nread, sizeof(buffer), initial_size);
ASSERT(nread <= (Unix::ssize_t)sizeof(buffer));
if (nread <= 0)
break;
memcpy(out, buffer, nread);
out += nread;
offset += nread;
ASSERT(offset <= (Unix::ssize_t)initial_size); // FIXME: Support dynamically growing the buffer.
}
if (nread < 0) {
kprintf("CoreInode::read_entire: ERROR: %d\n", nread);
return nullptr;
}
contents.trim(offset);
return contents;
*/
}
ByteBuffer FS::readEntireInode(InodeIdentifier inode, FileDescriptor* handle) const
{
2018-11-15 17:10:12 +03:00
ASSERT(inode.fsid() == id());
auto metadata = inodeMetadata(inode);
if (!metadata.isValid()) {
2018-10-17 11:55:43 +03:00
kprintf("[fs] readInode: metadata lookup for inode %u failed\n", inode.index());
return nullptr;
}
size_t initialSize = metadata.size ? metadata.size : 4096;
auto contents = ByteBuffer::createUninitialized(initialSize);
Unix::ssize_t nread;
byte buffer[4096];
byte* out = contents.pointer();
Unix::off_t offset = 0;
for (;;) {
2018-11-15 17:36:35 +03:00
nread = read_inode_bytes(inode, offset, sizeof(buffer), buffer, handle);
//kprintf("nread: %u, bufsiz: %u, initialSize: %u\n", nread, sizeof(buffer), initialSize);
ASSERT(nread <= (Unix::ssize_t)sizeof(buffer));
if (nread <= 0)
break;
memcpy(out, buffer, nread);
out += nread;
offset += nread;
ASSERT(offset <= (Unix::ssize_t)initialSize); // FIXME: Support dynamically growing the buffer.
}
if (nread < 0) {
2018-10-17 11:55:43 +03:00
kprintf("[fs] readInode: ERROR: %d\n", nread);
return nullptr;
}
contents.trim(offset);
return contents;
}
FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, byte ft)
: name_length(strlen(n))
, inode(i)
, fileType(ft)
{
memcpy(name, n, name_length);
name[name_length] = '\0';
}
FS::DirectoryEntry::DirectoryEntry(const char* n, Unix::size_t nl, InodeIdentifier i, byte ft)
: name_length(nl)
, inode(i)
, fileType(ft)
{
memcpy(name, n, nl);
name[nl] = '\0';
}
CoreInode::~CoreInode()
{
}