ASSERT(disk->read);
ASSERT(disk->load);
ASSERT(disk->bufferWrite);
+ ASSERT(disk->bufferRead);
ASSERT(disk->save);
ASSERT(disk->erase);
ASSERT(disk->close);
addr_offset = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN);
read_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset));
- /* Flush current page if needed */
+ /* Try to read from cache */
if (fdb->start[pg_offset] == fdb->disk->curr_page)
- battfs_flushBuffer(fdb->disk);
-
+ {
+ if (fdb->disk->bufferRead(fdb->disk, addr_offset, buf, read_len) != read_len)
+ {
+ #warning TODO set error?
+ }
+ }
/* Read from disk */
- if (fdb->disk->read(fdb->disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len)
+ else if (fdb->disk->read(fdb->disk, fdb->start[pg_offset], addr_offset, buf, read_len) != read_len)
{
#warning TODO set error?
}
*/
typedef size_t (*disk_buffer_write_t) (struct BattFsSuper *d, pgaddr_t addr, const void *buf, size_t);
+/**
+ * Type interface for disk pagebuffer read function.
+ * \a addr is the address inside the current loaded page,
+ * \a size the lenght to be read.
+ * \return the number of bytes read.
+ */
+typedef size_t (*disk_buffer_read_t) (struct BattFsSuper *d, pgaddr_t addr, void *buf, size_t);
+
/**
* Type interface for disk page save function.
* The disk should supply a buffer used for loading/saving pages.
disk_page_read_t read; ///< Page read.
disk_page_load_t load; ///< Page load.
disk_buffer_write_t bufferWrite; ///< Buffer write.
+ disk_buffer_read_t bufferRead; ///< Buffer read.
disk_page_save_t save; ///< Page save.
disk_page_erase_t erase; ///< Page erase.
disk_close_t close; ///< Disk deinit.
return size;
}
+static size_t disk_buffer_read(struct BattFsSuper *d, pgaddr_t addr, void *buf, size_t size)
+{
+ TRACEMSG("addr:%d, size:%d", addr, size);
+ ASSERT(addr + size <= d->page_size);
+ memcpy(buf, &page_buffer[addr], size);
+
+ return size;
+}
+
static bool disk_page_load(struct BattFsSuper *d, pgcnt_t page)
{
TRACEMSG("page:%d", page);
disk.read = disk_page_read;
disk.load = disk_page_load;
disk.bufferWrite = disk_buffer_write;
+ disk.bufferRead = disk_buffer_read;
disk.save = disk_page_save;
disk.erase = disk_page_erase;
disk.close = disk_close;