DiskBackedFileSystem.cpp 2.2 KB

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