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