if (fd->seek_pos < 0)
return total_write;
- if ((fd->seek_pos / fdb->disk->data_size) > fdb->max_off)
+ if (fd->seek_pos > fd->size)
{
- /*
- * 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->data_size - fdb->max_off;
-
- LOG_INFO("missing pages: %d\n", missing_pages);
+ /* 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;
}
- uint8_t dummy = 0xff;
- /* Add missing pages to reach current seek_pos */
- while (missing_pages--)
+ /* 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--)
{
- /* Update size and free space left */
- fd->size += fdb->disk->data_size - curr_hdr.fill;
- fdb->disk->free_bytes -= fdb->disk->data_size - curr_hdr.fill;
+ 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);
+ flushBuffer(fdb->disk);
- /* Fill empty space with 0xFF */
- for (addr_offset = curr_hdr.fill; addr_offset < fdb->disk->data_size; addr_offset++)
+ /* 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)
{
- if (fdb->disk->bufferWrite(fdb->disk, addr_offset, &dummy, 1) != 1)
- {
- #warning TODO set error?
- }
+ #warning TODO set error?
}
- curr_hdr.fill = fdb->disk->data_size;
- setBufferHdr(fdb->disk, &curr_hdr);
+ }
+ /* Allocate the missing pages first. */
+ pgoff_t missing_pages = fd->seek_pos / fdb->disk->data_size - fdb->max_off;
+ LOG_INFO("missing pages: %d\n", missing_pages);
+
+ 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++;
}
- /* Fix sizes for the last new page (could be only partially full) */
- 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;
}
else if (!getBufferHdr(fdb->disk, &curr_hdr))
return total_write;
memset(buf, 0, 2);
ASSERT(kfile_read(&fd1.fd, buf, 2) == 2);
for (int i = 0; i < 2; i++)
- ASSERT(buf[i] == 0xff);
+ ASSERT(buf[i] == 0);
memset(buf, 0, 2);
ASSERT(kfile_read(&fd1.fd, buf, 2) == 2);
ASSERT(kfile_read(&fd1.fd, buf, 2) == 2);
ASSERT(fd1.fd.seek_pos == 2);
for (int i = 0; i < 2; i++)
- ASSERT(buf[i] == 0xff);
+ ASSERT(buf[i] == 0);
memset(buf, 0, 2);
ASSERT(kfile_read(&fd1.fd, buf, 2) == 2);
ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
ASSERT(fd1.fd.seek_pos == sizeof(buf) + 4);
for (int i = 0; i < sizeof(buf); i++)
- ASSERT(buf[i] == 0xff);
+ ASSERT(buf[i] == 0);
memset(buf, 0, sizeof(buf));
ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));