Skip to content

Commit

Permalink
[FAT32] fix corruption when appending to empty file (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
mooinglemur authored Oct 5, 2024
1 parent c921a17 commit 21b52d8
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions fat32/fat32.s
Original file line number Diff line number Diff line change
Expand Up @@ -2436,16 +2436,33 @@ fat32_open:
lda #ERRNO_FILE_NOT_FOUND
jmp set_errno

@error: clc
rts

@1:
; Open file
stz cur_context + context::eof
set32_val cur_context + context::file_offset, 0
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
Expand All @@ -2454,9 +2471,6 @@ fat32_open:
sec
rts

@error: clc
rts

;-----------------------------------------------------------------------------
; find_space_for_lfn
;
Expand Down

0 comments on commit 21b52d8

Please sign in to comment.