DiskBackedFileSystem.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "DiskBackedFileSystem.h"
  2. //#define DBFS_DEBUG
  3. DiskBackedFileSystem::DiskBackedFileSystem(RetainPtr<DiskDevice>&& device)
  4. : m_device(std::move(device))
  5. {
  6. ASSERT(m_device);
  7. }
  8. DiskBackedFileSystem::~DiskBackedFileSystem()
  9. {
  10. }
  11. bool DiskBackedFileSystem::writeBlock(unsigned index, const ByteBuffer& data)
  12. {
  13. ASSERT(data.size() == blockSize());
  14. #ifdef DBFS_DEBUG
  15. printf("DiskBackedFileSystem::writeBlock %u\n", index);
  16. #endif
  17. qword baseOffset = static_cast<qword>(index) * static_cast<qword>(blockSize());
  18. return device().write(baseOffset, blockSize(), data.pointer());
  19. }
  20. bool DiskBackedFileSystem::writeBlocks(unsigned index, unsigned count, const ByteBuffer& data)
  21. {
  22. #ifdef DBFS_DEBUG
  23. printf("DiskBackedFileSystem::writeBlocks %u x%u\n", index, count);
  24. #endif
  25. qword baseOffset = static_cast<qword>(index) * static_cast<qword>(blockSize());
  26. return device().write(baseOffset, count * blockSize(), data.pointer());
  27. }
  28. ByteBuffer DiskBackedFileSystem::readBlock(unsigned index) const
  29. {
  30. #ifdef DBFS_DEBUG
  31. printf("DiskBackedFileSystem::readBlock %u\n", index);
  32. #endif
  33. auto buffer = ByteBuffer::createUninitialized(blockSize());
  34. qword baseOffset = static_cast<qword>(index) * static_cast<qword>(blockSize());
  35. auto* bufferPointer = buffer.pointer();
  36. device().read(baseOffset, blockSize(), bufferPointer);
  37. ASSERT(buffer.size() == blockSize());
  38. return buffer;
  39. }
  40. ByteBuffer DiskBackedFileSystem::readBlocks(unsigned index, unsigned count) const
  41. {
  42. if (!count)
  43. return nullptr;
  44. if (count == 1)
  45. return readBlock(index);
  46. auto blocks = ByteBuffer::createUninitialized(count * blockSize());
  47. byte* out = blocks.pointer();
  48. for (unsigned i = 0; i < count; ++i) {
  49. auto block = readBlock(index + i);
  50. if (!block)
  51. return nullptr;
  52. memcpy(out, block.pointer(), block.size());
  53. out += blockSize();
  54. }
  55. return blocks;
  56. }
  57. void DiskBackedFileSystem::setBlockSize(unsigned blockSize)
  58. {
  59. if (blockSize == m_blockSize)
  60. return;
  61. m_blockSize = blockSize;
  62. invalidateCaches();
  63. }
  64. void DiskBackedFileSystem::invalidateCaches()
  65. {
  66. // FIXME: Implement block cache.
  67. }