From 64f70f51b0733c2765491ffe0c734dfc6ee02feb Mon Sep 17 00:00:00 2001 From: WeiXiong Liao Date: Fri, 13 Mar 2020 14:33:12 +0800 Subject: [PATCH 01/26] lfs_bd_cmp() compares more bytes at one time It's very slowly to compare one byte at one time. Here are the performance I get from 128M spinand with NFTL by sequential writing. | file size | buffer size | write speed | | 10 MB | 0 B | 3206.01 KB/s | | 10 MB | 1 B | 2434.04 KB/s | | 10 MB | 2 B | 2685.78 KB/s | | 10 MB | 4 B | 2857.94 KB/s | | 10 MB | 8 B | 3060.68 KB/s | | 10 MB | 16 B | 3155.30 KB/s | | 10 MB | 64 B | 3193.68 KB/s | | 10 MB | 128 B | 3230.62 KB/s | | 10 MB | 256 B | 3153.03 KB/s | | 70 MB | 0 B | 2258.87 KB/s | | 70 MB | 1 B | 1827.83 KB/s | | 70 MB | 2 B | 1962.29 KB/s | | 70 MB | 4 B | 2074.01 KB/s | | 70 MB | 8 B | 2147.03 KB/s | | 70 MB | 64 B | 2179.92 KB/s | | 70 MB | 256 B | 2179.96 KB/s | The 0 Byte size means no validation and the 1 Byte size is how littlefs do before. Based on the above table and to save memory, comparing 8 bytes at one time is more wonderful. Signed-off-by: WeiXiong Liao --- lfs.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lfs.c b/lfs.c index 4553a6ed..2bb774b9 100644 --- a/lfs.c +++ b/lfs.c @@ -103,18 +103,22 @@ static int lfs_bd_cmp(lfs_t *lfs, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) { const uint8_t *data = buffer; + lfs_size_t diff = 0; - for (lfs_off_t i = 0; i < size; i++) { - uint8_t dat; - int err = lfs_bd_read(lfs, + for (lfs_off_t i = 0; i < size; i += diff) { + uint8_t dat[8]; + + diff = lfs_min(size-i, sizeof(dat)); + int res = lfs_bd_read(lfs, pcache, rcache, hint-i, - block, off+i, &dat, 1); - if (err) { - return err; + block, off+i, &dat, diff); + if (res) { + return res; } - if (dat != data[i]) { - return (dat < data[i]) ? LFS_CMP_LT : LFS_CMP_GT; + res = memcmp(dat, data + i, diff); + if (res) { + return res < 0 ? LFS_CMP_LT : LFS_CMP_GT; } } From 6d0ec5e85165bc43e00e8e8c295a21bed505f16c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Rast?= Date: Mon, 13 Apr 2020 21:33:30 +0200 Subject: [PATCH 02/26] Added littlefs-python to the related projects section As introduced in #297, I created a python wrapper for littlefs. The wrapper supports two API's: A C-like API which is the same as in C and a more pythonic API which is easier to use if you are more the python guy. The wrapper is built with littlefs 2.2.1 at the moment. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index f900674d..a30d43d3 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,11 @@ License Identifiers that are here available: http://spdx.org/licenses/ - [littlefs-js] - A javascript wrapper for littlefs. I'm not sure why you would want this, but it is handy for demos. You can see it in action [here][littlefs-js-demo]. + +- [littlefs-python] - A Python wrapper for littlefs. The project allows you + to create images of the filesystem on your PC. Check if littlefs will fit + your needs, create images for a later download to the target memory or + inspect the content of a binary image of the target memory. - [mklfs] - A command line tool built by the [Lua RTOS] guys for making littlefs images from a host PC. Supports Windows, Mac OS, and Linux. @@ -250,3 +255,4 @@ License Identifiers that are here available: http://spdx.org/licenses/ [LittleFileSystem]: https://os.mbed.com/docs/mbed-os/v5.12/apis/littlefilesystem.html [SPIFFS]: https://github.com/pellepl/spiffs [Dhara]: https://github.com/dlbeer/dhara +[littlefs-python]: https://pypi.org/project/littlefs-python/ From 87a2cb0e41b95c492808bfbf657c913d24d35617 Mon Sep 17 00:00:00 2001 From: Shiven Gupta Date: Tue, 18 Aug 2020 17:36:14 -0400 Subject: [PATCH 03/26] Fix assert --- bd/lfs_testbd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bd/lfs_testbd.c b/bd/lfs_testbd.c index 1ec6fb90..9d3f40cb 100644 --- a/bd/lfs_testbd.c +++ b/bd/lfs_testbd.c @@ -207,7 +207,7 @@ int lfs_testbd_prog(const struct lfs_config *cfg, lfs_block_t block, bd->power_cycles -= 1; if (bd->power_cycles == 0) { // sync to make sure we persist the last changes - assert(lfs_testbd_rawsync(cfg) == 0); + LFS_ASSERT(lfs_testbd_rawsync(cfg) == 0); // simulate power loss exit(33); } @@ -254,7 +254,7 @@ int lfs_testbd_erase(const struct lfs_config *cfg, lfs_block_t block) { bd->power_cycles -= 1; if (bd->power_cycles == 0) { // sync to make sure we persist the last changes - assert(lfs_testbd_rawsync(cfg) == 0); + LFS_ASSERT(lfs_testbd_rawsync(cfg) == 0); // simulate power loss exit(33); } From 10ac6b9cf074ec165b2bf07bd412e386461ae395 Mon Sep 17 00:00:00 2001 From: Bill Gesner Date: Thu, 17 Sep 2020 23:41:20 +0000 Subject: [PATCH 04/26] add thread safe wrappers --- lfs.c | 209 ++++++++++++++++++++++++++++++++++++++++++----------- lfs.h | 183 +++++++++++++++++++++++++++++++++++++--------- lfs_util.h | 3 + 3 files changed, 316 insertions(+), 79 deletions(-) diff --git a/lfs.c b/lfs.c index eb832fa0..e17e4bda 100644 --- a/lfs.c +++ b/lfs.c @@ -1525,7 +1525,7 @@ static int lfs_dir_compact(lfs_t *lfs, if (lfs_pair_cmp(dir->pair, (const lfs_block_t[2]){0, 1}) == 0) { // oh no! we're writing too much to the superblock, // should we expand? - lfs_ssize_t res = lfs_fs_size(lfs); + lfs_ssize_t res = _lfs_fs_size(lfs); if (res < 0) { return res; } @@ -1906,7 +1906,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, /// Top level directory operations /// -int lfs_mkdir(lfs_t *lfs, const char *path) { +int _lfs_mkdir(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -2005,7 +2005,7 @@ int lfs_mkdir(lfs_t *lfs, const char *path) { return 0; } -int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { +int _lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL); if (tag < 0) { @@ -2056,7 +2056,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { return 0; } -int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { +int _lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); // remove from list of mdirs for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { @@ -2070,7 +2070,7 @@ int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { return 0; } -int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { +int _lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { LFS_TRACE("lfs_dir_read(%p, %p, %p)", (void*)lfs, (void*)dir, (void*)info); memset(info, 0, sizeof(*info)); @@ -2123,11 +2123,11 @@ int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { return true; } -int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { +int _lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { LFS_TRACE("lfs_dir_seek(%p, %p, %"PRIu32")", (void*)lfs, (void*)dir, off); // simply walk from head dir - int err = lfs_dir_rewind(lfs, dir); + int err = _lfs_dir_rewind(lfs, dir); if (err) { LFS_TRACE("lfs_dir_seek -> %d", err); return err; @@ -2166,14 +2166,14 @@ int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { return 0; } -lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { +lfs_soff_t _lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_tell(%p, %p)", (void*)lfs, (void*)dir); (void)lfs; LFS_TRACE("lfs_dir_tell -> %"PRId32, dir->pos); return dir->pos; } -int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { +int _lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_rewind(%p, %p)", (void*)lfs, (void*)dir); // reload the head dir int err = lfs_dir_fetch(lfs, &dir->m, dir->head); @@ -2380,7 +2380,7 @@ static int lfs_ctz_traverse(lfs_t *lfs, /// Top level file operations /// -int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, +int _lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *cfg) { LFS_TRACE("lfs_file_opencfg(%p, %p, \"%s\", %x, %p {" @@ -2529,26 +2529,26 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, cleanup: // clean up lingering resources file->flags |= LFS_F_ERRED; - lfs_file_close(lfs, file); + _lfs_file_close(lfs, file); LFS_TRACE("lfs_file_opencfg -> %d", err); return err; } -int lfs_file_open(lfs_t *lfs, lfs_file_t *file, +int _lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { LFS_TRACE("lfs_file_open(%p, %p, \"%s\", %x)", (void*)lfs, (void*)file, path, flags); static const struct lfs_file_config defaults = {0}; - int err = lfs_file_opencfg(lfs, file, path, flags, &defaults); + int err = _lfs_file_opencfg(lfs, file, path, flags, &defaults); LFS_TRACE("lfs_file_open -> %d", err); return err; } -int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { +int _lfs_file_close(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); - int err = lfs_file_sync(lfs, file); + int err = _lfs_file_sync(lfs, file); // remove from list of mdirs for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { @@ -2679,12 +2679,12 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { // copy over a byte at a time, leave it up to caching // to make this efficient uint8_t data; - lfs_ssize_t res = lfs_file_read(lfs, &orig, &data, 1); + lfs_ssize_t res = _lfs_file_read(lfs, &orig, &data, 1); if (res < 0) { return res; } - res = lfs_file_write(lfs, file, &data, 1); + res = _lfs_file_write(lfs, file, &data, 1); if (res < 0) { return res; } @@ -2731,7 +2731,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { return 0; } -int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { +int _lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -2788,7 +2788,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { return 0; } -lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, +lfs_ssize_t _lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); @@ -2868,7 +2868,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, return size; } -lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, +lfs_ssize_t _lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); @@ -2903,7 +2903,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, file->pos = file->ctz.size; while (file->pos < pos) { - lfs_ssize_t res = lfs_file_write(lfs, file, &(uint8_t){0}, 1); + lfs_ssize_t res = _lfs_file_write(lfs, file, &(uint8_t){0}, 1); if (res < 0) { LFS_TRACE("lfs_file_write -> %"PRId32, res); return res; @@ -2999,7 +2999,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, return size; } -lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, +lfs_soff_t _lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { LFS_TRACE("lfs_file_seek(%p, %p, %"PRId32", %d)", (void*)lfs, (void*)file, off, whence); @@ -3034,7 +3034,7 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, return npos; } -int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { +int _lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")", (void*)lfs, (void*)file, size); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -3046,7 +3046,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } lfs_off_t pos = file->pos; - lfs_off_t oldsize = lfs_file_size(lfs, file); + lfs_off_t oldsize = _lfs_file_size(lfs, file); if (size < oldsize) { // need to flush since directly changing metadata int err = lfs_file_flush(lfs, file); @@ -3070,7 +3070,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } else if (size > oldsize) { // flush+seek if not already at end if (file->pos != oldsize) { - lfs_soff_t res = lfs_file_seek(lfs, file, 0, LFS_SEEK_END); + lfs_soff_t res = _lfs_file_seek(lfs, file, 0, LFS_SEEK_END); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; @@ -3079,7 +3079,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { // fill with zeros while (file->pos < size) { - lfs_ssize_t res = lfs_file_write(lfs, file, &(uint8_t){0}, 1); + lfs_ssize_t res = _lfs_file_write(lfs, file, &(uint8_t){0}, 1); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; @@ -3088,7 +3088,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } // restore pos - lfs_soff_t res = lfs_file_seek(lfs, file, pos, LFS_SEEK_SET); + lfs_soff_t res = _lfs_file_seek(lfs, file, pos, LFS_SEEK_SET); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; @@ -3098,7 +3098,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { return 0; } -lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { +lfs_soff_t _lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; @@ -3106,9 +3106,9 @@ lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { return file->pos; } -int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { +int _lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file); - lfs_soff_t res = lfs_file_seek(lfs, file, 0, LFS_SEEK_SET); + lfs_soff_t res = _lfs_file_seek(lfs, file, 0, LFS_SEEK_SET); if (res < 0) { LFS_TRACE("lfs_file_rewind -> %"PRId32, res); return (int)res; @@ -3118,7 +3118,7 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { return 0; } -lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { +lfs_soff_t _lfs_file_size(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; @@ -3134,7 +3134,7 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { /// General fs operations /// -int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { +int _lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { LFS_TRACE("lfs_stat(%p, \"%s\", %p)", (void*)lfs, path, (void*)info); lfs_mdir_t cwd; lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); @@ -3148,7 +3148,7 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { return err; } -int lfs_remove(lfs_t *lfs, const char *path) { +int _lfs_remove(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -3229,7 +3229,7 @@ int lfs_remove(lfs_t *lfs, const char *path) { return 0; } -int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { +int _lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); // deorphan if we haven't yet, needed at most once after poweron @@ -3374,7 +3374,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { return 0; } -lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, +lfs_ssize_t _lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { LFS_TRACE("lfs_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); @@ -3437,7 +3437,7 @@ static int lfs_commitattr(lfs_t *lfs, const char *path, {LFS_MKTAG(LFS_TYPE_USERATTR + type, id, size), buffer})); } -int lfs_setattr(lfs_t *lfs, const char *path, +int _lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); @@ -3451,7 +3451,7 @@ int lfs_setattr(lfs_t *lfs, const char *path, return err; } -int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { +int _lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); int err = lfs_commitattr(lfs, path, type, NULL, 0x3ff); LFS_TRACE("lfs_removeattr -> %d", err); @@ -3584,7 +3584,7 @@ static int lfs_deinit(lfs_t *lfs) { return 0; } -int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { +int _lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_format(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -3665,7 +3665,7 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { return err; } -int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { +int _lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_mount(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -3804,12 +3804,12 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { return 0; cleanup: - lfs_unmount(lfs); + _lfs_unmount(lfs); LFS_TRACE("lfs_mount -> %d", err); return err; } -int lfs_unmount(lfs_t *lfs) { +int _lfs_unmount(lfs_t *lfs) { LFS_TRACE("lfs_unmount(%p)", (void*)lfs); int err = lfs_deinit(lfs); LFS_TRACE("lfs_unmount -> %d", err); @@ -3914,7 +3914,7 @@ int lfs_fs_traverseraw(lfs_t *lfs, return 0; } -int lfs_fs_traverse(lfs_t *lfs, +int _lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *data, lfs_block_t block), void *data) { LFS_TRACE("lfs_fs_traverse(%p, %p, %p)", (void*)lfs, (void*)(uintptr_t)cb, data); @@ -4235,7 +4235,7 @@ static int lfs_fs_size_count(void *p, lfs_block_t block) { return 0; } -lfs_ssize_t lfs_fs_size(lfs_t *lfs) { +lfs_ssize_t _lfs_fs_size(lfs_t *lfs) { LFS_TRACE("lfs_fs_size(%p)", (void*)lfs); lfs_size_t size = 0; int err = lfs_fs_traverseraw(lfs, lfs_fs_size_count, &size, false); @@ -4669,7 +4669,7 @@ static int lfs1_unmount(lfs_t *lfs) { } /// v1 migration /// -int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { +int _lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_migrate(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -4911,3 +4911,124 @@ int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { } #endif + +#if LFS_THREAD_SAFE + +#define CREATE_LFS_TS_1(ret, function, b_type, b) \ + ret _ts ## function(b_type b) \ + { \ + int err = lfs->cfg->lock(lfs->cfg); \ + if (err) \ + { \ + return err; \ + } \ + err = function(b); \ + lfs->cfg->unlock(lfs->cfg); \ + return err; \ + } +#define CREATE_LFS_TS_2(ret, function, b_type, b, c_type, c) \ + ret _ts ## function(b_type b, c_type c) \ + { \ + int err = lfs->cfg->lock(lfs->cfg); \ + if (err) \ + { \ + return err; \ + } \ + err = function(b, c); \ + lfs->cfg->unlock(lfs->cfg); \ + return err; \ + } +#define CREATE_LFS_TS_2_CFG(ret, function, b_type, b, c_type, cfg) \ + ret _ts ## function(b_type b, c_type cfg) \ + { \ + int err = cfg->lock(cfg); \ + if (err) \ + { \ + return err; \ + } \ + err = function(b, cfg); \ + cfg->unlock(cfg); \ + return err; \ + } +#define CREATE_LFS_TS_3(ret, function, b_type, b, c_type, c, d_type, d) \ + ret _ts ## function(b_type b, c_type c, d_type d) \ + { \ + int err = lfs->cfg->lock(lfs->cfg); \ + if (err) \ + { \ + return err; \ + } \ + err = function(b, c, d); \ + lfs->cfg->unlock(lfs->cfg); \ + return err; \ + } +#define CREATE_LFS_TS_4(ret, function, b_type, b, c_type, c, d_type, d, e_type, e) \ + ret _ts ## function(b_type b, c_type c, d_type d, e_type e) \ + { \ + int err = lfs->cfg->lock(lfs->cfg); \ + if (err) \ + { \ + return err; \ + } \ + err = function(b, c, d, e); \ + lfs->cfg->unlock(lfs->cfg); \ + return err; \ + } +#define CREATE_LFS_TS_5(ret, function, b_type, b, c_type, c, d_type, d, e_type, e, f_type, f) \ + ret _ts ## function(b_type b, c_type c, d_type d, e_type e, f_type f) \ + { \ + int err = lfs->cfg->lock(lfs->cfg); \ + if (err) \ + { \ + return err; \ + } \ + err = function(b, c, d, e, f); \ + lfs->cfg->unlock(lfs->cfg); \ + return err; \ + } + +int _ts_lfs_fs_traverse (lfs_t * lfs, int (* cb)(void * data, lfs_block_t block), void * data) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) + { + return err; + } + + err = _lfs_fs_traverse(lfs, cb, data); + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +CREATE_LFS_TS_2_CFG(int, _lfs_format,lfs_t *, lfs, const struct lfs_config *, config) +CREATE_LFS_TS_2_CFG(int, _lfs_mount,lfs_t *, lfs, const struct lfs_config *, config) +CREATE_LFS_TS_1(int, _lfs_unmount,lfs_t *, lfs) +CREATE_LFS_TS_2(int, _lfs_remove,lfs_t *, lfs, const char *, path) +CREATE_LFS_TS_3(int, _lfs_rename, lfs_t *, lfs, const char *, oldpath, const char *, newpath) +CREATE_LFS_TS_3(int, _lfs_stat, lfs_t *, lfs, const char *, path, struct lfs_info *, info) +CREATE_LFS_TS_5(lfs_ssize_t, _lfs_getattr, lfs_t *, lfs, const char *, path, uint8_t, type, void *, buffer, lfs_size_t, size) +CREATE_LFS_TS_5(int, _lfs_setattr, lfs_t *, lfs, const char *, path, uint8_t, type, const void *, buffer, lfs_size_t, size) +CREATE_LFS_TS_3(int, _lfs_removeattr, lfs_t *, lfs, const char *, path, uint8_t, type) +CREATE_LFS_TS_4(int, _lfs_file_open, lfs_t *, lfs, lfs_file_t *, file, const char *, path, int, flags) +CREATE_LFS_TS_5(int, _lfs_file_opencfg, lfs_t *, lfs, lfs_file_t *, file, const char *, path, int, flags, const struct lfs_file_config *, config) +CREATE_LFS_TS_2(int, _lfs_file_close, lfs_t *, lfs, lfs_file_t *, file) +CREATE_LFS_TS_2(int, _lfs_file_sync, lfs_t *, lfs, lfs_file_t *, file) +CREATE_LFS_TS_4(lfs_ssize_t, _lfs_file_read, lfs_t *, lfs, lfs_file_t *, file, void *, buffer, lfs_size_t, size) +CREATE_LFS_TS_4(lfs_ssize_t, _lfs_file_write, lfs_t *, lfs, lfs_file_t *, file, const void *, buffer, lfs_size_t, size) +CREATE_LFS_TS_4(lfs_soff_t, _lfs_file_seek, lfs_t *, lfs, lfs_file_t *, file, lfs_soff_t, off, int, whence) +CREATE_LFS_TS_3(int, _lfs_file_truncate, lfs_t *, lfs, lfs_file_t *, file, lfs_off_t, size) +CREATE_LFS_TS_2(lfs_soff_t, _lfs_file_tell, lfs_t *, lfs, lfs_file_t *, file) +CREATE_LFS_TS_2(int, _lfs_file_rewind, lfs_t *, lfs, lfs_file_t *, file) +CREATE_LFS_TS_2(lfs_soff_t, _lfs_file_size, lfs_t *, lfs, lfs_file_t *, file) +CREATE_LFS_TS_2(int, _lfs_mkdir, lfs_t *, lfs, const char *, path) +CREATE_LFS_TS_3(int, _lfs_dir_open, lfs_t *, lfs, lfs_dir_t *, dir, const char *, path) +CREATE_LFS_TS_2(int, _lfs_dir_close, lfs_t *, lfs, lfs_dir_t *, dir) +CREATE_LFS_TS_3(int, _lfs_dir_read, lfs_t *, lfs, lfs_dir_t *, dir, struct lfs_info *, info) +CREATE_LFS_TS_3(int, _lfs_dir_seek, lfs_t *, lfs, lfs_dir_t *, dir, lfs_off_t, off) +CREATE_LFS_TS_2(lfs_soff_t, _lfs_dir_tell, lfs_t *, lfs, lfs_dir_t *, dir) +CREATE_LFS_TS_2(int, _lfs_dir_rewind, lfs_t *, lfs, lfs_dir_t *, dir) +CREATE_LFS_TS_1(lfs_ssize_t, _lfs_fs_size, lfs_t *, lfs) +#ifdef LFS_MIGRATE +CREATE_LFS_TS_2_CFG(int, _lfs_migrate, lfs_t *, lfs, const struct lfs_config *, cfg) +#endif +#endif \ No newline at end of file diff --git a/lfs.h b/lfs.h index 35bbbabf..92f04904 100644 --- a/lfs.h +++ b/lfs.h @@ -9,6 +9,7 @@ #include #include +#include "lfs_util.h" #ifdef __cplusplus extern "C" @@ -53,7 +54,7 @@ typedef uint32_t lfs_block_t; // Maximum size of a file in bytes, may be redefined to limit to support other // drivers. Limited on disk to <= 4294967296. However, above 2147483647 the -// functions lfs_file_seek, lfs_file_size, and lfs_file_tell will return +// functions _lfs_file_seek, _lfs_file_size, and _lfs_file_tell will return // incorrect values due to using signed integers. Stored in superblock and // must be respected by other littlefs drivers. #ifndef LFS_FILE_MAX @@ -84,6 +85,9 @@ enum lfs_error { LFS_ERR_NOMEM = -12, // No more memory available LFS_ERR_NOATTR = -61, // No data/attr available LFS_ERR_NAMETOOLONG = -36, // File name too long +#if LFS_THREAD_SAFE + LFS_ERR_LOCK = -23, // Failed to aquire lock +#endif }; // File types @@ -174,6 +178,16 @@ struct lfs_config { // are propogated to the user. int (*sync)(const struct lfs_config *c); + #if LFS_THREAD_SAFE + // Lock the underlying block device. Negative error codes + // are propogated to the user. + int (*lock)(const struct lfs_config *c); + + // Unlock the underlying block device. Negative error codes + // are propogated to the user. + int (*unlock)(const struct lfs_config *c); + #endif + // Minimum size of a block read. All read operations will be a // multiple of this value. lfs_size_t read_size; @@ -406,7 +420,7 @@ typedef struct lfs { // be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. -int lfs_format(lfs_t *lfs, const struct lfs_config *config); +int _lfs_format(lfs_t *lfs, const struct lfs_config *config); // Mounts a littlefs // @@ -416,13 +430,13 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *config); // be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. -int lfs_mount(lfs_t *lfs, const struct lfs_config *config); +int _lfs_mount(lfs_t *lfs, const struct lfs_config *config); // Unmounts a littlefs // // Does nothing besides releasing any allocated resources. // Returns a negative error code on failure. -int lfs_unmount(lfs_t *lfs); +int _lfs_unmount(lfs_t *lfs); /// General operations /// @@ -430,7 +444,7 @@ int lfs_unmount(lfs_t *lfs); // // If removing a directory, the directory must be empty. // Returns a negative error code on failure. -int lfs_remove(lfs_t *lfs, const char *path); +int _lfs_remove(lfs_t *lfs, const char *path); // Rename or move a file or directory // @@ -438,13 +452,13 @@ int lfs_remove(lfs_t *lfs, const char *path); // If the destination is a directory, the directory must be empty. // // Returns a negative error code on failure. -int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath); +int _lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath); // Find info about a file or directory // // Fills out the info structure, based on the specified file or directory. // Returns a negative error code on failure. -int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); +int _lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); // Get a custom attribute // @@ -458,7 +472,7 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); // Note, the returned size is the size of the attribute on disk, irrespective // of the size of the buffer. This can be used to dynamically allocate a buffer // or check for existance. -lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, +lfs_ssize_t _lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size); // Set custom attributes @@ -468,7 +482,7 @@ lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, // implicitly created. // // Returns a negative error code on failure. -int lfs_setattr(lfs_t *lfs, const char *path, +int _lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size); // Removes a custom attribute @@ -476,7 +490,7 @@ int lfs_setattr(lfs_t *lfs, const char *path, // If an attribute is not found, nothing happens. // // Returns a negative error code on failure. -int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); +int _lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); /// File operations /// @@ -487,7 +501,7 @@ int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); // are values from the enum lfs_open_flags that are bitwise-ored together. // // Returns a negative error code on failure. -int lfs_file_open(lfs_t *lfs, lfs_file_t *file, +int _lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags); // Open a file with extra configuration @@ -500,7 +514,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file, // config struct must be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. -int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, +int _lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config); @@ -510,19 +524,19 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, // sync had been called and releases any allocated resources. // // Returns a negative error code on failure. -int lfs_file_close(lfs_t *lfs, lfs_file_t *file); +int _lfs_file_close(lfs_t *lfs, lfs_file_t *file); // Synchronize a file on storage // // Any pending writes are written out to storage. // Returns a negative error code on failure. -int lfs_file_sync(lfs_t *lfs, lfs_file_t *file); +int _lfs_file_sync(lfs_t *lfs, lfs_file_t *file); // Read data from file // // Takes a buffer and size indicating where to store the read data. // Returns the number of bytes read, or a negative error code on failure. -lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, +lfs_ssize_t _lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); // Write data to file @@ -531,38 +545,38 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, // actually be updated on the storage until either sync or close is called. // // Returns the number of bytes written, or a negative error code on failure. -lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, +lfs_ssize_t _lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); // Change the position of the file // // The change in position is determined by the offset and whence flag. // Returns the new position of the file, or a negative error code on failure. -lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, +lfs_soff_t _lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence); // Truncates the size of the file to the specified size // // Returns a negative error code on failure. -int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); +int _lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); // Return the position of the file // -// Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR) +// Equivalent to _lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR) // Returns the position of the file, or a negative error code on failure. -lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file); +lfs_soff_t _lfs_file_tell(lfs_t *lfs, lfs_file_t *file); // Change the position of the file to the beginning of the file // -// Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_SET) +// Equivalent to _lfs_file_seek(lfs, file, 0, LFS_SEEK_SET) // Returns a negative error code on failure. -int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file); +int _lfs_file_rewind(lfs_t *lfs, lfs_file_t *file); // Return the size of the file // -// Similar to lfs_file_seek(lfs, file, 0, LFS_SEEK_END) +// Similar to _lfs_file_seek(lfs, file, 0, LFS_SEEK_END) // Returns the size of the file, or a negative error code on failure. -lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file); +lfs_soff_t _lfs_file_size(lfs_t *lfs, lfs_file_t *file); /// Directory operations /// @@ -570,26 +584,26 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file); // Create a directory // // Returns a negative error code on failure. -int lfs_mkdir(lfs_t *lfs, const char *path); +int _lfs_mkdir(lfs_t *lfs, const char *path); // Open a directory // // Once open a directory can be used with read to iterate over files. // Returns a negative error code on failure. -int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path); +int _lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path); // Close a directory // // Releases any allocated resources. // Returns a negative error code on failure. -int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir); +int _lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir); // Read an entry in the directory // // Fills out the info structure, based on the specified file or directory. // Returns a positive value on success, 0 at the end of directory, // or a negative error code on failure. -int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); +int _lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); // Change the position of the directory // @@ -597,7 +611,7 @@ int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); // an absolute offset in the directory seek. // // Returns a negative error code on failure. -int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); +int _lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); // Return the position of the directory // @@ -605,12 +619,12 @@ int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); // sense, but does indicate the current position in the directory iteration. // // Returns the position of the directory, or a negative error code on failure. -lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir); +lfs_soff_t _lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir); // Change the position of the directory to the beginning of the directory // // Returns a negative error code on failure. -int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir); +int _lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir); /// Filesystem-level filesystem operations @@ -621,7 +635,7 @@ int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir); // size may be larger than the filesystem actually is. // // Returns the number of allocated blocks, or a negative error code on failure. -lfs_ssize_t lfs_fs_size(lfs_t *lfs); +lfs_ssize_t _lfs_fs_size(lfs_t *lfs); // Traverse through all blocks in use by the filesystem // @@ -630,12 +644,12 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs); // blocks are in use or how much of the storage is available. // // Returns a negative error code on failure. -int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); +int _lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); #ifdef LFS_MIGRATE // Attempts to migrate a previous version of littlefs // -// Behaves similarly to the lfs_format function. Attempts to mount +// Behaves similarly to the _lfs_format function. Attempts to mount // the previous version of littlefs and update the filesystem so it can be // mounted with the current version of littlefs. // @@ -644,7 +658,106 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); // be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. -int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); +int _lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); +#endif + +#if LFS_THREAD_SAFE + +int _ts_lfs_format(lfs_t *lfs, const struct lfs_config *config); +int _ts_lfs_mount(lfs_t *lfs, const struct lfs_config *config); +int _ts_lfs_unmount(lfs_t *lfs); +int _ts_lfs_remove(lfs_t *lfs, const char *path); +int _ts_lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath); +int _ts_lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); +lfs_ssize_t _ts_lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size); +int _ts_lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size); +int _ts_lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); +int _ts_lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags); +int _ts_lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config); +int _ts_lfs_file_close(lfs_t *lfs, lfs_file_t *file); +int _ts_lfs_file_sync(lfs_t *lfs, lfs_file_t *file); +lfs_ssize_t _ts_lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); +lfs_ssize_t _ts_lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); +lfs_soff_t _ts_lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence); +int _ts_lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); +lfs_soff_t _ts_lfs_file_tell(lfs_t *lfs, lfs_file_t *file); +int _ts_lfs_file_rewind(lfs_t *lfs, lfs_file_t *file); +lfs_soff_t _ts_lfs_file_size(lfs_t *lfs, lfs_file_t *file); +int _ts_lfs_mkdir(lfs_t *lfs, const char *path); +int _ts_lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path); +int _ts_lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir); +int _ts_lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); +int _ts_lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); +lfs_soff_t _ts_lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir); +int _ts_lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir); +lfs_ssize_t _ts_lfs_fs_size(lfs_t *lfs); +int _ts_lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); +int _ts_lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); + +#define lfs_format _ts_lfs_format +#define lfs_mount _ts_lfs_mount +#define lfs_unmount _ts_lfs_unmount +#define lfs_remove _ts_lfs_remove +#define lfs_rename _ts_lfs_rename +#define lfs_stat _ts_lfs_stat +#define lfs_getattr _ts_lfs_getattr +#define lfs_setattr _ts_lfs_setattr +#define lfs_removeattr _ts_lfs_removeattr +#define lfs_file_open _ts_lfs_file_open +#define lfs_file_opencfg _ts_lfs_file_opencfg +#define lfs_file_close _ts_lfs_file_close +#define lfs_file_sync _ts_lfs_file_sync +#define lfs_file_read _ts_lfs_file_read +#define lfs_file_write _ts_lfs_file_write +#define lfs_file_seek _ts_lfs_file_seek +#define lfs_file_truncate _ts_lfs_file_truncate +#define lfs_file_tell _ts_lfs_file_tell +#define lfs_file_rewind _ts_lfs_file_rewind +#define lfs_file_size _ts_lfs_file_size +#define lfs_mkdir _ts_lfs_mkdir +#define lfs_dir_open _ts_lfs_dir_open +#define lfs_dir_close _ts_lfs_dir_close +#define lfs_dir_read _ts_lfs_dir_read +#define lfs_dir_seek _ts_lfs_dir_seek +#define lfs_dir_tell _ts_lfs_dir_tell +#define lfs_dir_rewind _ts_lfs_dir_rewind +#define lfs_fs_size _ts_lfs_fs_size +#define lfs_fs_traverse _ts_lfs_fs_traverse +#define lfs_migrate _ts_lfs_migrate + +#else + +#define lfs_format _lfs_format +#define lfs_mount _lfs_mount +#define lfs_unmount _lfs_unmount +#define lfs_remove _lfs_remove +#define lfs_rename _lfs_rename +#define lfs_stat _lfs_stat +#define lfs_getattr _lfs_getattr +#define lfs_setattr _lfs_setattr +#define lfs_removeattr _lfs_removeattr +#define lfs_file_open _lfs_file_open +#define lfs_file_opencfg _lfs_file_opencfg +#define lfs_file_close _lfs_file_close +#define lfs_file_sync _lfs_file_sync +#define lfs_file_read _lfs_file_read +#define lfs_file_write _lfs_file_write +#define lfs_file_seek _lfs_file_seek +#define lfs_file_truncate _lfs_file_truncate +#define lfs_file_tell _lfs_file_tell +#define lfs_file_rewind _lfs_file_rewind +#define lfs_file_size _lfs_file_size +#define lfs_mkdir _lfs_mkdir +#define lfs_dir_open _lfs_dir_open +#define lfs_dir_close _lfs_dir_close +#define lfs_dir_read _lfs_dir_read +#define lfs_dir_seek _lfs_dir_seek +#define lfs_dir_tell _lfs_dir_tell +#define lfs_dir_rewind _lfs_dir_rewind +#define lfs_fs_size _lfs_fs_size +#define lfs_fs_traverse _lfs_fs_traverse +#define lfs_migrate _lfs_migrate + #endif diff --git a/lfs_util.h b/lfs_util.h index dbb4c5ba..47e8a940 100644 --- a/lfs_util.h +++ b/lfs_util.h @@ -43,6 +43,9 @@ extern "C" { #endif +#ifndef LFS_THREAD_SAFE +#define LFS_THREAD_SAFE 0 +#endif // Macros, may be replaced by system specific wrappers. Arguments to these // macros must not have side-effects as the macros can be removed for a smaller From 8e6826c4e233dbd3f977eef535dab3b453d5b99e Mon Sep 17 00:00:00 2001 From: Maxime Vincent Date: Wed, 28 Oct 2020 16:09:13 +0100 Subject: [PATCH 05/26] Add LFS_READYONLY define, to allow smaller builds providing read-only mode --- lfs.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- lfs.h | 1 + 2 files changed, 133 insertions(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index eb832fa0..be70e207 100644 --- a/lfs.c +++ b/lfs.c @@ -136,6 +136,7 @@ static int lfs_bd_cmp(lfs_t *lfs, return LFS_CMP_EQ; } +#ifndef LFS_READONLY static int lfs_bd_flush(lfs_t *lfs, lfs_cache_t *pcache, lfs_cache_t *rcache, bool validate) { if (pcache->block != LFS_BLOCK_NULL && pcache->block != LFS_BLOCK_INLINE) { @@ -168,7 +169,9 @@ static int lfs_bd_flush(lfs_t *lfs, return 0; } +#endif +#ifndef LFS_READONLY static int lfs_bd_sync(lfs_t *lfs, lfs_cache_t *pcache, lfs_cache_t *rcache, bool validate) { lfs_cache_drop(lfs, rcache); @@ -182,7 +185,9 @@ static int lfs_bd_sync(lfs_t *lfs, LFS_ASSERT(err <= 0); return err; } +#endif +#ifndef LFS_READONLY static int lfs_bd_prog(lfs_t *lfs, lfs_cache_t *pcache, lfs_cache_t *rcache, bool validate, lfs_block_t block, lfs_off_t off, @@ -228,13 +233,16 @@ static int lfs_bd_prog(lfs_t *lfs, return 0; } +#endif +#ifndef LFS_READONLY static int lfs_bd_erase(lfs_t *lfs, lfs_block_t block) { LFS_ASSERT(block < lfs->cfg->block_count); int err = lfs->cfg->erase(lfs->cfg, block); LFS_ASSERT(err <= 0); return err; } +#endif /// Small type-level utilities /// @@ -388,10 +396,12 @@ static void lfs_ctz_fromle32(struct lfs_ctz *ctz) { ctz->size = lfs_fromle32(ctz->size); } +#ifndef LFS_READONLY static void lfs_ctz_tole32(struct lfs_ctz *ctz) { ctz->head = lfs_tole32(ctz->head); ctz->size = lfs_tole32(ctz->size); } +#endif static inline void lfs_superblock_fromle32(lfs_superblock_t *superblock) { superblock->version = lfs_fromle32(superblock->version); @@ -413,6 +423,7 @@ static inline void lfs_superblock_tole32(lfs_superblock_t *superblock) { /// Internal operations predeclared here /// +#ifndef LFS_READONLY static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, const struct lfs_mattr *attrs, int attrcount); static int lfs_dir_compact(lfs_t *lfs, @@ -429,10 +440,13 @@ static lfs_stag_t lfs_fs_parent(lfs_t *lfs, const lfs_block_t dir[2], lfs_mdir_t *parent); static int lfs_fs_relocate(lfs_t *lfs, const lfs_block_t oldpair[2], lfs_block_t newpair[2]); +#endif int lfs_fs_traverseraw(lfs_t *lfs, int (*cb)(void *data, lfs_block_t block), void *data, bool includeorphans); +#ifndef LFS_READONLY static int lfs_fs_forceconsistency(lfs_t *lfs); +#endif static int lfs_deinit(lfs_t *lfs); #ifdef LFS_MIGRATE static int lfs1_traverse(lfs_t *lfs, @@ -440,6 +454,7 @@ static int lfs1_traverse(lfs_t *lfs, #endif /// Block allocator /// +#ifndef LFS_READONLY static int lfs_alloc_lookahead(void *p, lfs_block_t block) { lfs_t *lfs = (lfs_t*)p; lfs_block_t off = ((block - lfs->free.off) @@ -451,6 +466,7 @@ static int lfs_alloc_lookahead(void *p, lfs_block_t block) { return 0; } +#endif static void lfs_alloc_ack(lfs_t *lfs) { lfs->free.ack = lfs->cfg->block_count; @@ -465,6 +481,7 @@ static void lfs_alloc_reset(lfs_t *lfs) { lfs_alloc_ack(lfs); } +#ifndef LFS_READONLY static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) { while (true) { while (lfs->free.i != lfs->free.size) { @@ -510,6 +527,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) { } } } +#endif /// Metadata pair and directory operations /// static lfs_stag_t lfs_dir_getslice(lfs_t *lfs, const lfs_mdir_t *dir, @@ -642,6 +660,7 @@ static int lfs_dir_getread(lfs_t *lfs, const lfs_mdir_t *dir, return 0; } +#ifndef LFS_READONLY static int lfs_dir_traverse_filter(void *p, lfs_tag_t tag, const void *buffer) { lfs_tag_t *filtertag = p; @@ -669,7 +688,9 @@ static int lfs_dir_traverse_filter(void *p, return false; } +#endif +#ifndef LFS_READONLY static int lfs_dir_traverse(lfs_t *lfs, const lfs_mdir_t *dir, lfs_off_t off, lfs_tag_t ptag, const struct lfs_mattr *attrs, int attrcount, @@ -763,6 +784,7 @@ static int lfs_dir_traverse(lfs_t *lfs, } } } +#endif static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs, lfs_mdir_t *dir, const lfs_block_t pair[2], @@ -1200,6 +1222,7 @@ struct lfs_commit { lfs_off_t end; }; +#ifndef LFS_READONLY static int lfs_dir_commitprog(lfs_t *lfs, struct lfs_commit *commit, const void *buffer, lfs_size_t size) { int err = lfs_bd_prog(lfs, @@ -1214,7 +1237,9 @@ static int lfs_dir_commitprog(lfs_t *lfs, struct lfs_commit *commit, commit->off += size; return 0; } +#endif +#ifndef LFS_READONLY static int lfs_dir_commitattr(lfs_t *lfs, struct lfs_commit *commit, lfs_tag_t tag, const void *buffer) { // check if we fit @@ -1259,7 +1284,9 @@ static int lfs_dir_commitattr(lfs_t *lfs, struct lfs_commit *commit, commit->ptag = tag & 0x7fffffff; return 0; } +#endif +#ifndef LFS_READONLY static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) { const lfs_off_t off1 = commit->off; const uint32_t crc1 = commit->crc; @@ -1352,7 +1379,9 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) { return 0; } +#endif +#ifndef LFS_READONLY static int lfs_dir_alloc(lfs_t *lfs, lfs_mdir_t *dir) { // allocate pair of dir blocks (backwards, so we write block 1 first) for (int i = 0; i < 2; i++) { @@ -1390,7 +1419,9 @@ static int lfs_dir_alloc(lfs_t *lfs, lfs_mdir_t *dir) { // don't write out yet, let caller take care of that return 0; } +#endif +#ifndef LFS_READONLY static int lfs_dir_drop(lfs_t *lfs, lfs_mdir_t *dir, lfs_mdir_t *tail) { // steal state int err = lfs_dir_getgstate(lfs, tail, &lfs->gdelta); @@ -1409,7 +1440,9 @@ static int lfs_dir_drop(lfs_t *lfs, lfs_mdir_t *dir, lfs_mdir_t *tail) { return 0; } +#endif +#ifndef LFS_READONLY static int lfs_dir_split(lfs_t *lfs, lfs_mdir_t *dir, const struct lfs_mattr *attrs, int attrcount, lfs_mdir_t *source, uint16_t split, uint16_t end) { @@ -1442,7 +1475,9 @@ static int lfs_dir_split(lfs_t *lfs, return 0; } +#endif +#ifndef LFS_READONLY static int lfs_dir_commit_size(void *p, lfs_tag_t tag, const void *buffer) { lfs_size_t *size = p; (void)buffer; @@ -1450,17 +1485,23 @@ static int lfs_dir_commit_size(void *p, lfs_tag_t tag, const void *buffer) { *size += lfs_tag_dsize(tag); return 0; } +#endif +#ifndef LFS_READONLY struct lfs_dir_commit_commit { lfs_t *lfs; struct lfs_commit *commit; }; +#endif +#ifndef LFS_READONLY static int lfs_dir_commit_commit(void *p, lfs_tag_t tag, const void *buffer) { struct lfs_dir_commit_commit *commit = p; return lfs_dir_commitattr(commit->lfs, commit->commit, tag, buffer); } +#endif +#ifndef LFS_READONLY static int lfs_dir_compact(lfs_t *lfs, lfs_mdir_t *dir, const struct lfs_mattr *attrs, int attrcount, lfs_mdir_t *source, uint16_t begin, uint16_t end) { @@ -1715,7 +1756,9 @@ static int lfs_dir_compact(lfs_t *lfs, return 0; } +#endif +#ifndef LFS_READONLY static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, const struct lfs_mattr *attrs, int attrcount) { // check for any inline files that aren't RAM backed and @@ -1903,10 +1946,14 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, return 0; } +#endif /// Top level directory operations /// int lfs_mkdir(lfs_t *lfs, const char *path) { +#ifdef LFS_READONLY + return LFS_ERR_NOSYS; +#else LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -2003,6 +2050,7 @@ int lfs_mkdir(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_mkdir -> %d", 0); return 0; +#endif } int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { @@ -2237,6 +2285,7 @@ static int lfs_ctz_find(lfs_t *lfs, return 0; } +#ifndef LFS_READONLY static int lfs_ctz_extend(lfs_t *lfs, lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_block_t head, lfs_size_t size, @@ -2334,6 +2383,7 @@ static int lfs_ctz_extend(lfs_t *lfs, lfs_cache_drop(lfs, pcache); } } +#endif static int lfs_ctz_traverse(lfs_t *lfs, const lfs_cache_t *pcache, lfs_cache_t *rcache, @@ -2390,11 +2440,15 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, // deorphan if we haven't yet, needed at most once after poweron if ((flags & 3) != LFS_O_RDONLY) { +#ifdef LFS_READONLY + return LFS_ERR_INVAL; +#else int err = lfs_fs_forceconsistency(lfs); if (err) { LFS_TRACE("lfs_file_opencfg -> %d", err); return err; } +#endif } // setup simple file details @@ -2418,6 +2472,10 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, lfs->mlist = (struct lfs_mlist*)file; if (tag == LFS_ERR_NOENT) { +#ifdef LFS_READONLY + err = LFS_ERR_NOENT; + goto cleanup; +#else if (!(flags & LFS_O_CREAT)) { err = LFS_ERR_NOENT; goto cleanup; @@ -2441,6 +2499,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, } tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, 0); +#endif } else if (flags & LFS_O_EXCL) { err = LFS_ERR_EXIST; goto cleanup; @@ -2568,6 +2627,8 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { return err; } + +#ifndef LFS_READONLY static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) { LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -2638,7 +2699,9 @@ static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) { lfs_cache_drop(lfs, &lfs->pcache); } } +#endif +#ifndef LFS_READONLY static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file) { file->off = file->pos; lfs_alloc_ack(lfs); @@ -2650,6 +2713,7 @@ static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file) { file->flags &= ~LFS_F_INLINE; return 0; } +#endif static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -2661,6 +2725,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { file->flags &= ~LFS_F_READING; } +#ifndef LFS_READONLY if (file->flags & LFS_F_WRITING) { lfs_off_t pos = file->pos; @@ -2727,6 +2792,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { file->pos = pos; } +#endif return 0; } @@ -2748,6 +2814,8 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { return err; } + +#ifndef LFS_READONLY if ((file->flags & LFS_F_DIRTY) && !lfs_pair_isnull(file->m.pair)) { // update dir entry @@ -2783,6 +2851,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { file->flags &= ~LFS_F_DIRTY; } +#endif LFS_TRACE("lfs_file_sync -> %d", 0); return 0; @@ -2798,6 +2867,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, uint8_t *data = buffer; lfs_size_t nsize = size; +#ifndef LFS_READONLY if (file->flags & LFS_F_WRITING) { // flush out any writes int err = lfs_file_flush(lfs, file); @@ -2806,6 +2876,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, return err; } } +#endif if (file->pos >= file->ctz.size) { // eof if past end @@ -2875,6 +2946,10 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, LFS_ASSERT(file->flags & LFS_F_OPENED); LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY); + +#ifdef LFS_READONLY + return LFS_ERR_NOSYS; +#else const uint8_t *data = buffer; lfs_size_t nsize = size; @@ -2997,6 +3072,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, file->flags &= ~LFS_F_ERRED; LFS_TRACE("lfs_file_write -> %"PRId32, size); return size; +#endif } lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, @@ -3045,6 +3121,9 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { return LFS_ERR_INVAL; } +#ifdef LFS_READONLY + return LFS_ERR_NOSYS; +#else lfs_off_t pos = file->pos; lfs_off_t oldsize = lfs_file_size(lfs, file); if (size < oldsize) { @@ -3096,6 +3175,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { LFS_TRACE("lfs_file_truncate -> %d", 0); return 0; +#endif } lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { @@ -3122,11 +3202,15 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; + +#ifndef LFS_READONLY if (file->flags & LFS_F_WRITING) { LFS_TRACE("lfs_file_size -> %"PRId32, lfs_max(file->pos, file->ctz.size)); return lfs_max(file->pos, file->ctz.size); - } else { + } else +#endif + { LFS_TRACE("lfs_file_size -> %"PRId32, file->ctz.size); return file->ctz.size; } @@ -3150,6 +3234,10 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { int lfs_remove(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); + +#ifdef LFS_READONLY + return LFS_ERR_NOSYS; +#else // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { @@ -3227,11 +3315,15 @@ int lfs_remove(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_remove -> %d", 0); return 0; +#endif } int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); +#ifdef LFS_READONLY + return LFS_ERR_NOSYS; +#else // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { @@ -3372,6 +3464,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_TRACE("lfs_rename -> %d", 0); return 0; +#endif } lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, @@ -3415,6 +3508,7 @@ lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, return size; } +#ifndef LFS_READONLY static int lfs_commitattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { lfs_mdir_t cwd; @@ -3436,11 +3530,15 @@ static int lfs_commitattr(lfs_t *lfs, const char *path, return lfs_dir_commit(lfs, &cwd, LFS_MKATTRS( {LFS_MKTAG(LFS_TYPE_USERATTR + type, id, size), buffer})); } +#endif int lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); +#ifdef LFS_READONLY + return LFS_ERR_NOSYS; +#else if (size > lfs->attr_max) { LFS_TRACE("lfs_setattr -> %d", LFS_ERR_NOSPC); return LFS_ERR_NOSPC; @@ -3449,13 +3547,18 @@ int lfs_setattr(lfs_t *lfs, const char *path, int err = lfs_commitattr(lfs, path, type, buffer, size); LFS_TRACE("lfs_setattr -> %d", err); return err; +#endif } int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); +#ifdef LFS_READONLY + return LFS_ERR_NOSYS; +#else int err = lfs_commitattr(lfs, path, type, NULL, 0x3ff); LFS_TRACE("lfs_removeattr -> %d", err); return err; +#endif } @@ -3601,6 +3704,10 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, cfg->name_max, cfg->file_max, cfg->attr_max); + +#ifdef LFS_READONLY + return LFS_ERR_NOSYS; +#else int err = 0; { err = lfs_init(lfs, cfg); @@ -3663,6 +3770,8 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { lfs_deinit(lfs); LFS_TRACE("lfs_format -> %d", err); return err; + +#endif } int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { @@ -3902,6 +4011,7 @@ int lfs_fs_traverseraw(lfs_t *lfs, } } +#ifndef LFS_READONLY if ((f->flags & LFS_F_WRITING) && !(f->flags & LFS_F_INLINE)) { int err = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache, f->block, f->pos, cb, data); @@ -3909,6 +4019,7 @@ int lfs_fs_traverseraw(lfs_t *lfs, return err; } } +#endif } return 0; @@ -3923,6 +4034,7 @@ int lfs_fs_traverse(lfs_t *lfs, return err; } +#ifndef LFS_READONLY static int lfs_fs_pred(lfs_t *lfs, const lfs_block_t pair[2], lfs_mdir_t *pdir) { // iterate over all directory directory entries @@ -3948,12 +4060,16 @@ static int lfs_fs_pred(lfs_t *lfs, return LFS_ERR_NOENT; } +#endif +#ifndef LFS_READONLY struct lfs_fs_parent_match { lfs_t *lfs; const lfs_block_t pair[2]; }; +#endif +#ifndef LFS_READONLY static int lfs_fs_parent_match(void *data, lfs_tag_t tag, const void *buffer) { struct lfs_fs_parent_match *find = data; @@ -3972,7 +4088,9 @@ static int lfs_fs_parent_match(void *data, lfs_pair_fromle32(child); return (lfs_pair_cmp(child, find->pair) == 0) ? LFS_CMP_EQ : LFS_CMP_LT; } +#endif +#ifndef LFS_READONLY static lfs_stag_t lfs_fs_parent(lfs_t *lfs, const lfs_block_t pair[2], lfs_mdir_t *parent) { // use fetchmatch with callback to find pairs @@ -3999,7 +4117,9 @@ static lfs_stag_t lfs_fs_parent(lfs_t *lfs, const lfs_block_t pair[2], return LFS_ERR_NOENT; } +#endif +#ifndef LFS_READONLY static int lfs_fs_relocate(lfs_t *lfs, const lfs_block_t oldpair[2], lfs_block_t newpair[2]) { // update internal root @@ -4094,14 +4214,18 @@ static int lfs_fs_relocate(lfs_t *lfs, return 0; } +#endif +#ifndef LFS_READONLY static void lfs_fs_preporphans(lfs_t *lfs, int8_t orphans) { LFS_ASSERT(lfs_tag_size(lfs->gstate.tag) > 0 || orphans >= 0); lfs->gstate.tag += orphans; lfs->gstate.tag = ((lfs->gstate.tag & ~LFS_MKTAG(0x800, 0, 0)) | ((uint32_t)lfs_gstate_hasorphans(&lfs->gstate) << 31)); } +#endif +#ifndef LFS_READONLY static void lfs_fs_prepmove(lfs_t *lfs, uint16_t id, const lfs_block_t pair[2]) { lfs->gstate.tag = ((lfs->gstate.tag & ~LFS_MKTAG(0x7ff, 0x3ff, 0)) | @@ -4109,7 +4233,9 @@ static void lfs_fs_prepmove(lfs_t *lfs, lfs->gstate.pair[0] = (id != 0x3ff) ? pair[0] : 0; lfs->gstate.pair[1] = (id != 0x3ff) ? pair[1] : 0; } +#endif +#ifndef LFS_READONLY static int lfs_fs_demove(lfs_t *lfs) { if (!lfs_gstate_hasmove(&lfs->gdisk)) { return 0; @@ -4139,7 +4265,9 @@ static int lfs_fs_demove(lfs_t *lfs) { return 0; } +#endif +#ifndef LFS_READONLY static int lfs_fs_deorphan(lfs_t *lfs) { if (!lfs_gstate_hasorphans(&lfs->gstate)) { return 0; @@ -4213,7 +4341,9 @@ static int lfs_fs_deorphan(lfs_t *lfs) { lfs_fs_preporphans(lfs, -lfs_gstate_getorphans(&lfs->gstate)); return 0; } +#endif +#ifndef LFS_READONLY static int lfs_fs_forceconsistency(lfs_t *lfs) { int err = lfs_fs_demove(lfs); if (err) { @@ -4227,6 +4357,7 @@ static int lfs_fs_forceconsistency(lfs_t *lfs) { return 0; } +#endif static int lfs_fs_size_count(void *p, lfs_block_t block) { (void)block; diff --git a/lfs.h b/lfs.h index 35bbbabf..e271a233 100644 --- a/lfs.h +++ b/lfs.h @@ -76,6 +76,7 @@ enum lfs_error { LFS_ERR_EXIST = -17, // Entry already exists LFS_ERR_NOTDIR = -20, // Entry is not a dir LFS_ERR_ISDIR = -21, // Entry is a dir + LFS_ERR_NOSYS = -38, // Function not implemened LFS_ERR_NOTEMPTY = -39, // Dir is not empty LFS_ERR_BADF = -9, // Bad file number LFS_ERR_FBIG = -27, // File too large From 4bd653dd006ec4568ebae6d710f30ff5d853ed8e Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Wed, 17 Jun 2020 15:42:46 +0300 Subject: [PATCH 06/26] Assert that file/dir struct is not reused in lfs_file_opencfg/lfs_dir_open --- lfs.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lfs.c b/lfs.c index eb832fa0..2b179a6a 100644 --- a/lfs.c +++ b/lfs.c @@ -411,6 +411,17 @@ static inline void lfs_superblock_tole32(lfs_superblock_t *superblock) { superblock->attr_max = lfs_tole32(superblock->attr_max); } +static inline bool lfs_mlist_isopen(struct lfs_mlist *head, + struct lfs_mlist *node) { + for (struct lfs_mlist **p = &head; *p; p = &(*p)->next) { + if (*p == (struct lfs_mlist*)node) { + return true; + } + } + + return false; +} + /// Internal operations predeclared here /// static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, @@ -2007,6 +2018,8 @@ int lfs_mkdir(lfs_t *lfs, const char *path) { int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); + LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)dir)); + lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL); if (tag < 0) { LFS_TRACE("lfs_dir_open -> %"PRId32, tag); @@ -2387,6 +2400,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, ".buffer=%p, .attrs=%p, .attr_count=%"PRIu32"})", (void*)lfs, (void*)file, path, flags, (void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count); + LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); // deorphan if we haven't yet, needed at most once after poweron if ((flags & 3) != LFS_O_RDONLY) { From 6303558aee5c7ab9a5eb3ac495b3094647e962b4 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Wed, 17 Jun 2020 20:46:11 +0300 Subject: [PATCH 07/26] Use LFS_O_RDWR instead of magic number in lfs_file_* asserts --- lfs.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lfs.c b/lfs.c index 2b179a6a..4b9fa6f7 100644 --- a/lfs.c +++ b/lfs.c @@ -2403,7 +2403,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); // deorphan if we haven't yet, needed at most once after poweron - if ((flags & 3) != LFS_O_RDONLY) { + if ((flags & LFS_O_RDWR) != LFS_O_RDONLY) { int err = lfs_fs_forceconsistency(lfs); if (err) { LFS_TRACE("lfs_file_opencfg -> %d", err); @@ -2478,7 +2478,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, // fetch attrs for (unsigned i = 0; i < file->cfg->attr_count; i++) { - if ((file->flags & 3) != LFS_O_WRONLY) { + if ((file->flags & LFS_O_RDWR) != LFS_O_WRONLY) { lfs_stag_t res = lfs_dir_get(lfs, &file->m, LFS_MKTAG(0x7ff, 0x3ff, 0), LFS_MKTAG(LFS_TYPE_USERATTR + file->cfg->attrs[i].type, @@ -2490,7 +2490,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, } } - if ((file->flags & 3) != LFS_O_RDONLY) { + if ((file->flags & LFS_O_RDWR) != LFS_O_RDONLY) { if (file->cfg->attrs[i].size > lfs->attr_max) { err = LFS_ERR_NOSPC; goto cleanup; @@ -2807,7 +2807,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); LFS_ASSERT(file->flags & LFS_F_OPENED); - LFS_ASSERT((file->flags & 3) != LFS_O_WRONLY); + LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_WRONLY); uint8_t *data = buffer; lfs_size_t nsize = size; @@ -2887,7 +2887,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); LFS_ASSERT(file->flags & LFS_F_OPENED); - LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY); + LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_RDONLY); const uint8_t *data = buffer; lfs_size_t nsize = size; @@ -3052,7 +3052,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")", (void*)lfs, (void*)file, size); LFS_ASSERT(file->flags & LFS_F_OPENED); - LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY); + LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_RDONLY); if (size > LFS_FILE_MAX) { LFS_TRACE("lfs_file_truncate -> %d", LFS_ERR_INVAL); From 480cdd9f815d1d78caf98f22ed94ef4f58e46d0c Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sat, 14 Nov 2020 09:32:34 -0600 Subject: [PATCH 08/26] Fixed incorrect modulus in lfs_alloc_reset Modulus of the offset by block_size was clearly a typo, and should be block_count. Interesting to note that later moduluses during alloc calculations prevents this from breaking anything, but as gtaska notes it could skew the wear-leveling distribution. Found by guiserle and gtaska --- lfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index eb832fa0..7f99ccb9 100644 --- a/lfs.c +++ b/lfs.c @@ -459,7 +459,7 @@ static void lfs_alloc_ack(lfs_t *lfs) { // Invalidate the lookahead buffer. This is done during mounting and // failed traversals static void lfs_alloc_reset(lfs_t *lfs) { - lfs->free.off = lfs->seed % lfs->cfg->block_size; + lfs->free.off = lfs->seed % lfs->cfg->block_count; lfs->free.size = 0; lfs->free.i = 0; lfs_alloc_ack(lfs); From 1ae4b36f2a717cd52601d10de6337f97bf4bcf50 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 20 Nov 2020 00:18:13 -0600 Subject: [PATCH 09/26] Removed unnecessary randomization of offsets in lfs_alloc_reset On first read, randomizing the allocators offset may seem appropriate for lfs_alloc_reset. However, it ends up using the filesystem-fed pseudorandom seed in situations it wasn't designed for. As noted by gtaska, the combination of using xors for feeding the seed and multiple traverses of the same CRCs can cause the seed to flip to zeros with concerning frequency. Removed the randomization from lfs_alloc_reset, leaving it in only lfs_mount. Found by gtaska --- lfs.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lfs.c b/lfs.c index 7f99ccb9..cc8ca381 100644 --- a/lfs.c +++ b/lfs.c @@ -452,14 +452,16 @@ static int lfs_alloc_lookahead(void *p, lfs_block_t block) { return 0; } +// indicate allocated blocks have been committed into the filesystem, this +// is to prevent blocks from being garbage collected in the middle of a +// commit operation static void lfs_alloc_ack(lfs_t *lfs) { lfs->free.ack = lfs->cfg->block_count; } -// Invalidate the lookahead buffer. This is done during mounting and -// failed traversals -static void lfs_alloc_reset(lfs_t *lfs) { - lfs->free.off = lfs->seed % lfs->cfg->block_count; +// drop the lookahead buffer, this is done during mounting and failed +// traversals in order to avoid invalid lookahead state +static void lfs_alloc_drop(lfs_t *lfs) { lfs->free.size = 0; lfs->free.i = 0; lfs_alloc_ack(lfs); @@ -505,7 +507,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) { memset(lfs->free.buffer, 0, lfs->cfg->lookahead_size); int err = lfs_fs_traverseraw(lfs, lfs_alloc_lookahead, lfs, true); if (err) { - lfs_alloc_reset(lfs); + lfs_alloc_drop(lfs); return err; } } @@ -3797,8 +3799,10 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { lfs->gstate.tag += !lfs_tag_isvalid(lfs->gstate.tag); lfs->gdisk = lfs->gstate; - // setup free lookahead - lfs_alloc_reset(lfs); + // setup free lookahead, to distribute allocations uniformly across + // boots, we start the allocator at a random location + lfs->free.off = lfs->seed % lfs->cfg->block_count; + lfs_alloc_drop(lfs); LFS_TRACE("lfs_mount -> %d", 0); return 0; From f215027fd420468f793743ef8729701f25554b34 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 20 Nov 2020 00:38:41 -0600 Subject: [PATCH 10/26] Switched to CRC as seed collection function instead of xor As noted by gtaska, we are sitting on a better hash-combining function than xor: CRC. Previous issues with xor were solvable, but relying on xor for this isn't really worth the risk when we already have a CRC function readily available. To quote a study found by gtaska: https://michiel.buddingh.eu/distribution-of-hash-values > CRC32 seems to score really well, but its graph is skewed by the results > of Dataset 5 (binary numbers), which may or may not be too synthetic to > be considered a fair benchmark. But even if you substract the results > from that test, it does not fare significantly worse than other, > cryptographic hash functions. --- lfs.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lfs.c b/lfs.c index cc8ca381..45210640 100644 --- a/lfs.c +++ b/lfs.c @@ -872,8 +872,10 @@ static lfs_stag_t lfs_dir_fetchmatch(lfs_t *lfs, ptag ^= (lfs_tag_t)(lfs_tag_chunk(tag) & 1U) << 31; // toss our crc into the filesystem seed for - // pseudorandom numbers - lfs->seed ^= crc; + // pseudorandom numbers, note we use another crc here + // as a collection function because it is sufficiently + // random and convenient + lfs->seed = lfs_crc(lfs->seed, &crc, sizeof(crc)); // update with what's found so far besttag = tempbesttag; From d04c1392c0b5bdd517a7032b158a689b5ab3d79e Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 22 Nov 2020 00:40:58 -0600 Subject: [PATCH 11/26] Fixed allocation-eviction issue when erase state is multiple of block_cycles+1 This rather interesting corner-case arises in lfs_dir_alloc anytime the uninitialized revision count happens to be a multiple of block_cycles+1. For example, the source of the bug found by tim-nordell-nimbelink: rev = 2742492087 block_cycles = 100 2742492087 % (100+1) = 0 The reason for this weird block_cycles+1 case is due to a fix for a previous bug in fe957de. To avoid aliasing, which would cause metadata pairs to wear unevenly, block_cycles incremented to the next odd number. Normally, littlefs tweaks the revision count of blocks during lfs_dir_alloc in order to make sure evictions can't happen on the first compact. Otherwise, higher-level logic such as lfs_format would break. However, this wasn't updated with the aliasing fix in fe957de, so lfs_dir_alloc was only rounding the revision count to the nearest even number. The current fix is to change the logic in lfs_dir_alloc to explicitly check for the eviction condition and increment if eviction would occur. Found by tim-nordell-nimbelink --- lfs.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lfs.c b/lfs.c index eb832fa0..3899c281 100644 --- a/lfs.c +++ b/lfs.c @@ -1375,8 +1375,12 @@ static int lfs_dir_alloc(lfs_t *lfs, lfs_mdir_t *dir) { return err; } - // make sure we don't immediately evict - dir->rev += dir->rev & 1; + // make sure we don't immediately evict, see lfs_dir_compact for why + // this check is so complicated + if (lfs->cfg->block_cycles > 0 && + (dir->rev + 1) % ((lfs->cfg->block_cycles+1)|1) == 0) { + dir->rev += 1; + } // set defaults dir->off = sizeof(dir->rev); From 0ea2871e2472c779e686f0af76e45927cb20b72d Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 22 Nov 2020 15:05:22 -0600 Subject: [PATCH 12/26] Fixed typo in scripts/readtree.py Not sure how this went unnoticed, I guess this is the first bug that needed in-depth inspection after the a last-minute argument cleanup in the debug scripts. --- scripts/readtree.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/readtree.py b/scripts/readtree.py index 36135ab4..be514e3b 100755 --- a/scripts/readtree.py +++ b/scripts/readtree.py @@ -106,7 +106,7 @@ def main(args): struct.unpack(' Date: Sun, 22 Nov 2020 15:07:16 -0600 Subject: [PATCH 13/26] Fixed single unchecked bit during commit verification This bug was exposed by the bad-block tests due to changes to block allocation, but could have been hit before these changes. In flash, when blocks fail, they don't fail in a predictable manner. To account for this, the bad-block tests check a number of failure behaviors. The interesting one here is "LFS_TESTBD_BADBLOCK_ERASENOOP", in which bad blocks can not be erased or programmed, and are stuck with the data written at the time the blocks go bad. This is actually a pretty realistic failure behavior, since flash needs a large voltage to force the electrons of the floating gates. Though realistically, such a failure would like corrupt the data a bit, not leave the underlying data perfectly intact. LFS_TESTBD_BADBLOCK_ERASENOOP is rather interesting to test for because it means bad blocks can end up with perfectly valid CRCs after a failed write, confusing littlefs. --- In this case, we had the perfect series of operations such that a test was repeatedly writing the same sequence of metadata commits to the same block, which eventually goes bad, leaving the block stuck with metadata that occurs later in the sequence. What this means is that after the first commit, the metadata block contained both the first and second commits, even though the loop in the test hadn't reached that point yet. expected actual .----------. .----------. | commit 1 | | commit 1 | | crc 1 | | crc 1 | | | | commit 2 <-- (from previous iteration) | | | crc 2 | '----------' '----------' To protect against this, littlefs normally compares the written CRC against the expected CRC, but because this was the exact same data that it was going to write, this CRCs end up the same. Ah! But doesn't littlefs also encode the state of the next page to keep track of if the next page has been erased or not? Wouldn't that change between iterations? It does! In a single bit in the CRC-tag. But thanks to some incorrect logic attempting to avoid an extra condition in the loop for writing out padding commits, the CRC that littlefs checked against was the CRC immediately before we include the "is-next-page-erased" bit. Changing the verification check to use the same CRC as what is used to verify commits on fetch solves this problem. --- lfs.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lfs.c b/lfs.c index 45210640..019db1f0 100644 --- a/lfs.c +++ b/lfs.c @@ -1265,12 +1265,13 @@ static int lfs_dir_commitattr(lfs_t *lfs, struct lfs_commit *commit, } static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) { - const lfs_off_t off1 = commit->off; - const uint32_t crc1 = commit->crc; // align to program units - const lfs_off_t end = lfs_alignup(off1 + 2*sizeof(uint32_t), + const lfs_off_t end = lfs_alignup(commit->off + 2*sizeof(uint32_t), lfs->cfg->prog_size); + lfs_off_t off1 = 0; + uint32_t crc1 = 0; + // create crc tags to fill up remainder of commit, note that // padding is not crced, which lets fetches skip padding but // makes committing a bit more complicated @@ -1306,6 +1307,12 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) { return err; } + // keep track of non-padding checksum to verify + if (off1 == 0) { + off1 = commit->off + sizeof(uint32_t); + crc1 = commit->crc; + } + commit->off += sizeof(tag)+lfs_tag_size(tag); commit->ptag = tag ^ ((lfs_tag_t)reset << 31); commit->crc = 0xffffffff; // reset crc for next "commit" @@ -1319,7 +1326,7 @@ static int lfs_dir_commitcrc(lfs_t *lfs, struct lfs_commit *commit) { // successful commit, check checksums to make sure lfs_off_t off = commit->begin; - lfs_off_t noff = off1 + sizeof(uint32_t); + lfs_off_t noff = off1; while (off < end) { uint32_t crc = 0xffffffff; for (lfs_off_t i = off; i < noff+sizeof(uint32_t); i++) { From b8dcf10974e893270e8dd7e5ed5b18536e36f84b Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sat, 28 Nov 2020 22:46:11 -0600 Subject: [PATCH 14/26] Changed lfs_dir_alloc to maximize block cycles for new metadata pairs Previously we only bumped the revision count if an eviction would occur immediately (and possibly corrupt littlefs). This works, but does risk an unoptimal superblock size if an almost-exhausted superblock was allocated during lfs_format. As pointed out by tim-nordell-nimbelink, we can align the revision count to maximize the number of block cycles without breaking the existing requirements of increasing revision counts. As an added benefit, littlefs's wear-leveling should behave more consistently after this change. --- lfs.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lfs.c b/lfs.c index 3899c281..ee47fb43 100644 --- a/lfs.c +++ b/lfs.c @@ -1375,11 +1375,11 @@ static int lfs_dir_alloc(lfs_t *lfs, lfs_mdir_t *dir) { return err; } - // make sure we don't immediately evict, see lfs_dir_compact for why - // this check is so complicated - if (lfs->cfg->block_cycles > 0 && - (dir->rev + 1) % ((lfs->cfg->block_cycles+1)|1) == 0) { - dir->rev += 1; + // to make sure we don't immediately evict, align the new revision count + // to our block_cycles modulus, see lfs_dir_compact for why our modulus + // is tweaked this way + if (lfs->cfg->block_cycles > 0) { + dir->rev = lfs_alignup(dir->rev, ((lfs->cfg->block_cycles+1)|1)); } // set defaults From 008ebc37dfe09c67b7b8b3b511f44399d3c3c684 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Wed, 18 Nov 2020 00:20:34 +0200 Subject: [PATCH 15/26] Add lfs_mlist_append/remove helper --- lfs.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/lfs.c b/lfs.c index 4b9fa6f7..d20f6f13 100644 --- a/lfs.c +++ b/lfs.c @@ -422,6 +422,20 @@ static inline bool lfs_mlist_isopen(struct lfs_mlist *head, return false; } +static inline void lfs_mlist_remove(lfs_t *lfs, struct lfs_mlist *mlist) { + for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { + if (*p == mlist) { + *p = (*p)->next; + break; + } + } +} + +static inline void lfs_mlist_append(lfs_t *lfs, struct lfs_mlist *mlist) { + mlist->next = lfs->mlist; + lfs->mlist = mlist; +} + /// Internal operations predeclared here /// static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, @@ -2062,8 +2076,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { // add to list of mdirs dir->type = LFS_TYPE_DIR; - dir->next = (lfs_dir_t*)lfs->mlist; - lfs->mlist = (struct lfs_mlist*)dir; + lfs_mlist_append(lfs, (struct lfs_mlist *)dir); LFS_TRACE("lfs_dir_open -> %d", 0); return 0; @@ -2072,12 +2085,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); // remove from list of mdirs - for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { - if (*p == (struct lfs_mlist*)dir) { - *p = (*p)->next; - break; - } - } + lfs_mlist_remove(lfs, (struct lfs_mlist *)dir); LFS_TRACE("lfs_dir_close -> %d", 0); return 0; @@ -2428,8 +2436,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, // get id, add to list of mdirs to catch update changes file->type = LFS_TYPE_REG; - file->next = (lfs_file_t*)lfs->mlist; - lfs->mlist = (struct lfs_mlist*)file; + lfs_mlist_append(lfs, (struct lfs_mlist *)file); if (tag == LFS_ERR_NOENT) { if (!(flags & LFS_O_CREAT)) { @@ -2565,12 +2572,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { int err = lfs_file_sync(lfs, file); // remove from list of mdirs - for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { - if (*p == (struct lfs_mlist*)file) { - *p = (*p)->next; - break; - } - } + lfs_mlist_remove(lfs, (struct lfs_mlist*)file); // clean up memory if (!file->cfg->buffer) { From 754b4c3cdaecf269a8382cf02e90b9eaa74eeb73 Mon Sep 17 00:00:00 2001 From: Maxime Vincent Date: Tue, 17 Nov 2020 14:48:46 +0100 Subject: [PATCH 16/26] Squash of LFS_READONLY cleanup - undef unavailable function declarations altogether - even less code, assert on write attempts - remove LFS_O_WRONLY and other flags when compiling with LFS_READONLY - do not annotate #endif, as requested - move ifdef before comments blocks, rework dangling opening bracket - ifdef file flags that are not needed in read-only mode - slight refactor - ifdef LFS_F_ERRED out as well --- lfs.c | 104 +++++++++++++++++++++++++++++----------------------------- lfs.h | 27 +++++++++++++-- 2 files changed, 77 insertions(+), 54 deletions(-) diff --git a/lfs.c b/lfs.c index be70e207..57769a99 100644 --- a/lfs.c +++ b/lfs.c @@ -1950,10 +1950,8 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, /// Top level directory operations /// +#ifndef LFS_READONLY int lfs_mkdir(lfs_t *lfs, const char *path) { -#ifdef LFS_READONLY - return LFS_ERR_NOSYS; -#else LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -2050,8 +2048,8 @@ int lfs_mkdir(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_mkdir -> %d", 0); return 0; -#endif } +#endif int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); @@ -2438,18 +2436,18 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, (void*)lfs, (void*)file, path, flags, (void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count); - // deorphan if we haven't yet, needed at most once after poweron - if ((flags & 3) != LFS_O_RDONLY) { #ifdef LFS_READONLY - return LFS_ERR_INVAL; + LFS_ASSERT((flags & 3) == LFS_O_RDONLY); #else + // deorphan if we haven't yet, needed at most once after poweron + if ((flags & 3) != LFS_O_RDONLY) { int err = lfs_fs_forceconsistency(lfs); if (err) { LFS_TRACE("lfs_file_opencfg -> %d", err); return err; } -#endif } +#endif // setup simple file details int err; @@ -2471,11 +2469,12 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, file->next = (lfs_file_t*)lfs->mlist; lfs->mlist = (struct lfs_mlist*)file; - if (tag == LFS_ERR_NOENT) { #ifdef LFS_READONLY + if (tag == LFS_ERR_NOENT) { err = LFS_ERR_NOENT; goto cleanup; #else + if (tag == LFS_ERR_NOENT) { if (!(flags & LFS_O_CREAT)) { err = LFS_ERR_NOENT; goto cleanup; @@ -2499,17 +2498,19 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, } tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, 0); -#endif } else if (flags & LFS_O_EXCL) { err = LFS_ERR_EXIST; goto cleanup; +#endif } else if (lfs_tag_type3(tag) != LFS_TYPE_REG) { err = LFS_ERR_ISDIR; goto cleanup; +#ifndef LFS_READONLY } else if (flags & LFS_O_TRUNC) { // truncate if requested tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0); file->flags |= LFS_F_DIRTY; +#endif } else { // try to load what's on disk, if it's inlined we'll fix it later tag = lfs_dir_get(lfs, &file->m, LFS_MKTAG(0x700, 0x3ff, 0), @@ -2523,7 +2524,8 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, // fetch attrs for (unsigned i = 0; i < file->cfg->attr_count; i++) { - if ((file->flags & 3) != LFS_O_WRONLY) { + // if opened for read / read-write operations + if ((file->flags & LFS_O_RDONLY) == LFS_O_RDONLY) { lfs_stag_t res = lfs_dir_get(lfs, &file->m, LFS_MKTAG(0x7ff, 0x3ff, 0), LFS_MKTAG(LFS_TYPE_USERATTR + file->cfg->attrs[i].type, @@ -2535,7 +2537,9 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, } } - if ((file->flags & 3) != LFS_O_RDONLY) { +#ifndef LFS_READONLY + // if opened for write / read-write operations + if ((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY) { if (file->cfg->attrs[i].size > lfs->attr_max) { err = LFS_ERR_NOSPC; goto cleanup; @@ -2543,6 +2547,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, file->flags |= LFS_F_DIRTY; } +#endif } // allocate buffer if needed @@ -2587,7 +2592,9 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, cleanup: // clean up lingering resources +#ifndef LFS_READONLY file->flags |= LFS_F_ERRED; +#endif lfs_file_close(lfs, file); LFS_TRACE("lfs_file_opencfg -> %d", err); return err; @@ -2607,7 +2614,11 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); +#ifdef LFS_READONLY + int err = 0; +#else int err = lfs_file_sync(lfs, file); +#endif // remove from list of mdirs for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { @@ -2715,6 +2726,7 @@ static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file) { } #endif +#ifndef LFS_READONLY static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -2725,7 +2737,6 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { file->flags &= ~LFS_F_READING; } -#ifndef LFS_READONLY if (file->flags & LFS_F_WRITING) { lfs_off_t pos = file->pos; @@ -2792,11 +2803,12 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { file->pos = pos; } -#endif return 0; } +#endif +#ifndef LFS_READONLY int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -2815,7 +2827,6 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { } -#ifndef LFS_READONLY if ((file->flags & LFS_F_DIRTY) && !lfs_pair_isnull(file->m.pair)) { // update dir entry @@ -2851,18 +2862,22 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { file->flags &= ~LFS_F_DIRTY; } -#endif LFS_TRACE("lfs_file_sync -> %d", 0); return 0; } +#endif lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); LFS_ASSERT(file->flags & LFS_F_OPENED); +#ifdef LFS_READONLY + // always LFS_O_RDONLY +#else LFS_ASSERT((file->flags & 3) != LFS_O_WRONLY); +#endif uint8_t *data = buffer; lfs_size_t nsize = size; @@ -2939,6 +2954,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, return size; } +#ifndef LFS_READONLY lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")", @@ -2946,10 +2962,6 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, LFS_ASSERT(file->flags & LFS_F_OPENED); LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY); - -#ifdef LFS_READONLY - return LFS_ERR_NOSYS; -#else const uint8_t *data = buffer; lfs_size_t nsize = size; @@ -3072,8 +3084,8 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, file->flags &= ~LFS_F_ERRED; LFS_TRACE("lfs_file_write -> %"PRId32, size); return size; -#endif } +#endif lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { @@ -3081,12 +3093,14 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, (void*)lfs, (void*)file, off, whence); LFS_ASSERT(file->flags & LFS_F_OPENED); +#ifndef LFS_READONLY // write out everything beforehand, may be noop if rdonly int err = lfs_file_flush(lfs, file); if (err) { LFS_TRACE("lfs_file_seek -> %d", err); return err; } +#endif // find new pos lfs_off_t npos = file->pos; @@ -3110,6 +3124,7 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, return npos; } +#ifndef LFS_READONLY int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")", (void*)lfs, (void*)file, size); @@ -3121,9 +3136,6 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { return LFS_ERR_INVAL; } -#ifdef LFS_READONLY - return LFS_ERR_NOSYS; -#else lfs_off_t pos = file->pos; lfs_off_t oldsize = lfs_file_size(lfs, file); if (size < oldsize) { @@ -3175,8 +3187,8 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { LFS_TRACE("lfs_file_truncate -> %d", 0); return 0; -#endif } +#endif lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file); @@ -3208,12 +3220,10 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_size -> %"PRId32, lfs_max(file->pos, file->ctz.size)); return lfs_max(file->pos, file->ctz.size); - } else -#endif - { - LFS_TRACE("lfs_file_size -> %"PRId32, file->ctz.size); - return file->ctz.size; } +#endif + LFS_TRACE("lfs_file_size -> %"PRId32, file->ctz.size); + return file->ctz.size; } @@ -3232,12 +3242,10 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { return err; } +#ifndef LFS_READONLY int lfs_remove(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); -#ifdef LFS_READONLY - return LFS_ERR_NOSYS; -#else // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { @@ -3315,15 +3323,13 @@ int lfs_remove(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_remove -> %d", 0); return 0; -#endif } +#endif +#ifndef LFS_READONLY int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); -#ifdef LFS_READONLY - return LFS_ERR_NOSYS; -#else // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { @@ -3464,8 +3470,8 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_TRACE("lfs_rename -> %d", 0); return 0; -#endif } +#endif lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { @@ -3532,13 +3538,11 @@ static int lfs_commitattr(lfs_t *lfs, const char *path, } #endif +#ifndef LFS_READONLY int lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); -#ifdef LFS_READONLY - return LFS_ERR_NOSYS; -#else if (size > lfs->attr_max) { LFS_TRACE("lfs_setattr -> %d", LFS_ERR_NOSPC); return LFS_ERR_NOSPC; @@ -3547,19 +3551,17 @@ int lfs_setattr(lfs_t *lfs, const char *path, int err = lfs_commitattr(lfs, path, type, buffer, size); LFS_TRACE("lfs_setattr -> %d", err); return err; -#endif } +#endif +#ifndef LFS_READONLY int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); -#ifdef LFS_READONLY - return LFS_ERR_NOSYS; -#else int err = lfs_commitattr(lfs, path, type, NULL, 0x3ff); LFS_TRACE("lfs_removeattr -> %d", err); return err; -#endif } +#endif /// Filesystem operations /// @@ -3687,6 +3689,7 @@ static int lfs_deinit(lfs_t *lfs) { return 0; } +#ifndef LFS_READONLY int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_format(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " @@ -3705,9 +3708,6 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, cfg->name_max, cfg->file_max, cfg->attr_max); -#ifdef LFS_READONLY - return LFS_ERR_NOSYS; -#else int err = 0; { err = lfs_init(lfs, cfg); @@ -3771,8 +3771,8 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_format -> %d", err); return err; -#endif } +#endif int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_mount(%p, %p {.context=%p, " @@ -3997,6 +3997,7 @@ int lfs_fs_traverseraw(lfs_t *lfs, } } +#ifndef LFS_READONLY // iterate over any open files for (lfs_file_t *f = (lfs_file_t*)lfs->mlist; f; f = f->next) { if (f->type != LFS_TYPE_REG) { @@ -4011,7 +4012,6 @@ int lfs_fs_traverseraw(lfs_t *lfs, } } -#ifndef LFS_READONLY if ((f->flags & LFS_F_WRITING) && !(f->flags & LFS_F_INLINE)) { int err = lfs_ctz_traverse(lfs, &f->cache, &lfs->rcache, f->block, f->pos, cb, data); @@ -4019,8 +4019,8 @@ int lfs_fs_traverseraw(lfs_t *lfs, return err; } } -#endif } +#endif return 0; } diff --git a/lfs.h b/lfs.h index e271a233..67a3f323 100644 --- a/lfs.h +++ b/lfs.h @@ -76,7 +76,6 @@ enum lfs_error { LFS_ERR_EXIST = -17, // Entry already exists LFS_ERR_NOTDIR = -20, // Entry is not a dir LFS_ERR_ISDIR = -21, // Entry is a dir - LFS_ERR_NOSYS = -38, // Function not implemened LFS_ERR_NOTEMPTY = -39, // Dir is not empty LFS_ERR_BADF = -9, // Bad file number LFS_ERR_FBIG = -27, // File too large @@ -124,18 +123,24 @@ enum lfs_type { enum lfs_open_flags { // open flags LFS_O_RDONLY = 1, // Open a file as read only +#ifndef LFS_READONLY LFS_O_WRONLY = 2, // Open a file as write only LFS_O_RDWR = 3, // Open a file as read and write LFS_O_CREAT = 0x0100, // Create a file if it does not exist LFS_O_EXCL = 0x0200, // Fail if a file already exists LFS_O_TRUNC = 0x0400, // Truncate the existing file to zero size LFS_O_APPEND = 0x0800, // Move to end of file on every write +#endif // internally used flags +#ifndef LFS_READONLY LFS_F_DIRTY = 0x010000, // File does not match storage LFS_F_WRITING = 0x020000, // File has been written since last flush +#endif LFS_F_READING = 0x040000, // File has been read since last flush - LFS_F_ERRED = 0x080000, // An error occured during write +#ifndef LFS_READONLY + LFS_F_ERRED = 0x080000, // An error occurred during write +#endif LFS_F_INLINE = 0x100000, // Currently inlined in directory entry LFS_F_OPENED = 0x200000, // File has been opened }; @@ -400,6 +405,7 @@ typedef struct lfs { /// Filesystem functions /// +#ifndef LFS_READONLY // Format a block device with the littlefs // // Requires a littlefs object and config struct. This clobbers the littlefs @@ -408,6 +414,7 @@ typedef struct lfs { // // Returns a negative error code on failure. int lfs_format(lfs_t *lfs, const struct lfs_config *config); +#endif // Mounts a littlefs // @@ -427,12 +434,15 @@ int lfs_unmount(lfs_t *lfs); /// General operations /// +#ifndef LFS_READONLY // Removes a file or directory // // If removing a directory, the directory must be empty. // Returns a negative error code on failure. int lfs_remove(lfs_t *lfs, const char *path); +#endif +#ifndef LFS_READONLY // Rename or move a file or directory // // If the destination exists, it must match the source in type. @@ -440,6 +450,7 @@ int lfs_remove(lfs_t *lfs, const char *path); // // Returns a negative error code on failure. int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath); +#endif // Find info about a file or directory // @@ -462,6 +473,7 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size); +#ifndef LFS_READONLY // Set custom attributes // // Custom attributes are uniquely identified by an 8-bit type and limited @@ -471,13 +483,16 @@ lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, // Returns a negative error code on failure. int lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size); +#endif +#ifndef LFS_READONLY // Removes a custom attribute // // If an attribute is not found, nothing happens. // // Returns a negative error code on failure. int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); +#endif /// File operations /// @@ -526,6 +541,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file); lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); +#ifndef LFS_READONLY // Write data to file // // Takes a buffer and size indicating the data to write. The file will not @@ -534,6 +550,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, // Returns the number of bytes written, or a negative error code on failure. lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); +#endif // Change the position of the file // @@ -542,10 +559,12 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence); +#ifndef LFS_READONLY // Truncates the size of the file to the specified size // // Returns a negative error code on failure. int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); +#endif // Return the position of the file // @@ -568,10 +587,12 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file); /// Directory operations /// +#ifndef LFS_READONLY // Create a directory // // Returns a negative error code on failure. int lfs_mkdir(lfs_t *lfs, const char *path); +#endif // Open a directory // @@ -633,6 +654,7 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs); // Returns a negative error code on failure. int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); +#ifndef LFS_READONLY #ifdef LFS_MIGRATE // Attempts to migrate a previous version of littlefs // @@ -647,6 +669,7 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); // Returns a negative error code on failure. int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); #endif +#endif #ifdef __cplusplus From 2efebf8e9bf18b13f33ed1fd357a67d0abd772ab Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sat, 21 Nov 2020 23:54:32 -0600 Subject: [PATCH 17/26] Added read-only build+size reporting to CI --- .travis.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.travis.yml b/.travis.yml index 78d964a4..fe3aefc5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -208,6 +208,22 @@ jobs: script: - make test TFLAGS+="-k --valgrind" + # test compilation in read-only mode + - stage: test + env: + - NAME=littlefs-readonly + - CC="arm-linux-gnueabi-gcc --static -mthumb" + - CFLAGS="-Werror -DLFS_READONLY" + if: branch !~ -prefix$ + install: + - *install-common + - sudo apt-get install + gcc-arm-linux-gnueabi + libc6-dev-armel-cross + - arm-linux-gnueabi-gcc --version + # report-size will compile littlefs and report the size + script: [*report-size] + # self-host with littlefs-fuse for fuzz test - stage: test env: From d0f055d321feaee6b846699d6bf5e9aa3f57f3b8 Mon Sep 17 00:00:00 2001 From: Bill Gesner Date: Thu, 1 Oct 2020 01:48:29 +0000 Subject: [PATCH 18/26] Squash of thread-safe PR cleanup - expand functions - add comment - rename functions - fix locking issue in format and mount - use global include - fix ac6 linker issue - use the global config file - address review comments - minor cleanup - minor cleanup - review comments --- lfs.c | 703 +++++++++++++++++++++++++++++++++++++++++------------ lfs.h | 209 ++++++---------- lfs_util.h | 5 +- 3 files changed, 621 insertions(+), 296 deletions(-) diff --git a/lfs.c b/lfs.c index e17e4bda..62efe7e4 100644 --- a/lfs.c +++ b/lfs.c @@ -5,7 +5,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ #include "lfs.h" -#include "lfs_util.h" +#include #define LFS_BLOCK_NULL ((lfs_block_t)-1) #define LFS_BLOCK_INLINE ((lfs_block_t)-2) @@ -1525,7 +1525,7 @@ static int lfs_dir_compact(lfs_t *lfs, if (lfs_pair_cmp(dir->pair, (const lfs_block_t[2]){0, 1}) == 0) { // oh no! we're writing too much to the superblock, // should we expand? - lfs_ssize_t res = _lfs_fs_size(lfs); + lfs_ssize_t res = lfs_fs_size_raw(lfs); if (res < 0) { return res; } @@ -1906,7 +1906,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, /// Top level directory operations /// -int _lfs_mkdir(lfs_t *lfs, const char *path) { +int lfs_mkdir_raw(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -2005,7 +2005,7 @@ int _lfs_mkdir(lfs_t *lfs, const char *path) { return 0; } -int _lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { +int lfs_dir_open_raw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL); if (tag < 0) { @@ -2056,7 +2056,7 @@ int _lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { return 0; } -int _lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { +int lfs_dir_close_raw(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); // remove from list of mdirs for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { @@ -2070,7 +2070,7 @@ int _lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { return 0; } -int _lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { +int lfs_dir_read_raw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { LFS_TRACE("lfs_dir_read(%p, %p, %p)", (void*)lfs, (void*)dir, (void*)info); memset(info, 0, sizeof(*info)); @@ -2123,11 +2123,11 @@ int _lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { return true; } -int _lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { +int lfs_dir_seek_raw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { LFS_TRACE("lfs_dir_seek(%p, %p, %"PRIu32")", (void*)lfs, (void*)dir, off); // simply walk from head dir - int err = _lfs_dir_rewind(lfs, dir); + int err = lfs_dir_rewind_raw(lfs, dir); if (err) { LFS_TRACE("lfs_dir_seek -> %d", err); return err; @@ -2166,14 +2166,14 @@ int _lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { return 0; } -lfs_soff_t _lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { +lfs_soff_t lfs_dir_tell_raw(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_tell(%p, %p)", (void*)lfs, (void*)dir); (void)lfs; LFS_TRACE("lfs_dir_tell -> %"PRId32, dir->pos); return dir->pos; } -int _lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { +int lfs_dir_rewind_raw(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_rewind(%p, %p)", (void*)lfs, (void*)dir); // reload the head dir int err = lfs_dir_fetch(lfs, &dir->m, dir->head); @@ -2380,7 +2380,7 @@ static int lfs_ctz_traverse(lfs_t *lfs, /// Top level file operations /// -int _lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, +int lfs_file_opencfg_raw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *cfg) { LFS_TRACE("lfs_file_opencfg(%p, %p, \"%s\", %x, %p {" @@ -2529,26 +2529,26 @@ int _lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, cleanup: // clean up lingering resources file->flags |= LFS_F_ERRED; - _lfs_file_close(lfs, file); + lfs_file_close_raw(lfs, file); LFS_TRACE("lfs_file_opencfg -> %d", err); return err; } -int _lfs_file_open(lfs_t *lfs, lfs_file_t *file, +int lfs_file_open_raw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { LFS_TRACE("lfs_file_open(%p, %p, \"%s\", %x)", (void*)lfs, (void*)file, path, flags); static const struct lfs_file_config defaults = {0}; - int err = _lfs_file_opencfg(lfs, file, path, flags, &defaults); + int err = lfs_file_opencfg_raw(lfs, file, path, flags, &defaults); LFS_TRACE("lfs_file_open -> %d", err); return err; } -int _lfs_file_close(lfs_t *lfs, lfs_file_t *file) { +int lfs_file_close_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); - int err = _lfs_file_sync(lfs, file); + int err = lfs_file_sync_raw(lfs, file); // remove from list of mdirs for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { @@ -2679,12 +2679,12 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { // copy over a byte at a time, leave it up to caching // to make this efficient uint8_t data; - lfs_ssize_t res = _lfs_file_read(lfs, &orig, &data, 1); + lfs_ssize_t res = lfs_file_read_raw(lfs, &orig, &data, 1); if (res < 0) { return res; } - res = _lfs_file_write(lfs, file, &data, 1); + res = lfs_file_write_raw(lfs, file, &data, 1); if (res < 0) { return res; } @@ -2731,7 +2731,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { return 0; } -int _lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { +int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -2788,7 +2788,7 @@ int _lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { return 0; } -lfs_ssize_t _lfs_file_read(lfs_t *lfs, lfs_file_t *file, +lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); @@ -2868,7 +2868,7 @@ lfs_ssize_t _lfs_file_read(lfs_t *lfs, lfs_file_t *file, return size; } -lfs_ssize_t _lfs_file_write(lfs_t *lfs, lfs_file_t *file, +lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); @@ -2903,7 +2903,7 @@ lfs_ssize_t _lfs_file_write(lfs_t *lfs, lfs_file_t *file, file->pos = file->ctz.size; while (file->pos < pos) { - lfs_ssize_t res = _lfs_file_write(lfs, file, &(uint8_t){0}, 1); + lfs_ssize_t res = lfs_file_write_raw(lfs, file, &(uint8_t){0}, 1); if (res < 0) { LFS_TRACE("lfs_file_write -> %"PRId32, res); return res; @@ -2999,7 +2999,7 @@ lfs_ssize_t _lfs_file_write(lfs_t *lfs, lfs_file_t *file, return size; } -lfs_soff_t _lfs_file_seek(lfs_t *lfs, lfs_file_t *file, +lfs_soff_t lfs_file_seek_raw(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { LFS_TRACE("lfs_file_seek(%p, %p, %"PRId32", %d)", (void*)lfs, (void*)file, off, whence); @@ -3034,7 +3034,7 @@ lfs_soff_t _lfs_file_seek(lfs_t *lfs, lfs_file_t *file, return npos; } -int _lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { +int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")", (void*)lfs, (void*)file, size); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -3046,7 +3046,7 @@ int _lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } lfs_off_t pos = file->pos; - lfs_off_t oldsize = _lfs_file_size(lfs, file); + lfs_off_t oldsize = lfs_file_size_raw(lfs, file); if (size < oldsize) { // need to flush since directly changing metadata int err = lfs_file_flush(lfs, file); @@ -3070,7 +3070,7 @@ int _lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } else if (size > oldsize) { // flush+seek if not already at end if (file->pos != oldsize) { - lfs_soff_t res = _lfs_file_seek(lfs, file, 0, LFS_SEEK_END); + lfs_soff_t res = lfs_file_seek_raw(lfs, file, 0, LFS_SEEK_END); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; @@ -3079,7 +3079,7 @@ int _lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { // fill with zeros while (file->pos < size) { - lfs_ssize_t res = _lfs_file_write(lfs, file, &(uint8_t){0}, 1); + lfs_ssize_t res = lfs_file_write_raw(lfs, file, &(uint8_t){0}, 1); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; @@ -3088,7 +3088,7 @@ int _lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } // restore pos - lfs_soff_t res = _lfs_file_seek(lfs, file, pos, LFS_SEEK_SET); + lfs_soff_t res = lfs_file_seek_raw(lfs, file, pos, LFS_SEEK_SET); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; @@ -3098,7 +3098,7 @@ int _lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { return 0; } -lfs_soff_t _lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { +lfs_soff_t lfs_file_tell_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; @@ -3106,9 +3106,9 @@ lfs_soff_t _lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { return file->pos; } -int _lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { +int lfs_file_rewind_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file); - lfs_soff_t res = _lfs_file_seek(lfs, file, 0, LFS_SEEK_SET); + lfs_soff_t res = lfs_file_seek_raw(lfs, file, 0, LFS_SEEK_SET); if (res < 0) { LFS_TRACE("lfs_file_rewind -> %"PRId32, res); return (int)res; @@ -3118,7 +3118,7 @@ int _lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { return 0; } -lfs_soff_t _lfs_file_size(lfs_t *lfs, lfs_file_t *file) { +lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; @@ -3134,7 +3134,7 @@ lfs_soff_t _lfs_file_size(lfs_t *lfs, lfs_file_t *file) { /// General fs operations /// -int _lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { +int lfs_stat_raw(lfs_t *lfs, const char *path, struct lfs_info *info) { LFS_TRACE("lfs_stat(%p, \"%s\", %p)", (void*)lfs, path, (void*)info); lfs_mdir_t cwd; lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); @@ -3148,7 +3148,7 @@ int _lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { return err; } -int _lfs_remove(lfs_t *lfs, const char *path) { +int lfs_remove_raw(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -3229,7 +3229,7 @@ int _lfs_remove(lfs_t *lfs, const char *path) { return 0; } -int _lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { +int lfs_rename_raw(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); // deorphan if we haven't yet, needed at most once after poweron @@ -3374,7 +3374,7 @@ int _lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { return 0; } -lfs_ssize_t _lfs_getattr(lfs_t *lfs, const char *path, +lfs_ssize_t lfs_getattr_raw(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { LFS_TRACE("lfs_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); @@ -3437,7 +3437,7 @@ static int lfs_commitattr(lfs_t *lfs, const char *path, {LFS_MKTAG(LFS_TYPE_USERATTR + type, id, size), buffer})); } -int _lfs_setattr(lfs_t *lfs, const char *path, +int lfs_setattr_raw(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); @@ -3451,7 +3451,7 @@ int _lfs_setattr(lfs_t *lfs, const char *path, return err; } -int _lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { +int lfs_removeattr_raw(lfs_t *lfs, const char *path, uint8_t type) { LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); int err = lfs_commitattr(lfs, path, type, NULL, 0x3ff); LFS_TRACE("lfs_removeattr -> %d", err); @@ -3584,7 +3584,7 @@ static int lfs_deinit(lfs_t *lfs) { return 0; } -int _lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { +int lfs_format_raw(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_format(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -3665,7 +3665,7 @@ int _lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { return err; } -int _lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { +int lfs_mount_raw(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_mount(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -3804,12 +3804,12 @@ int _lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { return 0; cleanup: - _lfs_unmount(lfs); + lfs_unmount_raw(lfs); LFS_TRACE("lfs_mount -> %d", err); return err; } -int _lfs_unmount(lfs_t *lfs) { +int lfs_unmount_raw(lfs_t *lfs) { LFS_TRACE("lfs_unmount(%p)", (void*)lfs); int err = lfs_deinit(lfs); LFS_TRACE("lfs_unmount -> %d", err); @@ -3914,7 +3914,7 @@ int lfs_fs_traverseraw(lfs_t *lfs, return 0; } -int _lfs_fs_traverse(lfs_t *lfs, +int lfs_fs_traverse_raw(lfs_t *lfs, int (*cb)(void *data, lfs_block_t block), void *data) { LFS_TRACE("lfs_fs_traverse(%p, %p, %p)", (void*)lfs, (void*)(uintptr_t)cb, data); @@ -4235,7 +4235,7 @@ static int lfs_fs_size_count(void *p, lfs_block_t block) { return 0; } -lfs_ssize_t _lfs_fs_size(lfs_t *lfs) { +lfs_ssize_t lfs_fs_size_raw(lfs_t *lfs) { LFS_TRACE("lfs_fs_size(%p)", (void*)lfs); lfs_size_t size = 0; int err = lfs_fs_traverseraw(lfs, lfs_fs_size_count, &size, false); @@ -4669,7 +4669,7 @@ static int lfs1_unmount(lfs_t *lfs) { } /// v1 migration /// -int _lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { +int lfs_migrate_raw(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_migrate(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -4912,123 +4912,520 @@ int _lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { #endif -#if LFS_THREAD_SAFE - -#define CREATE_LFS_TS_1(ret, function, b_type, b) \ - ret _ts ## function(b_type b) \ - { \ - int err = lfs->cfg->lock(lfs->cfg); \ - if (err) \ - { \ - return err; \ - } \ - err = function(b); \ - lfs->cfg->unlock(lfs->cfg); \ - return err; \ - } -#define CREATE_LFS_TS_2(ret, function, b_type, b, c_type, c) \ - ret _ts ## function(b_type b, c_type c) \ - { \ - int err = lfs->cfg->lock(lfs->cfg); \ - if (err) \ - { \ - return err; \ - } \ - err = function(b, c); \ - lfs->cfg->unlock(lfs->cfg); \ - return err; \ - } -#define CREATE_LFS_TS_2_CFG(ret, function, b_type, b, c_type, cfg) \ - ret _ts ## function(b_type b, c_type cfg) \ - { \ - int err = cfg->lock(cfg); \ - if (err) \ - { \ - return err; \ - } \ - err = function(b, cfg); \ - cfg->unlock(cfg); \ - return err; \ - } -#define CREATE_LFS_TS_3(ret, function, b_type, b, c_type, c, d_type, d) \ - ret _ts ## function(b_type b, c_type c, d_type d) \ - { \ - int err = lfs->cfg->lock(lfs->cfg); \ - if (err) \ - { \ - return err; \ - } \ - err = function(b, c, d); \ - lfs->cfg->unlock(lfs->cfg); \ - return err; \ - } -#define CREATE_LFS_TS_4(ret, function, b_type, b, c_type, c, d_type, d, e_type, e) \ - ret _ts ## function(b_type b, c_type c, d_type d, e_type e) \ - { \ - int err = lfs->cfg->lock(lfs->cfg); \ - if (err) \ - { \ - return err; \ - } \ - err = function(b, c, d, e); \ - lfs->cfg->unlock(lfs->cfg); \ - return err; \ - } -#define CREATE_LFS_TS_5(ret, function, b_type, b, c_type, c, d_type, d, e_type, e, f_type, f) \ - ret _ts ## function(b_type b, c_type c, d_type d, e_type e, f_type f) \ - { \ - int err = lfs->cfg->lock(lfs->cfg); \ - if (err) \ - { \ - return err; \ - } \ - err = function(b, c, d, e, f); \ - lfs->cfg->unlock(lfs->cfg); \ - return err; \ - } - -int _ts_lfs_fs_traverse (lfs_t * lfs, int (* cb)(void * data, lfs_block_t block), void * data) { +#if LFS_THREADSAFE + +int lfs_format (lfs_t * lfs, const struct lfs_config * config) { + int err = config->lock(config); + if (err) { + return err; + } + + err = lfs_format_raw(lfs, config); + config->unlock(config); + + return err; +} + +int lfs_mount (lfs_t * lfs, const struct lfs_config * config) { + int err = config->lock(config); + if (err) { + return err; + } + + err = lfs_mount_raw(lfs, config); + config->unlock(config); + + return err; +} + +int lfs_unmount (lfs_t * lfs) { int err = lfs->cfg->lock(lfs->cfg); - if (err) - { + if (err) { + return err; + } + + err = lfs_unmount_raw(lfs); + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_remove (lfs_t * lfs, const char * path) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_remove_raw(lfs, path); + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_rename (lfs_t * lfs, const char * oldpath, const char * newpath) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_rename_raw(lfs, oldpath, newpath); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_stat (lfs_t * lfs, const char * path, struct lfs_info * info) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_stat_raw(lfs, path, info); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +lfs_ssize_t lfs_getattr (lfs_t * lfs, const char * path, uint8_t type, void * buffer, lfs_size_t size) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_getattr_raw(lfs, path, type, buffer, size); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_setattr (lfs_t * lfs, const char * path, uint8_t type, const void * buffer, lfs_size_t size) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_setattr_raw(lfs, path, type, buffer, size); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_removeattr (lfs_t * lfs, const char * path, uint8_t type) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_removeattr_raw(lfs, path, type); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_file_open (lfs_t * lfs, lfs_file_t * file, const char * path, int flags) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_open_raw(lfs, file, path, flags); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_opencfg_raw(lfs, file, path, flags, config); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_file_close (lfs_t * lfs, lfs_file_t * file) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_close_raw(lfs, file); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_file_sync (lfs_t * lfs, lfs_file_t * file) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_sync_raw(lfs, file); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +lfs_ssize_t lfs_file_read (lfs_t * lfs, lfs_file_t * file, void * buffer, lfs_size_t size) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_read_raw(lfs, file, buffer, size); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +lfs_ssize_t lfs_file_write (lfs_t * lfs, lfs_file_t * file, const void * buffer, lfs_size_t size) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_write_raw(lfs, file, buffer, size); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +lfs_soff_t lfs_file_seek (lfs_t * lfs, lfs_file_t * file, lfs_soff_t off, int whence) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_seek_raw(lfs, file, off, whence); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_file_truncate (lfs_t * lfs, lfs_file_t * file, lfs_off_t size) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_truncate_raw(lfs, file, size); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +lfs_soff_t lfs_file_tell (lfs_t * lfs, lfs_file_t * file) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_tell_raw(lfs, file); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_file_rewind (lfs_t * lfs, lfs_file_t * file) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_rewind_raw(lfs, file); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +lfs_soff_t lfs_file_size (lfs_t * lfs, lfs_file_t * file) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_file_size_raw(lfs, file); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_mkdir (lfs_t * lfs, const char * path) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_mkdir_raw(lfs, path); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_dir_open (lfs_t * lfs, lfs_dir_t * dir, const char * path) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_dir_open_raw(lfs, dir, path); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_dir_close (lfs_t * lfs, lfs_dir_t * dir) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_dir_close_raw(lfs, dir); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_dir_read (lfs_t * lfs, lfs_dir_t * dir, struct lfs_info * info) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_dir_read_raw(lfs, dir, info); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_dir_seek (lfs_t * lfs, lfs_dir_t * dir, lfs_off_t off) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_dir_seek_raw(lfs, dir, off); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +lfs_soff_t lfs_dir_tell (lfs_t * lfs, lfs_dir_t * dir) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_dir_tell_raw(lfs, dir); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_dir_rewind (lfs_t * lfs, lfs_dir_t * dir) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_dir_rewind_raw(lfs, dir); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +lfs_ssize_t lfs_fs_size (lfs_t * lfs) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_fs_size_raw(lfs); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +int lfs_fs_traverse (lfs_t * lfs, int (* cb)(void *, lfs_block_t), void * data) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { return err; } - err = _lfs_fs_traverse(lfs, cb, data); + err = lfs_fs_traverse_raw(lfs, cb, data); + lfs->cfg->unlock(lfs->cfg); return err; } -CREATE_LFS_TS_2_CFG(int, _lfs_format,lfs_t *, lfs, const struct lfs_config *, config) -CREATE_LFS_TS_2_CFG(int, _lfs_mount,lfs_t *, lfs, const struct lfs_config *, config) -CREATE_LFS_TS_1(int, _lfs_unmount,lfs_t *, lfs) -CREATE_LFS_TS_2(int, _lfs_remove,lfs_t *, lfs, const char *, path) -CREATE_LFS_TS_3(int, _lfs_rename, lfs_t *, lfs, const char *, oldpath, const char *, newpath) -CREATE_LFS_TS_3(int, _lfs_stat, lfs_t *, lfs, const char *, path, struct lfs_info *, info) -CREATE_LFS_TS_5(lfs_ssize_t, _lfs_getattr, lfs_t *, lfs, const char *, path, uint8_t, type, void *, buffer, lfs_size_t, size) -CREATE_LFS_TS_5(int, _lfs_setattr, lfs_t *, lfs, const char *, path, uint8_t, type, const void *, buffer, lfs_size_t, size) -CREATE_LFS_TS_3(int, _lfs_removeattr, lfs_t *, lfs, const char *, path, uint8_t, type) -CREATE_LFS_TS_4(int, _lfs_file_open, lfs_t *, lfs, lfs_file_t *, file, const char *, path, int, flags) -CREATE_LFS_TS_5(int, _lfs_file_opencfg, lfs_t *, lfs, lfs_file_t *, file, const char *, path, int, flags, const struct lfs_file_config *, config) -CREATE_LFS_TS_2(int, _lfs_file_close, lfs_t *, lfs, lfs_file_t *, file) -CREATE_LFS_TS_2(int, _lfs_file_sync, lfs_t *, lfs, lfs_file_t *, file) -CREATE_LFS_TS_4(lfs_ssize_t, _lfs_file_read, lfs_t *, lfs, lfs_file_t *, file, void *, buffer, lfs_size_t, size) -CREATE_LFS_TS_4(lfs_ssize_t, _lfs_file_write, lfs_t *, lfs, lfs_file_t *, file, const void *, buffer, lfs_size_t, size) -CREATE_LFS_TS_4(lfs_soff_t, _lfs_file_seek, lfs_t *, lfs, lfs_file_t *, file, lfs_soff_t, off, int, whence) -CREATE_LFS_TS_3(int, _lfs_file_truncate, lfs_t *, lfs, lfs_file_t *, file, lfs_off_t, size) -CREATE_LFS_TS_2(lfs_soff_t, _lfs_file_tell, lfs_t *, lfs, lfs_file_t *, file) -CREATE_LFS_TS_2(int, _lfs_file_rewind, lfs_t *, lfs, lfs_file_t *, file) -CREATE_LFS_TS_2(lfs_soff_t, _lfs_file_size, lfs_t *, lfs, lfs_file_t *, file) -CREATE_LFS_TS_2(int, _lfs_mkdir, lfs_t *, lfs, const char *, path) -CREATE_LFS_TS_3(int, _lfs_dir_open, lfs_t *, lfs, lfs_dir_t *, dir, const char *, path) -CREATE_LFS_TS_2(int, _lfs_dir_close, lfs_t *, lfs, lfs_dir_t *, dir) -CREATE_LFS_TS_3(int, _lfs_dir_read, lfs_t *, lfs, lfs_dir_t *, dir, struct lfs_info *, info) -CREATE_LFS_TS_3(int, _lfs_dir_seek, lfs_t *, lfs, lfs_dir_t *, dir, lfs_off_t, off) -CREATE_LFS_TS_2(lfs_soff_t, _lfs_dir_tell, lfs_t *, lfs, lfs_dir_t *, dir) -CREATE_LFS_TS_2(int, _lfs_dir_rewind, lfs_t *, lfs, lfs_dir_t *, dir) -CREATE_LFS_TS_1(lfs_ssize_t, _lfs_fs_size, lfs_t *, lfs) #ifdef LFS_MIGRATE -CREATE_LFS_TS_2_CFG(int, _lfs_migrate, lfs_t *, lfs, const struct lfs_config *, cfg) + +int lfs_migrate (lfs_t * lfs, const struct lfs_config * cfg) { + int err = lfs->cfg->lock(lfs->cfg); + if (err) { + return err; + } + + err = lfs_migrate_raw(lfs, cfg); + + lfs->cfg->unlock(lfs->cfg); + + return err; +} + +#endif +#else + +int lfs_format (lfs_t * lfs, const struct lfs_config * config) { + return lfs_format_raw(lfs, config); +} + +int lfs_mount (lfs_t * lfs, const struct lfs_config * config) { + return lfs_mount_raw(lfs, config); +} + +int lfs_unmount (lfs_t * lfs) { + return lfs_unmount_raw(lfs); +} + +int lfs_remove (lfs_t * lfs, const char * path) { + return lfs_remove_raw(lfs, path); +} + +int lfs_rename (lfs_t * lfs, const char * oldpath, const char * newpath) { + return lfs_rename_raw(lfs, oldpath, newpath); +} + +int lfs_stat (lfs_t * lfs, const char * path, struct lfs_info * info) { + return lfs_stat_raw(lfs, path, info); +} + +lfs_ssize_t lfs_getattr (lfs_t * lfs, const char * path, uint8_t type, void * buffer, lfs_size_t size) { + return lfs_getattr_raw(lfs, path, type, buffer, size); +} + +int lfs_setattr (lfs_t * lfs, const char * path, uint8_t type, const void * buffer, lfs_size_t size) { + return lfs_setattr_raw(lfs, path, type, buffer, size); +} + +int lfs_removeattr (lfs_t * lfs, const char * path, uint8_t type) { + return lfs_removeattr_raw(lfs, path, type); +} + +int lfs_file_open (lfs_t * lfs, lfs_file_t * file, const char * path, int flags) { + return lfs_file_open_raw(lfs, file, path, flags); +} + +int lfs_file_opencfg_ts(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config) { + return lfs_file_opencfg_raw(lfs, file, path, flags, config); +} + +int lfs_file_close (lfs_t * lfs, lfs_file_t * file) { + return lfs_file_close_raw(lfs, file); +} + +int lfs_file_sync (lfs_t * lfs, lfs_file_t * file) { + return lfs_file_sync_raw(lfs, file); +} + +lfs_ssize_t lfs_file_read (lfs_t * lfs, lfs_file_t * file, void * buffer, lfs_size_t size) { + return lfs_file_read_raw(lfs, file, buffer, size); +} + +lfs_ssize_t lfs_file_write (lfs_t * lfs, lfs_file_t * file, const void * buffer, lfs_size_t size) { + return lfs_file_write_raw(lfs, file, buffer, size); +} + +lfs_soff_t lfs_file_seek (lfs_t * lfs, lfs_file_t * file, lfs_soff_t off, int whence) { + return lfs_file_seek_raw(lfs, file, off, whence); +} + +int lfs_file_truncate (lfs_t * lfs, lfs_file_t * file, lfs_off_t size) { + return lfs_file_truncate_raw(lfs, file, size); +} + +lfs_soff_t lfs_file_tell (lfs_t * lfs, lfs_file_t * file) { + return lfs_file_tell_raw(lfs, file); +} + +int lfs_file_rewind (lfs_t * lfs, lfs_file_t * file) { + return lfs_file_rewind_raw(lfs, file); +} + +lfs_soff_t lfs_file_size (lfs_t * lfs, lfs_file_t * file) { + return lfs_file_size_raw(lfs, file); +} + +int lfs_mkdir (lfs_t * lfs, const char * path) { + return lfs_mkdir_raw(lfs, path); +} + +int lfs_dir_open (lfs_t * lfs, lfs_dir_t * dir, const char * path) { + return lfs_dir_open_raw(lfs, dir, path); +} + +int lfs_dir_close (lfs_t * lfs, lfs_dir_t * dir) { + return lfs_dir_close_raw(lfs, dir); +} + +int lfs_dir_read (lfs_t * lfs, lfs_dir_t * dir, struct lfs_info * info) { + return lfs_dir_read_raw(lfs, dir, info); +} + +int lfs_dir_seek (lfs_t * lfs, lfs_dir_t * dir, lfs_off_t off) { + return lfs_dir_seek_raw(lfs, dir, off); +} + +lfs_soff_t lfs_dir_tell (lfs_t * lfs, lfs_dir_t * dir) { + return lfs_dir_tell_raw(lfs, dir); +} + +int lfs_dir_rewind (lfs_t * lfs, lfs_dir_t * dir) { + return lfs_dir_rewind_raw(lfs, dir); +} + +lfs_ssize_t lfs_fs_size (lfs_t * lfs) { + return lfs_fs_size_raw(lfs); +} + +int lfs_fs_traverse (lfs_t * lfs, int (* cb)(void *, lfs_block_t), void * data) { + return lfs_fs_traverse_raw(lfs, cb, data); +} + +#ifdef LFS_MIGRATE + +int lfs_migrate (lfs_t * lfs, const struct lfs_config * cfg) { + return lfs_migrate_raw(lfs, cfg); +} + #endif #endif \ No newline at end of file diff --git a/lfs.h b/lfs.h index 92f04904..19f9f678 100644 --- a/lfs.h +++ b/lfs.h @@ -9,14 +9,13 @@ #include #include -#include "lfs_util.h" +#include #ifdef __cplusplus extern "C" { #endif - /// Version info /// // Software library version @@ -54,7 +53,7 @@ typedef uint32_t lfs_block_t; // Maximum size of a file in bytes, may be redefined to limit to support other // drivers. Limited on disk to <= 4294967296. However, above 2147483647 the -// functions _lfs_file_seek, _lfs_file_size, and _lfs_file_tell will return +// functions lfs_file_seek, lfs_file_size, and lfs_file_tell will return // incorrect values due to using signed integers. Stored in superblock and // must be respected by other littlefs drivers. #ifndef LFS_FILE_MAX @@ -85,9 +84,6 @@ enum lfs_error { LFS_ERR_NOMEM = -12, // No more memory available LFS_ERR_NOATTR = -61, // No data/attr available LFS_ERR_NAMETOOLONG = -36, // File name too long -#if LFS_THREAD_SAFE - LFS_ERR_LOCK = -23, // Failed to aquire lock -#endif }; // File types @@ -178,7 +174,7 @@ struct lfs_config { // are propogated to the user. int (*sync)(const struct lfs_config *c); - #if LFS_THREAD_SAFE +#if LFS_THREADSAFE // Lock the underlying block device. Negative error codes // are propogated to the user. int (*lock)(const struct lfs_config *c); @@ -186,7 +182,7 @@ struct lfs_config { // Unlock the underlying block device. Negative error codes // are propogated to the user. int (*unlock)(const struct lfs_config *c); - #endif +#endif // Minimum size of a block read. All read operations will be a // multiple of this value. @@ -420,7 +416,7 @@ typedef struct lfs { // be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. -int _lfs_format(lfs_t *lfs, const struct lfs_config *config); +int lfs_format(lfs_t *lfs, const struct lfs_config *config); // Mounts a littlefs // @@ -430,13 +426,13 @@ int _lfs_format(lfs_t *lfs, const struct lfs_config *config); // be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. -int _lfs_mount(lfs_t *lfs, const struct lfs_config *config); +int lfs_mount(lfs_t *lfs, const struct lfs_config *config); // Unmounts a littlefs // // Does nothing besides releasing any allocated resources. // Returns a negative error code on failure. -int _lfs_unmount(lfs_t *lfs); +int lfs_unmount(lfs_t *lfs); /// General operations /// @@ -444,7 +440,7 @@ int _lfs_unmount(lfs_t *lfs); // // If removing a directory, the directory must be empty. // Returns a negative error code on failure. -int _lfs_remove(lfs_t *lfs, const char *path); +int lfs_remove(lfs_t *lfs, const char *path); // Rename or move a file or directory // @@ -452,13 +448,13 @@ int _lfs_remove(lfs_t *lfs, const char *path); // If the destination is a directory, the directory must be empty. // // Returns a negative error code on failure. -int _lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath); +int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath); // Find info about a file or directory // // Fills out the info structure, based on the specified file or directory. // Returns a negative error code on failure. -int _lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); +int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); // Get a custom attribute // @@ -472,7 +468,7 @@ int _lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); // Note, the returned size is the size of the attribute on disk, irrespective // of the size of the buffer. This can be used to dynamically allocate a buffer // or check for existance. -lfs_ssize_t _lfs_getattr(lfs_t *lfs, const char *path, +lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size); // Set custom attributes @@ -482,7 +478,7 @@ lfs_ssize_t _lfs_getattr(lfs_t *lfs, const char *path, // implicitly created. // // Returns a negative error code on failure. -int _lfs_setattr(lfs_t *lfs, const char *path, +int lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size); // Removes a custom attribute @@ -490,7 +486,7 @@ int _lfs_setattr(lfs_t *lfs, const char *path, // If an attribute is not found, nothing happens. // // Returns a negative error code on failure. -int _lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); +int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); /// File operations /// @@ -501,7 +497,7 @@ int _lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); // are values from the enum lfs_open_flags that are bitwise-ored together. // // Returns a negative error code on failure. -int _lfs_file_open(lfs_t *lfs, lfs_file_t *file, +int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags); // Open a file with extra configuration @@ -514,7 +510,7 @@ int _lfs_file_open(lfs_t *lfs, lfs_file_t *file, // config struct must be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. -int _lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, +int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config); @@ -524,19 +520,19 @@ int _lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, // sync had been called and releases any allocated resources. // // Returns a negative error code on failure. -int _lfs_file_close(lfs_t *lfs, lfs_file_t *file); +int lfs_file_close(lfs_t *lfs, lfs_file_t *file); // Synchronize a file on storage // // Any pending writes are written out to storage. // Returns a negative error code on failure. -int _lfs_file_sync(lfs_t *lfs, lfs_file_t *file); +int lfs_file_sync(lfs_t *lfs, lfs_file_t *file); // Read data from file // // Takes a buffer and size indicating where to store the read data. // Returns the number of bytes read, or a negative error code on failure. -lfs_ssize_t _lfs_file_read(lfs_t *lfs, lfs_file_t *file, +lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); // Write data to file @@ -545,38 +541,38 @@ lfs_ssize_t _lfs_file_read(lfs_t *lfs, lfs_file_t *file, // actually be updated on the storage until either sync or close is called. // // Returns the number of bytes written, or a negative error code on failure. -lfs_ssize_t _lfs_file_write(lfs_t *lfs, lfs_file_t *file, +lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); // Change the position of the file // // The change in position is determined by the offset and whence flag. // Returns the new position of the file, or a negative error code on failure. -lfs_soff_t _lfs_file_seek(lfs_t *lfs, lfs_file_t *file, +lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence); // Truncates the size of the file to the specified size // // Returns a negative error code on failure. -int _lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); +int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); // Return the position of the file // -// Equivalent to _lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR) +// Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR) // Returns the position of the file, or a negative error code on failure. -lfs_soff_t _lfs_file_tell(lfs_t *lfs, lfs_file_t *file); +lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file); // Change the position of the file to the beginning of the file // -// Equivalent to _lfs_file_seek(lfs, file, 0, LFS_SEEK_SET) +// Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_SET) // Returns a negative error code on failure. -int _lfs_file_rewind(lfs_t *lfs, lfs_file_t *file); +int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file); // Return the size of the file // -// Similar to _lfs_file_seek(lfs, file, 0, LFS_SEEK_END) +// Similar to lfs_file_seek(lfs, file, 0, LFS_SEEK_END) // Returns the size of the file, or a negative error code on failure. -lfs_soff_t _lfs_file_size(lfs_t *lfs, lfs_file_t *file); +lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file); /// Directory operations /// @@ -584,26 +580,26 @@ lfs_soff_t _lfs_file_size(lfs_t *lfs, lfs_file_t *file); // Create a directory // // Returns a negative error code on failure. -int _lfs_mkdir(lfs_t *lfs, const char *path); +int lfs_mkdir(lfs_t *lfs, const char *path); // Open a directory // // Once open a directory can be used with read to iterate over files. // Returns a negative error code on failure. -int _lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path); +int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path); // Close a directory // // Releases any allocated resources. // Returns a negative error code on failure. -int _lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir); +int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir); // Read an entry in the directory // // Fills out the info structure, based on the specified file or directory. // Returns a positive value on success, 0 at the end of directory, // or a negative error code on failure. -int _lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); +int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); // Change the position of the directory // @@ -611,7 +607,7 @@ int _lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); // an absolute offset in the directory seek. // // Returns a negative error code on failure. -int _lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); +int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); // Return the position of the directory // @@ -619,12 +615,12 @@ int _lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); // sense, but does indicate the current position in the directory iteration. // // Returns the position of the directory, or a negative error code on failure. -lfs_soff_t _lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir); +lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir); // Change the position of the directory to the beginning of the directory // // Returns a negative error code on failure. -int _lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir); +int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir); /// Filesystem-level filesystem operations @@ -635,7 +631,7 @@ int _lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir); // size may be larger than the filesystem actually is. // // Returns the number of allocated blocks, or a negative error code on failure. -lfs_ssize_t _lfs_fs_size(lfs_t *lfs); +lfs_ssize_t lfs_fs_size(lfs_t *lfs); // Traverse through all blocks in use by the filesystem // @@ -644,12 +640,12 @@ lfs_ssize_t _lfs_fs_size(lfs_t *lfs); // blocks are in use or how much of the storage is available. // // Returns a negative error code on failure. -int _lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); +int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); #ifdef LFS_MIGRATE // Attempts to migrate a previous version of littlefs // -// Behaves similarly to the _lfs_format function. Attempts to mount +// Behaves similarly to the lfs_format function. Attempts to mount // the previous version of littlefs and update the filesystem so it can be // mounted with the current version of littlefs. // @@ -658,108 +654,39 @@ int _lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); // be zeroed for defaults and backwards compatibility. // // Returns a negative error code on failure. -int _lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); -#endif - -#if LFS_THREAD_SAFE - -int _ts_lfs_format(lfs_t *lfs, const struct lfs_config *config); -int _ts_lfs_mount(lfs_t *lfs, const struct lfs_config *config); -int _ts_lfs_unmount(lfs_t *lfs); -int _ts_lfs_remove(lfs_t *lfs, const char *path); -int _ts_lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath); -int _ts_lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info); -lfs_ssize_t _ts_lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size); -int _ts_lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size); -int _ts_lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type); -int _ts_lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags); -int _ts_lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config); -int _ts_lfs_file_close(lfs_t *lfs, lfs_file_t *file); -int _ts_lfs_file_sync(lfs_t *lfs, lfs_file_t *file); -lfs_ssize_t _ts_lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); -lfs_ssize_t _ts_lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); -lfs_soff_t _ts_lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence); -int _ts_lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); -lfs_soff_t _ts_lfs_file_tell(lfs_t *lfs, lfs_file_t *file); -int _ts_lfs_file_rewind(lfs_t *lfs, lfs_file_t *file); -lfs_soff_t _ts_lfs_file_size(lfs_t *lfs, lfs_file_t *file); -int _ts_lfs_mkdir(lfs_t *lfs, const char *path); -int _ts_lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path); -int _ts_lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir); -int _ts_lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); -int _ts_lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); -lfs_soff_t _ts_lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir); -int _ts_lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir); -lfs_ssize_t _ts_lfs_fs_size(lfs_t *lfs); -int _ts_lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); -int _ts_lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); - -#define lfs_format _ts_lfs_format -#define lfs_mount _ts_lfs_mount -#define lfs_unmount _ts_lfs_unmount -#define lfs_remove _ts_lfs_remove -#define lfs_rename _ts_lfs_rename -#define lfs_stat _ts_lfs_stat -#define lfs_getattr _ts_lfs_getattr -#define lfs_setattr _ts_lfs_setattr -#define lfs_removeattr _ts_lfs_removeattr -#define lfs_file_open _ts_lfs_file_open -#define lfs_file_opencfg _ts_lfs_file_opencfg -#define lfs_file_close _ts_lfs_file_close -#define lfs_file_sync _ts_lfs_file_sync -#define lfs_file_read _ts_lfs_file_read -#define lfs_file_write _ts_lfs_file_write -#define lfs_file_seek _ts_lfs_file_seek -#define lfs_file_truncate _ts_lfs_file_truncate -#define lfs_file_tell _ts_lfs_file_tell -#define lfs_file_rewind _ts_lfs_file_rewind -#define lfs_file_size _ts_lfs_file_size -#define lfs_mkdir _ts_lfs_mkdir -#define lfs_dir_open _ts_lfs_dir_open -#define lfs_dir_close _ts_lfs_dir_close -#define lfs_dir_read _ts_lfs_dir_read -#define lfs_dir_seek _ts_lfs_dir_seek -#define lfs_dir_tell _ts_lfs_dir_tell -#define lfs_dir_rewind _ts_lfs_dir_rewind -#define lfs_fs_size _ts_lfs_fs_size -#define lfs_fs_traverse _ts_lfs_fs_traverse -#define lfs_migrate _ts_lfs_migrate - -#else - -#define lfs_format _lfs_format -#define lfs_mount _lfs_mount -#define lfs_unmount _lfs_unmount -#define lfs_remove _lfs_remove -#define lfs_rename _lfs_rename -#define lfs_stat _lfs_stat -#define lfs_getattr _lfs_getattr -#define lfs_setattr _lfs_setattr -#define lfs_removeattr _lfs_removeattr -#define lfs_file_open _lfs_file_open -#define lfs_file_opencfg _lfs_file_opencfg -#define lfs_file_close _lfs_file_close -#define lfs_file_sync _lfs_file_sync -#define lfs_file_read _lfs_file_read -#define lfs_file_write _lfs_file_write -#define lfs_file_seek _lfs_file_seek -#define lfs_file_truncate _lfs_file_truncate -#define lfs_file_tell _lfs_file_tell -#define lfs_file_rewind _lfs_file_rewind -#define lfs_file_size _lfs_file_size -#define lfs_mkdir _lfs_mkdir -#define lfs_dir_open _lfs_dir_open -#define lfs_dir_close _lfs_dir_close -#define lfs_dir_read _lfs_dir_read -#define lfs_dir_seek _lfs_dir_seek -#define lfs_dir_tell _lfs_dir_tell -#define lfs_dir_rewind _lfs_dir_rewind -#define lfs_fs_size _lfs_fs_size -#define lfs_fs_traverse _lfs_fs_traverse -#define lfs_migrate _lfs_migrate - +int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); #endif +int lfs_format_raw(lfs_t *lfs, const struct lfs_config *config); +int lfs_mount_raw(lfs_t *lfs, const struct lfs_config *config); +int lfs_unmount_raw(lfs_t *lfs); +int lfs_remove_raw(lfs_t *lfs, const char *path); +int lfs_rename_raw(lfs_t *lfs, const char *oldpath, const char *newpath); +int lfs_stat_raw(lfs_t *lfs, const char *path, struct lfs_info *info); +lfs_ssize_t lfs_getattr_raw(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size); +int lfs_setattr_raw(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size); +int lfs_removeattr_raw(lfs_t *lfs, const char *path, uint8_t type); +int lfs_file_open_raw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags); +int lfs_file_opencfg_raw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config); +int lfs_file_close_raw(lfs_t *lfs, lfs_file_t *file); +int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file); +lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); +lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); +lfs_soff_t lfs_file_seek_raw(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence); +int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); +lfs_soff_t lfs_file_tell_raw(lfs_t *lfs, lfs_file_t *file); +int lfs_file_rewind_raw(lfs_t *lfs, lfs_file_t *file); +lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file); +int lfs_mkdir_raw(lfs_t *lfs, const char *path); +int lfs_dir_open_raw(lfs_t *lfs, lfs_dir_t *dir, const char *path); +int lfs_dir_close_raw(lfs_t *lfs, lfs_dir_t *dir); +int lfs_dir_read_raw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); +int lfs_dir_seek_raw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); +lfs_soff_t lfs_dir_tell_raw(lfs_t *lfs, lfs_dir_t *dir); +int lfs_dir_rewind_raw(lfs_t *lfs, lfs_dir_t *dir); +lfs_ssize_t lfs_fs_size_raw(lfs_t *lfs); +int lfs_fs_traverse_raw(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); +int lfs_migrate_raw(lfs_t *lfs, const struct lfs_config *cfg); #ifdef __cplusplus } /* extern "C" */ diff --git a/lfs_util.h b/lfs_util.h index 47e8a940..84bab118 100644 --- a/lfs_util.h +++ b/lfs_util.h @@ -43,8 +43,9 @@ extern "C" { #endif -#ifndef LFS_THREAD_SAFE -#define LFS_THREAD_SAFE 0 +// Enables thread-safe wrappers using the lock/unlock callbacks in lfs_config +#ifndef LFS_THREADSAFE +#define LFS_THREADSAFE 0 #endif // Macros, may be replaced by system specific wrappers. Arguments to these From fc6988c7c3c796ffcb7973e5022321f87ede44fc Mon Sep 17 00:00:00 2001 From: Bill Gesner Date: Fri, 20 Nov 2020 17:01:04 +0000 Subject: [PATCH 19/26] make raw functions static. formatting tweaks --- lfs.c | 187 ++++++++++++++++++++++++++++++---------------------------- lfs.h | 31 ---------- 2 files changed, 98 insertions(+), 120 deletions(-) diff --git a/lfs.c b/lfs.c index 62efe7e4..b472ba5b 100644 --- a/lfs.c +++ b/lfs.c @@ -10,6 +10,15 @@ #define LFS_BLOCK_NULL ((lfs_block_t)-1) #define LFS_BLOCK_INLINE ((lfs_block_t)-2) +static int lfs_dir_rewind_raw(lfs_t *lfs, lfs_dir_t *dir); +static int lfs_file_close_raw(lfs_t *lfs, lfs_file_t *file); +static lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); +static lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file); +static int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file); +static lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); +static lfs_ssize_t lfs_fs_size_raw(lfs_t *lfs); +static int lfs_unmount_raw(lfs_t *lfs); + /// Caching block device operations /// static inline void lfs_cache_drop(lfs_t *lfs, lfs_cache_t *rcache) { // do not zero, cheaper if cache is readonly or only going to be @@ -1906,7 +1915,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, /// Top level directory operations /// -int lfs_mkdir_raw(lfs_t *lfs, const char *path) { +static int lfs_mkdir_raw(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -2005,7 +2014,7 @@ int lfs_mkdir_raw(lfs_t *lfs, const char *path) { return 0; } -int lfs_dir_open_raw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { +static int lfs_dir_open_raw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL); if (tag < 0) { @@ -2056,7 +2065,7 @@ int lfs_dir_open_raw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { return 0; } -int lfs_dir_close_raw(lfs_t *lfs, lfs_dir_t *dir) { +static int lfs_dir_close_raw(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); // remove from list of mdirs for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { @@ -2070,7 +2079,7 @@ int lfs_dir_close_raw(lfs_t *lfs, lfs_dir_t *dir) { return 0; } -int lfs_dir_read_raw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { +static int lfs_dir_read_raw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { LFS_TRACE("lfs_dir_read(%p, %p, %p)", (void*)lfs, (void*)dir, (void*)info); memset(info, 0, sizeof(*info)); @@ -2123,7 +2132,7 @@ int lfs_dir_read_raw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { return true; } -int lfs_dir_seek_raw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { +static int lfs_dir_seek_raw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { LFS_TRACE("lfs_dir_seek(%p, %p, %"PRIu32")", (void*)lfs, (void*)dir, off); // simply walk from head dir @@ -2166,14 +2175,14 @@ int lfs_dir_seek_raw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { return 0; } -lfs_soff_t lfs_dir_tell_raw(lfs_t *lfs, lfs_dir_t *dir) { +static lfs_soff_t lfs_dir_tell_raw(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_tell(%p, %p)", (void*)lfs, (void*)dir); (void)lfs; LFS_TRACE("lfs_dir_tell -> %"PRId32, dir->pos); return dir->pos; } -int lfs_dir_rewind_raw(lfs_t *lfs, lfs_dir_t *dir) { +static int lfs_dir_rewind_raw(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_rewind(%p, %p)", (void*)lfs, (void*)dir); // reload the head dir int err = lfs_dir_fetch(lfs, &dir->m, dir->head); @@ -2380,7 +2389,7 @@ static int lfs_ctz_traverse(lfs_t *lfs, /// Top level file operations /// -int lfs_file_opencfg_raw(lfs_t *lfs, lfs_file_t *file, +static int lfs_file_opencfg_raw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *cfg) { LFS_TRACE("lfs_file_opencfg(%p, %p, \"%s\", %x, %p {" @@ -2534,7 +2543,7 @@ int lfs_file_opencfg_raw(lfs_t *lfs, lfs_file_t *file, return err; } -int lfs_file_open_raw(lfs_t *lfs, lfs_file_t *file, +static int lfs_file_open_raw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { LFS_TRACE("lfs_file_open(%p, %p, \"%s\", %x)", (void*)lfs, (void*)file, path, flags); @@ -2544,7 +2553,7 @@ int lfs_file_open_raw(lfs_t *lfs, lfs_file_t *file, return err; } -int lfs_file_close_raw(lfs_t *lfs, lfs_file_t *file) { +static int lfs_file_close_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -2731,7 +2740,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { return 0; } -int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file) { +static int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -2788,7 +2797,7 @@ int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file) { return 0; } -lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, +static lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); @@ -2868,7 +2877,7 @@ lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, return size; } -lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, +static lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); @@ -2999,7 +3008,7 @@ lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, return size; } -lfs_soff_t lfs_file_seek_raw(lfs_t *lfs, lfs_file_t *file, +static lfs_soff_t lfs_file_seek_raw(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { LFS_TRACE("lfs_file_seek(%p, %p, %"PRId32", %d)", (void*)lfs, (void*)file, off, whence); @@ -3034,7 +3043,7 @@ lfs_soff_t lfs_file_seek_raw(lfs_t *lfs, lfs_file_t *file, return npos; } -int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { +static int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")", (void*)lfs, (void*)file, size); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -3098,7 +3107,7 @@ int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { return 0; } -lfs_soff_t lfs_file_tell_raw(lfs_t *lfs, lfs_file_t *file) { +static lfs_soff_t lfs_file_tell_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; @@ -3106,7 +3115,7 @@ lfs_soff_t lfs_file_tell_raw(lfs_t *lfs, lfs_file_t *file) { return file->pos; } -int lfs_file_rewind_raw(lfs_t *lfs, lfs_file_t *file) { +static int lfs_file_rewind_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file); lfs_soff_t res = lfs_file_seek_raw(lfs, file, 0, LFS_SEEK_SET); if (res < 0) { @@ -3118,7 +3127,7 @@ int lfs_file_rewind_raw(lfs_t *lfs, lfs_file_t *file) { return 0; } -lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file) { +static lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; @@ -3134,7 +3143,7 @@ lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file) { /// General fs operations /// -int lfs_stat_raw(lfs_t *lfs, const char *path, struct lfs_info *info) { +static int lfs_stat_raw(lfs_t *lfs, const char *path, struct lfs_info *info) { LFS_TRACE("lfs_stat(%p, \"%s\", %p)", (void*)lfs, path, (void*)info); lfs_mdir_t cwd; lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); @@ -3148,7 +3157,7 @@ int lfs_stat_raw(lfs_t *lfs, const char *path, struct lfs_info *info) { return err; } -int lfs_remove_raw(lfs_t *lfs, const char *path) { +static int lfs_remove_raw(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -3229,7 +3238,7 @@ int lfs_remove_raw(lfs_t *lfs, const char *path) { return 0; } -int lfs_rename_raw(lfs_t *lfs, const char *oldpath, const char *newpath) { +static int lfs_rename_raw(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); // deorphan if we haven't yet, needed at most once after poweron @@ -3374,7 +3383,7 @@ int lfs_rename_raw(lfs_t *lfs, const char *oldpath, const char *newpath) { return 0; } -lfs_ssize_t lfs_getattr_raw(lfs_t *lfs, const char *path, +static lfs_ssize_t lfs_getattr_raw(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { LFS_TRACE("lfs_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); @@ -3437,7 +3446,7 @@ static int lfs_commitattr(lfs_t *lfs, const char *path, {LFS_MKTAG(LFS_TYPE_USERATTR + type, id, size), buffer})); } -int lfs_setattr_raw(lfs_t *lfs, const char *path, +static int lfs_setattr_raw(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); @@ -3451,7 +3460,7 @@ int lfs_setattr_raw(lfs_t *lfs, const char *path, return err; } -int lfs_removeattr_raw(lfs_t *lfs, const char *path, uint8_t type) { +static int lfs_removeattr_raw(lfs_t *lfs, const char *path, uint8_t type) { LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); int err = lfs_commitattr(lfs, path, type, NULL, 0x3ff); LFS_TRACE("lfs_removeattr -> %d", err); @@ -3584,7 +3593,7 @@ static int lfs_deinit(lfs_t *lfs) { return 0; } -int lfs_format_raw(lfs_t *lfs, const struct lfs_config *cfg) { +static int lfs_format_raw(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_format(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -3665,7 +3674,7 @@ int lfs_format_raw(lfs_t *lfs, const struct lfs_config *cfg) { return err; } -int lfs_mount_raw(lfs_t *lfs, const struct lfs_config *cfg) { +static int lfs_mount_raw(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_mount(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -3809,7 +3818,7 @@ int lfs_mount_raw(lfs_t *lfs, const struct lfs_config *cfg) { return err; } -int lfs_unmount_raw(lfs_t *lfs) { +static int lfs_unmount_raw(lfs_t *lfs) { LFS_TRACE("lfs_unmount(%p)", (void*)lfs); int err = lfs_deinit(lfs); LFS_TRACE("lfs_unmount -> %d", err); @@ -3914,7 +3923,7 @@ int lfs_fs_traverseraw(lfs_t *lfs, return 0; } -int lfs_fs_traverse_raw(lfs_t *lfs, +static int lfs_fs_traverse_raw(lfs_t *lfs, int (*cb)(void *data, lfs_block_t block), void *data) { LFS_TRACE("lfs_fs_traverse(%p, %p, %p)", (void*)lfs, (void*)(uintptr_t)cb, data); @@ -4235,7 +4244,7 @@ static int lfs_fs_size_count(void *p, lfs_block_t block) { return 0; } -lfs_ssize_t lfs_fs_size_raw(lfs_t *lfs) { +static lfs_ssize_t lfs_fs_size_raw(lfs_t *lfs) { LFS_TRACE("lfs_fs_size(%p)", (void*)lfs); lfs_size_t size = 0; int err = lfs_fs_traverseraw(lfs, lfs_fs_size_count, &size, false); @@ -4669,7 +4678,7 @@ static int lfs1_unmount(lfs_t *lfs) { } /// v1 migration /// -int lfs_migrate_raw(lfs_t *lfs, const struct lfs_config *cfg) { +static int lfs_migrate_raw(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_migrate(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -4914,7 +4923,7 @@ int lfs_migrate_raw(lfs_t *lfs, const struct lfs_config *cfg) { #if LFS_THREADSAFE -int lfs_format (lfs_t * lfs, const struct lfs_config * config) { +int lfs_format(lfs_t *lfs, const struct lfs_config *config) { int err = config->lock(config); if (err) { return err; @@ -4926,7 +4935,7 @@ int lfs_format (lfs_t * lfs, const struct lfs_config * config) { return err; } -int lfs_mount (lfs_t * lfs, const struct lfs_config * config) { +int lfs_mount(lfs_t *lfs, const struct lfs_config *config) { int err = config->lock(config); if (err) { return err; @@ -4938,7 +4947,7 @@ int lfs_mount (lfs_t * lfs, const struct lfs_config * config) { return err; } -int lfs_unmount (lfs_t * lfs) { +int lfs_unmount(lfs_t *lfs) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -4950,7 +4959,7 @@ int lfs_unmount (lfs_t * lfs) { return err; } -int lfs_remove (lfs_t * lfs, const char * path) { +int lfs_remove(lfs_t *lfs, const char *path) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -4962,7 +4971,7 @@ int lfs_remove (lfs_t * lfs, const char * path) { return err; } -int lfs_rename (lfs_t * lfs, const char * oldpath, const char * newpath) { +int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -4975,7 +4984,7 @@ int lfs_rename (lfs_t * lfs, const char * oldpath, const char * newpath) { return err; } -int lfs_stat (lfs_t * lfs, const char * path, struct lfs_info * info) { +int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -4988,7 +4997,7 @@ int lfs_stat (lfs_t * lfs, const char * path, struct lfs_info * info) { return err; } -lfs_ssize_t lfs_getattr (lfs_t * lfs, const char * path, uint8_t type, void * buffer, lfs_size_t size) { +lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5001,7 +5010,7 @@ lfs_ssize_t lfs_getattr (lfs_t * lfs, const char * path, uint8_t type, void * bu return err; } -int lfs_setattr (lfs_t * lfs, const char * path, uint8_t type, const void * buffer, lfs_size_t size) { +int lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5014,7 +5023,7 @@ int lfs_setattr (lfs_t * lfs, const char * path, uint8_t type, const void * buff return err; } -int lfs_removeattr (lfs_t * lfs, const char * path, uint8_t type) { +int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5027,7 +5036,7 @@ int lfs_removeattr (lfs_t * lfs, const char * path, uint8_t type) { return err; } -int lfs_file_open (lfs_t * lfs, lfs_file_t * file, const char * path, int flags) { +int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5053,7 +5062,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, return err; } -int lfs_file_close (lfs_t * lfs, lfs_file_t * file) { +int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5066,7 +5075,7 @@ int lfs_file_close (lfs_t * lfs, lfs_file_t * file) { return err; } -int lfs_file_sync (lfs_t * lfs, lfs_file_t * file) { +int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5079,7 +5088,7 @@ int lfs_file_sync (lfs_t * lfs, lfs_file_t * file) { return err; } -lfs_ssize_t lfs_file_read (lfs_t * lfs, lfs_file_t * file, void * buffer, lfs_size_t size) { +lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5092,7 +5101,7 @@ lfs_ssize_t lfs_file_read (lfs_t * lfs, lfs_file_t * file, void * buffer, lfs_si return err; } -lfs_ssize_t lfs_file_write (lfs_t * lfs, lfs_file_t * file, const void * buffer, lfs_size_t size) { +lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5105,7 +5114,7 @@ lfs_ssize_t lfs_file_write (lfs_t * lfs, lfs_file_t * file, const void * buffer, return err; } -lfs_soff_t lfs_file_seek (lfs_t * lfs, lfs_file_t * file, lfs_soff_t off, int whence) { +lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5118,7 +5127,7 @@ lfs_soff_t lfs_file_seek (lfs_t * lfs, lfs_file_t * file, lfs_soff_t off, int wh return err; } -int lfs_file_truncate (lfs_t * lfs, lfs_file_t * file, lfs_off_t size) { +int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5131,7 +5140,7 @@ int lfs_file_truncate (lfs_t * lfs, lfs_file_t * file, lfs_off_t size) { return err; } -lfs_soff_t lfs_file_tell (lfs_t * lfs, lfs_file_t * file) { +lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5144,7 +5153,7 @@ lfs_soff_t lfs_file_tell (lfs_t * lfs, lfs_file_t * file) { return err; } -int lfs_file_rewind (lfs_t * lfs, lfs_file_t * file) { +int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5157,7 +5166,7 @@ int lfs_file_rewind (lfs_t * lfs, lfs_file_t * file) { return err; } -lfs_soff_t lfs_file_size (lfs_t * lfs, lfs_file_t * file) { +lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5170,7 +5179,7 @@ lfs_soff_t lfs_file_size (lfs_t * lfs, lfs_file_t * file) { return err; } -int lfs_mkdir (lfs_t * lfs, const char * path) { +int lfs_mkdir(lfs_t *lfs, const char *path) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5183,7 +5192,7 @@ int lfs_mkdir (lfs_t * lfs, const char * path) { return err; } -int lfs_dir_open (lfs_t * lfs, lfs_dir_t * dir, const char * path) { +int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5196,7 +5205,7 @@ int lfs_dir_open (lfs_t * lfs, lfs_dir_t * dir, const char * path) { return err; } -int lfs_dir_close (lfs_t * lfs, lfs_dir_t * dir) { +int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5209,7 +5218,7 @@ int lfs_dir_close (lfs_t * lfs, lfs_dir_t * dir) { return err; } -int lfs_dir_read (lfs_t * lfs, lfs_dir_t * dir, struct lfs_info * info) { +int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5222,7 +5231,7 @@ int lfs_dir_read (lfs_t * lfs, lfs_dir_t * dir, struct lfs_info * info) { return err; } -int lfs_dir_seek (lfs_t * lfs, lfs_dir_t * dir, lfs_off_t off) { +int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5235,7 +5244,7 @@ int lfs_dir_seek (lfs_t * lfs, lfs_dir_t * dir, lfs_off_t off) { return err; } -lfs_soff_t lfs_dir_tell (lfs_t * lfs, lfs_dir_t * dir) { +lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5248,7 +5257,7 @@ lfs_soff_t lfs_dir_tell (lfs_t * lfs, lfs_dir_t * dir) { return err; } -int lfs_dir_rewind (lfs_t * lfs, lfs_dir_t * dir) { +int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5261,7 +5270,7 @@ int lfs_dir_rewind (lfs_t * lfs, lfs_dir_t * dir) { return err; } -lfs_ssize_t lfs_fs_size (lfs_t * lfs) { +lfs_ssize_t lfs_fs_size(lfs_t *lfs) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5274,7 +5283,7 @@ lfs_ssize_t lfs_fs_size (lfs_t * lfs) { return err; } -int lfs_fs_traverse (lfs_t * lfs, int (* cb)(void *, lfs_block_t), void * data) { +int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5289,7 +5298,7 @@ int lfs_fs_traverse (lfs_t * lfs, int (* cb)(void *, lfs_block_t), void * data) #ifdef LFS_MIGRATE -int lfs_migrate (lfs_t * lfs, const struct lfs_config * cfg) { +int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { int err = lfs->cfg->lock(lfs->cfg); if (err) { return err; @@ -5305,125 +5314,125 @@ int lfs_migrate (lfs_t * lfs, const struct lfs_config * cfg) { #endif #else -int lfs_format (lfs_t * lfs, const struct lfs_config * config) { +int lfs_format(lfs_t *lfs, const struct lfs_config *config) { return lfs_format_raw(lfs, config); } -int lfs_mount (lfs_t * lfs, const struct lfs_config * config) { +int lfs_mount(lfs_t *lfs, const struct lfs_config *config) { return lfs_mount_raw(lfs, config); } -int lfs_unmount (lfs_t * lfs) { +int lfs_unmount(lfs_t *lfs) { return lfs_unmount_raw(lfs); } -int lfs_remove (lfs_t * lfs, const char * path) { +int lfs_remove(lfs_t *lfs, const char *path) { return lfs_remove_raw(lfs, path); } -int lfs_rename (lfs_t * lfs, const char * oldpath, const char * newpath) { +int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { return lfs_rename_raw(lfs, oldpath, newpath); } -int lfs_stat (lfs_t * lfs, const char * path, struct lfs_info * info) { +int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { return lfs_stat_raw(lfs, path, info); } -lfs_ssize_t lfs_getattr (lfs_t * lfs, const char * path, uint8_t type, void * buffer, lfs_size_t size) { +lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { return lfs_getattr_raw(lfs, path, type, buffer, size); } -int lfs_setattr (lfs_t * lfs, const char * path, uint8_t type, const void * buffer, lfs_size_t size) { +int lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { return lfs_setattr_raw(lfs, path, type, buffer, size); } -int lfs_removeattr (lfs_t * lfs, const char * path, uint8_t type) { +int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { return lfs_removeattr_raw(lfs, path, type); } -int lfs_file_open (lfs_t * lfs, lfs_file_t * file, const char * path, int flags) { +int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { return lfs_file_open_raw(lfs, file, path, flags); } -int lfs_file_opencfg_ts(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config) { +int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config) { return lfs_file_opencfg_raw(lfs, file, path, flags, config); } -int lfs_file_close (lfs_t * lfs, lfs_file_t * file) { +int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { return lfs_file_close_raw(lfs, file); } -int lfs_file_sync (lfs_t * lfs, lfs_file_t * file) { +int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { return lfs_file_sync_raw(lfs, file); } -lfs_ssize_t lfs_file_read (lfs_t * lfs, lfs_file_t * file, void * buffer, lfs_size_t size) { +lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { return lfs_file_read_raw(lfs, file, buffer, size); } -lfs_ssize_t lfs_file_write (lfs_t * lfs, lfs_file_t * file, const void * buffer, lfs_size_t size) { +lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { return lfs_file_write_raw(lfs, file, buffer, size); } -lfs_soff_t lfs_file_seek (lfs_t * lfs, lfs_file_t * file, lfs_soff_t off, int whence) { +lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { return lfs_file_seek_raw(lfs, file, off, whence); } -int lfs_file_truncate (lfs_t * lfs, lfs_file_t * file, lfs_off_t size) { +int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { return lfs_file_truncate_raw(lfs, file, size); } -lfs_soff_t lfs_file_tell (lfs_t * lfs, lfs_file_t * file) { +lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { return lfs_file_tell_raw(lfs, file); } -int lfs_file_rewind (lfs_t * lfs, lfs_file_t * file) { +int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { return lfs_file_rewind_raw(lfs, file); } -lfs_soff_t lfs_file_size (lfs_t * lfs, lfs_file_t * file) { +lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { return lfs_file_size_raw(lfs, file); } -int lfs_mkdir (lfs_t * lfs, const char * path) { +int lfs_mkdir(lfs_t *lfs, const char *path) { return lfs_mkdir_raw(lfs, path); } -int lfs_dir_open (lfs_t * lfs, lfs_dir_t * dir, const char * path) { +int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { return lfs_dir_open_raw(lfs, dir, path); } -int lfs_dir_close (lfs_t * lfs, lfs_dir_t * dir) { +int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { return lfs_dir_close_raw(lfs, dir); } -int lfs_dir_read (lfs_t * lfs, lfs_dir_t * dir, struct lfs_info * info) { +int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { return lfs_dir_read_raw(lfs, dir, info); } -int lfs_dir_seek (lfs_t * lfs, lfs_dir_t * dir, lfs_off_t off) { +int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { return lfs_dir_seek_raw(lfs, dir, off); } -lfs_soff_t lfs_dir_tell (lfs_t * lfs, lfs_dir_t * dir) { +lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { return lfs_dir_tell_raw(lfs, dir); } -int lfs_dir_rewind (lfs_t * lfs, lfs_dir_t * dir) { +int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { return lfs_dir_rewind_raw(lfs, dir); } -lfs_ssize_t lfs_fs_size (lfs_t * lfs) { +lfs_ssize_t lfs_fs_size(lfs_t *lfs) { return lfs_fs_size_raw(lfs); } -int lfs_fs_traverse (lfs_t * lfs, int (* cb)(void *, lfs_block_t), void * data) { +int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) { return lfs_fs_traverse_raw(lfs, cb, data); } #ifdef LFS_MIGRATE -int lfs_migrate (lfs_t * lfs, const struct lfs_config * cfg) { +int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { return lfs_migrate_raw(lfs, cfg); } diff --git a/lfs.h b/lfs.h index 19f9f678..4f7eea58 100644 --- a/lfs.h +++ b/lfs.h @@ -657,37 +657,6 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); #endif -int lfs_format_raw(lfs_t *lfs, const struct lfs_config *config); -int lfs_mount_raw(lfs_t *lfs, const struct lfs_config *config); -int lfs_unmount_raw(lfs_t *lfs); -int lfs_remove_raw(lfs_t *lfs, const char *path); -int lfs_rename_raw(lfs_t *lfs, const char *oldpath, const char *newpath); -int lfs_stat_raw(lfs_t *lfs, const char *path, struct lfs_info *info); -lfs_ssize_t lfs_getattr_raw(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size); -int lfs_setattr_raw(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size); -int lfs_removeattr_raw(lfs_t *lfs, const char *path, uint8_t type); -int lfs_file_open_raw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags); -int lfs_file_opencfg_raw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config); -int lfs_file_close_raw(lfs_t *lfs, lfs_file_t *file); -int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file); -lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); -lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); -lfs_soff_t lfs_file_seek_raw(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence); -int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size); -lfs_soff_t lfs_file_tell_raw(lfs_t *lfs, lfs_file_t *file); -int lfs_file_rewind_raw(lfs_t *lfs, lfs_file_t *file); -lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file); -int lfs_mkdir_raw(lfs_t *lfs, const char *path); -int lfs_dir_open_raw(lfs_t *lfs, lfs_dir_t *dir, const char *path); -int lfs_dir_close_raw(lfs_t *lfs, lfs_dir_t *dir); -int lfs_dir_read_raw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info); -int lfs_dir_seek_raw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off); -lfs_soff_t lfs_dir_tell_raw(lfs_t *lfs, lfs_dir_t *dir); -int lfs_dir_rewind_raw(lfs_t *lfs, lfs_dir_t *dir); -lfs_ssize_t lfs_fs_size_raw(lfs_t *lfs); -int lfs_fs_traverse_raw(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); -int lfs_migrate_raw(lfs_t *lfs, const struct lfs_config *cfg); - #ifdef __cplusplus } /* extern "C" */ #endif From 00a9ba7826318408d280aafe5dc527a43b2c965d Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sat, 28 Nov 2020 11:15:23 -0600 Subject: [PATCH 20/26] Tweaked thread-safe implementation - Stayed on non-system include for lfs_util.h for now - Named internal functions "lfs_functionraw" - Merged lfs_fs_traverseraw - Added LFS_LOCK/UNLOCK macros - Changed LFS_THREADSAFE from 1/0 to defined/undefined to match LFS_READONLY --- lfs.c | 495 ++++++++++++++++++++--------------------------------- lfs.h | 4 +- lfs_util.h | 5 - 3 files changed, 183 insertions(+), 321 deletions(-) diff --git a/lfs.c b/lfs.c index b472ba5b..edcbb6ac 100644 --- a/lfs.c +++ b/lfs.c @@ -5,20 +5,11 @@ * SPDX-License-Identifier: BSD-3-Clause */ #include "lfs.h" -#include +#include "lfs_util.h" #define LFS_BLOCK_NULL ((lfs_block_t)-1) #define LFS_BLOCK_INLINE ((lfs_block_t)-2) -static int lfs_dir_rewind_raw(lfs_t *lfs, lfs_dir_t *dir); -static int lfs_file_close_raw(lfs_t *lfs, lfs_file_t *file); -static lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); -static lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file); -static int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file); -static lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); -static lfs_ssize_t lfs_fs_size_raw(lfs_t *lfs); -static int lfs_unmount_raw(lfs_t *lfs); - /// Caching block device operations /// static inline void lfs_cache_drop(lfs_t *lfs, lfs_cache_t *rcache) { // do not zero, cheaper if cache is readonly or only going to be @@ -427,8 +418,19 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, static int lfs_dir_compact(lfs_t *lfs, lfs_mdir_t *dir, const struct lfs_mattr *attrs, int attrcount, lfs_mdir_t *source, uint16_t begin, uint16_t end); +static int lfs_dir_rewindraw(lfs_t *lfs, lfs_dir_t *dir); + +static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, + void *buffer, lfs_size_t size); +static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, + const void *buffer, lfs_size_t size); +static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file); +static lfs_soff_t lfs_file_sizeraw(lfs_t *lfs, lfs_file_t *file); +static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file); static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file); static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file); + +static lfs_ssize_t lfs_fs_sizeraw(lfs_t *lfs); static void lfs_fs_preporphans(lfs_t *lfs, int8_t orphans); static void lfs_fs_prepmove(lfs_t *lfs, uint16_t id, const lfs_block_t pair[2]); @@ -442,12 +444,16 @@ int lfs_fs_traverseraw(lfs_t *lfs, int (*cb)(void *data, lfs_block_t block), void *data, bool includeorphans); static int lfs_fs_forceconsistency(lfs_t *lfs); + static int lfs_deinit(lfs_t *lfs); +static int lfs_unmountraw(lfs_t *lfs); + #ifdef LFS_MIGRATE static int lfs1_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); #endif + /// Block allocator /// static int lfs_alloc_lookahead(void *p, lfs_block_t block) { lfs_t *lfs = (lfs_t*)p; @@ -1534,7 +1540,7 @@ static int lfs_dir_compact(lfs_t *lfs, if (lfs_pair_cmp(dir->pair, (const lfs_block_t[2]){0, 1}) == 0) { // oh no! we're writing too much to the superblock, // should we expand? - lfs_ssize_t res = lfs_fs_size_raw(lfs); + lfs_ssize_t res = lfs_fs_sizeraw(lfs); if (res < 0) { return res; } @@ -1915,7 +1921,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, /// Top level directory operations /// -static int lfs_mkdir_raw(lfs_t *lfs, const char *path) { +static int lfs_mkdirraw(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -2014,7 +2020,7 @@ static int lfs_mkdir_raw(lfs_t *lfs, const char *path) { return 0; } -static int lfs_dir_open_raw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { +static int lfs_dir_openraw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL); if (tag < 0) { @@ -2065,7 +2071,7 @@ static int lfs_dir_open_raw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { return 0; } -static int lfs_dir_close_raw(lfs_t *lfs, lfs_dir_t *dir) { +static int lfs_dir_closeraw(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); // remove from list of mdirs for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { @@ -2079,7 +2085,7 @@ static int lfs_dir_close_raw(lfs_t *lfs, lfs_dir_t *dir) { return 0; } -static int lfs_dir_read_raw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { +static int lfs_dir_readraw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { LFS_TRACE("lfs_dir_read(%p, %p, %p)", (void*)lfs, (void*)dir, (void*)info); memset(info, 0, sizeof(*info)); @@ -2132,11 +2138,11 @@ static int lfs_dir_read_raw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { return true; } -static int lfs_dir_seek_raw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { +static int lfs_dir_seekraw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { LFS_TRACE("lfs_dir_seek(%p, %p, %"PRIu32")", (void*)lfs, (void*)dir, off); // simply walk from head dir - int err = lfs_dir_rewind_raw(lfs, dir); + int err = lfs_dir_rewindraw(lfs, dir); if (err) { LFS_TRACE("lfs_dir_seek -> %d", err); return err; @@ -2175,14 +2181,14 @@ static int lfs_dir_seek_raw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { return 0; } -static lfs_soff_t lfs_dir_tell_raw(lfs_t *lfs, lfs_dir_t *dir) { +static lfs_soff_t lfs_dir_tellraw(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_tell(%p, %p)", (void*)lfs, (void*)dir); (void)lfs; LFS_TRACE("lfs_dir_tell -> %"PRId32, dir->pos); return dir->pos; } -static int lfs_dir_rewind_raw(lfs_t *lfs, lfs_dir_t *dir) { +static int lfs_dir_rewindraw(lfs_t *lfs, lfs_dir_t *dir) { LFS_TRACE("lfs_dir_rewind(%p, %p)", (void*)lfs, (void*)dir); // reload the head dir int err = lfs_dir_fetch(lfs, &dir->m, dir->head); @@ -2389,7 +2395,7 @@ static int lfs_ctz_traverse(lfs_t *lfs, /// Top level file operations /// -static int lfs_file_opencfg_raw(lfs_t *lfs, lfs_file_t *file, +static int lfs_file_opencfgraw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *cfg) { LFS_TRACE("lfs_file_opencfg(%p, %p, \"%s\", %x, %p {" @@ -2538,26 +2544,26 @@ static int lfs_file_opencfg_raw(lfs_t *lfs, lfs_file_t *file, cleanup: // clean up lingering resources file->flags |= LFS_F_ERRED; - lfs_file_close_raw(lfs, file); + lfs_file_closeraw(lfs, file); LFS_TRACE("lfs_file_opencfg -> %d", err); return err; } -static int lfs_file_open_raw(lfs_t *lfs, lfs_file_t *file, +static int lfs_file_openraw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { LFS_TRACE("lfs_file_open(%p, %p, \"%s\", %x)", (void*)lfs, (void*)file, path, flags); static const struct lfs_file_config defaults = {0}; - int err = lfs_file_opencfg_raw(lfs, file, path, flags, &defaults); + int err = lfs_file_opencfgraw(lfs, file, path, flags, &defaults); LFS_TRACE("lfs_file_open -> %d", err); return err; } -static int lfs_file_close_raw(lfs_t *lfs, lfs_file_t *file) { +static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); - int err = lfs_file_sync_raw(lfs, file); + int err = lfs_file_syncraw(lfs, file); // remove from list of mdirs for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { @@ -2688,12 +2694,12 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { // copy over a byte at a time, leave it up to caching // to make this efficient uint8_t data; - lfs_ssize_t res = lfs_file_read_raw(lfs, &orig, &data, 1); + lfs_ssize_t res = lfs_file_readraw(lfs, &orig, &data, 1); if (res < 0) { return res; } - res = lfs_file_write_raw(lfs, file, &data, 1); + res = lfs_file_writeraw(lfs, file, &data, 1); if (res < 0) { return res; } @@ -2740,7 +2746,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { return 0; } -static int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file) { +static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -2797,7 +2803,7 @@ static int lfs_file_sync_raw(lfs_t *lfs, lfs_file_t *file) { return 0; } -static lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, +static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); @@ -2877,7 +2883,7 @@ static lfs_ssize_t lfs_file_read_raw(lfs_t *lfs, lfs_file_t *file, return size; } -static lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, +static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); @@ -2912,7 +2918,7 @@ static lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, file->pos = file->ctz.size; while (file->pos < pos) { - lfs_ssize_t res = lfs_file_write_raw(lfs, file, &(uint8_t){0}, 1); + lfs_ssize_t res = lfs_file_writeraw(lfs, file, &(uint8_t){0}, 1); if (res < 0) { LFS_TRACE("lfs_file_write -> %"PRId32, res); return res; @@ -3008,7 +3014,7 @@ static lfs_ssize_t lfs_file_write_raw(lfs_t *lfs, lfs_file_t *file, return size; } -static lfs_soff_t lfs_file_seek_raw(lfs_t *lfs, lfs_file_t *file, +static lfs_soff_t lfs_file_seekraw(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { LFS_TRACE("lfs_file_seek(%p, %p, %"PRId32", %d)", (void*)lfs, (void*)file, off, whence); @@ -3043,7 +3049,7 @@ static lfs_soff_t lfs_file_seek_raw(lfs_t *lfs, lfs_file_t *file, return npos; } -static int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { +static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")", (void*)lfs, (void*)file, size); LFS_ASSERT(file->flags & LFS_F_OPENED); @@ -3055,7 +3061,7 @@ static int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } lfs_off_t pos = file->pos; - lfs_off_t oldsize = lfs_file_size_raw(lfs, file); + lfs_off_t oldsize = lfs_file_sizeraw(lfs, file); if (size < oldsize) { // need to flush since directly changing metadata int err = lfs_file_flush(lfs, file); @@ -3079,7 +3085,7 @@ static int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } else if (size > oldsize) { // flush+seek if not already at end if (file->pos != oldsize) { - lfs_soff_t res = lfs_file_seek_raw(lfs, file, 0, LFS_SEEK_END); + lfs_soff_t res = lfs_file_seekraw(lfs, file, 0, LFS_SEEK_END); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; @@ -3088,7 +3094,7 @@ static int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { // fill with zeros while (file->pos < size) { - lfs_ssize_t res = lfs_file_write_raw(lfs, file, &(uint8_t){0}, 1); + lfs_ssize_t res = lfs_file_writeraw(lfs, file, &(uint8_t){0}, 1); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; @@ -3097,7 +3103,7 @@ static int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } // restore pos - lfs_soff_t res = lfs_file_seek_raw(lfs, file, pos, LFS_SEEK_SET); + lfs_soff_t res = lfs_file_seekraw(lfs, file, pos, LFS_SEEK_SET); if (res < 0) { LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; @@ -3107,7 +3113,7 @@ static int lfs_file_truncate_raw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { return 0; } -static lfs_soff_t lfs_file_tell_raw(lfs_t *lfs, lfs_file_t *file) { +static lfs_soff_t lfs_file_tellraw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; @@ -3115,9 +3121,9 @@ static lfs_soff_t lfs_file_tell_raw(lfs_t *lfs, lfs_file_t *file) { return file->pos; } -static int lfs_file_rewind_raw(lfs_t *lfs, lfs_file_t *file) { +static int lfs_file_rewindraw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file); - lfs_soff_t res = lfs_file_seek_raw(lfs, file, 0, LFS_SEEK_SET); + lfs_soff_t res = lfs_file_seekraw(lfs, file, 0, LFS_SEEK_SET); if (res < 0) { LFS_TRACE("lfs_file_rewind -> %"PRId32, res); return (int)res; @@ -3127,7 +3133,7 @@ static int lfs_file_rewind_raw(lfs_t *lfs, lfs_file_t *file) { return 0; } -static lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file) { +static lfs_soff_t lfs_file_sizeraw(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; @@ -3143,7 +3149,7 @@ static lfs_soff_t lfs_file_size_raw(lfs_t *lfs, lfs_file_t *file) { /// General fs operations /// -static int lfs_stat_raw(lfs_t *lfs, const char *path, struct lfs_info *info) { +static int lfs_statraw(lfs_t *lfs, const char *path, struct lfs_info *info) { LFS_TRACE("lfs_stat(%p, \"%s\", %p)", (void*)lfs, path, (void*)info); lfs_mdir_t cwd; lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); @@ -3157,7 +3163,7 @@ static int lfs_stat_raw(lfs_t *lfs, const char *path, struct lfs_info *info) { return err; } -static int lfs_remove_raw(lfs_t *lfs, const char *path) { +static int lfs_removeraw(lfs_t *lfs, const char *path) { LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); @@ -3238,7 +3244,7 @@ static int lfs_remove_raw(lfs_t *lfs, const char *path) { return 0; } -static int lfs_rename_raw(lfs_t *lfs, const char *oldpath, const char *newpath) { +static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); // deorphan if we haven't yet, needed at most once after poweron @@ -3383,7 +3389,7 @@ static int lfs_rename_raw(lfs_t *lfs, const char *oldpath, const char *newpath) return 0; } -static lfs_ssize_t lfs_getattr_raw(lfs_t *lfs, const char *path, +static lfs_ssize_t lfs_getattrraw(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { LFS_TRACE("lfs_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); @@ -3446,7 +3452,7 @@ static int lfs_commitattr(lfs_t *lfs, const char *path, {LFS_MKTAG(LFS_TYPE_USERATTR + type, id, size), buffer})); } -static int lfs_setattr_raw(lfs_t *lfs, const char *path, +static int lfs_setattrraw(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); @@ -3460,7 +3466,7 @@ static int lfs_setattr_raw(lfs_t *lfs, const char *path, return err; } -static int lfs_removeattr_raw(lfs_t *lfs, const char *path, uint8_t type) { +static int lfs_removeattrraw(lfs_t *lfs, const char *path, uint8_t type) { LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); int err = lfs_commitattr(lfs, path, type, NULL, 0x3ff); LFS_TRACE("lfs_removeattr -> %d", err); @@ -3593,7 +3599,7 @@ static int lfs_deinit(lfs_t *lfs) { return 0; } -static int lfs_format_raw(lfs_t *lfs, const struct lfs_config *cfg) { +static int lfs_formatraw(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_format(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -3674,7 +3680,7 @@ static int lfs_format_raw(lfs_t *lfs, const struct lfs_config *cfg) { return err; } -static int lfs_mount_raw(lfs_t *lfs, const struct lfs_config *cfg) { +static int lfs_mountraw(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_mount(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -3813,12 +3819,12 @@ static int lfs_mount_raw(lfs_t *lfs, const struct lfs_config *cfg) { return 0; cleanup: - lfs_unmount_raw(lfs); + lfs_unmountraw(lfs); LFS_TRACE("lfs_mount -> %d", err); return err; } -static int lfs_unmount_raw(lfs_t *lfs) { +static int lfs_unmountraw(lfs_t *lfs) { LFS_TRACE("lfs_unmount(%p)", (void*)lfs); int err = lfs_deinit(lfs); LFS_TRACE("lfs_unmount -> %d", err); @@ -3923,15 +3929,6 @@ int lfs_fs_traverseraw(lfs_t *lfs, return 0; } -static int lfs_fs_traverse_raw(lfs_t *lfs, - int (*cb)(void *data, lfs_block_t block), void *data) { - LFS_TRACE("lfs_fs_traverse(%p, %p, %p)", - (void*)lfs, (void*)(uintptr_t)cb, data); - int err = lfs_fs_traverseraw(lfs, cb, data, true); - LFS_TRACE("lfs_fs_traverse -> %d", 0); - return err; -} - static int lfs_fs_pred(lfs_t *lfs, const lfs_block_t pair[2], lfs_mdir_t *pdir) { // iterate over all directory directory entries @@ -4244,7 +4241,7 @@ static int lfs_fs_size_count(void *p, lfs_block_t block) { return 0; } -static lfs_ssize_t lfs_fs_size_raw(lfs_t *lfs) { +static lfs_ssize_t lfs_fs_sizeraw(lfs_t *lfs) { LFS_TRACE("lfs_fs_size(%p)", (void*)lfs); lfs_size_t size = 0; int err = lfs_fs_traverseraw(lfs, lfs_fs_size_count, &size, false); @@ -4678,7 +4675,7 @@ static int lfs1_unmount(lfs_t *lfs) { } /// v1 migration /// -static int lfs_migrate_raw(lfs_t *lfs, const struct lfs_config *cfg) { +static int lfs_migrateraw(lfs_t *lfs, const struct lfs_config *cfg) { LFS_TRACE("lfs_migrate(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -4921,520 +4918,390 @@ static int lfs_migrate_raw(lfs_t *lfs, const struct lfs_config *cfg) { #endif -#if LFS_THREADSAFE -int lfs_format(lfs_t *lfs, const struct lfs_config *config) { - int err = config->lock(config); +/// Public API wrappers /// + +// Here we can add tracing/thread safety easily + +// Thread-safe wrappers if enabled +#ifdef LFS_THREADSAFE +#define LFS_LOCK(cfg) cfg->lock(cfg) +#define LFS_UNLOCK(cfg) cfg->unlock(cfg) +#else +#define LFS_LOCK(cfg) ((void)cfg, 0) +#define LFS_UNLOCK(cfg) ((void)cfg) +#endif + +// Public API +int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { + int err = LFS_LOCK(cfg); if (err) { return err; } - err = lfs_format_raw(lfs, config); - config->unlock(config); + err = lfs_formatraw(lfs, cfg); + LFS_UNLOCK(cfg); return err; } -int lfs_mount(lfs_t *lfs, const struct lfs_config *config) { - int err = config->lock(config); +int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { + int err = LFS_LOCK(cfg); if (err) { return err; } - err = lfs_mount_raw(lfs, config); - config->unlock(config); + err = lfs_mountraw(lfs, cfg); + LFS_UNLOCK(cfg); return err; } int lfs_unmount(lfs_t *lfs) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_unmount_raw(lfs); - lfs->cfg->unlock(lfs->cfg); + err = lfs_unmountraw(lfs); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_remove(lfs_t *lfs, const char *path) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_remove_raw(lfs, path); - lfs->cfg->unlock(lfs->cfg); + err = lfs_removeraw(lfs, path); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_rename_raw(lfs, oldpath, newpath); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_renameraw(lfs, oldpath, newpath); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_stat_raw(lfs, path, info); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_statraw(lfs, path, info); + LFS_UNLOCK(lfs->cfg); return err; } -lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { - int err = lfs->cfg->lock(lfs->cfg); +lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, + uint8_t type, void *buffer, lfs_size_t size) { + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_getattr_raw(lfs, path, type, buffer, size); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_getattrraw(lfs, path, type, buffer, size); + LFS_UNLOCK(lfs->cfg); return err; } -int lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { - int err = lfs->cfg->lock(lfs->cfg); +int lfs_setattr(lfs_t *lfs, const char *path, + uint8_t type, const void *buffer, lfs_size_t size) { + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_setattr_raw(lfs, path, type, buffer, size); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_setattrraw(lfs, path, type, buffer, size); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_removeattr_raw(lfs, path, type); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_removeattrraw(lfs, path, type); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_open_raw(lfs, file, path, flags); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_openraw(lfs, file, path, flags); + LFS_UNLOCK(lfs->cfg); return err; } -int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config) { - int err = lfs->cfg->lock(lfs->cfg); +int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, + const char *path, int flags, + const struct lfs_file_config *config) { + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_opencfg_raw(lfs, file, path, flags, config); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_opencfgraw(lfs, file, path, flags, config); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_close_raw(lfs, file); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_closeraw(lfs, file); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_sync_raw(lfs, file); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_syncraw(lfs, file); + LFS_UNLOCK(lfs->cfg); return err; } -lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { - int err = lfs->cfg->lock(lfs->cfg); +lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, + void *buffer, lfs_size_t size) { + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_read_raw(lfs, file, buffer, size); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_readraw(lfs, file, buffer, size); + LFS_UNLOCK(lfs->cfg); return err; } -lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { - int err = lfs->cfg->lock(lfs->cfg); +lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, + const void *buffer, lfs_size_t size) { + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_write_raw(lfs, file, buffer, size); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_writeraw(lfs, file, buffer, size); + LFS_UNLOCK(lfs->cfg); return err; } -lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { - int err = lfs->cfg->lock(lfs->cfg); +lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, + lfs_soff_t off, int whence) { + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_seek_raw(lfs, file, off, whence); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_seekraw(lfs, file, off, whence); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_truncate_raw(lfs, file, size); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_truncateraw(lfs, file, size); + LFS_UNLOCK(lfs->cfg); return err; } lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_tell_raw(lfs, file); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_tellraw(lfs, file); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_rewind_raw(lfs, file); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_rewindraw(lfs, file); + LFS_UNLOCK(lfs->cfg); return err; } lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_size_raw(lfs, file); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_file_sizeraw(lfs, file); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_mkdir(lfs_t *lfs, const char *path) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_mkdir_raw(lfs, path); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_mkdirraw(lfs, path); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_dir_open_raw(lfs, dir, path); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_dir_openraw(lfs, dir, path); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_dir_close_raw(lfs, dir); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_dir_closeraw(lfs, dir); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_dir_read_raw(lfs, dir, info); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_dir_readraw(lfs, dir, info); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_dir_seek_raw(lfs, dir, off); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_dir_seekraw(lfs, dir, off); + LFS_UNLOCK(lfs->cfg); return err; } lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_dir_tell_raw(lfs, dir); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_dir_tellraw(lfs, dir); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_dir_rewind_raw(lfs, dir); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_dir_rewindraw(lfs, dir); + LFS_UNLOCK(lfs->cfg); return err; } lfs_ssize_t lfs_fs_size(lfs_t *lfs) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_fs_size_raw(lfs); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_fs_sizeraw(lfs); + LFS_UNLOCK(lfs->cfg); return err; } int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_fs_traverse_raw(lfs, cb, data); - - lfs->cfg->unlock(lfs->cfg); + LFS_TRACE("lfs_fs_traverse(%p, %p, %p)", + (void*)lfs, (void*)(uintptr_t)cb, data); + err = lfs_fs_traverseraw(lfs, cb, data, true); + LFS_TRACE("lfs_fs_traverse -> %d", err); + LFS_UNLOCK(lfs->cfg); return err; } #ifdef LFS_MIGRATE - int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { - int err = lfs->cfg->lock(lfs->cfg); + int err = LFS_LOCK(cfg); if (err) { return err; } - err = lfs_migrate_raw(lfs, cfg); - - lfs->cfg->unlock(lfs->cfg); + err = lfs_migrateraw(lfs, cfg); + LFS_UNLOCK(cfg); return err; } - #endif -#else -int lfs_format(lfs_t *lfs, const struct lfs_config *config) { - return lfs_format_raw(lfs, config); -} - -int lfs_mount(lfs_t *lfs, const struct lfs_config *config) { - return lfs_mount_raw(lfs, config); -} - -int lfs_unmount(lfs_t *lfs) { - return lfs_unmount_raw(lfs); -} - -int lfs_remove(lfs_t *lfs, const char *path) { - return lfs_remove_raw(lfs, path); -} - -int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { - return lfs_rename_raw(lfs, oldpath, newpath); -} - -int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { - return lfs_stat_raw(lfs, path, info); -} - -lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { - return lfs_getattr_raw(lfs, path, type, buffer, size); -} - -int lfs_setattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { - return lfs_setattr_raw(lfs, path, type, buffer, size); -} - -int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { - return lfs_removeattr_raw(lfs, path, type); -} - -int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { - return lfs_file_open_raw(lfs, file, path, flags); -} - -int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *config) { - return lfs_file_opencfg_raw(lfs, file, path, flags, config); -} - -int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { - return lfs_file_close_raw(lfs, file); -} - -int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { - return lfs_file_sync_raw(lfs, file); -} - -lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { - return lfs_file_read_raw(lfs, file, buffer, size); -} - -lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { - return lfs_file_write_raw(lfs, file, buffer, size); -} - -lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { - return lfs_file_seek_raw(lfs, file, off, whence); -} - -int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { - return lfs_file_truncate_raw(lfs, file, size); -} - -lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { - return lfs_file_tell_raw(lfs, file); -} - -int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { - return lfs_file_rewind_raw(lfs, file); -} - -lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { - return lfs_file_size_raw(lfs, file); -} - -int lfs_mkdir(lfs_t *lfs, const char *path) { - return lfs_mkdir_raw(lfs, path); -} - -int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { - return lfs_dir_open_raw(lfs, dir, path); -} - -int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { - return lfs_dir_close_raw(lfs, dir); -} - -int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { - return lfs_dir_read_raw(lfs, dir, info); -} - -int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { - return lfs_dir_seek_raw(lfs, dir, off); -} - -lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { - return lfs_dir_tell_raw(lfs, dir); -} - -int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { - return lfs_dir_rewind_raw(lfs, dir); -} - -lfs_ssize_t lfs_fs_size(lfs_t *lfs) { - return lfs_fs_size_raw(lfs); -} - -int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) { - return lfs_fs_traverse_raw(lfs, cb, data); -} - -#ifdef LFS_MIGRATE - -int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { - return lfs_migrate_raw(lfs, cfg); -} - -#endif -#endif \ No newline at end of file diff --git a/lfs.h b/lfs.h index 4f7eea58..3fec4df6 100644 --- a/lfs.h +++ b/lfs.h @@ -9,7 +9,7 @@ #include #include -#include +#include "lfs_util.h" #ifdef __cplusplus extern "C" @@ -174,7 +174,7 @@ struct lfs_config { // are propogated to the user. int (*sync)(const struct lfs_config *c); -#if LFS_THREADSAFE +#ifdef LFS_THREADSAFE // Lock the underlying block device. Negative error codes // are propogated to the user. int (*lock)(const struct lfs_config *c); diff --git a/lfs_util.h b/lfs_util.h index 84bab118..d3baffdb 100644 --- a/lfs_util.h +++ b/lfs_util.h @@ -43,11 +43,6 @@ extern "C" { #endif -// Enables thread-safe wrappers using the lock/unlock callbacks in lfs_config -#ifndef LFS_THREADSAFE -#define LFS_THREADSAFE 0 -#endif - // Macros, may be replaced by system specific wrappers. Arguments to these // macros must not have side-effects as the macros can be removed for a smaller // code footprint From 45afded784dfb6ad8cc92adbf9e3570686d7ecf5 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sat, 28 Nov 2020 12:37:01 -0600 Subject: [PATCH 21/26] Moved LFS_TRACE calls to API wrapper functions This removes quite a bit of extra code needed to entertwine the LFS_TRACE calls into the original funcions. Also changed temporary return type to match API declaration where necessary. --- lfs.c | 367 ++++++++++++++++++++--------------------------------- lfs.h | 2 + lfs_util.h | 1 + 3 files changed, 141 insertions(+), 229 deletions(-) diff --git a/lfs.c b/lfs.c index edcbb6ac..b2e6c208 100644 --- a/lfs.c +++ b/lfs.c @@ -1922,11 +1922,9 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, /// Top level directory operations /// static int lfs_mkdirraw(lfs_t *lfs, const char *path) { - LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { - LFS_TRACE("lfs_mkdir -> %d", err); return err; } @@ -1935,14 +1933,12 @@ static int lfs_mkdirraw(lfs_t *lfs, const char *path) { uint16_t id; err = lfs_dir_find(lfs, &cwd.m, &path, &id); if (!(err == LFS_ERR_NOENT && id != 0x3ff)) { - LFS_TRACE("lfs_mkdir -> %d", (err < 0) ? err : LFS_ERR_EXIST); return (err < 0) ? err : LFS_ERR_EXIST; } // check that name fits lfs_size_t nlen = strlen(path); if (nlen > lfs->name_max) { - LFS_TRACE("lfs_mkdir -> %d", LFS_ERR_NAMETOOLONG); return LFS_ERR_NAMETOOLONG; } @@ -1951,7 +1947,6 @@ static int lfs_mkdirraw(lfs_t *lfs, const char *path) { lfs_mdir_t dir; err = lfs_dir_alloc(lfs, &dir); if (err) { - LFS_TRACE("lfs_mkdir -> %d", err); return err; } @@ -1960,7 +1955,6 @@ static int lfs_mkdirraw(lfs_t *lfs, const char *path) { while (pred.split) { err = lfs_dir_fetch(lfs, &pred, pred.tail); if (err) { - LFS_TRACE("lfs_mkdir -> %d", err); return err; } } @@ -1971,7 +1965,6 @@ static int lfs_mkdirraw(lfs_t *lfs, const char *path) { {LFS_MKTAG(LFS_TYPE_SOFTTAIL, 0x3ff, 8), pred.tail})); lfs_pair_fromle32(pred.tail); if (err) { - LFS_TRACE("lfs_mkdir -> %d", err); return err; } @@ -1994,7 +1987,6 @@ static int lfs_mkdirraw(lfs_t *lfs, const char *path) { lfs_pair_fromle32(dir.pair); if (err) { lfs->mlist = cwd.next; - LFS_TRACE("lfs_mkdir -> %d", err); return err; } @@ -2012,24 +2004,19 @@ static int lfs_mkdirraw(lfs_t *lfs, const char *path) { LFS_TYPE_SOFTTAIL, 0x3ff, 8), dir.pair})); lfs_pair_fromle32(dir.pair); if (err) { - LFS_TRACE("lfs_mkdir -> %d", err); return err; } - LFS_TRACE("lfs_mkdir -> %d", 0); return 0; } static int lfs_dir_openraw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { - LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL); if (tag < 0) { - LFS_TRACE("lfs_dir_open -> %"PRId32, tag); return tag; } if (lfs_tag_type3(tag) != LFS_TYPE_DIR) { - LFS_TRACE("lfs_dir_open -> %d", LFS_ERR_NOTDIR); return LFS_ERR_NOTDIR; } @@ -2043,7 +2030,6 @@ static int lfs_dir_openraw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { lfs_stag_t res = lfs_dir_get(lfs, &dir->m, LFS_MKTAG(0x700, 0x3ff, 0), LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tag_id(tag), 8), pair); if (res < 0) { - LFS_TRACE("lfs_dir_open -> %"PRId32, res); return res; } lfs_pair_fromle32(pair); @@ -2052,7 +2038,6 @@ static int lfs_dir_openraw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { // fetch first pair int err = lfs_dir_fetch(lfs, &dir->m, pair); if (err) { - LFS_TRACE("lfs_dir_open -> %d", err); return err; } @@ -2067,12 +2052,10 @@ static int lfs_dir_openraw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { dir->next = (lfs_dir_t*)lfs->mlist; lfs->mlist = (struct lfs_mlist*)dir; - LFS_TRACE("lfs_dir_open -> %d", 0); return 0; } static int lfs_dir_closeraw(lfs_t *lfs, lfs_dir_t *dir) { - LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); // remove from list of mdirs for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) { if (*p == (struct lfs_mlist*)dir) { @@ -2081,13 +2064,10 @@ static int lfs_dir_closeraw(lfs_t *lfs, lfs_dir_t *dir) { } } - LFS_TRACE("lfs_dir_close -> %d", 0); return 0; } static int lfs_dir_readraw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { - LFS_TRACE("lfs_dir_read(%p, %p, %p)", - (void*)lfs, (void*)dir, (void*)info); memset(info, 0, sizeof(*info)); // special offset for '.' and '..' @@ -2095,26 +2075,22 @@ static int lfs_dir_readraw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { info->type = LFS_TYPE_DIR; strcpy(info->name, "."); dir->pos += 1; - LFS_TRACE("lfs_dir_read -> %d", true); return true; } else if (dir->pos == 1) { info->type = LFS_TYPE_DIR; strcpy(info->name, ".."); dir->pos += 1; - LFS_TRACE("lfs_dir_read -> %d", true); return true; } while (true) { if (dir->id == dir->m.count) { if (!dir->m.split) { - LFS_TRACE("lfs_dir_read -> %d", false); return false; } int err = lfs_dir_fetch(lfs, &dir->m, dir->m.tail); if (err) { - LFS_TRACE("lfs_dir_read -> %d", err); return err; } @@ -2123,7 +2099,6 @@ static int lfs_dir_readraw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { int err = lfs_dir_getinfo(lfs, &dir->m, dir->id, info); if (err && err != LFS_ERR_NOENT) { - LFS_TRACE("lfs_dir_read -> %d", err); return err; } @@ -2134,17 +2109,13 @@ static int lfs_dir_readraw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { } dir->pos += 1; - LFS_TRACE("lfs_dir_read -> %d", true); return true; } static int lfs_dir_seekraw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { - LFS_TRACE("lfs_dir_seek(%p, %p, %"PRIu32")", - (void*)lfs, (void*)dir, off); // simply walk from head dir int err = lfs_dir_rewindraw(lfs, dir); if (err) { - LFS_TRACE("lfs_dir_seek -> %d", err); return err; } @@ -2163,13 +2134,11 @@ static int lfs_dir_seekraw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { if (dir->id == dir->m.count) { if (!dir->m.split) { - LFS_TRACE("lfs_dir_seek -> %d", LFS_ERR_INVAL); return LFS_ERR_INVAL; } err = lfs_dir_fetch(lfs, &dir->m, dir->m.tail); if (err) { - LFS_TRACE("lfs_dir_seek -> %d", err); return err; } @@ -2177,29 +2146,23 @@ static int lfs_dir_seekraw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { } } - LFS_TRACE("lfs_dir_seek -> %d", 0); return 0; } static lfs_soff_t lfs_dir_tellraw(lfs_t *lfs, lfs_dir_t *dir) { - LFS_TRACE("lfs_dir_tell(%p, %p)", (void*)lfs, (void*)dir); (void)lfs; - LFS_TRACE("lfs_dir_tell -> %"PRId32, dir->pos); return dir->pos; } static int lfs_dir_rewindraw(lfs_t *lfs, lfs_dir_t *dir) { - LFS_TRACE("lfs_dir_rewind(%p, %p)", (void*)lfs, (void*)dir); // reload the head dir int err = lfs_dir_fetch(lfs, &dir->m, dir->head); if (err) { - LFS_TRACE("lfs_dir_rewind -> %d", err); return err; } dir->id = 0; dir->pos = 0; - LFS_TRACE("lfs_dir_rewind -> %d", 0); return 0; } @@ -2398,16 +2361,10 @@ static int lfs_ctz_traverse(lfs_t *lfs, static int lfs_file_opencfgraw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *cfg) { - LFS_TRACE("lfs_file_opencfg(%p, %p, \"%s\", %x, %p {" - ".buffer=%p, .attrs=%p, .attr_count=%"PRIu32"})", - (void*)lfs, (void*)file, path, flags, - (void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count); - // deorphan if we haven't yet, needed at most once after poweron if ((flags & 3) != LFS_O_RDONLY) { int err = lfs_fs_forceconsistency(lfs); if (err) { - LFS_TRACE("lfs_file_opencfg -> %d", err); return err; } } @@ -2538,29 +2495,23 @@ static int lfs_file_opencfgraw(lfs_t *lfs, lfs_file_t *file, } } - LFS_TRACE("lfs_file_opencfg -> %d", 0); return 0; cleanup: // clean up lingering resources file->flags |= LFS_F_ERRED; lfs_file_closeraw(lfs, file); - LFS_TRACE("lfs_file_opencfg -> %d", err); return err; } static int lfs_file_openraw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { - LFS_TRACE("lfs_file_open(%p, %p, \"%s\", %x)", - (void*)lfs, (void*)file, path, flags); static const struct lfs_file_config defaults = {0}; int err = lfs_file_opencfgraw(lfs, file, path, flags, &defaults); - LFS_TRACE("lfs_file_open -> %d", err); return err; } static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file) { - LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); int err = lfs_file_syncraw(lfs, file); @@ -2579,7 +2530,6 @@ static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file) { } file->flags &= ~LFS_F_OPENED; - LFS_TRACE("lfs_file_close -> %d", err); return err; } @@ -2747,19 +2697,16 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { } static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file) { - LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); if (file->flags & LFS_F_ERRED) { // it's not safe to do anything if our file errored - LFS_TRACE("lfs_file_sync -> %d", 0); return 0; } int err = lfs_file_flush(lfs, file); if (err) { file->flags |= LFS_F_ERRED; - LFS_TRACE("lfs_file_sync -> %d", err); return err; } @@ -2792,21 +2739,17 @@ static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file) { file->cfg->attr_count), file->cfg->attrs})); if (err) { file->flags |= LFS_F_ERRED; - LFS_TRACE("lfs_file_sync -> %d", err); return err; } file->flags &= ~LFS_F_DIRTY; } - LFS_TRACE("lfs_file_sync -> %d", 0); return 0; } static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { - LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")", - (void*)lfs, (void*)file, buffer, size); LFS_ASSERT(file->flags & LFS_F_OPENED); LFS_ASSERT((file->flags & 3) != LFS_O_WRONLY); @@ -2817,14 +2760,12 @@ static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, // flush out any writes int err = lfs_file_flush(lfs, file); if (err) { - LFS_TRACE("lfs_file_read -> %d", err); return err; } } if (file->pos >= file->ctz.size) { // eof if past end - LFS_TRACE("lfs_file_read -> %d", 0); return 0; } @@ -2840,7 +2781,6 @@ static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, file->ctz.head, file->ctz.size, file->pos, &file->block, &file->off); if (err) { - LFS_TRACE("lfs_file_read -> %d", err); return err; } } else { @@ -2860,7 +2800,6 @@ static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, LFS_MKTAG(LFS_TYPE_INLINESTRUCT, file->id, 0), file->off, data, diff); if (err) { - LFS_TRACE("lfs_file_read -> %d", err); return err; } } else { @@ -2868,7 +2807,6 @@ static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, NULL, &file->cache, lfs->cfg->block_size, file->block, file->off, data, diff); if (err) { - LFS_TRACE("lfs_file_read -> %d", err); return err; } } @@ -2879,14 +2817,11 @@ static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, nsize -= diff; } - LFS_TRACE("lfs_file_read -> %"PRId32, size); return size; } static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { - LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")", - (void*)lfs, (void*)file, buffer, size); LFS_ASSERT(file->flags & LFS_F_OPENED); LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY); @@ -2897,7 +2832,6 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, // drop any reads int err = lfs_file_flush(lfs, file); if (err) { - LFS_TRACE("lfs_file_write -> %d", err); return err; } } @@ -2908,7 +2842,6 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, if (file->pos + size > lfs->file_max) { // Larger than file limit? - LFS_TRACE("lfs_file_write -> %d", LFS_ERR_FBIG); return LFS_ERR_FBIG; } @@ -2920,7 +2853,6 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, while (file->pos < pos) { lfs_ssize_t res = lfs_file_writeraw(lfs, file, &(uint8_t){0}, 1); if (res < 0) { - LFS_TRACE("lfs_file_write -> %"PRId32, res); return res; } } @@ -2934,7 +2866,6 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, int err = lfs_file_outline(lfs, file); if (err) { file->flags |= LFS_F_ERRED; - LFS_TRACE("lfs_file_write -> %d", err); return err; } } @@ -2951,7 +2882,6 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, file->pos-1, &file->block, &file->off); if (err) { file->flags |= LFS_F_ERRED; - LFS_TRACE("lfs_file_write -> %d", err); return err; } @@ -2966,7 +2896,6 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, &file->block, &file->off); if (err) { file->flags |= LFS_F_ERRED; - LFS_TRACE("lfs_file_write -> %d", err); return err; } } else { @@ -2987,7 +2916,6 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, goto relocate; } file->flags |= LFS_F_ERRED; - LFS_TRACE("lfs_file_write -> %d", err); return err; } @@ -2996,7 +2924,6 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, err = lfs_file_relocate(lfs, file); if (err) { file->flags |= LFS_F_ERRED; - LFS_TRACE("lfs_file_write -> %d", err); return err; } } @@ -3010,20 +2937,16 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, } file->flags &= ~LFS_F_ERRED; - LFS_TRACE("lfs_file_write -> %"PRId32, size); return size; } static lfs_soff_t lfs_file_seekraw(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { - LFS_TRACE("lfs_file_seek(%p, %p, %"PRId32", %d)", - (void*)lfs, (void*)file, off, 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) { - LFS_TRACE("lfs_file_seek -> %d", err); return err; } @@ -3039,24 +2962,19 @@ static lfs_soff_t lfs_file_seekraw(lfs_t *lfs, lfs_file_t *file, if (npos > lfs->file_max) { // file position out of range - LFS_TRACE("lfs_file_seek -> %d", LFS_ERR_INVAL); return LFS_ERR_INVAL; } // update pos file->pos = npos; - LFS_TRACE("lfs_file_seek -> %"PRId32, npos); return npos; } static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { - LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")", - (void*)lfs, (void*)file, size); LFS_ASSERT(file->flags & LFS_F_OPENED); LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY); if (size > LFS_FILE_MAX) { - LFS_TRACE("lfs_file_truncate -> %d", LFS_ERR_INVAL); return LFS_ERR_INVAL; } @@ -3066,7 +2984,6 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { // need to flush since directly changing metadata int err = lfs_file_flush(lfs, file); if (err) { - LFS_TRACE("lfs_file_truncate -> %d", err); return err; } @@ -3075,7 +2992,6 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { file->ctz.head, file->ctz.size, size, &file->block, &file->off); if (err) { - LFS_TRACE("lfs_file_truncate -> %d", err); return err; } @@ -3087,7 +3003,6 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { if (file->pos != oldsize) { lfs_soff_t res = lfs_file_seekraw(lfs, file, 0, LFS_SEEK_END); if (res < 0) { - LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; } } @@ -3096,7 +3011,6 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { while (file->pos < size) { lfs_ssize_t res = lfs_file_writeraw(lfs, file, &(uint8_t){0}, 1); if (res < 0) { - LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; } } @@ -3105,44 +3019,33 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { // restore pos lfs_soff_t res = lfs_file_seekraw(lfs, file, pos, LFS_SEEK_SET); if (res < 0) { - LFS_TRACE("lfs_file_truncate -> %"PRId32, res); return (int)res; } - LFS_TRACE("lfs_file_truncate -> %d", 0); return 0; } static lfs_soff_t lfs_file_tellraw(lfs_t *lfs, lfs_file_t *file) { - LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; - LFS_TRACE("lfs_file_tell -> %"PRId32, file->pos); return file->pos; } static int lfs_file_rewindraw(lfs_t *lfs, lfs_file_t *file) { - LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file); lfs_soff_t res = lfs_file_seekraw(lfs, file, 0, LFS_SEEK_SET); if (res < 0) { - LFS_TRACE("lfs_file_rewind -> %"PRId32, res); return (int)res; } - LFS_TRACE("lfs_file_rewind -> %d", 0); return 0; } static lfs_soff_t lfs_file_sizeraw(lfs_t *lfs, lfs_file_t *file) { - LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(file->flags & LFS_F_OPENED); (void)lfs; if (file->flags & LFS_F_WRITING) { - LFS_TRACE("lfs_file_size -> %"PRId32, - lfs_max(file->pos, file->ctz.size)); return lfs_max(file->pos, file->ctz.size); } else { - LFS_TRACE("lfs_file_size -> %"PRId32, file->ctz.size); return file->ctz.size; } } @@ -3150,32 +3053,25 @@ static lfs_soff_t lfs_file_sizeraw(lfs_t *lfs, lfs_file_t *file) { /// General fs operations /// static int lfs_statraw(lfs_t *lfs, const char *path, struct lfs_info *info) { - LFS_TRACE("lfs_stat(%p, \"%s\", %p)", (void*)lfs, path, (void*)info); lfs_mdir_t cwd; lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); if (tag < 0) { - LFS_TRACE("lfs_stat -> %"PRId32, tag); return (int)tag; } - int err = lfs_dir_getinfo(lfs, &cwd, lfs_tag_id(tag), info); - LFS_TRACE("lfs_stat -> %d", err); - return err; + return lfs_dir_getinfo(lfs, &cwd, lfs_tag_id(tag), info); } static int lfs_removeraw(lfs_t *lfs, const char *path) { - LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { - LFS_TRACE("lfs_remove -> %d", err); return err; } lfs_mdir_t cwd; lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); if (tag < 0 || lfs_tag_id(tag) == 0x3ff) { - LFS_TRACE("lfs_remove -> %"PRId32, (tag < 0) ? tag : LFS_ERR_INVAL); return (tag < 0) ? (int)tag : LFS_ERR_INVAL; } @@ -3187,19 +3083,16 @@ static int lfs_removeraw(lfs_t *lfs, const char *path) { lfs_stag_t res = lfs_dir_get(lfs, &cwd, LFS_MKTAG(0x700, 0x3ff, 0), LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tag_id(tag), 8), pair); if (res < 0) { - LFS_TRACE("lfs_remove -> %"PRId32, res); return (int)res; } lfs_pair_fromle32(pair); err = lfs_dir_fetch(lfs, &dir.m, pair); if (err) { - LFS_TRACE("lfs_remove -> %d", err); return err; } if (dir.m.count > 0 || dir.m.split) { - LFS_TRACE("lfs_remove -> %d", LFS_ERR_NOTEMPTY); return LFS_ERR_NOTEMPTY; } @@ -3218,7 +3111,6 @@ static int lfs_removeraw(lfs_t *lfs, const char *path) { {LFS_MKTAG(LFS_TYPE_DELETE, lfs_tag_id(tag), 0), NULL})); if (err) { lfs->mlist = dir.next; - LFS_TRACE("lfs_remove -> %d", err); return err; } @@ -3229,28 +3121,22 @@ static int lfs_removeraw(lfs_t *lfs, const char *path) { err = lfs_fs_pred(lfs, dir.m.pair, &cwd); if (err) { - LFS_TRACE("lfs_remove -> %d", err); return err; } err = lfs_dir_drop(lfs, &cwd, &dir.m); if (err) { - LFS_TRACE("lfs_remove -> %d", err); return err; } } - LFS_TRACE("lfs_remove -> %d", 0); return 0; } static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { - LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); - // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { - LFS_TRACE("lfs_rename -> %d", err); return err; } @@ -3258,8 +3144,6 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { lfs_mdir_t oldcwd; lfs_stag_t oldtag = lfs_dir_find(lfs, &oldcwd, &oldpath, NULL); if (oldtag < 0 || lfs_tag_id(oldtag) == 0x3ff) { - LFS_TRACE("lfs_rename -> %"PRId32, - (oldtag < 0) ? oldtag : LFS_ERR_INVAL); return (oldtag < 0) ? (int)oldtag : LFS_ERR_INVAL; } @@ -3269,8 +3153,6 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { lfs_stag_t prevtag = lfs_dir_find(lfs, &newcwd, &newpath, &newid); if ((prevtag < 0 || lfs_tag_id(prevtag) == 0x3ff) && !(prevtag == LFS_ERR_NOENT && newid != 0x3ff)) { - LFS_TRACE("lfs_rename -> %"PRId32, - (prevtag < 0) ? prevtag : LFS_ERR_INVAL); return (prevtag < 0) ? (int)prevtag : LFS_ERR_INVAL; } @@ -3284,7 +3166,6 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { // check that name fits lfs_size_t nlen = strlen(newpath); if (nlen > lfs->name_max) { - LFS_TRACE("lfs_rename -> %d", LFS_ERR_NAMETOOLONG); return LFS_ERR_NAMETOOLONG; } @@ -3295,11 +3176,9 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { newoldid += 1; } } else if (lfs_tag_type3(prevtag) != lfs_tag_type3(oldtag)) { - LFS_TRACE("lfs_rename -> %d", LFS_ERR_ISDIR); return LFS_ERR_ISDIR; } else if (samepair && newid == newoldid) { // we're renaming to ourselves?? - LFS_TRACE("lfs_rename -> %d", 0); return 0; } else if (lfs_tag_type3(prevtag) == LFS_TYPE_DIR) { // must be empty before removal @@ -3307,7 +3186,6 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { lfs_stag_t res = lfs_dir_get(lfs, &newcwd, LFS_MKTAG(0x700, 0x3ff, 0), LFS_MKTAG(LFS_TYPE_STRUCT, newid, 8), prevpair); if (res < 0) { - LFS_TRACE("lfs_rename -> %"PRId32, res); return (int)res; } lfs_pair_fromle32(prevpair); @@ -3315,12 +3193,10 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { // must be empty before removal err = lfs_dir_fetch(lfs, &prevdir.m, prevpair); if (err) { - LFS_TRACE("lfs_rename -> %d", err); return err; } if (prevdir.m.count > 0 || prevdir.m.split) { - LFS_TRACE("lfs_rename -> %d", LFS_ERR_NOTEMPTY); return LFS_ERR_NOTEMPTY; } @@ -3349,7 +3225,6 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { LFS_TYPE_DELETE, newoldid, 0), NULL})); if (err) { lfs->mlist = prevdir.next; - LFS_TRACE("lfs_rename -> %d", err); return err; } @@ -3362,7 +3237,6 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { {LFS_MKTAG(LFS_TYPE_DELETE, lfs_tag_id(oldtag), 0), NULL})); if (err) { lfs->mlist = prevdir.next; - LFS_TRACE("lfs_rename -> %d", err); return err; } } @@ -3374,29 +3248,23 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { err = lfs_fs_pred(lfs, prevdir.m.pair, &newcwd); if (err) { - LFS_TRACE("lfs_rename -> %d", err); return err; } err = lfs_dir_drop(lfs, &newcwd, &prevdir.m); if (err) { - LFS_TRACE("lfs_rename -> %d", err); return err; } } - LFS_TRACE("lfs_rename -> %d", 0); return 0; } static lfs_ssize_t lfs_getattrraw(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { - LFS_TRACE("lfs_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", - (void*)lfs, path, type, buffer, size); lfs_mdir_t cwd; lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); if (tag < 0) { - LFS_TRACE("lfs_getattr -> %"PRId32, tag); return tag; } @@ -3406,7 +3274,6 @@ static lfs_ssize_t lfs_getattrraw(lfs_t *lfs, const char *path, id = 0; int err = lfs_dir_fetch(lfs, &cwd, lfs->root); if (err) { - LFS_TRACE("lfs_getattr -> %d", err); return err; } } @@ -3417,17 +3284,13 @@ static lfs_ssize_t lfs_getattrraw(lfs_t *lfs, const char *path, buffer); if (tag < 0) { if (tag == LFS_ERR_NOENT) { - LFS_TRACE("lfs_getattr -> %d", LFS_ERR_NOATTR); return LFS_ERR_NOATTR; } - LFS_TRACE("lfs_getattr -> %"PRId32, tag); return tag; } - size = lfs_tag_size(tag); - LFS_TRACE("lfs_getattr -> %"PRId32, size); - return size; + return lfs_tag_size(tag); } static int lfs_commitattr(lfs_t *lfs, const char *path, @@ -3454,23 +3317,15 @@ static int lfs_commitattr(lfs_t *lfs, const char *path, static int lfs_setattrraw(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { - LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", - (void*)lfs, path, type, buffer, size); if (size > lfs->attr_max) { - LFS_TRACE("lfs_setattr -> %d", LFS_ERR_NOSPC); return LFS_ERR_NOSPC; } - int err = lfs_commitattr(lfs, path, type, buffer, size); - LFS_TRACE("lfs_setattr -> %d", err); - return err; + return lfs_commitattr(lfs, path, type, buffer, size); } static int lfs_removeattrraw(lfs_t *lfs, const char *path, uint8_t type) { - LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); - int err = lfs_commitattr(lfs, path, type, NULL, 0x3ff); - LFS_TRACE("lfs_removeattr -> %d", err); - return err; + return lfs_commitattr(lfs, path, type, NULL, 0x3ff); } @@ -3600,27 +3455,10 @@ static int lfs_deinit(lfs_t *lfs) { } static int lfs_formatraw(lfs_t *lfs, const struct lfs_config *cfg) { - LFS_TRACE("lfs_format(%p, %p {.context=%p, " - ".read=%p, .prog=%p, .erase=%p, .sync=%p, " - ".read_size=%"PRIu32", .prog_size=%"PRIu32", " - ".block_size=%"PRIu32", .block_count=%"PRIu32", " - ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " - ".lookahead_size=%"PRIu32", .read_buffer=%p, " - ".prog_buffer=%p, .lookahead_buffer=%p, " - ".name_max=%"PRIu32", .file_max=%"PRIu32", " - ".attr_max=%"PRIu32"})", - (void*)lfs, (void*)cfg, cfg->context, - (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, - (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, - cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, - cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, - cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, - cfg->name_max, cfg->file_max, cfg->attr_max); int err = 0; { err = lfs_init(lfs, cfg); if (err) { - LFS_TRACE("lfs_format -> %d", err); return err; } @@ -3676,30 +3514,12 @@ static int lfs_formatraw(lfs_t *lfs, const struct lfs_config *cfg) { cleanup: lfs_deinit(lfs); - LFS_TRACE("lfs_format -> %d", err); return err; } static int lfs_mountraw(lfs_t *lfs, const struct lfs_config *cfg) { - LFS_TRACE("lfs_mount(%p, %p {.context=%p, " - ".read=%p, .prog=%p, .erase=%p, .sync=%p, " - ".read_size=%"PRIu32", .prog_size=%"PRIu32", " - ".block_size=%"PRIu32", .block_count=%"PRIu32", " - ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " - ".lookahead_size=%"PRIu32", .read_buffer=%p, " - ".prog_buffer=%p, .lookahead_buffer=%p, " - ".name_max=%"PRIu32", .file_max=%"PRIu32", " - ".attr_max=%"PRIu32"})", - (void*)lfs, (void*)cfg, cfg->context, - (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, - (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, - cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, - cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, - cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, - cfg->name_max, cfg->file_max, cfg->attr_max); int err = lfs_init(lfs, cfg); if (err) { - LFS_TRACE("lfs_mount -> %d", err); return err; } @@ -3815,20 +3635,15 @@ static int lfs_mountraw(lfs_t *lfs, const struct lfs_config *cfg) { // setup free lookahead lfs_alloc_reset(lfs); - LFS_TRACE("lfs_mount -> %d", 0); return 0; cleanup: lfs_unmountraw(lfs); - LFS_TRACE("lfs_mount -> %d", err); return err; } static int lfs_unmountraw(lfs_t *lfs) { - LFS_TRACE("lfs_unmount(%p)", (void*)lfs); - int err = lfs_deinit(lfs); - LFS_TRACE("lfs_unmount -> %d", err); - return err; + return lfs_deinit(lfs); } @@ -4242,15 +4057,12 @@ static int lfs_fs_size_count(void *p, lfs_block_t block) { } static lfs_ssize_t lfs_fs_sizeraw(lfs_t *lfs) { - LFS_TRACE("lfs_fs_size(%p)", (void*)lfs); lfs_size_t size = 0; int err = lfs_fs_traverseraw(lfs, lfs_fs_size_count, &size, false); if (err) { - LFS_TRACE("lfs_fs_size -> %d", err); return err; } - LFS_TRACE("lfs_fs_size -> %d", err); return size; } @@ -4676,26 +4488,9 @@ static int lfs1_unmount(lfs_t *lfs) { /// v1 migration /// static int lfs_migrateraw(lfs_t *lfs, const struct lfs_config *cfg) { - LFS_TRACE("lfs_migrate(%p, %p {.context=%p, " - ".read=%p, .prog=%p, .erase=%p, .sync=%p, " - ".read_size=%"PRIu32", .prog_size=%"PRIu32", " - ".block_size=%"PRIu32", .block_count=%"PRIu32", " - ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " - ".lookahead_size=%"PRIu32", .read_buffer=%p, " - ".prog_buffer=%p, .lookahead_buffer=%p, " - ".name_max=%"PRIu32", .file_max=%"PRIu32", " - ".attr_max=%"PRIu32"})", - (void*)lfs, (void*)cfg, cfg->context, - (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, - (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, - cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, - cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, - cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, - cfg->name_max, cfg->file_max, cfg->attr_max); struct lfs1 lfs1; int err = lfs1_mount(lfs, &lfs1, cfg); if (err) { - LFS_TRACE("lfs_migrate -> %d", err); return err; } @@ -4912,7 +4707,6 @@ static int lfs_migrateraw(lfs_t *lfs, const struct lfs_config *cfg) { cleanup: lfs1_unmount(lfs); - LFS_TRACE("lfs_migrate -> %d", err); return err; } @@ -4939,7 +4733,24 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { return err; } + LFS_TRACE("lfs_format(%p, %p {.context=%p, " + ".read=%p, .prog=%p, .erase=%p, .sync=%p, " + ".read_size=%"PRIu32", .prog_size=%"PRIu32", " + ".block_size=%"PRIu32", .block_count=%"PRIu32", " + ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " + ".lookahead_size=%"PRIu32", .read_buffer=%p, " + ".prog_buffer=%p, .lookahead_buffer=%p, " + ".name_max=%"PRIu32", .file_max=%"PRIu32", " + ".attr_max=%"PRIu32"})", + (void*)lfs, (void*)cfg, cfg->context, + (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, + (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, + cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, + cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, + cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, + cfg->name_max, cfg->file_max, cfg->attr_max); err = lfs_formatraw(lfs, cfg); + LFS_TRACE("lfs_format -> %d", err); LFS_UNLOCK(cfg); return err; @@ -4951,7 +4762,24 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { return err; } + LFS_TRACE("lfs_mount(%p, %p {.context=%p, " + ".read=%p, .prog=%p, .erase=%p, .sync=%p, " + ".read_size=%"PRIu32", .prog_size=%"PRIu32", " + ".block_size=%"PRIu32", .block_count=%"PRIu32", " + ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " + ".lookahead_size=%"PRIu32", .read_buffer=%p, " + ".prog_buffer=%p, .lookahead_buffer=%p, " + ".name_max=%"PRIu32", .file_max=%"PRIu32", " + ".attr_max=%"PRIu32"})", + (void*)lfs, (void*)cfg, cfg->context, + (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, + (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, + cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, + cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, + cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, + cfg->name_max, cfg->file_max, cfg->attr_max); err = lfs_mountraw(lfs, cfg); + LFS_TRACE("lfs_mount -> %d", err); LFS_UNLOCK(cfg); return err; @@ -4963,7 +4791,9 @@ int lfs_unmount(lfs_t *lfs) { return err; } + LFS_TRACE("lfs_unmount(%p)", (void*)lfs); err = lfs_unmountraw(lfs); + LFS_TRACE("lfs_unmount -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -4975,7 +4805,9 @@ int lfs_remove(lfs_t *lfs, const char *path) { return err; } + LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); err = lfs_removeraw(lfs, path); + LFS_TRACE("lfs_remove -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -4987,7 +4819,9 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { return err; } + LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); err = lfs_renameraw(lfs, oldpath, newpath); + LFS_TRACE("lfs_rename -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -4999,7 +4833,9 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { return err; } + LFS_TRACE("lfs_stat(%p, \"%s\", %p)", (void*)lfs, path, (void*)info); err = lfs_statraw(lfs, path, info); + LFS_TRACE("lfs_stat -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5012,10 +4848,13 @@ lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, return err; } - err = lfs_getattrraw(lfs, path, type, buffer, size); + LFS_TRACE("lfs_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", + (void*)lfs, path, type, buffer, size); + lfs_ssize_t res = lfs_getattrraw(lfs, path, type, buffer, size); + LFS_TRACE("lfs_getattr -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); - return err; + return res; } int lfs_setattr(lfs_t *lfs, const char *path, @@ -5025,7 +4864,10 @@ int lfs_setattr(lfs_t *lfs, const char *path, return err; } + LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", + (void*)lfs, path, type, buffer, size); err = lfs_setattrraw(lfs, path, type, buffer, size); + LFS_TRACE("lfs_setattr -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5037,7 +4879,9 @@ int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { return err; } + LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); err = lfs_removeattrraw(lfs, path, type); + LFS_TRACE("lfs_removeattr -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5049,7 +4893,10 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { return err; } + LFS_TRACE("lfs_file_open(%p, %p, \"%s\", %x)", + (void*)lfs, (void*)file, path, flags); err = lfs_file_openraw(lfs, file, path, flags); + LFS_TRACE("lfs_file_open -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5057,13 +4904,18 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, - const struct lfs_file_config *config) { + const struct lfs_file_config *cfg) { int err = LFS_LOCK(lfs->cfg); if (err) { return err; } - err = lfs_file_opencfgraw(lfs, file, path, flags, config); + LFS_TRACE("lfs_file_opencfg(%p, %p, \"%s\", %x, %p {" + ".buffer=%p, .attrs=%p, .attr_count=%"PRIu32"})", + (void*)lfs, (void*)file, path, flags, + (void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count); + err = lfs_file_opencfgraw(lfs, file, path, flags, cfg); + LFS_TRACE("lfs_file_opencfg -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5075,7 +4927,9 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { return err; } + LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file); err = lfs_file_closeraw(lfs, file); + LFS_TRACE("lfs_file_close -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5087,7 +4941,9 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { return err; } + LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file); err = lfs_file_syncraw(lfs, file); + LFS_TRACE("lfs_file_sync -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5100,10 +4956,13 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, return err; } - err = lfs_file_readraw(lfs, file, buffer, size); + LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")", + (void*)lfs, (void*)file, buffer, size); + lfs_ssize_t res = lfs_file_readraw(lfs, file, buffer, size); + LFS_TRACE("lfs_file_read -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); - return err; + return res; } lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, @@ -5113,10 +4972,13 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, return err; } - err = lfs_file_writeraw(lfs, file, buffer, size); + LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")", + (void*)lfs, (void*)file, buffer, size); + lfs_ssize_t res = lfs_file_writeraw(lfs, file, buffer, size); + LFS_TRACE("lfs_file_write -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); - return err; + return res; } lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, @@ -5126,10 +4988,13 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, return err; } - err = lfs_file_seekraw(lfs, file, off, whence); + LFS_TRACE("lfs_file_seek(%p, %p, %"PRId32", %d)", + (void*)lfs, (void*)file, off, whence); + lfs_soff_t res = lfs_file_seekraw(lfs, file, off, whence); + LFS_TRACE("lfs_file_seek -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); - return err; + return res; } int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { @@ -5138,7 +5003,10 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { return err; } + LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")", + (void*)lfs, (void*)file, size); err = lfs_file_truncateraw(lfs, file, size); + LFS_TRACE("lfs_file_truncate -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5150,10 +5018,12 @@ lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { return err; } - err = lfs_file_tellraw(lfs, file); + LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file); + lfs_soff_t res = lfs_file_tellraw(lfs, file); + LFS_TRACE("lfs_file_tell -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); - return err; + return res; } int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { @@ -5162,7 +5032,9 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { return err; } + LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file); err = lfs_file_rewindraw(lfs, file); + LFS_TRACE("lfs_file_rewind -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5174,10 +5046,12 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { return err; } - err = lfs_file_sizeraw(lfs, file); + LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file); + lfs_soff_t res = lfs_file_sizeraw(lfs, file); + LFS_TRACE("lfs_file_size -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); - return err; + return res; } int lfs_mkdir(lfs_t *lfs, const char *path) { @@ -5186,7 +5060,9 @@ int lfs_mkdir(lfs_t *lfs, const char *path) { return err; } + LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); err = lfs_mkdirraw(lfs, path); + LFS_TRACE("lfs_mkdir -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5198,7 +5074,9 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { return err; } + LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); err = lfs_dir_openraw(lfs, dir, path); + LFS_TRACE("lfs_dir_open -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5210,7 +5088,9 @@ int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { return err; } + LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); err = lfs_dir_closeraw(lfs, dir); + LFS_TRACE("lfs_dir_close -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5222,7 +5102,10 @@ int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { return err; } + LFS_TRACE("lfs_dir_read(%p, %p, %p)", + (void*)lfs, (void*)dir, (void*)info); err = lfs_dir_readraw(lfs, dir, info); + LFS_TRACE("lfs_dir_read -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5234,7 +5117,10 @@ int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { return err; } + LFS_TRACE("lfs_dir_seek(%p, %p, %"PRIu32")", + (void*)lfs, (void*)dir, off); err = lfs_dir_seekraw(lfs, dir, off); + LFS_TRACE("lfs_dir_seek -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5246,10 +5132,12 @@ lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { return err; } - err = lfs_dir_tellraw(lfs, dir); + LFS_TRACE("lfs_dir_tell(%p, %p)", (void*)lfs, (void*)dir); + lfs_soff_t res = lfs_dir_tellraw(lfs, dir); + LFS_TRACE("lfs_dir_tell -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); - return err; + return res; } int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { @@ -5258,7 +5146,9 @@ int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { return err; } + LFS_TRACE("lfs_dir_rewind(%p, %p)", (void*)lfs, (void*)dir); err = lfs_dir_rewindraw(lfs, dir); + LFS_TRACE("lfs_dir_rewind -> %d", err); LFS_UNLOCK(lfs->cfg); return err; @@ -5270,10 +5160,12 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs) { return err; } - err = lfs_fs_sizeraw(lfs); + LFS_TRACE("lfs_fs_size(%p)", (void*)lfs); + lfs_ssize_t res = lfs_fs_sizeraw(lfs); + LFS_TRACE("lfs_fs_size -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); - return err; + return res; } int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) { @@ -5298,7 +5190,24 @@ int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { return err; } + LFS_TRACE("lfs_migrate(%p, %p {.context=%p, " + ".read=%p, .prog=%p, .erase=%p, .sync=%p, " + ".read_size=%"PRIu32", .prog_size=%"PRIu32", " + ".block_size=%"PRIu32", .block_count=%"PRIu32", " + ".block_cycles=%"PRIu32", .cache_size=%"PRIu32", " + ".lookahead_size=%"PRIu32", .read_buffer=%p, " + ".prog_buffer=%p, .lookahead_buffer=%p, " + ".name_max=%"PRIu32", .file_max=%"PRIu32", " + ".attr_max=%"PRIu32"})", + (void*)lfs, (void*)cfg, cfg->context, + (void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog, + (void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync, + cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count, + cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, + cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, + cfg->name_max, cfg->file_max, cfg->attr_max); err = lfs_migrateraw(lfs, cfg); + LFS_TRACE("lfs_migrate -> %d", err); LFS_UNLOCK(cfg); return err; diff --git a/lfs.h b/lfs.h index 3fec4df6..0ac06394 100644 --- a/lfs.h +++ b/lfs.h @@ -16,6 +16,7 @@ extern "C" { #endif + /// Version info /// // Software library version @@ -657,6 +658,7 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg); #endif + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/lfs_util.h b/lfs_util.h index d3baffdb..dbb4c5ba 100644 --- a/lfs_util.h +++ b/lfs_util.h @@ -43,6 +43,7 @@ extern "C" { #endif + // Macros, may be replaced by system specific wrappers. Arguments to these // macros must not have side-effects as the macros can be removed for a smaller // code footprint From a99a93fb2704b0a3ea6904b5676e9e13c66556ce Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sat, 28 Nov 2020 12:41:36 -0600 Subject: [PATCH 22/26] Added thread-safe build+size reporting to CI --- .travis.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.travis.yml b/.travis.yml index 78d964a4..0d357815 100644 --- a/.travis.yml +++ b/.travis.yml @@ -208,6 +208,22 @@ jobs: script: - make test TFLAGS+="-k --valgrind" + # test compilation in thread-safe mode + - stage: test + env: + - NAME=littlefs-threadsafe + - CC="arm-linux-gnueabi-gcc --static -mthumb" + - CFLAGS="-Werror -DLFS_THREADSAFE" + if: branch !~ -prefix$ + install: + - *install-common + - sudo apt-get install + gcc-arm-linux-gnueabi + libc6-dev-armel-cross + - arm-linux-gnueabi-gcc --version + # report-size will compile littlefs and report the size + script: [*report-size] + # self-host with littlefs-fuse for fuzz test - stage: test env: From 7388b2938ac093843ea4997ddcf1dd2a24012b01 Mon Sep 17 00:00:00 2001 From: Noah Gorny Date: Tue, 17 Nov 2020 16:32:00 -0600 Subject: [PATCH 23/26] Deprecate LFS_F_OPENED and use lfs_mlist_isused instead Instead of additional flag, we can just go through the mlist. --- lfs.c | 25 ++++++++++++------------- lfs.h | 1 - 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lfs.c b/lfs.c index 70fed81c..a8ab631a 100644 --- a/lfs.c +++ b/lfs.c @@ -2462,7 +2462,7 @@ static int lfs_file_opencfgraw(lfs_t *lfs, lfs_file_t *file, // setup simple file details int err; file->cfg = cfg; - file->flags = flags | LFS_F_OPENED; + file->flags = flags; file->pos = 0; file->off = 0; file->cache.buffer = NULL; @@ -2615,7 +2615,7 @@ static int lfs_file_openraw(lfs_t *lfs, lfs_file_t *file, } static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); #ifdef LFS_READONLY int err = 0; @@ -2631,14 +2631,13 @@ static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file) { lfs_free(file->cache.buffer); } - file->flags &= ~LFS_F_OPENED; return err; } #ifndef LFS_READONLY static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); while (true) { // just relocate what exists into new block @@ -2725,7 +2724,7 @@ static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file) { #ifndef LFS_READONLY static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); if (file->flags & LFS_F_READING) { if (!(file->flags & LFS_F_INLINE)) { @@ -2742,7 +2741,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 | LFS_F_OPENED, + .flags = LFS_O_RDONLY, .pos = file->pos, .cache = lfs->rcache, }; @@ -2807,7 +2806,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { #ifndef LFS_READONLY static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); if (file->flags & LFS_F_ERRED) { // it's not safe to do anything if our file errored @@ -2862,7 +2861,7 @@ static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file) { static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); #ifndef LFS_READONLY LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_WRONLY); #endif @@ -2939,7 +2938,7 @@ static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, #ifndef LFS_READONLY static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_RDONLY); const uint8_t *data = buffer; @@ -3060,7 +3059,7 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, static lfs_soff_t lfs_file_seekraw(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); #ifndef LFS_READONLY // write out everything beforehand, may be noop if rdonly @@ -3092,7 +3091,7 @@ static lfs_soff_t lfs_file_seekraw(lfs_t *lfs, lfs_file_t *file, #ifndef LFS_READONLY static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_RDONLY); if (size > LFS_FILE_MAX) { @@ -3148,7 +3147,7 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { #endif static lfs_soff_t lfs_file_tellraw(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); (void)lfs; return file->pos; } @@ -3163,7 +3162,7 @@ static int lfs_file_rewindraw(lfs_t *lfs, lfs_file_t *file) { } static lfs_soff_t lfs_file_sizeraw(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(file->flags & LFS_F_OPENED); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); (void)lfs; #ifndef LFS_READONLY diff --git a/lfs.h b/lfs.h index a752f8c7..27469a14 100644 --- a/lfs.h +++ b/lfs.h @@ -143,7 +143,6 @@ enum lfs_open_flags { LFS_F_ERRED = 0x080000, // An error occurred during write #endif LFS_F_INLINE = 0x100000, // Currently inlined in directory entry - LFS_F_OPENED = 0x200000, // File has been opened }; // File seek flags From 2bb523421ea9d366022096a5cfaef46f3d459d8e Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 4 Dec 2020 00:42:32 -0600 Subject: [PATCH 24/26] Moved lfs_mlist_isopen checks into the API wrappers This indirectly solves an issue with lfs_file_rawclose asserting when lfs_file_opencfg errors since appending to the mlist occurs after open. It also may speed up some of the internal operations such as the lfs_file_write used to resolve unflushed data. The idea behind adopting mlist over flags is that realistically it's unlikely for the user to open a significant number of files (enough for big O to kick in). That being said, moving the mlist asserts into the API wrappers does protect some of the internal operations from scaling based on the number of open files. --- lfs.c | 171 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 81 insertions(+), 90 deletions(-) diff --git a/lfs.c b/lfs.c index a8ab631a..e7a52571 100644 --- a/lfs.c +++ b/lfs.c @@ -2098,8 +2098,6 @@ static int lfs_mkdirraw(lfs_t *lfs, const char *path) { #endif static int lfs_dir_openraw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { - LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)dir)); - lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL); if (tag < 0) { return tag; @@ -2446,17 +2444,16 @@ static int lfs_ctz_traverse(lfs_t *lfs, static int lfs_file_opencfgraw(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *cfg) { - LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); -#ifdef LFS_READONLY - LFS_ASSERT((flags & 3) == LFS_O_RDONLY); -#else +#ifndef LFS_READONLY // deorphan if we haven't yet, needed at most once after poweron - if ((flags & LFS_O_RDWR) != LFS_O_RDONLY) { + if ((flags & LFS_O_WRONLY) == LFS_O_WRONLY) { int err = lfs_fs_forceconsistency(lfs); if (err) { return err; } } +#else + LFS_ASSERT((flags & LFS_O_RDONLY) == LFS_O_RDONLY); #endif // setup simple file details @@ -2615,12 +2612,10 @@ static int lfs_file_openraw(lfs_t *lfs, lfs_file_t *file, } static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - -#ifdef LFS_READONLY - int err = 0; -#else +#ifndef LFS_READONLY int err = lfs_file_syncraw(lfs, file); +#else + int err = 0; #endif // remove from list of mdirs @@ -2637,8 +2632,6 @@ static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file) { #ifndef LFS_READONLY static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - while (true) { // just relocate what exists into new block lfs_block_t nblock; @@ -2724,8 +2717,6 @@ static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file) { #ifndef LFS_READONLY static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - if (file->flags & LFS_F_READING) { if (!(file->flags & LFS_F_INLINE)) { lfs_cache_drop(lfs, &file->cache); @@ -2806,8 +2797,6 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { #ifndef LFS_READONLY static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - if (file->flags & LFS_F_ERRED) { // it's not safe to do anything if our file errored return 0; @@ -2861,10 +2850,7 @@ static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file) { static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); -#ifndef LFS_READONLY - LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_WRONLY); -#endif + LFS_ASSERT((file->flags & LFS_O_RDONLY) == LFS_O_RDONLY); uint8_t *data = buffer; lfs_size_t nsize = size; @@ -2938,8 +2924,7 @@ static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, #ifndef LFS_READONLY static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_RDONLY); + LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY); const uint8_t *data = buffer; lfs_size_t nsize = size; @@ -3059,8 +3044,6 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, static lfs_soff_t lfs_file_seekraw(lfs_t *lfs, lfs_file_t *file, lfs_soff_t off, int whence) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - #ifndef LFS_READONLY // write out everything beforehand, may be noop if rdonly int err = lfs_file_flush(lfs, file); @@ -3091,8 +3074,7 @@ static lfs_soff_t lfs_file_seekraw(lfs_t *lfs, lfs_file_t *file, #ifndef LFS_READONLY static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_RDONLY); + LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY); if (size > LFS_FILE_MAX) { return LFS_ERR_INVAL; @@ -3147,7 +3129,6 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { #endif static lfs_soff_t lfs_file_tellraw(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); (void)lfs; return file->pos; } @@ -3162,7 +3143,6 @@ static int lfs_file_rewindraw(lfs_t *lfs, lfs_file_t *file) { } static lfs_soff_t lfs_file_sizeraw(lfs_t *lfs, lfs_file_t *file) { - LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); (void)lfs; #ifndef LFS_READONLY @@ -4894,7 +4874,6 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { if (err) { return err; } - LFS_TRACE("lfs_format(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -4911,9 +4890,10 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, cfg->name_max, cfg->file_max, cfg->attr_max); + err = lfs_formatraw(lfs, cfg); - LFS_TRACE("lfs_format -> %d", err); + LFS_TRACE("lfs_format -> %d", err); LFS_UNLOCK(cfg); return err; } @@ -4924,7 +4904,6 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { if (err) { return err; } - LFS_TRACE("lfs_mount(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -4941,9 +4920,10 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, cfg->name_max, cfg->file_max, cfg->attr_max); + err = lfs_mountraw(lfs, cfg); - LFS_TRACE("lfs_mount -> %d", err); + LFS_TRACE("lfs_mount -> %d", err); LFS_UNLOCK(cfg); return err; } @@ -4953,11 +4933,11 @@ int lfs_unmount(lfs_t *lfs) { if (err) { return err; } - LFS_TRACE("lfs_unmount(%p)", (void*)lfs); + err = lfs_unmountraw(lfs); - LFS_TRACE("lfs_unmount -> %d", err); + LFS_TRACE("lfs_unmount -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -4968,11 +4948,11 @@ int lfs_remove(lfs_t *lfs, const char *path) { if (err) { return err; } - LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); + err = lfs_removeraw(lfs, path); - LFS_TRACE("lfs_remove -> %d", err); + LFS_TRACE("lfs_remove -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -4984,11 +4964,11 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { if (err) { return err; } - LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); + err = lfs_renameraw(lfs, oldpath, newpath); - LFS_TRACE("lfs_rename -> %d", err); + LFS_TRACE("lfs_rename -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -4999,11 +4979,11 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { if (err) { return err; } - LFS_TRACE("lfs_stat(%p, \"%s\", %p)", (void*)lfs, path, (void*)info); + err = lfs_statraw(lfs, path, info); - LFS_TRACE("lfs_stat -> %d", err); + LFS_TRACE("lfs_stat -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5014,12 +4994,12 @@ lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, if (err) { return err; } - LFS_TRACE("lfs_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); + lfs_ssize_t res = lfs_getattrraw(lfs, path, type, buffer, size); - LFS_TRACE("lfs_getattr -> %"PRId32, res); + LFS_TRACE("lfs_getattr -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); return res; } @@ -5031,12 +5011,12 @@ int lfs_setattr(lfs_t *lfs, const char *path, if (err) { return err; } - LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); + err = lfs_setattrraw(lfs, path, type, buffer, size); - LFS_TRACE("lfs_setattr -> %d", err); + LFS_TRACE("lfs_setattr -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5048,11 +5028,11 @@ int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { if (err) { return err; } - LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); + err = lfs_removeattrraw(lfs, path, type); - LFS_TRACE("lfs_removeattr -> %d", err); + LFS_TRACE("lfs_removeattr -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5063,12 +5043,13 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { if (err) { return err; } - LFS_TRACE("lfs_file_open(%p, %p, \"%s\", %x)", (void*)lfs, (void*)file, path, flags); + LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + err = lfs_file_openraw(lfs, file, path, flags); - LFS_TRACE("lfs_file_open -> %d", err); + LFS_TRACE("lfs_file_open -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5080,14 +5061,15 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, if (err) { return err; } - LFS_TRACE("lfs_file_opencfg(%p, %p, \"%s\", %x, %p {" ".buffer=%p, .attrs=%p, .attr_count=%"PRIu32"})", (void*)lfs, (void*)file, path, flags, (void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count); + LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + err = lfs_file_opencfgraw(lfs, file, path, flags, cfg); - LFS_TRACE("lfs_file_opencfg -> %d", err); + LFS_TRACE("lfs_file_opencfg -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5097,11 +5079,12 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { if (err) { return err; } - LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + err = lfs_file_closeraw(lfs, file); - LFS_TRACE("lfs_file_close -> %d", err); + LFS_TRACE("lfs_file_close -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5112,11 +5095,12 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { if (err) { return err; } - LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + err = lfs_file_syncraw(lfs, file); - LFS_TRACE("lfs_file_sync -> %d", err); + LFS_TRACE("lfs_file_sync -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5128,12 +5112,13 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, if (err) { return err; } - LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + lfs_ssize_t res = lfs_file_readraw(lfs, file, buffer, size); - LFS_TRACE("lfs_file_read -> %"PRId32, res); + LFS_TRACE("lfs_file_read -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); return res; } @@ -5145,12 +5130,13 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, if (err) { return err; } - LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")", (void*)lfs, (void*)file, buffer, size); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + lfs_ssize_t res = lfs_file_writeraw(lfs, file, buffer, size); - LFS_TRACE("lfs_file_write -> %"PRId32, res); + LFS_TRACE("lfs_file_write -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); return res; } @@ -5162,12 +5148,13 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, if (err) { return err; } - LFS_TRACE("lfs_file_seek(%p, %p, %"PRId32", %d)", (void*)lfs, (void*)file, off, whence); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + lfs_soff_t res = lfs_file_seekraw(lfs, file, off, whence); - LFS_TRACE("lfs_file_seek -> %"PRId32, res); + LFS_TRACE("lfs_file_seek -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); return res; } @@ -5178,12 +5165,13 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { if (err) { return err; } - LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")", (void*)lfs, (void*)file, size); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + err = lfs_file_truncateraw(lfs, file, size); - LFS_TRACE("lfs_file_truncate -> %d", err); + LFS_TRACE("lfs_file_truncate -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5194,11 +5182,12 @@ lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { if (err) { return err; } - LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + lfs_soff_t res = lfs_file_tellraw(lfs, file); - LFS_TRACE("lfs_file_tell -> %"PRId32, res); + LFS_TRACE("lfs_file_tell -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); return res; } @@ -5208,11 +5197,11 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { if (err) { return err; } - LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file); + err = lfs_file_rewindraw(lfs, file); - LFS_TRACE("lfs_file_rewind -> %d", err); + LFS_TRACE("lfs_file_rewind -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5222,11 +5211,12 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { if (err) { return err; } - LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file); + LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); + lfs_soff_t res = lfs_file_sizeraw(lfs, file); - LFS_TRACE("lfs_file_size -> %"PRId32, res); + LFS_TRACE("lfs_file_size -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); return res; } @@ -5237,11 +5227,11 @@ int lfs_mkdir(lfs_t *lfs, const char *path) { if (err) { return err; } - LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); + err = lfs_mkdirraw(lfs, path); - LFS_TRACE("lfs_mkdir -> %d", err); + LFS_TRACE("lfs_mkdir -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5252,11 +5242,12 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { if (err) { return err; } - LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); + LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)dir)); + err = lfs_dir_openraw(lfs, dir, path); - LFS_TRACE("lfs_dir_open -> %d", err); + LFS_TRACE("lfs_dir_open -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5266,11 +5257,11 @@ int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { if (err) { return err; } - LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); + err = lfs_dir_closeraw(lfs, dir); - LFS_TRACE("lfs_dir_close -> %d", err); + LFS_TRACE("lfs_dir_close -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5280,12 +5271,12 @@ int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { if (err) { return err; } - LFS_TRACE("lfs_dir_read(%p, %p, %p)", (void*)lfs, (void*)dir, (void*)info); + err = lfs_dir_readraw(lfs, dir, info); - LFS_TRACE("lfs_dir_read -> %d", err); + LFS_TRACE("lfs_dir_read -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5295,12 +5286,12 @@ int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { if (err) { return err; } - LFS_TRACE("lfs_dir_seek(%p, %p, %"PRIu32")", (void*)lfs, (void*)dir, off); + err = lfs_dir_seekraw(lfs, dir, off); - LFS_TRACE("lfs_dir_seek -> %d", err); + LFS_TRACE("lfs_dir_seek -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5310,11 +5301,11 @@ lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { if (err) { return err; } - LFS_TRACE("lfs_dir_tell(%p, %p)", (void*)lfs, (void*)dir); + lfs_soff_t res = lfs_dir_tellraw(lfs, dir); - LFS_TRACE("lfs_dir_tell -> %"PRId32, res); + LFS_TRACE("lfs_dir_tell -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); return res; } @@ -5324,11 +5315,11 @@ int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { if (err) { return err; } - LFS_TRACE("lfs_dir_rewind(%p, %p)", (void*)lfs, (void*)dir); + err = lfs_dir_rewindraw(lfs, dir); - LFS_TRACE("lfs_dir_rewind -> %d", err); + LFS_TRACE("lfs_dir_rewind -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5338,11 +5329,11 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs) { if (err) { return err; } - LFS_TRACE("lfs_fs_size(%p)", (void*)lfs); + lfs_ssize_t res = lfs_fs_sizeraw(lfs); - LFS_TRACE("lfs_fs_size -> %"PRId32, res); + LFS_TRACE("lfs_fs_size -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); return res; } @@ -5352,12 +5343,12 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) { if (err) { return err; } - LFS_TRACE("lfs_fs_traverse(%p, %p, %p)", (void*)lfs, (void*)(uintptr_t)cb, data); + err = lfs_fs_traverseraw(lfs, cb, data, true); - LFS_TRACE("lfs_fs_traverse -> %d", err); + LFS_TRACE("lfs_fs_traverse -> %d", err); LFS_UNLOCK(lfs->cfg); return err; } @@ -5368,7 +5359,6 @@ int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { if (err) { return err; } - LFS_TRACE("lfs_migrate(%p, %p {.context=%p, " ".read=%p, .prog=%p, .erase=%p, .sync=%p, " ".read_size=%"PRIu32", .prog_size=%"PRIu32", " @@ -5385,9 +5375,10 @@ int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { cfg->block_cycles, cfg->cache_size, cfg->lookahead_size, cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, cfg->name_max, cfg->file_max, cfg->attr_max); + err = lfs_migrateraw(lfs, cfg); - LFS_TRACE("lfs_migrate -> %d", err); + LFS_TRACE("lfs_migrate -> %d", err); LFS_UNLOCK(cfg); return err; } From 288a5cbc8d1628539fe5c9875be4157f2b593d77 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 4 Dec 2020 01:31:27 -0600 Subject: [PATCH 25/26] Bumped minor version to v2.3 --- lfs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lfs.h b/lfs.h index 27469a14..3b02b6a7 100644 --- a/lfs.h +++ b/lfs.h @@ -22,7 +22,7 @@ extern "C" // Software library version // Major (top-nibble), incremented on backwards incompatible changes // Minor (bottom-nibble), incremented on feature additions -#define LFS_VERSION 0x00020002 +#define LFS_VERSION 0x00020003 #define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16)) #define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0)) From 6a7012774d8bf750f08f13fee43200b9d44df9f3 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 6 Dec 2020 00:26:24 -0600 Subject: [PATCH 26/26] Renamed internal lfs_*raw -> lfs_raw* functions - Prefixing with raw is slightly more readable, follows common-prefix rule - Matches existing raw prefixes in testbd --- lfs.c | 170 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 85 insertions(+), 85 deletions(-) diff --git a/lfs.c b/lfs.c index 1d9a24f3..d7439fe3 100644 --- a/lfs.c +++ b/lfs.c @@ -459,9 +459,9 @@ static int lfs_dir_compact(lfs_t *lfs, lfs_mdir_t *dir, const struct lfs_mattr *attrs, int attrcount, lfs_mdir_t *source, uint16_t begin, uint16_t end); -static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, +static lfs_ssize_t lfs_file_rawwrite(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size); -static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file); +static int lfs_file_rawsync(lfs_t *lfs, lfs_file_t *file); static int lfs_file_outline(lfs_t *lfs, lfs_file_t *file); static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file); @@ -482,20 +482,20 @@ static int lfs1_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data); #endif -static int lfs_dir_rewindraw(lfs_t *lfs, lfs_dir_t *dir); +static int lfs_dir_rawrewind(lfs_t *lfs, lfs_dir_t *dir); -static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, +static lfs_ssize_t lfs_file_rawread(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size); -static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file); -static lfs_soff_t lfs_file_sizeraw(lfs_t *lfs, lfs_file_t *file); +static int lfs_file_rawclose(lfs_t *lfs, lfs_file_t *file); +static lfs_soff_t lfs_file_rawsize(lfs_t *lfs, lfs_file_t *file); -static lfs_ssize_t lfs_fs_sizeraw(lfs_t *lfs); -static int lfs_fs_traverseraw(lfs_t *lfs, +static lfs_ssize_t lfs_fs_rawsize(lfs_t *lfs); +static int lfs_fs_rawtraverse(lfs_t *lfs, int (*cb)(void *data, lfs_block_t block), void *data, bool includeorphans); static int lfs_deinit(lfs_t *lfs); -static int lfs_unmountraw(lfs_t *lfs); +static int lfs_rawunmount(lfs_t *lfs); /// Block allocator /// @@ -567,7 +567,7 @@ static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) { // find mask of free blocks from tree memset(lfs->free.buffer, 0, lfs->cfg->lookahead_size); - int err = lfs_fs_traverseraw(lfs, lfs_alloc_lookahead, lfs, true); + int err = lfs_fs_rawtraverse(lfs, lfs_alloc_lookahead, lfs, true); if (err) { lfs_alloc_drop(lfs); return err; @@ -1626,7 +1626,7 @@ static int lfs_dir_compact(lfs_t *lfs, if (lfs_pair_cmp(dir->pair, (const lfs_block_t[2]){0, 1}) == 0) { // oh no! we're writing too much to the superblock, // should we expand? - lfs_ssize_t res = lfs_fs_sizeraw(lfs); + lfs_ssize_t res = lfs_fs_rawsize(lfs); if (res < 0) { return res; } @@ -2011,7 +2011,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, /// Top level directory operations /// #ifndef LFS_READONLY -static int lfs_mkdirraw(lfs_t *lfs, const char *path) { +static int lfs_rawmkdir(lfs_t *lfs, const char *path) { // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { @@ -2101,7 +2101,7 @@ static int lfs_mkdirraw(lfs_t *lfs, const char *path) { } #endif -static int lfs_dir_openraw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { +static int lfs_dir_rawopen(lfs_t *lfs, lfs_dir_t *dir, const char *path) { lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL); if (tag < 0) { return tag; @@ -2145,14 +2145,14 @@ static int lfs_dir_openraw(lfs_t *lfs, lfs_dir_t *dir, const char *path) { return 0; } -static int lfs_dir_closeraw(lfs_t *lfs, lfs_dir_t *dir) { +static int lfs_dir_rawclose(lfs_t *lfs, lfs_dir_t *dir) { // remove from list of mdirs lfs_mlist_remove(lfs, (struct lfs_mlist *)dir); return 0; } -static int lfs_dir_readraw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { +static int lfs_dir_rawread(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { memset(info, 0, sizeof(*info)); // special offset for '.' and '..' @@ -2197,9 +2197,9 @@ static int lfs_dir_readraw(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { return true; } -static int lfs_dir_seekraw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { +static int lfs_dir_rawseek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { // simply walk from head dir - int err = lfs_dir_rewindraw(lfs, dir); + int err = lfs_dir_rawrewind(lfs, dir); if (err) { return err; } @@ -2234,12 +2234,12 @@ static int lfs_dir_seekraw(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { return 0; } -static lfs_soff_t lfs_dir_tellraw(lfs_t *lfs, lfs_dir_t *dir) { +static lfs_soff_t lfs_dir_rawtell(lfs_t *lfs, lfs_dir_t *dir) { (void)lfs; return dir->pos; } -static int lfs_dir_rewindraw(lfs_t *lfs, lfs_dir_t *dir) { +static int lfs_dir_rawrewind(lfs_t *lfs, lfs_dir_t *dir) { // reload the head dir int err = lfs_dir_fetch(lfs, &dir->m, dir->head); if (err) { @@ -2445,7 +2445,7 @@ static int lfs_ctz_traverse(lfs_t *lfs, /// Top level file operations /// -static int lfs_file_opencfgraw(lfs_t *lfs, lfs_file_t *file, +static int lfs_file_rawopencfg(lfs_t *lfs, lfs_file_t *file, const char *path, int flags, const struct lfs_file_config *cfg) { #ifndef LFS_READONLY @@ -2604,20 +2604,20 @@ static int lfs_file_opencfgraw(lfs_t *lfs, lfs_file_t *file, #ifndef LFS_READONLY file->flags |= LFS_F_ERRED; #endif - lfs_file_closeraw(lfs, file); + lfs_file_rawclose(lfs, file); return err; } -static int lfs_file_openraw(lfs_t *lfs, lfs_file_t *file, +static int lfs_file_rawopen(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { static const struct lfs_file_config defaults = {0}; - int err = lfs_file_opencfgraw(lfs, file, path, flags, &defaults); + int err = lfs_file_rawopencfg(lfs, file, path, flags, &defaults); return err; } -static int lfs_file_closeraw(lfs_t *lfs, lfs_file_t *file) { +static int lfs_file_rawclose(lfs_t *lfs, lfs_file_t *file) { #ifndef LFS_READONLY - int err = lfs_file_syncraw(lfs, file); + int err = lfs_file_rawsync(lfs, file); #else int err = 0; #endif @@ -2746,12 +2746,12 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { // copy over a byte at a time, leave it up to caching // to make this efficient uint8_t data; - lfs_ssize_t res = lfs_file_readraw(lfs, &orig, &data, 1); + lfs_ssize_t res = lfs_file_rawread(lfs, &orig, &data, 1); if (res < 0) { return res; } - res = lfs_file_writeraw(lfs, file, &data, 1); + res = lfs_file_rawwrite(lfs, file, &data, 1); if (res < 0) { return res; } @@ -2800,7 +2800,7 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) { #endif #ifndef LFS_READONLY -static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file) { +static int lfs_file_rawsync(lfs_t *lfs, lfs_file_t *file) { if (file->flags & LFS_F_ERRED) { // it's not safe to do anything if our file errored return 0; @@ -2852,7 +2852,7 @@ static int lfs_file_syncraw(lfs_t *lfs, lfs_file_t *file) { } #endif -static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, +static lfs_ssize_t lfs_file_rawread(lfs_t *lfs, lfs_file_t *file, void *buffer, lfs_size_t size) { LFS_ASSERT((file->flags & LFS_O_RDONLY) == LFS_O_RDONLY); @@ -2926,7 +2926,7 @@ static lfs_ssize_t lfs_file_readraw(lfs_t *lfs, lfs_file_t *file, } #ifndef LFS_READONLY -static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, +static lfs_ssize_t lfs_file_rawwrite(lfs_t *lfs, lfs_file_t *file, const void *buffer, lfs_size_t size) { LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY); @@ -2956,7 +2956,7 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, file->pos = file->ctz.size; while (file->pos < pos) { - lfs_ssize_t res = lfs_file_writeraw(lfs, file, &(uint8_t){0}, 1); + lfs_ssize_t res = lfs_file_rawwrite(lfs, file, &(uint8_t){0}, 1); if (res < 0) { return res; } @@ -3046,7 +3046,7 @@ static lfs_ssize_t lfs_file_writeraw(lfs_t *lfs, lfs_file_t *file, } #endif -static lfs_soff_t lfs_file_seekraw(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 @@ -3077,7 +3077,7 @@ static lfs_soff_t lfs_file_seekraw(lfs_t *lfs, lfs_file_t *file, } #ifndef LFS_READONLY -static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { +static int lfs_file_rawtruncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { LFS_ASSERT((file->flags & LFS_O_WRONLY) == LFS_O_WRONLY); if (size > LFS_FILE_MAX) { @@ -3085,7 +3085,7 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } lfs_off_t pos = file->pos; - lfs_off_t oldsize = lfs_file_sizeraw(lfs, file); + lfs_off_t oldsize = lfs_file_rawsize(lfs, file); if (size < oldsize) { // need to flush since directly changing metadata int err = lfs_file_flush(lfs, file); @@ -3107,7 +3107,7 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } else if (size > oldsize) { // flush+seek if not already at end if (file->pos != oldsize) { - lfs_soff_t res = lfs_file_seekraw(lfs, file, 0, LFS_SEEK_END); + lfs_soff_t res = lfs_file_rawseek(lfs, file, 0, LFS_SEEK_END); if (res < 0) { return (int)res; } @@ -3115,7 +3115,7 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { // fill with zeros while (file->pos < size) { - lfs_ssize_t res = lfs_file_writeraw(lfs, file, &(uint8_t){0}, 1); + lfs_ssize_t res = lfs_file_rawwrite(lfs, file, &(uint8_t){0}, 1); if (res < 0) { return (int)res; } @@ -3123,7 +3123,7 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } // restore pos - lfs_soff_t res = lfs_file_seekraw(lfs, file, pos, LFS_SEEK_SET); + lfs_soff_t res = lfs_file_rawseek(lfs, file, pos, LFS_SEEK_SET); if (res < 0) { return (int)res; } @@ -3132,13 +3132,13 @@ static int lfs_file_truncateraw(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { } #endif -static lfs_soff_t lfs_file_tellraw(lfs_t *lfs, lfs_file_t *file) { +static lfs_soff_t lfs_file_rawtell(lfs_t *lfs, lfs_file_t *file) { (void)lfs; return file->pos; } -static int lfs_file_rewindraw(lfs_t *lfs, lfs_file_t *file) { - lfs_soff_t res = lfs_file_seekraw(lfs, file, 0, LFS_SEEK_SET); +static int lfs_file_rawrewind(lfs_t *lfs, lfs_file_t *file) { + lfs_soff_t res = lfs_file_rawseek(lfs, file, 0, LFS_SEEK_SET); if (res < 0) { return (int)res; } @@ -3146,7 +3146,7 @@ static int lfs_file_rewindraw(lfs_t *lfs, lfs_file_t *file) { return 0; } -static lfs_soff_t lfs_file_sizeraw(lfs_t *lfs, lfs_file_t *file) { +static lfs_soff_t lfs_file_rawsize(lfs_t *lfs, lfs_file_t *file) { (void)lfs; #ifndef LFS_READONLY @@ -3160,7 +3160,7 @@ static lfs_soff_t lfs_file_sizeraw(lfs_t *lfs, lfs_file_t *file) { /// General fs operations /// -static int lfs_statraw(lfs_t *lfs, const char *path, struct lfs_info *info) { +static int lfs_rawstat(lfs_t *lfs, const char *path, struct lfs_info *info) { lfs_mdir_t cwd; lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); if (tag < 0) { @@ -3171,7 +3171,7 @@ static int lfs_statraw(lfs_t *lfs, const char *path, struct lfs_info *info) { } #ifndef LFS_READONLY -static int lfs_removeraw(lfs_t *lfs, const char *path) { +static int lfs_rawremove(lfs_t *lfs, const char *path) { // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { @@ -3244,7 +3244,7 @@ static int lfs_removeraw(lfs_t *lfs, const char *path) { #endif #ifndef LFS_READONLY -static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { +static int lfs_rawrename(lfs_t *lfs, const char *oldpath, const char *newpath) { // deorphan if we haven't yet, needed at most once after poweron int err = lfs_fs_forceconsistency(lfs); if (err) { @@ -3372,7 +3372,7 @@ static int lfs_renameraw(lfs_t *lfs, const char *oldpath, const char *newpath) { } #endif -static lfs_ssize_t lfs_getattrraw(lfs_t *lfs, const char *path, +static lfs_ssize_t lfs_rawgetattr(lfs_t *lfs, const char *path, uint8_t type, void *buffer, lfs_size_t size) { lfs_mdir_t cwd; lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL); @@ -3430,7 +3430,7 @@ static int lfs_commitattr(lfs_t *lfs, const char *path, #endif #ifndef LFS_READONLY -static int lfs_setattrraw(lfs_t *lfs, const char *path, +static int lfs_rawsetattr(lfs_t *lfs, const char *path, uint8_t type, const void *buffer, lfs_size_t size) { if (size > lfs->attr_max) { return LFS_ERR_NOSPC; @@ -3441,7 +3441,7 @@ static int lfs_setattrraw(lfs_t *lfs, const char *path, #endif #ifndef LFS_READONLY -static int lfs_removeattrraw(lfs_t *lfs, const char *path, uint8_t type) { +static int lfs_rawremoveattr(lfs_t *lfs, const char *path, uint8_t type) { return lfs_commitattr(lfs, path, type, NULL, 0x3ff); } #endif @@ -3573,7 +3573,7 @@ static int lfs_deinit(lfs_t *lfs) { } #ifndef LFS_READONLY -static int lfs_formatraw(lfs_t *lfs, const struct lfs_config *cfg) { +static int lfs_rawformat(lfs_t *lfs, const struct lfs_config *cfg) { int err = 0; { err = lfs_init(lfs, cfg); @@ -3638,7 +3638,7 @@ static int lfs_formatraw(lfs_t *lfs, const struct lfs_config *cfg) { } #endif -static int lfs_mountraw(lfs_t *lfs, const struct lfs_config *cfg) { +static int lfs_rawmount(lfs_t *lfs, const struct lfs_config *cfg) { int err = lfs_init(lfs, cfg); if (err) { return err; @@ -3761,17 +3761,17 @@ static int lfs_mountraw(lfs_t *lfs, const struct lfs_config *cfg) { return 0; cleanup: - lfs_unmountraw(lfs); + lfs_rawunmount(lfs); return err; } -static int lfs_unmountraw(lfs_t *lfs) { +static int lfs_rawunmount(lfs_t *lfs) { return lfs_deinit(lfs); } /// Filesystem filesystem operations /// -int lfs_fs_traverseraw(lfs_t *lfs, +int lfs_fs_rawtraverse(lfs_t *lfs, int (*cb)(void *data, lfs_block_t block), void *data, bool includeorphans) { // iterate over metadata pairs @@ -4201,9 +4201,9 @@ static int lfs_fs_size_count(void *p, lfs_block_t block) { return 0; } -static lfs_ssize_t lfs_fs_sizeraw(lfs_t *lfs) { +static lfs_ssize_t lfs_fs_rawsize(lfs_t *lfs) { lfs_size_t size = 0; - int err = lfs_fs_traverseraw(lfs, lfs_fs_size_count, &size, false); + int err = lfs_fs_rawtraverse(lfs, lfs_fs_size_count, &size, false); if (err) { return err; } @@ -4632,7 +4632,7 @@ static int lfs1_unmount(lfs_t *lfs) { } /// v1 migration /// -static int lfs_migrateraw(lfs_t *lfs, const struct lfs_config *cfg) { +static int lfs_rawmigrate(lfs_t *lfs, const struct lfs_config *cfg) { struct lfs1 lfs1; int err = lfs1_mount(lfs, &lfs1, cfg); if (err) { @@ -4895,7 +4895,7 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) { cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, cfg->name_max, cfg->file_max, cfg->attr_max); - err = lfs_formatraw(lfs, cfg); + err = lfs_rawformat(lfs, cfg); LFS_TRACE("lfs_format -> %d", err); LFS_UNLOCK(cfg); @@ -4925,7 +4925,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) { cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, cfg->name_max, cfg->file_max, cfg->attr_max); - err = lfs_mountraw(lfs, cfg); + err = lfs_rawmount(lfs, cfg); LFS_TRACE("lfs_mount -> %d", err); LFS_UNLOCK(cfg); @@ -4939,7 +4939,7 @@ int lfs_unmount(lfs_t *lfs) { } LFS_TRACE("lfs_unmount(%p)", (void*)lfs); - err = lfs_unmountraw(lfs); + err = lfs_rawunmount(lfs); LFS_TRACE("lfs_unmount -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -4954,7 +4954,7 @@ int lfs_remove(lfs_t *lfs, const char *path) { } LFS_TRACE("lfs_remove(%p, \"%s\")", (void*)lfs, path); - err = lfs_removeraw(lfs, path); + err = lfs_rawremove(lfs, path); LFS_TRACE("lfs_remove -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -4970,7 +4970,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { } LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath); - err = lfs_renameraw(lfs, oldpath, newpath); + err = lfs_rawrename(lfs, oldpath, newpath); LFS_TRACE("lfs_rename -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -4985,7 +4985,7 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { } LFS_TRACE("lfs_stat(%p, \"%s\", %p)", (void*)lfs, path, (void*)info); - err = lfs_statraw(lfs, path, info); + err = lfs_rawstat(lfs, path, info); LFS_TRACE("lfs_stat -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5001,7 +5001,7 @@ lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path, LFS_TRACE("lfs_getattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); - lfs_ssize_t res = lfs_getattrraw(lfs, path, type, buffer, size); + lfs_ssize_t res = lfs_rawgetattr(lfs, path, type, buffer, size); LFS_TRACE("lfs_getattr -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); @@ -5018,7 +5018,7 @@ int lfs_setattr(lfs_t *lfs, const char *path, LFS_TRACE("lfs_setattr(%p, \"%s\", %"PRIu8", %p, %"PRIu32")", (void*)lfs, path, type, buffer, size); - err = lfs_setattrraw(lfs, path, type, buffer, size); + err = lfs_rawsetattr(lfs, path, type, buffer, size); LFS_TRACE("lfs_setattr -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5034,7 +5034,7 @@ int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type) { } LFS_TRACE("lfs_removeattr(%p, \"%s\", %"PRIu8")", (void*)lfs, path, type); - err = lfs_removeattrraw(lfs, path, type); + err = lfs_rawremoveattr(lfs, path, type); LFS_TRACE("lfs_removeattr -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5051,7 +5051,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file, const char *path, int flags) { (void*)lfs, (void*)file, path, flags); LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - err = lfs_file_openraw(lfs, file, path, flags); + err = lfs_file_rawopen(lfs, file, path, flags); LFS_TRACE("lfs_file_open -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5071,7 +5071,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file, (void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count); LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - err = lfs_file_opencfgraw(lfs, file, path, flags, cfg); + err = lfs_file_rawopencfg(lfs, file, path, flags, cfg); LFS_TRACE("lfs_file_opencfg -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5086,7 +5086,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_close(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - err = lfs_file_closeraw(lfs, file); + err = lfs_file_rawclose(lfs, file); LFS_TRACE("lfs_file_close -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5102,7 +5102,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_sync(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - err = lfs_file_syncraw(lfs, file); + err = lfs_file_rawsync(lfs, file); LFS_TRACE("lfs_file_sync -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5120,7 +5120,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file, (void*)lfs, (void*)file, buffer, size); LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - lfs_ssize_t res = lfs_file_readraw(lfs, file, buffer, size); + lfs_ssize_t res = lfs_file_rawread(lfs, file, buffer, size); LFS_TRACE("lfs_file_read -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); @@ -5138,7 +5138,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file, (void*)lfs, (void*)file, buffer, size); LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - lfs_ssize_t res = lfs_file_writeraw(lfs, file, buffer, size); + lfs_ssize_t res = lfs_file_rawwrite(lfs, file, buffer, size); LFS_TRACE("lfs_file_write -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); @@ -5156,7 +5156,7 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, (void*)lfs, (void*)file, off, whence); LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - lfs_soff_t res = lfs_file_seekraw(lfs, file, off, whence); + lfs_soff_t res = lfs_file_rawseek(lfs, file, off, whence); LFS_TRACE("lfs_file_seek -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); @@ -5173,7 +5173,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { (void*)lfs, (void*)file, size); LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - err = lfs_file_truncateraw(lfs, file, size); + err = lfs_file_rawtruncate(lfs, file, size); LFS_TRACE("lfs_file_truncate -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5189,7 +5189,7 @@ lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_tell(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - lfs_soff_t res = lfs_file_tellraw(lfs, file); + lfs_soff_t res = lfs_file_rawtell(lfs, file); LFS_TRACE("lfs_file_tell -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); @@ -5203,7 +5203,7 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) { } LFS_TRACE("lfs_file_rewind(%p, %p)", (void*)lfs, (void*)file); - err = lfs_file_rewindraw(lfs, file); + err = lfs_file_rawrewind(lfs, file); LFS_TRACE("lfs_file_rewind -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5218,7 +5218,7 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) { LFS_TRACE("lfs_file_size(%p, %p)", (void*)lfs, (void*)file); LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file)); - lfs_soff_t res = lfs_file_sizeraw(lfs, file); + lfs_soff_t res = lfs_file_rawsize(lfs, file); LFS_TRACE("lfs_file_size -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); @@ -5233,7 +5233,7 @@ int lfs_mkdir(lfs_t *lfs, const char *path) { } LFS_TRACE("lfs_mkdir(%p, \"%s\")", (void*)lfs, path); - err = lfs_mkdirraw(lfs, path); + err = lfs_rawmkdir(lfs, path); LFS_TRACE("lfs_mkdir -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5249,7 +5249,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) { LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path); LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)dir)); - err = lfs_dir_openraw(lfs, dir, path); + err = lfs_dir_rawopen(lfs, dir, path); LFS_TRACE("lfs_dir_open -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5263,7 +5263,7 @@ int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) { } LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir); - err = lfs_dir_closeraw(lfs, dir); + err = lfs_dir_rawclose(lfs, dir); LFS_TRACE("lfs_dir_close -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5278,7 +5278,7 @@ int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) { LFS_TRACE("lfs_dir_read(%p, %p, %p)", (void*)lfs, (void*)dir, (void*)info); - err = lfs_dir_readraw(lfs, dir, info); + err = lfs_dir_rawread(lfs, dir, info); LFS_TRACE("lfs_dir_read -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5293,7 +5293,7 @@ int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { LFS_TRACE("lfs_dir_seek(%p, %p, %"PRIu32")", (void*)lfs, (void*)dir, off); - err = lfs_dir_seekraw(lfs, dir, off); + err = lfs_dir_rawseek(lfs, dir, off); LFS_TRACE("lfs_dir_seek -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5307,7 +5307,7 @@ lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir) { } LFS_TRACE("lfs_dir_tell(%p, %p)", (void*)lfs, (void*)dir); - lfs_soff_t res = lfs_dir_tellraw(lfs, dir); + lfs_soff_t res = lfs_dir_rawtell(lfs, dir); LFS_TRACE("lfs_dir_tell -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); @@ -5321,7 +5321,7 @@ int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir) { } LFS_TRACE("lfs_dir_rewind(%p, %p)", (void*)lfs, (void*)dir); - err = lfs_dir_rewindraw(lfs, dir); + err = lfs_dir_rawrewind(lfs, dir); LFS_TRACE("lfs_dir_rewind -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5335,7 +5335,7 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs) { } LFS_TRACE("lfs_fs_size(%p)", (void*)lfs); - lfs_ssize_t res = lfs_fs_sizeraw(lfs); + lfs_ssize_t res = lfs_fs_rawsize(lfs); LFS_TRACE("lfs_fs_size -> %"PRId32, res); LFS_UNLOCK(lfs->cfg); @@ -5350,7 +5350,7 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) { LFS_TRACE("lfs_fs_traverse(%p, %p, %p)", (void*)lfs, (void*)(uintptr_t)cb, data); - err = lfs_fs_traverseraw(lfs, cb, data, true); + err = lfs_fs_rawtraverse(lfs, cb, data, true); LFS_TRACE("lfs_fs_traverse -> %d", err); LFS_UNLOCK(lfs->cfg); @@ -5380,7 +5380,7 @@ int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg) { cfg->read_buffer, cfg->prog_buffer, cfg->lookahead_buffer, cfg->name_max, cfg->file_max, cfg->attr_max); - err = lfs_migrateraw(lfs, cfg); + err = lfs_rawmigrate(lfs, cfg); LFS_TRACE("lfs_migrate -> %d", err); LFS_UNLOCK(cfg);