FileSystem.cpp 3.2 KB

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