Added more seek tests, fixed some annoying POSIX/etc subtleties

What do you think a file's size becomes when you:

1. seek past the end of a file
2. call write with zero data!

POSIX/etc has this case explicitly mentioned, noting that zero-sized
writes should never update the file size.

This clashes with the assumption that file writes always update the file
position, but I suppose it makes a bit of practical sense if you want
zero-sized file writes to be idempotent.
This commit is contained in:
Christopher Haster
2023-09-28 12:45:32 -05:00
parent 0638b09d18
commit 981e64f524
2 changed files with 453 additions and 5 deletions
+13 -1
View File
@@ -8585,7 +8585,9 @@ lfs_ssize_t lfsr_file_read(lfs_t *lfs, lfsr_file_t *file,
void *buffer, lfs_size_t size) {
LFS_ASSERT(lfsr_file_isreadable(file));
lfs_ssize_t d = lfs_min32(size, file->size - file->pos);
lfs_ssize_t d = lfs_min32(
size,
file->size - lfs_min32(file->pos, file->size));
int err = lfsr_file_read_(lfs, file, file->pos, buffer, d);
if (err < 0) {
return err;
@@ -8765,12 +8767,22 @@ lfs_ssize_t lfsr_file_write(lfs_t *lfs, lfsr_file_t *file,
LFS_ASSERT(lfsr_file_iswriteable(file));
LFS_ASSERT(size <= 0x7fffffff);
// size=0 is a bit special and is gauranteed to have no effects on the
// underlying file, this means no updating file pos or file size
//
// since we need to test for this, just return early
if (size == 0) {
return 0;
}
// update pos if we are appending
// TODO wait, what does POSIX do here if we've seeked past the eof?
if (lfsr_file_isappend(file) && file->pos < file->size) {
file->pos = file->size;
}
// TODO do we need to prepare mutation?
lfs_off_t pos = file->pos;
const uint8_t *buffer_ = buffer;
int err;