From 5927e7917c903b223a869e21d45f4e91eb3a8a25 Mon Sep 17 00:00:00 2001 From: mooinglemur Date: Fri, 4 Oct 2024 19:21:23 -0700 Subject: [PATCH] [FAT32] fix corruption when appending to empty file --- fat32/fat32.s | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/fat32/fat32.s b/fat32/fat32.s index 816f8070..27903a6e 100644 --- a/fat32/fat32.s +++ b/fat32/fat32.s @@ -2436,6 +2436,9 @@ fat32_open: lda #ERRNO_FILE_NOT_FOUND jmp set_errno +@error: clc + rts + @1: ; Open file stz cur_context + context::eof @@ -2443,9 +2446,23 @@ fat32_open: set32 cur_context + context::file_size, fat32_dirent + dirent::size set32 cur_context + context::start_cluster, fat32_dirent + dirent::start set32 cur_context + context::cluster, fat32_dirent + dirent::start + + ; If the file is of size 0, then any write must allocate the first cluster + lda cur_context + context::file_size + 0 + ora cur_context + context::file_size + 1 + ora cur_context + context::file_size + 2 + ora cur_context + context::file_size + 3 + bne @2 + + ; Set up fat32_bufptr to trigger cluster allocation at first write + set16_val fat32_bufptr, sector_buffer_end + bra @3 + +@2: jsr open_cluster bcc @error +@3: ; Set context as in-use lda #FLAG_IN_USE sta cur_context + context::flags @@ -2454,9 +2471,6 @@ fat32_open: sec rts -@error: clc - rts - ;----------------------------------------------------------------------------- ; find_space_for_lfn ;