diff options
author | Alexis211 <alexis211@gmail.com> | 2009-09-06 18:05:31 +0200 |
---|---|---|
committer | Alexis211 <alexis211@gmail.com> | 2009-09-06 18:05:31 +0200 |
commit | d95452c5452b4ca7418505fa5597f000596fcb78 (patch) | |
tree | 3f8ce8a72620648a03cf882332ed3d10cf96bc2b /Source/Kernel/VFS | |
parent | 6e83d6c7b9fc36f7e7826451899c564a34a2c761 (diff) | |
download | Melon-d95452c5452b4ca7418505fa5597f000596fcb78.tar.gz Melon-d95452c5452b4ca7418505fa5597f000596fcb78.zip |
Added read() and write() functions to Partition::
These functions make it possible to read or write bytes on a partition
without worrying of sectors.
Diffstat (limited to 'Source/Kernel/VFS')
-rw-r--r-- | Source/Kernel/VFS/Partition.class.cpp | 41 | ||||
-rw-r--r-- | Source/Kernel/VFS/Partition.class.h | 4 |
2 files changed, 45 insertions, 0 deletions
diff --git a/Source/Kernel/VFS/Partition.class.cpp b/Source/Kernel/VFS/Partition.class.cpp index 0c7832b..1032dee 100644 --- a/Source/Kernel/VFS/Partition.class.cpp +++ b/Source/Kernel/VFS/Partition.class.cpp @@ -17,6 +17,47 @@ bool Partition::writeBlocks(u64int startblock, u32int count, u8int *data) { return m_device->writeBlocks(startblock - m_startblock, count, data); } +bool Partition::read(u64int start, u32int length, u8int *data) { + u32int blksz = m_device->blockSize(); + u64int startBlock = start / blksz; + u32int offset = start % blksz; + u32int blocks = (length + offset) / blksz; + if ((length + offset) % blksz != 0) blocks++; + + u8int* buff = new u8int [blocks * blksz]; //THIS MAY FAIL :D + if (buff == 0) return false; + + if (!readBlocks(startBlock, blocks, buff)) return false; + memcpy(data, buff + offset, length); + + delete [] buff; + return true; +} + +bool Partition::write(u64int start, u32int length, u8int *data) { + u32int blksz = m_device->blockSize(); + u64int startBlock = start / blksz; + u32int offset = start % blksz; + u32int blocks = (length + offset) / blksz; + if ((length + offset) % blksz != 0) blocks++; + u64int lastBlock = startBlock + (u64int)blocks - 1; + + u8int* buff = new u8int [blocks * blksz]; + if (buff == 0) return false; + + if (offset != 0) { + if (!readBlocks(startBlock, 1, buff)) return false; + } + if (lastBlock != startBlock and (length + offset) % blksz != 0) { + if (!readBlocks(lastBlock, 1, buff + ((blocks - 1) * blksize))) return false; + } + memcpy(buff + offset, data, length); + if (!writeBlocks(startBlock, blocks, buff)) return false; + + delete[] buff; + return true; +} + //Accessors BlockDevice* Partition::getDevice() { return m_device; } u64int Partition::getStartBlock() { return m_startblock; } diff --git a/Source/Kernel/VFS/Partition.class.h b/Source/Kernel/VFS/Partition.class.h index e9087bf..8df1c4f 100644 --- a/Source/Kernel/VFS/Partition.class.h +++ b/Source/Kernel/VFS/Partition.class.h @@ -15,6 +15,10 @@ class Partition { bool readBlocks(u64int startblock, u32int count, u8int *data); bool writeBlocks(u64int startblock, u32int count, u8int *data); + //These two just use the readBlocks && writeBlocks defined above + bool read(u64int start, u32int length, u8int *data); + bool write(u64int start, u32int length, u8int *data); + //Accessors : BlockDevice* getDevice(); u64int getStartBlock(); |