From c38f1fcf8f0fab90785b0aec4361d53673bbc864 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Mon, 13 Mar 2017 15:20:20 +0100 Subject: dosfs: Fix fat_file_write() Remove forced overwrite which leads to file data corruption. The logic to determine a forced overwrite was fundamentally broken. For simplity, disable this feature. Close #2622. --- cpukit/libfs/src/dosfs/fat.c | 15 +++++---------- cpukit/libfs/src/dosfs/fat.h | 3 +-- cpukit/libfs/src/dosfs/fat_file.c | 23 +++++------------------ 3 files changed, 11 insertions(+), 30 deletions(-) (limited to 'cpukit') diff --git a/cpukit/libfs/src/dosfs/fat.c b/cpukit/libfs/src/dosfs/fat.c index 2176ff3496..a0475d4610 100644 --- a/cpukit/libfs/src/dosfs/fat.c +++ b/cpukit/libfs/src/dosfs/fat.c @@ -200,13 +200,12 @@ _fat_block_read( } static ssize_t - fat_block_write( +fat_block_write( fat_fs_info_t *fs_info, const uint32_t start_blk, const uint32_t offset, const uint32_t count, - const void *buf, - const bool overwrite_block) + const void *buf) { int rc = RC_OK; uint32_t bytes_to_write = MIN(count, (fs_info->vol.bytes_per_block - offset)); @@ -215,8 +214,7 @@ static ssize_t if (0 < bytes_to_write) { - if ( overwrite_block - || (bytes_to_write == fs_info->vol.bytes_per_block)) + if (bytes_to_write == fs_info->vol.bytes_per_block) { rc = fat_buf_access(fs_info, sec_num, FAT_OP_TYPE_GET, &blk_buf); } @@ -399,7 +397,6 @@ _fat_block_release(fat_fs_info_t *fs_info) * offset - offset inside cluster 'start' * count - count of bytes to write * buff - buffer provided by user - * overwrite_cluster - true if cluster can get overwritten, false if cluster content must be kept * * RETURNS: * bytes written on success, or -1 if error occured @@ -411,8 +408,7 @@ fat_cluster_write( const uint32_t start_cln, const uint32_t offset, const uint32_t count, - const void *buff, - const bool overwrite_cluster) + const void *buff) { ssize_t rc = RC_OK; uint32_t bytes_to_write = MIN(count, (fs_info->vol.bpc - offset)); @@ -436,8 +432,7 @@ fat_cluster_write( cur_blk, ofs_blk, c, - &buffer[bytes_written], - overwrite_cluster); + &buffer[bytes_written]); if (c != ret) rc = -1; else diff --git a/cpukit/libfs/src/dosfs/fat.h b/cpukit/libfs/src/dosfs/fat.h index 6b866794e1..00d4b945a4 100644 --- a/cpukit/libfs/src/dosfs/fat.h +++ b/cpukit/libfs/src/dosfs/fat.h @@ -511,8 +511,7 @@ fat_cluster_write(fat_fs_info_t *fs_info, uint32_t start_cln, uint32_t offset, uint32_t count, - const void *buff, - bool overwrite_cluster); + const void *buff); ssize_t fat_sector_write(fat_fs_info_t *fs_info, diff --git a/cpukit/libfs/src/dosfs/fat_file.c b/cpukit/libfs/src/dosfs/fat_file.c index af71ee239a..cbc0ab3b78 100644 --- a/cpukit/libfs/src/dosfs/fat_file.c +++ b/cpukit/libfs/src/dosfs/fat_file.c @@ -403,20 +403,18 @@ static bool * start - offset(in bytes) to write from * count - count * buf - buffer provided by user - * file_cln_initial - initial current cluster number of the file * * RETURNS: * number of bytes actually written to the file on success, or -1 if * error occured (errno set appropriately) */ static ssize_t - fat_file_write_fat32_or_non_root_dir( +fat_file_write_fat32_or_non_root_dir( fat_fs_info_t *fs_info, fat_file_fd_t *fat_fd, const uint32_t start, const uint32_t count, - const uint8_t *buf, - const uint32_t file_cln_initial) + const uint8_t *buf) { int rc = RC_OK; uint32_t cmpltd = 0; @@ -426,35 +424,27 @@ static ssize_t uint32_t ofs_cln = start - (start_cln << fs_info->vol.bpc_log2); uint32_t ofs_cln_save = ofs_cln; uint32_t bytes_to_write = count; - uint32_t file_cln_cnt; ssize_t ret; uint32_t c; - bool overwrite_cluster = false; rc = fat_file_lseek(fs_info, fat_fd, start_cln, &cur_cln); if (RC_OK == rc) { - file_cln_cnt = cur_cln - fat_fd->cln; while ( (RC_OK == rc) && (bytes_to_write > 0)) { c = MIN(bytes_to_write, (fs_info->vol.bpc - ofs_cln)); - if (file_cln_initial < file_cln_cnt) - overwrite_cluster = true; - ret = fat_cluster_write(fs_info, cur_cln, ofs_cln, c, - &buf[cmpltd], - overwrite_cluster); + &buf[cmpltd]); if (0 > ret) rc = -1; if (RC_OK == rc) { - ++file_cln_cnt; bytes_to_write -= ret; cmpltd += ret; save_cln = cur_cln; @@ -509,7 +499,6 @@ fat_file_write( uint32_t byte; uint32_t c = 0; bool zero_fill = start > fat_fd->fat_file_size; - uint32_t file_cln_initial = fat_fd->map.file_cln; uint32_t cln; @@ -543,8 +532,7 @@ fat_file_write( cln, byte, count, - buf, - false); + buf); if (0 > ret) rc = -1; else @@ -556,8 +544,7 @@ fat_file_write( fat_fd, start, count, - buf, - file_cln_initial); + buf); if (0 > ret) rc = -1; else -- cgit v1.2.3