FileSystem.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <AK/Assertions.h>
  2. #include <AK/HashMap.h>
  3. #include "FileSystem.h"
  4. static dword s_lastFileSystemID;
  5. static HashMap<dword, FileSystem*>* map;
  6. static HashMap<dword, FileSystem*>& fileSystems()
  7. {
  8. if (!map)
  9. map = new HashMap<dword, FileSystem*>();
  10. return *map;
  11. }
  12. void FileSystem::initializeGlobals()
  13. {
  14. s_lastFileSystemID = 0;
  15. map = 0;
  16. }
  17. FileSystem::FileSystem()
  18. : m_id(++s_lastFileSystemID)
  19. {
  20. fileSystems().set(m_id, this);
  21. }
  22. FileSystem::~FileSystem()
  23. {
  24. fileSystems().remove(m_id);
  25. }
  26. FileSystem* FileSystem::fromID(dword id)
  27. {
  28. auto it = fileSystems().find(id);
  29. if (it != fileSystems().end())
  30. return (*it).value;
  31. return nullptr;
  32. }
  33. InodeIdentifier FileSystem::childOfDirectoryInodeWithName(InodeIdentifier inode, const String& name) const
  34. {
  35. InodeIdentifier foundInode;
  36. enumerateDirectoryInode(inode, [&] (const DirectoryEntry& entry) {
  37. if (!strcmp(entry.name, name.characters())) {
  38. foundInode = entry.inode;
  39. return false;
  40. }
  41. return true;
  42. });
  43. return foundInode;
  44. }
  45. String FileSystem::nameOfChildInDirectory(InodeIdentifier parent, InodeIdentifier child) const
  46. {
  47. String name;
  48. bool success = enumerateDirectoryInode(parent, [&] (auto& entry) {
  49. if (entry.inode == child) {
  50. name = entry.name;
  51. return false;
  52. }
  53. return true;
  54. });
  55. ASSERT(success);
  56. return name;
  57. }
  58. ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode, FileDescriptor* handle) const
  59. {
  60. ASSERT(inode.fileSystemID() == id());
  61. auto metadata = inodeMetadata(inode);
  62. if (!metadata.isValid()) {
  63. kprintf("[fs] readInode: metadata lookup for inode %u failed\n", inode.index());
  64. return nullptr;
  65. }
  66. size_t initialSize = metadata.size ? metadata.size : 4096;
  67. auto contents = ByteBuffer::createUninitialized(initialSize);
  68. Unix::ssize_t nread;
  69. byte buffer[4096];
  70. byte* out = contents.pointer();
  71. Unix::off_t offset = 0;
  72. for (;;) {
  73. nread = readInodeBytes(inode, offset, sizeof(buffer), buffer, handle);
  74. //kprintf("nread: %u, bufsiz: %u, initialSize: %u\n", nread, sizeof(buffer), initialSize);
  75. ASSERT(nread <= (Unix::ssize_t)sizeof(buffer));
  76. if (nread <= 0)
  77. break;
  78. memcpy(out, buffer, nread);
  79. out += nread;
  80. offset += nread;
  81. ASSERT(offset <= (Unix::ssize_t)initialSize); // FIXME: Support dynamically growing the buffer.
  82. }
  83. if (nread < 0) {
  84. kprintf("[fs] readInode: ERROR: %d\n", nread);
  85. return nullptr;
  86. }
  87. contents.trim(offset);
  88. return contents;
  89. }
  90. FileSystem::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, byte ft)
  91. : name_length(strlen(name))
  92. , inode(i)
  93. , fileType(ft)
  94. {
  95. memcpy(name, n, name_length);
  96. name[name_length] = '\0';
  97. }
  98. FileSystem::DirectoryEntry::DirectoryEntry(const char* n, Unix::size_t nl, InodeIdentifier i, byte ft)
  99. : name_length(nl)
  100. , inode(i)
  101. , fileType(ft)
  102. {
  103. memcpy(name, n, nl);
  104. name[nl] = '\0';
  105. }