Fixed file truncation without writes

In the open call, the LFS_O_TRUNC flag was correctly zeroing the file, but
it wasn't actually writing the change out to disk. This went unnoticed because
in the cases where the truncate was followed by a file write, the
updated contents would be written out correctly.

Marking the file as dirty if the file isn't already truncated fixes the
problem with the least impact. Also added better test cases around
truncating files.
This commit is contained in:
Christopher Haster
2018-01-11 10:26:33 -06:00
parent aea3d3db46
commit 472ccc4203
2 changed files with 30 additions and 2 deletions

3
lfs.c
View File

@@ -1261,6 +1261,9 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
file->pos = 0;
if (flags & LFS_O_TRUNC) {
if (file->size != 0) {
file->flags |= LFS_F_DIRTY;
}
file->head = 0xffffffff;
file->size = 0;
}