FileSystem.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. FS::FS()
  23. : m_fsid(++s_lastFileSystemID)
  24. {
  25. all_fses().set(m_fsid, this);
  26. }
  27. FS::~FS()
  28. {
  29. all_fses().remove(m_fsid);
  30. }
  31. FS* FS::from_fsid(dword id)
  32. {
  33. auto it = all_fses().find(id);
  34. if (it != all_fses().end())
  35. return (*it).value;
  36. return nullptr;
  37. }
  38. ByteBuffer Inode::read_entire(FileDescriptor* descriptor) const
  39. {
  40. size_t initial_size = metadata().size ? metadata().size : 4096;
  41. StringBuilder builder(initial_size);
  42. ssize_t nread;
  43. byte buffer[4096];
  44. off_t offset = 0;
  45. for (;;) {
  46. nread = read_bytes(offset, sizeof(buffer), buffer, descriptor);
  47. ASSERT(nread <= (ssize_t)sizeof(buffer));
  48. if (nread <= 0)
  49. break;
  50. builder.append((const char*)buffer, nread);
  51. offset += nread;
  52. }
  53. if (nread < 0) {
  54. kprintf("Inode::read_entire: ERROR: %d\n", nread);
  55. return nullptr;
  56. }
  57. return builder.to_byte_buffer();
  58. }
  59. FS::DirectoryEntry::DirectoryEntry(const char* n, InodeIdentifier i, byte ft)
  60. : name_length(strlen(n))
  61. , inode(i)
  62. , file_type(ft)
  63. {
  64. memcpy(name, n, name_length);
  65. name[name_length] = '\0';
  66. }
  67. FS::DirectoryEntry::DirectoryEntry(const char* n, size_t nl, InodeIdentifier i, byte ft)
  68. : name_length(nl)
  69. , inode(i)
  70. , file_type(ft)
  71. {
  72. memcpy(name, n, nl);
  73. name[nl] = '\0';
  74. }
  75. Inode::Inode(FS& fs, unsigned index)
  76. : m_fs(fs)
  77. , m_index(index)
  78. {
  79. all_inodes().set(this);
  80. }
  81. Inode::~Inode()
  82. {
  83. all_inodes().remove(this);
  84. }
  85. void Inode::will_be_destroyed()
  86. {
  87. if (m_metadata_dirty)
  88. flush_metadata();
  89. }
  90. void Inode::inode_contents_changed(off_t offset, size_t size, const byte* data)
  91. {
  92. if (m_vmo)
  93. m_vmo->inode_contents_changed(Badge<Inode>(), offset, size, data);
  94. }
  95. void Inode::inode_size_changed(size_t old_size, size_t new_size)
  96. {
  97. if (m_vmo)
  98. m_vmo->inode_size_changed(Badge<Inode>(), old_size, new_size);
  99. }
  100. int Inode::set_atime(time_t)
  101. {
  102. return -ENOTIMPL;
  103. }
  104. int Inode::set_ctime(time_t)
  105. {
  106. return -ENOTIMPL;
  107. }
  108. int Inode::set_mtime(time_t)
  109. {
  110. return -ENOTIMPL;
  111. }
  112. int Inode::increment_link_count()
  113. {
  114. return -ENOTIMPL;
  115. }
  116. int Inode::decrement_link_count()
  117. {
  118. return -ENOTIMPL;
  119. }
  120. void FS::sync()
  121. {
  122. for (auto* inode : all_inodes()) {
  123. if (inode->is_metadata_dirty())
  124. inode->flush_metadata();
  125. }
  126. }
  127. void Inode::set_vmo(RetainPtr<VMObject>&& vmo)
  128. {
  129. m_vmo = move(vmo);
  130. }