Rework DiskDevice's read() and write() to be non-virtual wrappers.

This way subclasses only have to implement readBlock() and writeBlock().
read() and write() require that the offset and length are both divisible
by the blockSize().
This commit is contained in:
Andreas Kling 2018-10-16 14:11:58 +02:00
parent cafb5b2ad6
commit 8293a0ff36
Notes: sideshowbarker 2024-07-19 18:47:22 +09:00
5 changed files with 47 additions and 10 deletions

View file

@ -7,3 +7,39 @@ DiskDevice::DiskDevice()
DiskDevice::~DiskDevice()
{
}
bool DiskDevice::read(qword offset, unsigned length, byte* out) const
{
ASSERT((offset % blockSize()) == 0);
ASSERT((length % blockSize()) == 0);
qword firstBlock = offset / blockSize();
qword endBlock = (offset + length) / blockSize();
ASSERT(firstBlock <= 0xffffffff);
ASSERT(endBlock <= 0xffffffff);
byte* outptr = out;
unsigned remainingCount = length;
for (unsigned bi = firstBlock; bi < endBlock; ++bi) {
if (!readBlock(bi, outptr))
return false;
outptr += blockSize();
}
return true;
}
bool DiskDevice::write(qword offset, unsigned length, const byte* in)
{
ASSERT((offset % blockSize()) == 0);
ASSERT((length % blockSize()) == 0);
qword firstBlock = offset / blockSize();
qword endBlock = (offset + length) / blockSize();
ASSERT(firstBlock <= 0xffffffff);
ASSERT(endBlock <= 0xffffffff);
const byte* inptr = in;
for (unsigned bi = firstBlock; bi < endBlock; ++bi) {
if (!writeBlock(bi, inptr))
return false;
inptr += blockSize();
}
return true;
}

View file

@ -11,8 +11,8 @@ public:
virtual bool readBlock(unsigned index, byte*) const = 0;
virtual bool writeBlock(unsigned index, const byte*) = 0;
virtual const char* className() const = 0;
virtual bool read(qword offset, unsigned length, byte*) const = 0;
virtual bool write(qword offset, unsigned length, const byte*) = 0;
bool read(qword offset, unsigned length, byte*) const;
bool write(qword offset, unsigned length, const byte*);
protected:
DiskDevice();

View file

@ -965,8 +965,8 @@ InodeIdentifier Ext2FileSystem::createInode(InodeIdentifier parentInode, const S
// FIXME: Implement writing out indirect blocks!
ASSERT(blocks.size() < EXT2_NDIR_BLOCKS);
printf("[XXX] writing %u blocks to i_block array\n", min((unsigned)EXT2_NDIR_BLOCKS, blocks.size()));
for (unsigned i = 0; i < min((unsigned)EXT2_NDIR_BLOCKS, blocks.size()); ++i) {
printf("[XXX] writing %u blocks to i_block array\n", min((size_t)EXT2_NDIR_BLOCKS, blocks.size()));
for (unsigned i = 0; i < min((size_t)EXT2_NDIR_BLOCKS, blocks.size()); ++i) {
e2inode->i_block[i] = blocks[i];
}

View file

@ -35,16 +35,16 @@ unsigned FileBackedDiskDevice::blockSize() const
bool FileBackedDiskDevice::readBlock(unsigned index, byte* out) const
{
qword offset = index * m_blockSize;
return read(offset, blockSize(), out);
return readInternal(offset, blockSize(), out);
}
bool FileBackedDiskDevice::writeBlock(unsigned index, const byte* data)
{
qword offset = index * m_blockSize;
return write(offset, blockSize(), data);
return writeInternal(offset, blockSize(), data);
}
bool FileBackedDiskDevice::read(qword offset, unsigned length, byte* out) const
bool FileBackedDiskDevice::readInternal(qword offset, unsigned length, byte* out) const
{
#ifndef IGNORE_FILE_LENGTH
if (offset + length >= m_fileLength)
@ -59,7 +59,7 @@ bool FileBackedDiskDevice::read(qword offset, unsigned length, byte* out) const
return true;
}
bool FileBackedDiskDevice::write(qword offset, unsigned length, const byte* data)
bool FileBackedDiskDevice::writeInternal(qword offset, unsigned length, const byte* data)
{
#ifndef IGNORE_FILE_LENGTH
if (offset + length >= m_fileLength)

View file

@ -16,12 +16,13 @@ public:
virtual unsigned blockSize() const override;
virtual bool readBlock(unsigned index, byte* out) const override;
virtual bool writeBlock(unsigned index, const byte*) override;
virtual bool read(qword offset, unsigned length, byte* out) const override;
virtual bool write(qword offset, unsigned length, const byte* data) override;
private:
virtual const char* className() const override;
bool readInternal(qword offset, unsigned length, byte* out) const;
bool writeInternal(qword offset, unsigned length, const byte* data);
FileBackedDiskDevice(String&& imagePath, unsigned blockSize);
String m_imagePath;