forked from Imagelibrary/littlefs
Compare commits
26 Commits
rbyd-devel
...
fix-dir-se
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6773e68bf | ||
|
|
92298c749d | ||
|
|
50b394ca36 | ||
|
|
a99574cd5b | ||
|
|
363a8b56cf | ||
|
|
e43d381135 | ||
|
|
ee6a51bbbe | ||
|
|
01ac033d47 | ||
|
|
2a18e03cb8 | ||
|
|
6f074ebe31 | ||
|
|
0a7eca0bd5 | ||
|
|
3e25dfc16c | ||
|
|
9e28c75482 | ||
|
|
4c9360020e | ||
|
|
ca0da3d490 | ||
|
|
116332d3f7 | ||
|
|
f0cc1db793 | ||
|
|
bf045dd13c | ||
|
|
b33a5b3f85 | ||
|
|
384a498762 | ||
|
|
b0a4a44e5b | ||
|
|
aae897ffd0 | ||
|
|
24795e6b74 | ||
|
|
1278ec1d08 | ||
|
|
d9333ecbd4 | ||
|
|
3ae87f4e29 |
36
.github/workflows/test.yml
vendored
36
.github/workflows/test.yml
vendored
@@ -473,6 +473,42 @@ jobs:
|
||||
path: status
|
||||
retention-days: 1
|
||||
|
||||
# run compatibility tests using the current master as the previous version
|
||||
test-compat:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
if: ${{github.event_name == 'pull_request'}}
|
||||
# checkout the current pr target into lfsp
|
||||
- uses: actions/checkout@v2
|
||||
if: ${{github.event_name == 'pull_request'}}
|
||||
with:
|
||||
ref: ${{github.event.pull_request.base.ref}}
|
||||
path: lfsp
|
||||
- name: install
|
||||
if: ${{github.event_name == 'pull_request'}}
|
||||
run: |
|
||||
# need a few things
|
||||
sudo apt-get update -qq
|
||||
sudo apt-get install -qq gcc python3 python3-pip
|
||||
pip3 install toml
|
||||
gcc --version
|
||||
python3 --version
|
||||
# adjust prefix of lfsp
|
||||
- name: changeprefix
|
||||
if: ${{github.event_name == 'pull_request'}}
|
||||
run: |
|
||||
./scripts/changeprefix.py lfs lfsp lfsp/*.h lfsp/*.c
|
||||
- name: test-compat
|
||||
if: ${{github.event_name == 'pull_request'}}
|
||||
run: |
|
||||
TESTS=tests/test_compat.toml \
|
||||
SRC="$(find . lfsp -name '*.c' -maxdepth 1 \
|
||||
-and -not -name '*.t.*' \
|
||||
-and -not -name '*.b.*')" \
|
||||
CFLAGS="-DLFSP=lfsp/lfsp.h" \
|
||||
make test
|
||||
|
||||
# self-host with littlefs-fuse for a fuzz-like test
|
||||
fuse:
|
||||
runs-on: ubuntu-22.04
|
||||
|
||||
27
Makefile
27
Makefile
@@ -1,15 +1,5 @@
|
||||
ifdef BUILDDIR
|
||||
# bit of a hack, but we want to make sure BUILDDIR directory structure
|
||||
# is correct before any commands
|
||||
$(if $(findstring n,$(MAKEFLAGS)),, $(shell mkdir -p \
|
||||
$(BUILDDIR)/ \
|
||||
$(BUILDDIR)/bd \
|
||||
$(BUILDDIR)/runners \
|
||||
$(BUILDDIR)/tests \
|
||||
$(BUILDDIR)/benches))
|
||||
endif
|
||||
# overrideable build dir, default is in-place
|
||||
BUILDDIR ?= .
|
||||
|
||||
# overridable target/src/tools/flags/etc
|
||||
ifneq ($(wildcard test.c main.c),)
|
||||
TARGET ?= $(BUILDDIR)/lfs
|
||||
@@ -163,6 +153,18 @@ TESTFLAGS += --perf-path="$(PERF)"
|
||||
BENCHFLAGS += --perf-path="$(PERF)"
|
||||
endif
|
||||
|
||||
# this is a bit of a hack, but we want to make sure the BUILDDIR
|
||||
# directory structure is correct before we run any commands
|
||||
ifneq ($(BUILDDIR),.)
|
||||
$(if $(findstring n,$(MAKEFLAGS)),, $(shell mkdir -p \
|
||||
$(addprefix $(BUILDDIR)/,$(dir \
|
||||
$(SRC) \
|
||||
$(TESTS) \
|
||||
$(TEST_SRC) \
|
||||
$(BENCHES) \
|
||||
$(BENCH_SRC)))))
|
||||
endif
|
||||
|
||||
|
||||
# commands
|
||||
|
||||
@@ -514,6 +516,9 @@ $(BUILDDIR)/runners/bench_runner: $(BENCH_OBJ)
|
||||
$(BUILDDIR)/%.o $(BUILDDIR)/%.ci: %.c
|
||||
$(CC) -c -MMD $(CFLAGS) $< -o $(BUILDDIR)/$*.o
|
||||
|
||||
$(BUILDDIR)/%.o $(BUILDDIR)/%.ci: $(BUILDDIR)/%.c
|
||||
$(CC) -c -MMD $(CFLAGS) $< -o $(BUILDDIR)/$*.o
|
||||
|
||||
$(BUILDDIR)/%.s: %.c
|
||||
$(CC) -S $(CFLAGS) $< -o $@
|
||||
|
||||
|
||||
13
README.md
13
README.md
@@ -226,6 +226,13 @@ License Identifiers that are here available: http://spdx.org/licenses/
|
||||
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.
|
||||
|
||||
- [littlefs2-rust] - A Rust wrapper for littlefs. This project allows you
|
||||
to use littlefs in a Rust-friendly API, reaping the benefits of Rust's memory
|
||||
safety and other guarantees.
|
||||
|
||||
- [littlefs-disk-img-viewer] - A memory-efficient web application for viewing
|
||||
littlefs disk images in your web browser.
|
||||
|
||||
- [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.
|
||||
@@ -243,8 +250,12 @@ License Identifiers that are here available: http://spdx.org/licenses/
|
||||
MCUs. It offers static wear-leveling and power-resilience with only a fixed
|
||||
_O(|address|)_ pointer structure stored on each block and in RAM.
|
||||
|
||||
- [chamelon] - A pure-OCaml implementation of (most of) littlefs, designed for
|
||||
use with the MirageOS library operating system project. It is interoperable
|
||||
with the reference implementation, with some caveats.
|
||||
|
||||
[BSD-3-Clause]: https://spdx.org/licenses/BSD-3-Clause.html
|
||||
[littlefs-disk-img-viewer]: https://github.com/tniessen/littlefs-disk-img-viewer
|
||||
[littlefs-fuse]: https://github.com/geky/littlefs-fuse
|
||||
[FUSE]: https://github.com/libfuse/libfuse
|
||||
[littlefs-js]: https://github.com/geky/littlefs-js
|
||||
@@ -256,3 +267,5 @@ License Identifiers that are here available: http://spdx.org/licenses/
|
||||
[SPIFFS]: https://github.com/pellepl/spiffs
|
||||
[Dhara]: https://github.com/dlbeer/dhara
|
||||
[littlefs-python]: https://pypi.org/project/littlefs-python/
|
||||
[littlefs2-rust]: https://crates.io/crates/littlefs2
|
||||
[chamelon]: https://github.com/yomimono/chamelon
|
||||
|
||||
101
SPEC.md
101
SPEC.md
@@ -1,10 +1,10 @@
|
||||
## littlefs technical specification
|
||||
|
||||
This is the technical specification of the little filesystem. This document
|
||||
covers the technical details of how the littlefs is stored on disk for
|
||||
introspection and tooling. This document assumes you are familiar with the
|
||||
design of the littlefs, for more info on how littlefs works check
|
||||
out [DESIGN.md](DESIGN.md).
|
||||
This is the technical specification of the little filesystem with on-disk
|
||||
version lfs2.1. This document covers the technical details of how the littlefs
|
||||
is stored on disk for introspection and tooling. This document assumes you are
|
||||
familiar with the design of the littlefs, for more info on how littlefs works
|
||||
check out [DESIGN.md](DESIGN.md).
|
||||
|
||||
```
|
||||
| | | .---._____
|
||||
@@ -133,12 +133,6 @@ tags XORed together, starting with `0xffffffff`.
|
||||
'-------------------' '-------------------'
|
||||
```
|
||||
|
||||
One last thing to note before we get into the details around tag encoding. Each
|
||||
tag contains a valid bit used to indicate if the tag and containing commit is
|
||||
valid. This valid bit is the first bit found in the tag and the commit and can
|
||||
be used to tell if we've attempted to write to the remaining space in the
|
||||
block.
|
||||
|
||||
Here's a more complete example of metadata block containing 4 entries:
|
||||
|
||||
```
|
||||
@@ -191,6 +185,53 @@ Here's a more complete example of metadata block containing 4 entries:
|
||||
'---- most recent D
|
||||
```
|
||||
|
||||
Two things to note before we get into the details around tag encoding:
|
||||
|
||||
1. Each tag contains a valid bit used to indicate if the tag and containing
|
||||
commit is valid. After XORing, this bit should always be zero.
|
||||
|
||||
At the end of each commit, the valid bit of the previous tag is XORed
|
||||
with the lowest bit in the type field of the CRC tag. This allows
|
||||
the CRC tag to force the next commit to fail the valid bit test if it
|
||||
has not yet been written to.
|
||||
|
||||
2. The valid bit alone is not enough info to know if the next commit has been
|
||||
erased. We don't know the order bits will be programmed in a program block,
|
||||
so it's possible that the next commit had an attempted program that left the
|
||||
valid bit unchanged.
|
||||
|
||||
To ensure we only ever program erased bytes, each commit can contain an
|
||||
optional forward-CRC (FCRC). An FCRC contains a checksum of some amount of
|
||||
bytes in the next commit at the time it was erased.
|
||||
|
||||
```
|
||||
.-------------------. \ \
|
||||
| revision count | | |
|
||||
|-------------------| | |
|
||||
| metadata | | |
|
||||
| | +---. +-- current commit
|
||||
| | | | |
|
||||
|-------------------| | | |
|
||||
| FCRC ---|-. | |
|
||||
|-------------------| / | | |
|
||||
| CRC -----|-' /
|
||||
|-------------------| |
|
||||
| padding | | padding (does't need CRC)
|
||||
| | |
|
||||
|-------------------| \ | \
|
||||
| erased? | +-' |
|
||||
| | | | +-- next commit
|
||||
| v | / |
|
||||
| | /
|
||||
| |
|
||||
'-------------------'
|
||||
```
|
||||
|
||||
If the FCRC is missing or the checksum does not match, we must assume a
|
||||
commit was attempted but failed due to power-loss.
|
||||
|
||||
Note that end-of-block commits do not need an FCRC.
|
||||
|
||||
## Metadata tags
|
||||
|
||||
So in littlefs, 32-bit tags describe every type of metadata. And this means
|
||||
@@ -785,3 +826,41 @@ CRC fields:
|
||||
are made about the contents.
|
||||
|
||||
---
|
||||
#### `0x5ff` LFS_TYPE_FCRC
|
||||
|
||||
Added in lfs2.1, the optional FCRC tag contains a checksum of some amount of
|
||||
bytes in the next commit at the time it was erased. This allows us to ensure
|
||||
that we only ever program erased bytes, even if a previous commit failed due
|
||||
to power-loss.
|
||||
|
||||
When programming a commit, the FCRC size must be at least as large as the
|
||||
program block size. However, the program block is not saved on disk, and can
|
||||
change between mounts, so the FCRC size on disk may be different than the
|
||||
current program block size.
|
||||
|
||||
If the FCRC is missing or the checksum does not match, we must assume a
|
||||
commit was attempted but failed due to power-loss.
|
||||
|
||||
Layout of the FCRC tag:
|
||||
|
||||
```
|
||||
tag data
|
||||
[-- 32 --][-- 32 --|-- 32 --]
|
||||
[1|- 11 -| 10 | 10 ][-- 32 --|-- 32 --]
|
||||
^ ^ ^ ^ ^- fcrc size ^- fcrc
|
||||
| | | '- size (8)
|
||||
| | '------ id (0x3ff)
|
||||
| '------------ type (0x5ff)
|
||||
'----------------- valid bit
|
||||
```
|
||||
|
||||
FCRC fields:
|
||||
|
||||
1. **FCRC size (32-bits)** - Number of bytes after this commit's CRC tag's
|
||||
padding to include in the FCRC.
|
||||
|
||||
2. **FCRC (32-bits)** - CRC of the bytes after this commit's CRC tag's padding
|
||||
when erased. Like the CRC tag, this uses a CRC-32 with a polynomial of
|
||||
`0x04c11db7` initialized with `0xffffffff`.
|
||||
|
||||
---
|
||||
|
||||
178
lfs.c
178
lfs.c
@@ -300,14 +300,12 @@ static inline int lfs_pair_cmp(
|
||||
paira[0] == pairb[1] || paira[1] == pairb[0]);
|
||||
}
|
||||
|
||||
#ifndef LFS_READONLY
|
||||
static inline bool lfs_pair_sync(
|
||||
static inline bool lfs_pair_issync(
|
||||
const lfs_block_t paira[2],
|
||||
const lfs_block_t pairb[2]) {
|
||||
return (paira[0] == pairb[0] && paira[1] == pairb[1]) ||
|
||||
(paira[0] == pairb[1] && paira[1] == pairb[0]);
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline void lfs_pair_fromle32(lfs_block_t pair[2]) {
|
||||
pair[0] = lfs_fromle32(pair[0]);
|
||||
@@ -411,12 +409,16 @@ static inline bool lfs_gstate_hasorphans(const lfs_gstate_t *a) {
|
||||
}
|
||||
|
||||
static inline uint8_t lfs_gstate_getorphans(const lfs_gstate_t *a) {
|
||||
return lfs_tag_size(a->tag);
|
||||
return lfs_tag_size(a->tag) & 0x1ff;
|
||||
}
|
||||
|
||||
static inline bool lfs_gstate_hasmove(const lfs_gstate_t *a) {
|
||||
return lfs_tag_type1(a->tag);
|
||||
}
|
||||
|
||||
static inline bool lfs_gstate_needssuperblock(const lfs_gstate_t *a) {
|
||||
return lfs_tag_size(a->tag) >> 9;
|
||||
}
|
||||
#endif
|
||||
|
||||
static inline bool lfs_gstate_hasmovehere(const lfs_gstate_t *a,
|
||||
@@ -533,6 +535,7 @@ 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 int lfs_fs_deorphan(lfs_t *lfs, bool powerloss);
|
||||
static void lfs_fs_prepsuperblock(lfs_t *lfs, bool needssuperblock);
|
||||
static int 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]);
|
||||
@@ -2709,11 +2712,6 @@ static int lfs_dir_rawseek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) {
|
||||
dir->id = (off > 0 && lfs_pair_cmp(dir->head, lfs->root) == 0);
|
||||
|
||||
while (off > 0) {
|
||||
int diff = lfs_min(dir->m.count - dir->id, off);
|
||||
dir->id += diff;
|
||||
dir->pos += diff;
|
||||
off -= diff;
|
||||
|
||||
if (dir->id == dir->m.count) {
|
||||
if (!dir->m.split) {
|
||||
return LFS_ERR_INVAL;
|
||||
@@ -2726,6 +2724,11 @@ static int lfs_dir_rawseek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) {
|
||||
|
||||
dir->id = 0;
|
||||
}
|
||||
|
||||
int diff = lfs_min(dir->m.count - dir->id, off);
|
||||
dir->id += diff;
|
||||
dir->pos += diff;
|
||||
off -= diff;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -4019,6 +4022,12 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
lfs->cfg = cfg;
|
||||
int err = 0;
|
||||
|
||||
// check that bool is a truthy-preserving type
|
||||
//
|
||||
// note the most common reason for this failure is a before-c99 compiler,
|
||||
// which littlefs currently does not support
|
||||
LFS_ASSERT((bool)0x80000000);
|
||||
|
||||
// validate that the lfs-cfg sizes were initiated properly before
|
||||
// performing any arithmetic logics with them
|
||||
LFS_ASSERT(lfs->cfg->read_size != 0);
|
||||
@@ -4031,7 +4040,10 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->prog_size == 0);
|
||||
LFS_ASSERT(lfs->cfg->block_size % lfs->cfg->cache_size == 0);
|
||||
|
||||
// check that the block size is large enough to fit ctz pointers
|
||||
// check that the block size is large enough to fit all ctz pointers
|
||||
LFS_ASSERT(lfs->cfg->block_size >= 128);
|
||||
// this is the exact calculation for all ctz pointers, if this fails
|
||||
// and the simpler assert above does not, math must be broken
|
||||
LFS_ASSERT(4*lfs_npw2(0xffffffff / (lfs->cfg->block_size-2*4))
|
||||
<= lfs->cfg->block_size);
|
||||
|
||||
@@ -4215,14 +4227,23 @@ static int lfs_rawmount(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
|
||||
// scan directory blocks for superblock and any global updates
|
||||
lfs_mdir_t dir = {.tail = {0, 1}};
|
||||
lfs_block_t cycle = 0;
|
||||
lfs_block_t tortoise[2] = {LFS_BLOCK_NULL, LFS_BLOCK_NULL};
|
||||
lfs_size_t tortoise_i = 1;
|
||||
lfs_size_t tortoise_period = 1;
|
||||
while (!lfs_pair_isnull(dir.tail)) {
|
||||
if (cycle >= lfs->cfg->block_count/2) {
|
||||
// loop detected
|
||||
// detect cycles with Brent's algorithm
|
||||
if (lfs_pair_issync(dir.tail, tortoise)) {
|
||||
LFS_WARN("Cycle detected in tail list");
|
||||
err = LFS_ERR_CORRUPT;
|
||||
goto cleanup;
|
||||
}
|
||||
cycle += 1;
|
||||
if (tortoise_i == tortoise_period) {
|
||||
tortoise[0] = dir.tail[0];
|
||||
tortoise[1] = dir.tail[1];
|
||||
tortoise_i = 0;
|
||||
tortoise_period *= 2;
|
||||
}
|
||||
tortoise_i += 1;
|
||||
|
||||
// fetch next block in tail list
|
||||
lfs_stag_t tag = lfs_dir_fetchmatch(lfs, &dir, dir.tail,
|
||||
@@ -4258,12 +4279,29 @@ static int lfs_rawmount(lfs_t *lfs, const struct lfs_config *cfg) {
|
||||
uint16_t minor_version = (0xffff & (superblock.version >> 0));
|
||||
if ((major_version != LFS_DISK_VERSION_MAJOR ||
|
||||
minor_version > LFS_DISK_VERSION_MINOR)) {
|
||||
LFS_ERROR("Invalid version v%"PRIu16".%"PRIu16,
|
||||
major_version, minor_version);
|
||||
LFS_ERROR("Invalid version "
|
||||
"v%"PRIu16".%"PRIu16" != v%"PRIu16".%"PRIu16,
|
||||
major_version, minor_version,
|
||||
LFS_DISK_VERSION_MAJOR, LFS_DISK_VERSION_MINOR);
|
||||
err = LFS_ERR_INVAL;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
// found older minor version? set an in-device only bit in the
|
||||
// gstate so we know we need to rewrite the superblock before
|
||||
// the first write
|
||||
if (minor_version < LFS_DISK_VERSION_MINOR) {
|
||||
LFS_DEBUG("Found older minor version "
|
||||
"v%"PRIu16".%"PRIu16" < v%"PRIu16".%"PRIu16,
|
||||
major_version, minor_version,
|
||||
LFS_DISK_VERSION_MAJOR, LFS_DISK_VERSION_MINOR);
|
||||
#ifndef LFS_READONLY
|
||||
// note this bit is reserved on disk, so fetching more gstate
|
||||
// will not interfere here
|
||||
lfs_fs_prepsuperblock(lfs, true);
|
||||
#endif
|
||||
}
|
||||
|
||||
// check superblock configuration
|
||||
if (superblock.name_max) {
|
||||
if (superblock.name_max > lfs->name_max) {
|
||||
@@ -4373,13 +4411,22 @@ int lfs_fs_rawtraverse(lfs_t *lfs,
|
||||
}
|
||||
#endif
|
||||
|
||||
lfs_block_t cycle = 0;
|
||||
lfs_block_t tortoise[2] = {LFS_BLOCK_NULL, LFS_BLOCK_NULL};
|
||||
lfs_size_t tortoise_i = 1;
|
||||
lfs_size_t tortoise_period = 1;
|
||||
while (!lfs_pair_isnull(dir.tail)) {
|
||||
if (cycle >= lfs->cfg->block_count/2) {
|
||||
// loop detected
|
||||
// detect cycles with Brent's algorithm
|
||||
if (lfs_pair_issync(dir.tail, tortoise)) {
|
||||
LFS_WARN("Cycle detected in tail list");
|
||||
return LFS_ERR_CORRUPT;
|
||||
}
|
||||
cycle += 1;
|
||||
if (tortoise_i == tortoise_period) {
|
||||
tortoise[0] = dir.tail[0];
|
||||
tortoise[1] = dir.tail[1];
|
||||
tortoise_i = 0;
|
||||
tortoise_period *= 2;
|
||||
}
|
||||
tortoise_i += 1;
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
int err = cb(data, dir.tail[i]);
|
||||
@@ -4458,13 +4505,22 @@ static int lfs_fs_pred(lfs_t *lfs,
|
||||
// iterate over all directory directory entries
|
||||
pdir->tail[0] = 0;
|
||||
pdir->tail[1] = 1;
|
||||
lfs_block_t cycle = 0;
|
||||
lfs_block_t tortoise[2] = {LFS_BLOCK_NULL, LFS_BLOCK_NULL};
|
||||
lfs_size_t tortoise_i = 1;
|
||||
lfs_size_t tortoise_period = 1;
|
||||
while (!lfs_pair_isnull(pdir->tail)) {
|
||||
if (cycle >= lfs->cfg->block_count/2) {
|
||||
// loop detected
|
||||
// detect cycles with Brent's algorithm
|
||||
if (lfs_pair_issync(pdir->tail, tortoise)) {
|
||||
LFS_WARN("Cycle detected in tail list");
|
||||
return LFS_ERR_CORRUPT;
|
||||
}
|
||||
cycle += 1;
|
||||
if (tortoise_i == tortoise_period) {
|
||||
tortoise[0] = pdir->tail[0];
|
||||
tortoise[1] = pdir->tail[1];
|
||||
tortoise_i = 0;
|
||||
tortoise_period *= 2;
|
||||
}
|
||||
tortoise_i += 1;
|
||||
|
||||
if (lfs_pair_cmp(pdir->tail, pair) == 0) {
|
||||
return 0;
|
||||
@@ -4514,13 +4570,22 @@ static lfs_stag_t lfs_fs_parent(lfs_t *lfs, const lfs_block_t pair[2],
|
||||
// use fetchmatch with callback to find pairs
|
||||
parent->tail[0] = 0;
|
||||
parent->tail[1] = 1;
|
||||
lfs_block_t cycle = 0;
|
||||
lfs_block_t tortoise[2] = {LFS_BLOCK_NULL, LFS_BLOCK_NULL};
|
||||
lfs_size_t tortoise_i = 1;
|
||||
lfs_size_t tortoise_period = 1;
|
||||
while (!lfs_pair_isnull(parent->tail)) {
|
||||
if (cycle >= lfs->cfg->block_count/2) {
|
||||
// loop detected
|
||||
// detect cycles with Brent's algorithm
|
||||
if (lfs_pair_issync(parent->tail, tortoise)) {
|
||||
LFS_WARN("Cycle detected in tail list");
|
||||
return LFS_ERR_CORRUPT;
|
||||
}
|
||||
cycle += 1;
|
||||
if (tortoise_i == tortoise_period) {
|
||||
tortoise[0] = parent->tail[0];
|
||||
tortoise[1] = parent->tail[1];
|
||||
tortoise_i = 0;
|
||||
tortoise_period *= 2;
|
||||
}
|
||||
tortoise_i += 1;
|
||||
|
||||
lfs_stag_t tag = lfs_dir_fetchmatch(lfs, parent, parent->tail,
|
||||
LFS_MKTAG(0x7ff, 0, 0x3ff),
|
||||
@@ -4537,10 +4602,17 @@ static lfs_stag_t lfs_fs_parent(lfs_t *lfs, const lfs_block_t pair[2],
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef LFS_READONLY
|
||||
static void lfs_fs_prepsuperblock(lfs_t *lfs, bool needssuperblock) {
|
||||
lfs->gstate.tag = (lfs->gstate.tag & ~LFS_MKTAG(0, 0, 0x200))
|
||||
| (uint32_t)needssuperblock << 9;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef LFS_READONLY
|
||||
static int lfs_fs_preporphans(lfs_t *lfs, int8_t orphans) {
|
||||
LFS_ASSERT(lfs_tag_size(lfs->gstate.tag) > 0x000 || orphans >= 0);
|
||||
LFS_ASSERT(lfs_tag_size(lfs->gstate.tag) < 0x3ff || orphans <= 0);
|
||||
LFS_ASSERT(lfs_tag_size(lfs->gstate.tag) < 0x1ff || 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));
|
||||
@@ -4559,6 +4631,45 @@ static void lfs_fs_prepmove(lfs_t *lfs,
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef LFS_READONLY
|
||||
static int lfs_fs_desuperblock(lfs_t *lfs) {
|
||||
if (!lfs_gstate_needssuperblock(&lfs->gstate)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
LFS_DEBUG("Rewriting superblock {0x%"PRIx32", 0x%"PRIx32"}",
|
||||
lfs->root[0],
|
||||
lfs->root[1]);
|
||||
|
||||
lfs_mdir_t root;
|
||||
int err = lfs_dir_fetch(lfs, &root, lfs->root);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// write a new superblock
|
||||
lfs_superblock_t superblock = {
|
||||
.version = LFS_DISK_VERSION,
|
||||
.block_size = lfs->cfg->block_size,
|
||||
.block_count = lfs->cfg->block_count,
|
||||
.name_max = lfs->name_max,
|
||||
.file_max = lfs->file_max,
|
||||
.attr_max = lfs->attr_max,
|
||||
};
|
||||
|
||||
lfs_superblock_tole32(&superblock);
|
||||
err = lfs_dir_commit(lfs, &root, LFS_MKATTRS(
|
||||
{LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
|
||||
&superblock}));
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
lfs_fs_prepsuperblock(lfs, false);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef LFS_READONLY
|
||||
static int lfs_fs_demove(lfs_t *lfs) {
|
||||
if (!lfs_gstate_hasmove(&lfs->gdisk)) {
|
||||
@@ -4643,7 +4754,7 @@ static int lfs_fs_deorphan(lfs_t *lfs, bool powerloss) {
|
||||
}
|
||||
lfs_pair_fromle32(pair);
|
||||
|
||||
if (!lfs_pair_sync(pair, pdir.tail)) {
|
||||
if (!lfs_pair_issync(pair, pdir.tail)) {
|
||||
// we have desynced
|
||||
LFS_DEBUG("Fixing half-orphan "
|
||||
"{0x%"PRIx32", 0x%"PRIx32"} "
|
||||
@@ -4736,7 +4847,12 @@ static int lfs_fs_deorphan(lfs_t *lfs, bool powerloss) {
|
||||
|
||||
#ifndef LFS_READONLY
|
||||
static int lfs_fs_forceconsistency(lfs_t *lfs) {
|
||||
int err = lfs_fs_demove(lfs);
|
||||
int err = lfs_fs_desuperblock(lfs);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = lfs_fs_demove(lfs);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
4
lfs.h
4
lfs.h
@@ -21,14 +21,14 @@ extern "C"
|
||||
// Software library version
|
||||
// Major (top-nibble), incremented on backwards incompatible changes
|
||||
// Minor (bottom-nibble), incremented on feature additions
|
||||
#define LFS_VERSION 0x00020005
|
||||
#define LFS_VERSION 0x00020006
|
||||
#define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
|
||||
#define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
|
||||
|
||||
// Version of On-disk data structures
|
||||
// Major (top-nibble), incremented on backwards incompatible changes
|
||||
// Minor (bottom-nibble), incremented on feature additions
|
||||
#define LFS_DISK_VERSION 0x00020000
|
||||
#define LFS_DISK_VERSION 0x00020001
|
||||
#define LFS_DISK_VERSION_MAJOR (0xffff & (LFS_DISK_VERSION >> 16))
|
||||
#define LFS_DISK_VERSION_MINOR (0xffff & (LFS_DISK_VERSION >> 0))
|
||||
|
||||
|
||||
@@ -107,7 +107,10 @@ def main(from_prefix, to_prefix, paths=[], *,
|
||||
elif no_renames:
|
||||
to_path = from_path
|
||||
else:
|
||||
to_path, _ = changeprefix(from_prefix, to_prefix, from_path)
|
||||
to_path = os.path.join(
|
||||
os.path.dirname(from_path),
|
||||
changeprefix(from_prefix, to_prefix,
|
||||
os.path.basename(from_path))[0])
|
||||
|
||||
# rename contents
|
||||
changefile(from_prefix, to_prefix, from_path, to_path,
|
||||
|
||||
1360
tests/test_compat.toml
Normal file
1360
tests/test_compat.toml
Normal file
File diff suppressed because it is too large
Load Diff
@@ -810,7 +810,8 @@ code = '''
|
||||
}
|
||||
lfs_unmount(&lfs) => 0;
|
||||
|
||||
for (int j = 2; j < COUNT; j++) {
|
||||
// try seeking to each dir entry
|
||||
for (int j = 0; j < COUNT; j++) {
|
||||
lfs_mount(&lfs, cfg) => 0;
|
||||
lfs_dir_t dir;
|
||||
lfs_dir_open(&lfs, &dir, "hello") => 0;
|
||||
@@ -822,16 +823,15 @@ code = '''
|
||||
assert(strcmp(info.name, "..") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
|
||||
lfs_soff_t pos;
|
||||
for (int i = 0; i < j; i++) {
|
||||
char path[1024];
|
||||
sprintf(path, "kitty%03d", i);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, path) == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
pos = lfs_dir_tell(&lfs, &dir);
|
||||
assert(pos >= 0);
|
||||
}
|
||||
lfs_soff_t pos = lfs_dir_tell(&lfs, &dir);
|
||||
assert(pos >= 0);
|
||||
|
||||
lfs_dir_seek(&lfs, &dir, pos) => 0;
|
||||
char path[1024];
|
||||
@@ -861,6 +861,52 @@ code = '''
|
||||
lfs_dir_close(&lfs, &dir) => 0;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
}
|
||||
|
||||
// try seeking to end of dir
|
||||
lfs_mount(&lfs, cfg) => 0;
|
||||
lfs_dir_t dir;
|
||||
lfs_dir_open(&lfs, &dir, "hello") => 0;
|
||||
struct lfs_info info;
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, ".") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, "..") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
|
||||
for (int i = 0; i < COUNT; i++) {
|
||||
char path[1024];
|
||||
sprintf(path, "kitty%03d", i);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, path) == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
}
|
||||
lfs_soff_t pos = lfs_dir_tell(&lfs, &dir);
|
||||
assert(pos >= 0);
|
||||
|
||||
lfs_dir_read(&lfs, &dir, &info) => 0;
|
||||
|
||||
lfs_dir_seek(&lfs, &dir, pos) => 0;
|
||||
lfs_dir_read(&lfs, &dir, &info) => 0;
|
||||
|
||||
lfs_dir_rewind(&lfs, &dir) => 0;
|
||||
char path[1024];
|
||||
sprintf(path, "kitty%03d", 0);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, ".") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, "..") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, path) == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
|
||||
lfs_dir_seek(&lfs, &dir, pos) => 0;
|
||||
lfs_dir_read(&lfs, &dir, &info) => 0;
|
||||
|
||||
lfs_dir_close(&lfs, &dir) => 0;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
'''
|
||||
|
||||
[cases.test_dirs_toot_seek]
|
||||
@@ -877,7 +923,7 @@ code = '''
|
||||
}
|
||||
lfs_unmount(&lfs) => 0;
|
||||
|
||||
for (int j = 2; j < COUNT; j++) {
|
||||
for (int j = 0; j < COUNT; j++) {
|
||||
lfs_mount(&lfs, cfg) => 0;
|
||||
lfs_dir_t dir;
|
||||
lfs_dir_open(&lfs, &dir, "/") => 0;
|
||||
@@ -889,16 +935,15 @@ code = '''
|
||||
assert(strcmp(info.name, "..") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
|
||||
lfs_soff_t pos;
|
||||
for (int i = 0; i < j; i++) {
|
||||
char path[1024];
|
||||
sprintf(path, "hi%03d", i);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, path) == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
pos = lfs_dir_tell(&lfs, &dir);
|
||||
assert(pos >= 0);
|
||||
}
|
||||
lfs_soff_t pos = lfs_dir_tell(&lfs, &dir);
|
||||
assert(pos >= 0);
|
||||
|
||||
lfs_dir_seek(&lfs, &dir, pos) => 0;
|
||||
char path[1024];
|
||||
@@ -928,5 +973,51 @@ code = '''
|
||||
lfs_dir_close(&lfs, &dir) => 0;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
}
|
||||
|
||||
// try seeking to end of dir
|
||||
lfs_mount(&lfs, cfg) => 0;
|
||||
lfs_dir_t dir;
|
||||
lfs_dir_open(&lfs, &dir, "/") => 0;
|
||||
struct lfs_info info;
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, ".") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, "..") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
|
||||
for (int i = 0; i < COUNT; i++) {
|
||||
char path[1024];
|
||||
sprintf(path, "hi%03d", i);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, path) == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
}
|
||||
lfs_soff_t pos = lfs_dir_tell(&lfs, &dir);
|
||||
assert(pos >= 0);
|
||||
|
||||
lfs_dir_read(&lfs, &dir, &info) => 0;
|
||||
|
||||
lfs_dir_seek(&lfs, &dir, pos) => 0;
|
||||
lfs_dir_read(&lfs, &dir, &info) => 0;
|
||||
|
||||
lfs_dir_rewind(&lfs, &dir) => 0;
|
||||
char path[1024];
|
||||
sprintf(path, "hi%03d", 0);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, ".") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, "..") == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
lfs_dir_read(&lfs, &dir, &info) => 1;
|
||||
assert(strcmp(info.name, path) == 0);
|
||||
assert(info.type == LFS_TYPE_DIR);
|
||||
|
||||
lfs_dir_seek(&lfs, &dir, pos) => 0;
|
||||
lfs_dir_read(&lfs, &dir, &info) => 0;
|
||||
|
||||
lfs_dir_close(&lfs, &dir) => 0;
|
||||
lfs_unmount(&lfs) => 0;
|
||||
'''
|
||||
|
||||
|
||||
Reference in New Issue
Block a user