12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #include <AK/Assertions.h>
- #include <AK/HashMap.h>
- #include "FileSystem.h"
- static dword s_lastFileSystemID = 0;
- static HashMap<dword, FileSystem*>& fileSystems()
- {
- static auto* map = new HashMap<dword, FileSystem*>();
- return *map;
- }
- FileSystem::FileSystem()
- : m_id(++s_lastFileSystemID)
- {
- fileSystems().set(m_id, this);
- }
- FileSystem::~FileSystem()
- {
- fileSystems().remove(m_id);
- }
- FileSystem* FileSystem::fromID(dword id)
- {
- auto it = fileSystems().find(id);
- if (it != fileSystems().end())
- return (*it).value;
- return nullptr;
- }
- InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode, const String& name) const
- {
- InodeIdentifier foundInode;
- enumerateDirectoryInode(inode, [&] (const DirectoryEntry& entry) {
- if (entry.name == name) {
- foundInode = entry.inode;
- return false;
- }
- return true;
- });
- return foundInode;
- }
- ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const
- {
- ASSERT(inode.fileSystemID() == id());
- auto metadata = inodeMetadata(inode);
- if (!metadata.isValid()) {
- printf("[fs] readInode: metadata lookup for inode %u failed\n", inode.index());
- 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) {
- printf("[fs] readInode: ERROR: %d\n", nread);
- return nullptr;
- }
- return contents;
- }
|