SyntheticFileSystem.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "SyntheticFileSystem.h"
  2. #include <AK/StdLib.h>
  3. RetainPtr<SyntheticFileSystem> SyntheticFileSystem::create()
  4. {
  5. return adopt(*new SyntheticFileSystem);
  6. }
  7. SyntheticFileSystem::SyntheticFileSystem()
  8. {
  9. }
  10. SyntheticFileSystem::~SyntheticFileSystem()
  11. {
  12. }
  13. bool SyntheticFileSystem::initialize()
  14. {
  15. // Add a File for the root directory.
  16. // FIXME: This needs work.
  17. auto rootDir = make<File>();
  18. rootDir->metadata.inode = { id(), 1 };
  19. rootDir->metadata.mode = 0040555;
  20. rootDir->metadata.uid = 0;
  21. rootDir->metadata.gid = 0;
  22. rootDir->metadata.size = 0;
  23. rootDir->metadata.mtime = mepoch;
  24. m_files.append(std::move(rootDir));
  25. addFile(createTextFile("file", "I'm a synthetic file!\n"));
  26. addFile(createTextFile("message", "Hey! This isn't my bottle!\n"));
  27. return true;
  28. }
  29. auto SyntheticFileSystem::createTextFile(String&& name, String&& text) -> OwnPtr<File>
  30. {
  31. auto file = make<File>();
  32. file->data = text.toByteBuffer();
  33. file->name = std::move(name);
  34. file->metadata.size = file->data.size();
  35. file->metadata.uid = 100;
  36. file->metadata.gid = 200;
  37. file->metadata.mode = 0100644;
  38. file->metadata.mtime = mepoch;
  39. return file;
  40. }
  41. void SyntheticFileSystem::addFile(OwnPtr<File>&& file)
  42. {
  43. ASSERT(file);
  44. file->metadata.inode = { id(), m_files.size() + 1 };
  45. m_files.append(std::move(file));
  46. }
  47. const char* SyntheticFileSystem::className() const
  48. {
  49. return "synthfs";
  50. }
  51. InodeIdentifier SyntheticFileSystem::rootInode() const
  52. {
  53. return { id(), 1 };
  54. }
  55. bool SyntheticFileSystem::enumerateDirectoryInode(InodeIdentifier inode, std::function<bool(const DirectoryEntry&)> callback) const
  56. {
  57. ASSERT(inode.fileSystemID() == id());
  58. #ifdef SYNTHFS_DEBUG
  59. printf("[synthfs] enumerateDirectoryInode %u\n", inode.index());
  60. #endif
  61. if (inode.index() != 1)
  62. return false;
  63. callback({ ".", m_files[0]->metadata.inode });
  64. callback({ "..", m_files[0]->metadata.inode });
  65. for (unsigned i = 1; i < m_files.size(); ++i)
  66. callback({ m_files[i]->name, m_files[i]->metadata.inode });
  67. return true;
  68. }
  69. InodeMetadata SyntheticFileSystem::inodeMetadata(InodeIdentifier inode) const
  70. {
  71. ASSERT(inode.fileSystemID() == id());
  72. #ifdef SYNTHFS_DEBUG
  73. printf("[synthfs] inodeMetadata(%u)\n", inode.index);
  74. #endif
  75. if (inode.index() == 0 || inode.index() > m_files.size())
  76. return InodeMetadata();
  77. return m_files[inode.index() - 1]->metadata;
  78. }
  79. bool SyntheticFileSystem::setModificationTime(InodeIdentifier, dword timestamp)
  80. {
  81. (void) timestamp;
  82. printf("FIXME: Implement SyntheticFileSystem::setModificationTime().\n");
  83. return false;
  84. }
  85. InodeIdentifier SyntheticFileSystem::createInode(InodeIdentifier parentInode, const String& name, word mode)
  86. {
  87. (void) parentInode;
  88. (void) name;
  89. (void) mode;
  90. printf("FIXME: Implement SyntheticFileSystem::createDirectoryInode().\n");
  91. return { };
  92. }
  93. bool SyntheticFileSystem::writeInode(InodeIdentifier, const ByteBuffer&)
  94. {
  95. printf("FIXME: Implement SyntheticFileSystem::writeInode().\n");
  96. return false;
  97. }
  98. ssize_t SyntheticFileSystem::readInodeBytes(InodeIdentifier inode, Unix::off_t offset, Unix::size_t count, byte* buffer) const
  99. {
  100. ASSERT(inode.fileSystemID() == id());
  101. #ifdef SYNTHFS_DEBUG
  102. printf("[synthfs] readInode %u\n", inode.index());
  103. #endif
  104. ASSERT(inode.index() != 1);
  105. ASSERT(inode.index() <= m_files.size());
  106. ASSERT(offset >= 0);
  107. ASSERT(buffer);
  108. auto& file = *m_files[inode.index() - 1];
  109. Unix::ssize_t nread = min(file.data.size() - offset, static_cast<Unix::off_t>(count));
  110. memcpy(buffer, file.data.pointer() + offset, nread);
  111. return nread;
  112. }