Added LFS_T_RDONLY, LFS_T_RDWR, etc

These mimic the relevant LFS_O_* flags, and allow users to assert
whether or not a traversal will mutate the filesystem:

  LFS_T_MODE          0x00000001  The traversal's access mode
  LFS_T_RDWR          0x00000000  Open traversal as read and write
  LFS_T_RDONLY        0x00000001  Open traversal as read only

In theory, these could also change internal allocations, but littlefs
doesn't really work that way.

Note we _don't_ add related LFS_GC_RDONLY, LFS_GC_RDWR, etc flags. These
are sort of implied by the relevant LFS_M_* flags.

Adds a bit more code, probably because of the slightly more complicated
internal constants for the internal traversals. But I think the
self-documentingness is worth it:

           code          stack          ctx
  before: 37200           2288          636
  after:  37220 (+0.1%)   2288 (+0.0%)  636 (+0.0%)
This commit is contained in:
Christopher Haster
2025-05-24 22:57:35 -05:00
parent 5b74aafa17
commit f7e17c8aad
9 changed files with 249 additions and 150 deletions
+15 -8
View File
@@ -3490,7 +3490,8 @@ code = '''
lfsr_traversal_t t;
lfsr_traversal_init(&t,
LFS_T_MTREEONLY
LFS_T_RDONLY
| LFS_T_MTREEONLY
| ((CKMETA) ? LFS_T_CKMETA : 0));
for (lfs_block_t i = 0;; i++) {
// a bit hacky, but this catches infinite loops
@@ -3607,7 +3608,8 @@ code = '''
lfsr_traversal_t t;
lfsr_traversal_init(&t,
LFS_T_MTREEONLY
LFS_T_RDONLY
| LFS_T_MTREEONLY
| ((CKMETA) ? LFS_T_CKMETA : 0));
for (lfs_block_t i = 0;; i++) {
// a bit hacky, but this catches infinite loops
@@ -3750,7 +3752,8 @@ code = '''
lfsr_traversal_t t;
lfsr_traversal_init(&t,
LFS_T_MTREEONLY
LFS_T_RDONLY
| LFS_T_MTREEONLY
| ((CKMETA) ? LFS_T_CKMETA : 0));
for (lfs_block_t i = 0;; i++) {
// a bit hacky, but this catches infinite loops
@@ -3916,7 +3919,8 @@ code = '''
lfsr_traversal_t t;
lfsr_traversal_init(&t,
LFS_T_MTREEONLY
LFS_T_RDONLY
| LFS_T_MTREEONLY
| ((CKMETA) ? LFS_T_CKMETA : 0));
for (lfs_block_t i = 0;; i++) {
// a bit hacky, but this catches infinite loops
@@ -4060,7 +4064,8 @@ code = '''
lfsr_traversal_t t;
lfsr_traversal_init(&t,
LFS_T_MTREEONLY
LFS_T_RDONLY
| LFS_T_MTREEONLY
| ((CKMETA) ? LFS_T_CKMETA : 0));
for (lfs_block_t i = 0;; i++) {
// a bit hacky, but this catches infinite loops
@@ -4182,7 +4187,8 @@ code = '''
lfsr_traversal_t t;
lfsr_traversal_init(&t,
LFS_T_MTREEONLY
LFS_T_RDONLY
| LFS_T_MTREEONLY
| ((CKMETA) ? LFS_T_CKMETA : 0));
for (lfs_block_t i = 0;; i++) {
// a bit hacky, but this catches infinite loops
@@ -4346,7 +4352,8 @@ code = '''
lfsr_traversal_t t;
lfsr_traversal_init(&t,
LFS_T_MTREEONLY
LFS_T_RDONLY
| LFS_T_MTREEONLY
| ((CKMETA) ? LFS_T_CKMETA : 0));
for (lfs_block_t i = 0;; i++) {
// a bit hacky, but this catches infinite loops
@@ -4474,7 +4481,7 @@ code = '''
// technically, cycle detection only needs to work when we're validating
lfsr_traversal_t t;
lfsr_traversal_init(&t,
LFS_T_MTREEONLY | LFS_T_CKMETA);
LFS_T_RDONLY | LFS_T_MTREEONLY | LFS_T_CKMETA);
for (lfs_block_t i = 0;; i++) {
// assert that we detect the cycle in a reasonable number of iterations
assert(i < 2*BLOCK_COUNT);