* Header is actually a footer, and so
* resides at page end.
*/
- if (diskRead(disk, page, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
+ if (diskRead(disk, page, disk->data_size, buf, BATTFS_HEADER_LEN)
!= BATTFS_HEADER_LEN)
{
LOG_ERR("page[%d]\n", page);
* Header is actually a footer, and so
* resides at page end.
*/
- if (disk->bufferWrite(disk, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
+ if (disk->bufferWrite(disk, disk->data_size, buf, BATTFS_HEADER_LEN)
!= BATTFS_HEADER_LEN)
{
LOG_ERR("writing to buffer\n");
{
uint8_t buf[BATTFS_HEADER_LEN];
- if (disk->bufferRead(disk, disk->page_size - BATTFS_HEADER_LEN, buf, BATTFS_HEADER_LEN)
+ if (disk->bufferRead(disk, disk->data_size, buf, BATTFS_HEADER_LEN)
!= BATTFS_HEADER_LEN)
{
LOG_ERR("reading from buffer\n");
return false;
/* Increase free space */
- disk->free_bytes += disk->page_size - BATTFS_HEADER_LEN;
+ disk->free_bytes += disk->data_size;
/* Check header FCS */
if (hdr.fcs == computeFcs(&hdr))
{
- ASSERT(hdr.fill <= disk->page_size - BATTFS_HEADER_LEN);
+ ASSERT(hdr.fill <= disk->data_size);
/* Page is valid and is owned by a file */
filelen_table[hdr.inode]++;
ASSERT(disk->save);
ASSERT(disk->erase);
ASSERT(disk->close);
- ASSERT(disk->page_size);
+ ASSERT(disk->page_size > BATTFS_HEADER_LEN);
+ /* Fill page_size with the usable space */
+ disk->data_size = disk->page_size - BATTFS_HEADER_LEN;
ASSERT(disk->page_count);
ASSERT(disk->page_count < PAGE_UNSET_SENTINEL - 1);
ASSERT(disk->page_array);
memset(filelen_table, 0, BATTFS_MAX_FILES * sizeof(pgoff_t));
disk->free_bytes = 0;
- disk->disk_size = (disk_size_t)(disk->page_size - BATTFS_HEADER_LEN) * disk->page_count;
+ disk->disk_size = (disk_size_t)disk->data_size * disk->page_count;
/* Initialize page buffer cache */
disk->cache_dirty = false;
if (fd->seek_pos < 0)
return total_write;
- if ((fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN)) > fdb->max_off)
+ if ((fd->seek_pos / fdb->disk->data_size) > fdb->max_off)
{
/*
* Handle writing when seek pos if far over EOF,
* We need to allocate the missing pages first.
*/
- pgoff_t missing_pages = fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN) - fdb->max_off;
+ pgoff_t missing_pages = fd->seek_pos / fdb->disk->data_size - fdb->max_off;
LOG_INFO("missing pages: %d\n", missing_pages);
if (!loadPage(fdb->disk, fdb->start[fdb->max_off], &curr_hdr))
while (missing_pages--)
{
/* Update size and free space left */
- fd->size += (fdb->disk->page_size - BATTFS_HEADER_LEN) - curr_hdr.fill;
- fdb->disk->free_bytes -= (fdb->disk->page_size - BATTFS_HEADER_LEN) - curr_hdr.fill;
+ fd->size += fdb->disk->data_size - curr_hdr.fill;
+ fdb->disk->free_bytes -= fdb->disk->data_size - curr_hdr.fill;
/* Fill empty space with 0xFF */
- for (addr_offset = curr_hdr.fill; addr_offset < (fdb->disk->page_size - BATTFS_HEADER_LEN); addr_offset++)
+ for (addr_offset = curr_hdr.fill; addr_offset < fdb->disk->data_size; addr_offset++)
{
if (fdb->disk->bufferWrite(fdb->disk, addr_offset, &dummy, 1) != 1)
{
#warning TODO set error?
}
}
- curr_hdr.fill = (fdb->disk->page_size - BATTFS_HEADER_LEN);
+ curr_hdr.fill = fdb->disk->data_size;
setBufferHdr(fdb->disk, &curr_hdr);
/* Get the new page needed */
fdb->max_off++;
}
/* Fix sizes for the last new page (could be only partially full) */
- curr_hdr.fill = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN);
+ curr_hdr.fill = fd->seek_pos % fdb->disk->data_size;
setBufferHdr(fdb->disk, &curr_hdr);
fd->size += curr_hdr.fill;
fdb->disk->free_bytes -= curr_hdr.fill;
while (size)
{
- pg_offset = fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN);
- addr_offset = fd->seek_pos % (fdb->disk->page_size - BATTFS_HEADER_LEN);
- wr_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset));
+ pg_offset = fd->seek_pos / fdb->disk->data_size;
+ addr_offset = fd->seek_pos % fdb->disk->data_size;
+ wr_len = MIN(size, (size_t)(fdb->disk->data_size - addr_offset));
/* Handle write outside EOF */
if (pg_offset > fdb->max_off)
while (size)
{
- pg_offset = fd->seek_pos / (fdb->disk->page_size - BATTFS_HEADER_LEN);
- 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));
+ pg_offset = fd->seek_pos / fdb->disk->data_size;
+ addr_offset = fd->seek_pos % fdb->disk->data_size;
+ read_len = MIN(size, (size_t)(fdb->disk->data_size - addr_offset));
//LOG_INFO("reading from page %d, offset %d, size %d\n", fdb->start[pg_offset], addr_offset, read_len);
/* Read from disk */
/* Fill file size */
if ((fd->fd.size = countFileSize(disk, fd->start, inode)) == EOF)
return false;
- fd->max_off = fd->fd.size / (disk->page_size - BATTFS_HEADER_LEN);
+ fd->max_off = fd->fd.size / disk->data_size;
/* Reset seek position */
fd->fd.seek_pos = 0;