+
+static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, pgoff_t pgoff, BattFsPageHeader *new_hdr)
+{
+ if (disk->free_page_start >= disk->page_count)
+ {
+ #warning TODO space over!
+ LOG_INFO("No disk space available\n");
+ return false;
+ }
+ flushBuffer(disk);
+ LOG_INFO("Getting new page %d, pos %d\n", disk->page_array[disk->free_page_start], new_pos);
+ disk->curr_page = disk->page_array[disk->free_page_start++];
+ memmove(&disk->page_array[new_pos + 1], &disk->page_array[new_pos], (disk->free_page_start - new_pos - 1) * sizeof(pgcnt_t));
+
+ Node *n;
+ /* Move following file start point one position ahead. */
+ FOREACH_NODE(n, &disk->file_opened_list)
+ {
+ BattFs *file = containerof(n, BattFs, link);
+ if (file->inode > inode)
+ {
+ LOG_INFO("Move file %d start pos\n", file->inode);
+ file->start++;
+ }
+ }
+
+ disk->page_array[new_pos] = disk->curr_page;
+ disk->cache_dirty = true;
+
+ new_hdr->inode = inode;
+ new_hdr->pgoff = pgoff;
+ new_hdr->fill = 0;
+ new_hdr->seq = 0;
+ return setBufferHdr(disk, new_hdr);
+}
+
+/**
+ * Write to file \a fd \a size bytes from \a buf.
+ * \return The number of bytes written.
+ */
+static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
+{
+ BattFs *fdb = BATTFS_CAST(fd);
+ const uint8_t *buf = (const uint8_t *)_buf;
+
+ size_t total_write = 0;
+ pgoff_t pg_offset;
+ pgaddr_t addr_offset;
+ pgaddr_t wr_len;
+
+ BattFsPageHeader curr_hdr;
+ if (!getBufferHdr(fdb->disk, &curr_hdr))
+ return total_write;
+
+ #warning TODO seek_pos > size?
+
+ 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));
+
+ /* Handle write outside EOF */
+ if (pg_offset > fdb->max_off)
+ {
+ LOG_INFO("New page needed, pg_offset %d, pos %d\n", pg_offset, (fdb->start - fdb->disk->page_array) + pg_offset);
+ if (!getNewPage(fdb->disk, (fdb->start - fdb->disk->page_array) + pg_offset, fdb->inode, pg_offset, &curr_hdr))
+ return total_write;
+ fdb->max_off = pg_offset;
+ }
+ /* Handle cache load of a new page*/
+ else if (fdb->start[pg_offset] != fdb->disk->curr_page)
+ {
+ LOG_INFO("Re-writing page %d to %d\n", fdb->start[pg_offset], fdb->disk->page_array[fdb->disk->free_page_start]);
+ if (!loadPage(fdb->disk, fdb->start[pg_offset], &curr_hdr))
+ {
+ #warning TODO set error?
+ return total_write;
+ }
+
+ /* Get a free page */
+ fdb->disk->curr_page = fdb->disk->page_array[fdb->disk->free_page_start];
+ movePages(fdb->disk, fdb->disk->free_page_start + 1, -1);
+
+ /* Insert previous page in free blocks list */
+ LOG_INFO("Setting page %d as free\n", fdb->start[pg_offset]);
+ fdb->disk->page_array[fdb->disk->page_count - 1] = fdb->start[pg_offset];
+ /* Assign new page */
+ fdb->start[pg_offset] = fdb->disk->curr_page;
+ curr_hdr.seq++;
+ }
+
+
+ //LOG_INFO("writing to buffer for page %d, offset %d, size %d\n", fdb->disk->curr_page, addr_offset, wr_len);
+ if (fdb->disk->bufferWrite(fdb->disk, addr_offset, buf, wr_len) != wr_len)
+ {
+ #warning TODO set error?
+ }
+ fdb->disk->cache_dirty = true;
+
+ size -= wr_len;
+ fd->seek_pos += wr_len;
+ total_write += wr_len;
+ buf += wr_len;
+ fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - curr_hdr.fill, (int32_t)0);
+ fdb->disk->free_bytes -= fill_delta;
+ fd->size += fill_delta;
+ curr_hdr.fill += fill_delta;
+
+ if (!setBufferHdr(fdb->disk, &curr_hdr))
+ return total_write;
+
+ //LOG_INFO("free_bytes %d, seek_pos %d, size %d, curr_hdr.fill %d\n", fdb->disk->free_bytes, fd->seek_pos, fd->size, curr_hdr.fill);
+ }
+ return total_write;
+}
+
+