+
+static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, pgoff_t pgoff, BattFsPageHeader *new_hdr)
+{
+ if (SPACE_OVER(disk))
+ {
+ #warning TODO space over!
+ LOG_ERR("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 (fd->seek_pos < 0)
+ return total_write;
+
+ if (fd->seek_pos > fd->size)
+ {
+ /* Handle writing when seek pos if far over EOF */
+ if (!loadPage(fdb->disk, fdb->start[fdb->max_off], &curr_hdr))
+ {
+ #warning TODO set error?
+ return total_write;
+ }
+
+ /* Fill unused space of first page with 0s */
+ uint8_t dummy = 0;
+ pgaddr_t zero_bytes = MIN(fd->seek_pos - fd->size, fdb->disk->data_size - curr_hdr.fill);
+ while (zero_bytes--)
+ {
+ if (fdb->disk->bufferWrite(fdb->disk, curr_hdr.fill, &dummy, 1) != 1)
+ {
+ #warning TODO set error?
+ }
+ curr_hdr.fill++;
+ fd->size++;
+ fdb->disk->free_bytes--;
+ fdb->disk->cache_dirty = true;
+ }
+ setBufferHdr(fdb->disk, &curr_hdr);
+
+ /* Allocate the missing pages first. */
+ pgoff_t missing_pages = fd->seek_pos / fdb->disk->data_size - fdb->max_off;
+
+ if (missing_pages)
+ {
+ LOG_INFO("missing pages: %d\n", missing_pages);
+ flushBuffer(fdb->disk);
+
+ /* Fill page buffer with 0 to avoid filling unused pages with garbage */
+ for (pgaddr_t off = 0; off < fdb->disk->data_size; off++)
+ {
+ if (fdb->disk->bufferWrite(fdb->disk, off, &dummy, 1) != 1)
+ {
+ #warning TODO set error?
+ }
+ }
+
+ while (missing_pages--)
+ {
+ zero_bytes = MIN((kfile_off_t)fdb->disk->data_size, fd->seek_pos - fd->size);
+ /* Get the new page needed */
+ if (!getNewPage(fdb->disk, (fdb->start - fdb->disk->page_array) + fdb->max_off + 1, fdb->inode, fdb->max_off + 1, &curr_hdr))
+ return total_write;
+
+ /* Update size and free space left */
+ fd->size += zero_bytes;
+ fdb->disk->free_bytes -= zero_bytes;
+
+ curr_hdr.fill = zero_bytes;
+ setBufferHdr(fdb->disk, &curr_hdr);
+
+ fdb->max_off++;
+ }
+ }
+ }
+ else if (!getBufferHdr(fdb->disk, &curr_hdr))
+ return total_write;
+
+ while (size)
+ {
+ 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)
+ {
+ 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)
+ {
+ if (SPACE_OVER(fdb->disk))
+ {
+ LOG_ERR("No disk space available!\n");
+ return total_write;
+ }
+ 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;
+}
+
+