+ BattFsPageHeader hdr_prv;
+
+ if (!battfs_readHeader(disk, disk->page_array[array_pos], &hdr_prv))
+ return false;
+
+ /* Check header FCS */
+ ASSERT(hdr_prv.fcs == computeFcs(&hdr_prv));
+
+ /* Only the very same page with a different seq number can be here */
+ ASSERT(hdr.inode == hdr_prv.inode);
+ ASSERT(hdr.pgoff == hdr_prv.pgoff);
+ ASSERT(hdr.seq != hdr_prv.seq);
+
+ pgcnt_t new_page, old_page;
+ fill_t old_fill;
+
+ /*
+ * Sequence number comparison: since
+ * seq is 40 bits wide, it wraps once
+ * every 1.1E12 times.
+ * The memory will not live enough to
+ * see a wraparound, so we can use a simple
+ * compare here.
+ */
+ if (hdr.seq > hdr_prv.seq)
+ {
+ /* Current header is newer than the previuos one */
+ old_page = disk->page_array[array_pos];
+ new_page = page;
+ old_fill = hdr_prv.fill;
+ }
+ else
+ {
+ /* Previous header is newer than the current one */
+ old_page = page;
+ new_page = disk->page_array[array_pos];
+ old_fill = hdr.fill;
+ }
+
+ /* Set new page */
+ disk->page_array[array_pos] = new_page;
+ /* Add free space */
+ disk->free_bytes += old_fill;
+ /* Shift all array one position to the left, overwriting duplicate page */
+ array_pos -= hdr.pgoff;
+ array_pos += filelen_table[hdr.inode];
+ movePages(disk, array_pos, -1);
+ /* Move back all indexes */
+ filelen_table[hdr.inode]--;
+ disk->free_page_start--;
+ curr_free_page--;
+ /* Set old page as free */
+ ASSERT(disk->page_array[curr_free_page] == PAGE_UNSET_SENTINEL);
+ disk->page_array[curr_free_page++] = old_page;