From b73ac594f23e47382adcc7d541c314a918a8480d Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 31 May 2019 00:58:48 -0500 Subject: [PATCH] Fixed issues with reading and caching inline files Kind of a two-fold issue. One, the programming to the middle of inline files was causing the cache to get updated to a half programmed state. While fine, as all programs do occur in order in a block, this is less efficient when writing to inline files as it would cause the inline file to need to be reread even if it fits in the cache. Two, the rereading of the inline file was broken and passed the file's tag all the way to where a user would expect an error. This was easy to fix but adds to the reasons we should have test coverage information. Found by ebinans --- lfs.c | 4 ++-- tests/test_seek.sh | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lfs.c b/lfs.c index ca6d587..c554135 100644 --- a/lfs.c +++ b/lfs.c @@ -194,7 +194,7 @@ static int lfs_bd_prog(lfs_t *lfs, off += diff; size -= diff; - pcache->size = off - pcache->off; + pcache->size = lfs_max(pcache->size, off - pcache->off); if (pcache->size == lfs->cfg->cache_size) { // eagerly flush out pcache if we fill up int err = lfs_bd_flush(lfs, pcache, rcache, validate); @@ -617,7 +617,7 @@ static int lfs_dir_getread(lfs_t *lfs, const lfs_mdir_t *dir, lfs->cfg->cache_size); int err = lfs_dir_getslice(lfs, dir, gmask, gtag, rcache->off, rcache->buffer, rcache->size); - if (err) { + if (err < 0) { return err; } } diff --git a/tests/test_seek.sh b/tests/test_seek.sh index 1803317..97a6f15 100755 --- a/tests/test_seek.sh +++ b/tests/test_seek.sh @@ -395,6 +395,16 @@ tests/test.py << TEST lfs_file_sync(&lfs, &file[0]) => 0; lfs_file_tell(&lfs, &file[0]) => i+1; lfs_file_size(&lfs, &file[0]) => $SIZE; + if (i < $SIZE-2) { + uint8_t c[3]; + lfs_file_seek(&lfs, &file[0], -1, LFS_SEEK_CUR) => i; + lfs_file_read(&lfs, &file[0], &c, 3) => 3; + lfs_file_tell(&lfs, &file[0]) => i+3; + lfs_file_size(&lfs, &file[0]) => $SIZE; + lfs_file_seek(&lfs, &file[0], i+1, LFS_SEEK_SET) => i+1; + lfs_file_tell(&lfs, &file[0]) => i+1; + lfs_file_size(&lfs, &file[0]) => $SIZE; + } } lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_SET) => 0;