FileSystem.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 CoreInode::read_entire(FileDescriptor* descriptor)
  59. {
  60. return fs().readEntireInode(identifier(), descriptor);
  61. /*
  62. size_t initial_size = metadata().size ? metadata().size : 4096;
  63. auto contents = ByteBuffer::createUninitialized(initial_size);
  64. Unix::ssize_t nread;
  65. byte buffer[4096];
  66. byte* out = contents.pointer();
  67. Unix::off_t offset = 0;
  68. for (;;) {
  69. nread = read_bytes(offset, sizeof(buffer), buffer, descriptor);
  70. //kprintf("nread: %u, bufsiz: %u, initial_size: %u\n", nread, sizeof(buffer), initial_size);
  71. ASSERT(nread <= (Unix::ssize_t)sizeof(buffer));
  72. if (nread <= 0)
  73. break;
  74. memcpy(out, buffer, nread);
  75. out += nread;
  76. offset += nread;
  77. ASSERT(offset <= (Unix::ssize_t)initial_size); // FIXME: Support dynamically growing the buffer.
  78. }
  79. if (nread < 0) {
  80. kprintf("CoreInode::read_entire: ERROR: %d\n", nread);
  81. return nullptr;
  82. }
  83. contents.trim(offset);
  84. return contents;
  85. */
  86. }
  87. ByteBuffer FileSystem::readEntireInode(InodeIdentifier inode, FileDescriptor* handle) const
  88. {
  89. ASSERT(inode.fileSystemID() == id());
  90. auto metadata = inodeMetadata(inode);
  91. if (!metadata.isValid()) {
  92. kprintf("[fs] readInode: metadata lookup for inode %u failed\n", inode.index());
  93. return nullptr;
  94. }
  95. size_t initialSize = metadata.size ? metadata.size : 4096;
  96. auto contents = ByteBuffer::createUninitialized(initialSize);
  97. Unix::ssize_t nread;
  98. byte buffer[4096];
  99. byte* out = contents.pointer();
  100. Unix::off_t offset = 0;
  101. for (;;) {
  102. nread = readInodeBytes(inode, offset, sizeof(buffer), buffer, handle);
  103. //kprintf("nread: %u, bufsiz: %u, initialSize: %u\n", nread, sizeof(buffer), initialSize);
  104. ASSERT(nread <= (Unix::ssize_t)sizeof(buffer));
  105. if (nread <= 0)
  106. break;
  107. memcpy(out, buffer, nread);
  108. out += nread;
  109. offset += nread;
  110. ASSERT(offset <= (Unix::ssize_t)initialSize); // FIXME: Support dynamically growing the buffer.
  111. }
  112. if (nread < 0) {
  113. kprintf("[fs] readInode: ERROR: %d\n", nread);
  114. return nullptr;
  115. }
  116. contents.trim(offset);
  117. return contents;
  118. }
  119. FileSystem::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, byte ft)
  120. : name_length(strlen(name))
  121. , inode(i)
  122. , fileType(ft)
  123. {
  124. memcpy(name, n, name_length);
  125. name[name_length] = '\0';
  126. }
  127. FileSystem::DirectoryEntry::DirectoryEntry(const char* n, Unix::size_t nl, InodeIdentifier i, byte ft)
  128. : name_length(nl)
  129. , inode(i)
  130. , fileType(ft)
  131. {
  132. memcpy(name, n, nl);
  133. name[nl] = '\0';
  134. }
  135. CoreInode::~CoreInode()
  136. {
  137. }