Skip to content

Commit

Permalink
Fixed file corruption bug in flogfs_write
Browse files Browse the repository at this point in the history
  • Loading branch information
bnahill committed Aug 5, 2013
1 parent 4900f4d commit 8fd30e4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 0 additions & 1 deletion inc/flogfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ typedef struct flog_read_file_t {
uint16_t offset;
//! Number of bytes remaining in current sector
uint16_t sector_remaining_bytes;
uint8_t at_eof;

uint32_t id;

Expand Down
8 changes: 5 additions & 3 deletions src/flogfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,22 +972,24 @@ uint32_t flogfs_read(flog_read_file_t * file, uint8_t * dst, uint32_t nbytes){
uint32_t flogfs_write(flog_write_file_t * file, uint8_t const * src,
uint32_t nbytes){
uint32_t count = 0;
flog_sector_nbytes_t bytes_written;

flog_lock_fs();
flash_lock();

while(nbytes){
if(nbytes >= file->sector_remaining_bytes){
bytes_written = file->sector_remaining_bytes;
if(flog_commit_file_sector(file, src,
file->sector_remaining_bytes) == FLOG_FAILURE){
// Couldn't allocate or something
goto done;
}

// Now that sector is completely written
src += file->sector_remaining_bytes;
nbytes -= file->sector_remaining_bytes;
count += file->sector_remaining_bytes;
src += bytes_written;
nbytes -= bytes_written;
count += bytes_written;
} else {
// This is smaller than a sector; cache it
memcpy(file->sector_buffer + file->offset, src, nbytes);
Expand Down

0 comments on commit 8fd30e4

Please sign in to comment.