FileSystem.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <AK/Assertions.h>
  2. #include <AK/HashMap.h>
  3. #include <AK/StringBuilder.h>
  4. #include <LibC/errno_numbers.h>
  5. #include "FileSystem.h"
  6. #include "MemoryManager.h"
  7. static dword s_lastFileSystemID;
  8. static HashMap<dword, FS*>* s_fs_map;
  9. static HashTable<Inode*>* s_inode_set;
  10. static HashMap<dword, FS*>& all_fses()
  11. {
  12. if (!s_fs_map)
  13. s_fs_map = new HashMap<dword, FS*>();
  14. return *s_fs_map;
  15. }
  16. HashTable<Inode*>& all_inodes()
  17. {
  18. if (!s_inode_set)
  19. s_inode_set = new HashTable<Inode*>();
  20. return *s_inode_set;
  21. }
  22. void FS::initialize_globals()
  23. {
  24. s_lastFileSystemID = 0;
  25. s_fs_map = nullptr;
  26. s_inode_set = nullptr;
  27. }
  28. FS::FS()
  29. : m_fsid(++s_lastFileSystemID)
  30. {
  31. all_fses().set(m_fsid, this);
  32. }
  33. FS::~FS()
  34. {
  35. all_fses().remove(m_fsid);
  36. }
  37. FS* FS::from_fsid(dword id)
  38. {
  39. auto it = all_fses().find(id);
  40. if (it != all_fses().end())
  41. return (*it).value;
  42. return nullptr;
  43. }
  44. ByteBuffer Inode::read_entire(FileDescriptor* descriptor) const
  45. {
  46. size_t initial_size = metadata().size ? metadata().size : 4096;
  47. StringBuilder builder(initial_size);
  48. ssize_t nread;
  49. byte buffer[4096];
  50. off_t offset = 0;
  51. for (;;) {
  52. nread = read_bytes(offset, sizeof(buffer), buffer, descriptor);
  53. ASSERT(nread <= (ssize_t)sizeof(buffer));
  54. if (nread <= 0)
  55. break;
  56. builder.append((const char*)buffer, nread);
  57. offset += nread;
  58. }
  59. if (nread < 0) {
  60. kprintf("Inode::read_entire: ERROR: %d\n", nread);
  61. return nullptr;
  62. }
  63. return builder.to_byte_buffer();
  64. }
  65. FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, byte ft)
  66. : name_length(strlen(n))
  67. , inode(i)
  68. , fileType(ft)
  69. {
  70. memcpy(name, n, name_length);
  71. name[name_length] = '\0';
  72. }
  73. FS::DirectoryEntry::DirectoryEntry(const char* n, size_t nl, InodeIdentifier i, byte ft)
  74. : name_length(nl)
  75. , inode(i)
  76. , fileType(ft)
  77. {
  78. memcpy(name, n, nl);
  79. name[nl] = '\0';
  80. }
  81. Inode::Inode(FS& fs, unsigned index)
  82. : m_fs(fs)
  83. , m_index(index)
  84. {
  85. all_inodes().set(this);
  86. }
  87. Inode::~Inode()
  88. {
  89. all_inodes().remove(this);
  90. }
  91. void Inode::will_be_destroyed()
  92. {
  93. if (m_metadata_dirty)
  94. flush_metadata();
  95. }
  96. int Inode::set_atime(time_t)
  97. {
  98. return -ENOTIMPL;
  99. }
  100. int Inode::set_ctime(time_t)
  101. {
  102. return -ENOTIMPL;
  103. }
  104. int Inode::set_mtime(time_t)
  105. {
  106. return -ENOTIMPL;
  107. }
  108. int Inode::increment_link_count()
  109. {
  110. return -ENOTIMPL;
  111. }
  112. int Inode::decrement_link_count()
  113. {
  114. return -ENOTIMPL;
  115. }
  116. void FS::sync()
  117. {
  118. for (auto* inode : all_inodes()) {
  119. if (inode->is_metadata_dirty())
  120. inode->flush_metadata();
  121. }
  122. }
  123. void Inode::set_vmo(RetainPtr<VMObject>&& vmo)
  124. {
  125. m_vmo = move(vmo);
  126. }