Skip to content

Commit

Permalink
Skip flushing file if lfs_file_rawseek() doesn't change position
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Dec 24, 2020
1 parent 1a59954 commit 6bb4043
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3048,14 +3048,6 @@ static lfs_ssize_t lfs_file_rawwrite(lfs_t *lfs, lfs_file_t *file,

static lfs_soff_t lfs_file_rawseek(lfs_t *lfs, lfs_file_t *file,
lfs_soff_t off, int whence) {
#ifndef LFS_READONLY
// write out everything beforehand, may be noop if rdonly
int err = lfs_file_flush(lfs, file);
if (err) {
return err;
}
#endif

// find new pos
lfs_off_t npos = file->pos;
if (whence == LFS_SEEK_SET) {
Expand All @@ -3071,6 +3063,19 @@ static lfs_soff_t lfs_file_rawseek(lfs_t *lfs, lfs_file_t *file,
return LFS_ERR_INVAL;
}

if (file->pos == npos) {
// noop - position has not changed
return npos;
}

#ifndef LFS_READONLY
// write out everything beforehand, may be noop if rdonly
int err = lfs_file_flush(lfs, file);
if (err) {
return err;
}
#endif

// update pos
file->pos = npos;
return npos;
Expand Down

0 comments on commit 6bb4043

Please sign in to comment.