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
This commit is contained in:
Christopher Haster
2019-05-31 00:58:48 -05:00
parent 614f7b1e68
commit b73ac594f2
2 changed files with 12 additions and 2 deletions

4
lfs.c
View File

@@ -194,7 +194,7 @@ static int lfs_bd_prog(lfs_t *lfs,
off += diff; off += diff;
size -= diff; size -= diff;
pcache->size = off - pcache->off; pcache->size = lfs_max(pcache->size, off - pcache->off);
if (pcache->size == lfs->cfg->cache_size) { if (pcache->size == lfs->cfg->cache_size) {
// eagerly flush out pcache if we fill up // eagerly flush out pcache if we fill up
int err = lfs_bd_flush(lfs, pcache, rcache, validate); 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); lfs->cfg->cache_size);
int err = lfs_dir_getslice(lfs, dir, gmask, gtag, int err = lfs_dir_getslice(lfs, dir, gmask, gtag,
rcache->off, rcache->buffer, rcache->size); rcache->off, rcache->buffer, rcache->size);
if (err) { if (err < 0) {
return err; return err;
} }
} }

View File

@@ -395,6 +395,16 @@ tests/test.py << TEST
lfs_file_sync(&lfs, &file[0]) => 0; lfs_file_sync(&lfs, &file[0]) => 0;
lfs_file_tell(&lfs, &file[0]) => i+1; lfs_file_tell(&lfs, &file[0]) => i+1;
lfs_file_size(&lfs, &file[0]) => $SIZE; 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; lfs_file_seek(&lfs, &file[0], 0, LFS_SEEK_SET) => 0;