2018-10-10 12:53:07 +03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include "FileSystem.h"
|
|
|
|
|
2018-10-19 12:20:49 +03:00
|
|
|
static dword s_lastFileSystemID;
|
2018-11-15 19:13:10 +03:00
|
|
|
static HashMap<dword, FS*>* map;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
static HashMap<dword, FS*>& fileSystems()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-10-17 11:55:43 +03:00
|
|
|
if (!map)
|
2018-11-15 19:13:10 +03:00
|
|
|
map = new HashMap<dword, FS*>();
|
2018-10-10 12:53:07 +03:00
|
|
|
return *map;
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
void FS::initializeGlobals()
|
2018-10-19 12:20:49 +03:00
|
|
|
{
|
|
|
|
s_lastFileSystemID = 0;
|
|
|
|
map = 0;
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
FS::FS()
|
2018-10-10 12:53:07 +03:00
|
|
|
: m_id(++s_lastFileSystemID)
|
|
|
|
{
|
|
|
|
fileSystems().set(m_id, this);
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
FS::~FS()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2018-10-13 15:26:37 +03:00
|
|
|
fileSystems().remove(m_id);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
FS* FS::fromID(dword id)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
ByteBuffer FS::readEntireInode(InodeIdentifier inode, FileDescriptor* handle) const
|
2018-10-15 01:16:14 +03:00
|
|
|
{
|
2018-11-15 17:10:12 +03:00
|
|
|
ASSERT(inode.fsid() == id());
|
2018-10-15 01:16:14 +03:00
|
|
|
|
|
|
|
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());
|
2018-10-15 01:16:14 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-28 16:11:51 +03:00
|
|
|
size_t initialSize = metadata.size ? metadata.size : 4096;
|
|
|
|
auto contents = ByteBuffer::createUninitialized(initialSize);
|
2018-10-15 01:16:14 +03:00
|
|
|
|
|
|
|
Unix::ssize_t nread;
|
2018-10-29 23:54:11 +03:00
|
|
|
byte buffer[4096];
|
2018-10-15 01:16:14 +03:00
|
|
|
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);
|
2018-10-29 23:54:11 +03:00
|
|
|
//kprintf("nread: %u, bufsiz: %u, initialSize: %u\n", nread, sizeof(buffer), initialSize);
|
2018-11-09 12:03:21 +03:00
|
|
|
ASSERT(nread <= (Unix::ssize_t)sizeof(buffer));
|
2018-10-15 01:16:14 +03:00
|
|
|
if (nread <= 0)
|
|
|
|
break;
|
|
|
|
memcpy(out, buffer, nread);
|
|
|
|
out += nread;
|
|
|
|
offset += nread;
|
2018-11-09 12:03:21 +03:00
|
|
|
ASSERT(offset <= (Unix::ssize_t)initialSize); // FIXME: Support dynamically growing the buffer.
|
2018-10-15 01:16:14 +03:00
|
|
|
}
|
|
|
|
if (nread < 0) {
|
2018-10-17 11:55:43 +03:00
|
|
|
kprintf("[fs] readInode: ERROR: %d\n", nread);
|
2018-10-15 01:16:14 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:54:11 +03:00
|
|
|
contents.trim(offset);
|
2018-10-15 01:16:14 +03:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, byte ft)
|
2018-11-18 16:57:41 +03:00
|
|
|
: name_length(strlen(n))
|
2018-11-13 02:17:30 +03:00
|
|
|
, inode(i)
|
|
|
|
, fileType(ft)
|
|
|
|
{
|
|
|
|
memcpy(name, n, name_length);
|
|
|
|
name[name_length] = '\0';
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
FS::DirectoryEntry::DirectoryEntry(const char* n, Unix::size_t nl, InodeIdentifier i, byte ft)
|
2018-11-13 02:17:30 +03:00
|
|
|
: name_length(nl)
|
|
|
|
, inode(i)
|
|
|
|
, fileType(ft)
|
|
|
|
{
|
|
|
|
memcpy(name, n, nl);
|
|
|
|
name[nl] = '\0';
|
|
|
|
}
|
2018-11-13 15:02:39 +03:00
|
|
|
|
|
|
|
CoreInode::~CoreInode()
|
|
|
|
{
|
|
|
|
}
|