diff options
-rw-r--r-- | lfs.c | 31 | ||||
-rw-r--r-- | lfs.h | 1 |
2 files changed, 28 insertions, 4 deletions
@@ -2237,6 +2237,9 @@ static int lfs_ctz_traverse(lfs_t *lfs, int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *cfg) { + // do not allow open for already opened file + LFS_ASSERT(0 == (file->flags & LFS_F_OPENED)); + // deorphan if we haven't yet, needed at most once after poweron if ((flags & 3) != LFS_O_RDONLY) { int err = lfs_fs_forceconsistency(lfs); @@ -2248,7 +2251,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, // setup simple file details int err; file->cfg = cfg; - file->flags = flags; + file->flags = flags | LFS_F_OPENED; file->pos = 0; file->cache.buffer = NULL; @@ -2386,6 +2389,8 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file, } int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { + LFS_ASSERT(file->flags & LFS_F_OPENED); + int err = lfs_file_sync(lfs, file); // remove from list of mdirs @@ -2401,10 +2406,14 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { lfs_free(file->cache.buffer); } + file->flags &= ~LFS_F_OPENED; + return err; } static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) { + LFS_ASSERT(file->flags & LFS_F_OPENED); + while (true) { // just relocate what exists into new block lfs_block_t nblock; @@ -2475,6 +2484,8 @@ relocate: } static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { + LFS_ASSERT(file->flags & LFS_F_OPENED); + if (file->flags & LFS_F_READING) { if (!(file->flags & LFS_F_INLINE)) { lfs_cache_drop(lfs, &file->cache); @@ -2490,7 +2501,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { lfs_file_t orig = { .ctz.head = file->ctz.head, .ctz.size = file->ctz.size, - .flags = LFS_O_RDONLY, + .flags = LFS_O_RDONLY | LFS_F_OPENED, .pos = file->pos, .cache = lfs->rcache, }; @@ -2553,6 +2564,8 @@ relocate: } int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { + LFS_ASSERT(file->flags & LFS_F_OPENED); + while (true) { int err = lfs_file_flush(lfs, file); if (err) { @@ -2617,6 +2630,8 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, uint8_t *data = buffer; lfs_size_t nsize = size; + LFS_ASSERT(file->flags & LFS_F_OPENED); + if ((file->flags & 3) == LFS_O_WRONLY) { return LFS_ERR_BADF; } @@ -2690,6 +2705,8 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, const uint8_t *data = buffer; lfs_size_t nsize = size; + LFS_ASSERT(file->flags & LFS_F_OPENED); + if ((file->flags & 3) == LFS_O_RDONLY) { return LFS_ERR_BADF; } @@ -2810,6 +2827,8 @@ relocate: lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { + LFS_ASSERT(file->flags & LFS_F_OPENED); + // write out everything beforehand, may be noop if rdonly int err = lfs_file_flush(lfs, file); if (err) { @@ -2837,6 +2856,8 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, } int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { + LFS_ASSERT(file->flags & LFS_F_OPENED); + if ((file->flags & 3) == LFS_O_RDONLY) { return LFS_ERR_BADF; } @@ -2895,6 +2916,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { (void)lfs; + LFS_ASSERT(file->flags & LFS_F_OPENED); return file->pos; } @@ -2909,6 +2931,7 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { (void)lfs; + LFS_ASSERT(file->flags & LFS_F_OPENED); if (file->flags & LFS_F_WRITING) { return lfs_max(file->pos, file->ctz.size); } else { @@ -3313,7 +3336,7 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { }; lfs_superblock_tole32(&superblock); - err = lfs_dir_commit(lfs, &root, LFS_MKATTRS( + err = lfs_dir_commit(lfs, &root, LFS_MKATTRS( {LFS_MKTAG(LFS_TYPE_CREATE, 0, 0), NULL}, {LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), "littlefs"}, {LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)), @@ -4300,7 +4323,7 @@ int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { entry1.d.type &= ~0x80; } - + // also fetch name char name[LFS_NAME_MAX+1]; memset(name, 0, sizeof(name)); @@ -136,6 +136,7 @@ enum lfs_open_flags { LFS_F_READING = 0x040000, // File has been read since last flush LFS_F_ERRED = 0x080000, // An error occured during write LFS_F_INLINE = 0x100000, // Currently inlined in directory entry + LFS_F_OPENED = 0x200000, // File has been opened }; // File seek flags |