FileSystem.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <AK/Assertions.h>
  2. #include <AK/HashMap.h>
  3. #include "FileSystem.h"
  4. static dword s_lastFileSystemID = 0;
  5. static HashMap<dword, FileSystem*>& fileSystems()
  6. {
  7. static auto* map = new HashMap<dword, FileSystem*>();
  8. return *map;
  9. }
  10. FileSystem::FileSystem()
  11. : m_id(++s_lastFileSystemID)
  12. {
  13. fileSystems().set(m_id, this);
  14. }
  15. FileSystem::~FileSystem()
  16. {
  17. fileSystems().remove(m_id);
  18. }
  19. FileSystem* FileSystem::fromID(dword id)
  20. {
  21. auto it = fileSystems().find(id);
  22. if (it != fileSystems().end())
  23. return (*it).value;
  24. return nullptr;
  25. }
  26. InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode, const String& name) const
  27. {
  28. InodeIdentifier foundInode;
  29. enumerateDirectoryInode(inode, [&] (const DirectoryEntry& entry) {
  30. if (entry.name == name) {
  31. foundInode = entry.inode;
  32. return false;
  33. }
  34. return true;
  35. });
  36. return foundInode;
  37. }
  38. ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode) const
  39. {
  40. ASSERT(inode.fileSystemID() == id());
  41. auto metadata = inodeMetadata(inode);
  42. if (!metadata.isValid()) {
  43. printf("[fs] readInode: metadata lookup for inode %u failed\n", inode.index());
  44. return nullptr;
  45. }
  46. auto contents = ByteBuffer::createUninitialized(metadata.size);
  47. Unix::ssize_t nread;
  48. byte buffer[512];
  49. byte* out = contents.pointer();
  50. Unix::off_t offset = 0;
  51. for (;;) {
  52. nread = readInodeBytes(inode, offset, sizeof(buffer), buffer);
  53. if (nread <= 0)
  54. break;
  55. memcpy(out, buffer, nread);
  56. out += nread;
  57. offset += nread;
  58. }
  59. if (nread < 0) {
  60. printf("[fs] readInode: ERROR: %d\n", nread);
  61. return nullptr;
  62. }
  63. return contents;
  64. }