Skip to content

Commit 76d00eb

Browse files
committed
Squashed 'littlefs/' changes from 663e953..c2283a2
c2283a2 Extended entry tag to support attributes 8795f0e Added build size output to CI 47db7a7 Added sanity check for compiling example 476915f Removed a few "what"s from the documentation git-subtree-dir: littlefs git-subtree-split: c2283a2
1 parent 98af79f commit 76d00eb

File tree

5 files changed

+58
-38
lines changed

5 files changed

+58
-38
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
script:
2+
# make sure example can at least compile
3+
- sed -n '/``` c/,/```/{/```/d; p;}' README.md > test.c &&
4+
CFLAGS='
5+
-Duser_provided_block_device_read=NULL
6+
-Duser_provided_block_device_prog=NULL
7+
-Duser_provided_block_device_erase=NULL
8+
-Duser_provided_block_device_sync=NULL
9+
-include stdio.h -Werror' make all size
10+
11+
# run tests
212
- CFLAGS="-DLFS_READ_SIZE=16 -DLFS_PROG_SIZE=16" make test
313
- CFLAGS="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1" make test
414
- CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test

DESIGN.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,9 @@ v
639639
'--------' '--------'
640640
```
641641

642-
Wait, wait, wait, wait, wait, that's not atomic at all! If power is lost after
643-
removing directory B from the root, directory B is still in the linked-list.
644-
We've just created a memory leak!
642+
Wait, wait, wait, that's not atomic at all! If power is lost after removing
643+
directory B from the root, directory B is still in the linked-list. We've
644+
just created a memory leak!
645645

646646
And to be honest, I don't have a clever solution for this case. As a
647647
side-effect of using multiple pointers in the threaded tree, the littlefs
@@ -969,5 +969,5 @@ So, to summarize:
969969
10. Any case where an atomic operation is not possible, it is taken care of
970970
by a deorphan step that occurs on the first allocation after boot
971971

972-
Welp, that's the little filesystem. Thanks for reading!
972+
That's the little filesystem. Thanks for reading!
973973

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ int main(void) {
7777

7878
// update boot count
7979
boot_count += 1;
80-
printf("boot_count: %ld\n", boot_count);
8180
lfs_file_rewind(&lfs, &file);
8281
lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
8382

@@ -86,6 +85,9 @@ int main(void) {
8685

8786
// release any resources we were using
8887
lfs_unmount(&lfs);
88+
89+
// print the boot count
90+
printf("boot_count: %d\n", boot_count);
8991
}
9092
```
9193

lfs.c

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -560,20 +560,20 @@ static int lfs_dir_update(lfs_t *lfs, lfs_dir_t *dir,
560560
const lfs_entry_t *entry, const void *data) {
561561
return lfs_dir_commit(lfs, dir, (struct lfs_region[]){
562562
{entry->off, sizeof(entry->d), &entry->d, sizeof(entry->d)},
563-
{entry->off+sizeof(entry->d), entry->d.len-sizeof(entry->d),
564-
data, entry->d.len-sizeof(entry->d)}
563+
{entry->off+sizeof(entry->d), entry->d.nlen, data, entry->d.nlen}
565564
}, data ? 2 : 1);
566565
}
567566

568567
static int lfs_dir_append(lfs_t *lfs, lfs_dir_t *dir,
569568
lfs_entry_t *entry, const void *data) {
570569
// check if we fit, if top bit is set we do not and move on
571570
while (true) {
572-
if (dir->d.size + entry->d.len <= lfs->cfg->block_size) {
571+
if (dir->d.size + 4+entry->d.elen+entry->d.alen+entry->d.nlen
572+
<= lfs->cfg->block_size) {
573573
entry->off = dir->d.size - 4;
574574
return lfs_dir_commit(lfs, dir, (struct lfs_region[]){
575575
{entry->off, 0, &entry->d, sizeof(entry->d)},
576-
{entry->off, 0, data, entry->d.len - sizeof(entry->d)}
576+
{entry->off, 0, data, entry->d.nlen}
577577
}, 2);
578578
}
579579

@@ -590,7 +590,7 @@ static int lfs_dir_append(lfs_t *lfs, lfs_dir_t *dir,
590590
entry->off = newdir.d.size - 4;
591591
err = lfs_dir_commit(lfs, &newdir, (struct lfs_region[]){
592592
{entry->off, 0, &entry->d, sizeof(entry->d)},
593-
{entry->off, 0, data, entry->d.len - sizeof(entry->d)}
593+
{entry->off, 0, data, entry->d.nlen}
594594
}, 2);
595595
if (err) {
596596
return err;
@@ -620,7 +620,8 @@ static int lfs_dir_remove(lfs_t *lfs, lfs_dir_t *dir, lfs_entry_t *entry) {
620620

621621
if (!(pdir.d.size & 0x80000000)) {
622622
return lfs_dir_commit(lfs, dir, (struct lfs_region[]){
623-
{entry->off, entry->d.len, NULL, 0},
623+
{entry->off, 4+entry->d.elen+entry->d.alen+entry->d.nlen,
624+
NULL, 0},
624625
}, 1);
625626
} else {
626627
pdir.d.tail[0] = dir->d.tail[0];
@@ -629,7 +630,8 @@ static int lfs_dir_remove(lfs_t *lfs, lfs_dir_t *dir, lfs_entry_t *entry) {
629630
}
630631
} else {
631632
return lfs_dir_commit(lfs, dir, (struct lfs_region[]){
632-
{entry->off, entry->d.len, NULL, 0},
633+
{entry->off, 4+entry->d.elen+entry->d.alen+entry->d.nlen,
634+
NULL, 0},
633635
}, 1);
634636
}
635637
}
@@ -656,9 +658,9 @@ static int lfs_dir_next(lfs_t *lfs, lfs_dir_t *dir, lfs_entry_t *entry) {
656658
return err;
657659
}
658660

659-
dir->off += entry->d.len;
660-
dir->pos += entry->d.len;
661-
entry->off = dir->off - entry->d.len;
661+
entry->off = dir->off;
662+
dir->off += 4+entry->d.elen+entry->d.alen+entry->d.nlen;
663+
dir->pos += 4+entry->d.elen+entry->d.alen+entry->d.nlen;
662664
return 0;
663665
}
664666

@@ -713,12 +715,13 @@ static int lfs_dir_find(lfs_t *lfs, lfs_dir_t *dir,
713715

714716
if ((entry->d.type != LFS_TYPE_REG &&
715717
entry->d.type != LFS_TYPE_DIR) ||
716-
entry->d.name != pathlen) {
718+
entry->d.nlen != pathlen) {
717719
continue;
718720
}
719721

720722
int res = lfs_bd_cmp(lfs, dir->pair[0],
721-
entry->off + sizeof(entry->d), pathname, pathlen);
723+
entry->off + 4+entry->d.elen+entry->d.alen,
724+
pathname, pathlen);
722725
if (res < 0) {
723726
return res;
724727
}
@@ -784,8 +787,9 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
784787
}
785788

786789
entry.d.type = LFS_TYPE_DIR;
787-
entry.d.name = strlen(path);
788-
entry.d.len = sizeof(entry.d) + entry.d.name;
790+
entry.d.elen = sizeof(entry.d) - 4;
791+
entry.d.alen = 0;
792+
entry.d.nlen = strlen(path);
789793
entry.d.u.dir[0] = dir.pair[0];
790794
entry.d.u.dir[1] = dir.pair[1];
791795

@@ -880,8 +884,9 @@ int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) {
880884
info->size = entry.d.u.file.size;
881885
}
882886

883-
int err = lfs_bd_read(lfs, dir->pair[0], entry.off + sizeof(entry.d),
884-
info->name, entry.d.name);
887+
int err = lfs_bd_read(lfs, dir->pair[0],
888+
entry.off + 4+entry.d.elen+entry.d.alen,
889+
info->name, entry.d.nlen);
885890
if (err) {
886891
return err;
887892
}
@@ -1117,8 +1122,9 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
11171122

11181123
// create entry to remember name
11191124
entry.d.type = LFS_TYPE_REG;
1120-
entry.d.name = strlen(path);
1121-
entry.d.len = sizeof(entry.d) + entry.d.name;
1125+
entry.d.elen = sizeof(entry.d) - 4;
1126+
entry.d.alen = 0;
1127+
entry.d.nlen = strlen(path);
11221128
entry.d.u.file.head = -1;
11231129
entry.d.u.file.size = 0;
11241130
err = lfs_dir_append(lfs, &cwd, &entry, path);
@@ -1537,8 +1543,9 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
15371543
info->size = entry.d.u.file.size;
15381544
}
15391545

1540-
err = lfs_bd_read(lfs, cwd.pair[0], entry.off + sizeof(entry.d),
1541-
info->name, entry.d.name);
1546+
err = lfs_bd_read(lfs, cwd.pair[0],
1547+
entry.off + 4+entry.d.elen+entry.d.alen,
1548+
info->name, entry.d.nlen);
15421549
if (err) {
15431550
return err;
15441551
}
@@ -1585,7 +1592,7 @@ int lfs_remove(lfs_t *lfs, const char *path) {
15851592
f->pair[0] = 0xffffffff;
15861593
f->pair[1] = 0xffffffff;
15871594
} else if (f->poff > entry.off) {
1588-
f->poff -= entry.d.len;
1595+
f->poff -= 4 + entry.d.elen + entry.d.alen + entry.d.nlen;
15891596
}
15901597
}
15911598
}
@@ -1651,8 +1658,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
16511658
// move to new location
16521659
lfs_entry_t newentry = preventry;
16531660
newentry.d = oldentry.d;
1654-
newentry.d.name = strlen(newpath);
1655-
newentry.d.len = sizeof(newentry.d) + newentry.d.name;
1661+
newentry.d.nlen = strlen(newpath);
16561662

16571663
if (prevexists) {
16581664
int err = lfs_dir_update(lfs, &newcwd, &newentry, newpath);
@@ -1690,7 +1696,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
16901696
f->pair[0] = 0xffffffff;
16911697
f->pair[1] = 0xffffffff;
16921698
} else if (f->poff > oldentry.off) {
1693-
f->poff -= oldentry.d.len;
1699+
f->poff -= 4+oldentry.d.elen+oldentry.d.alen+oldentry.d.nlen;
16941700
}
16951701
}
16961702
}
@@ -1809,8 +1815,8 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
18091815
lfs_superblock_t superblock = {
18101816
.off = sizeof(superdir.d),
18111817
.d.type = LFS_TYPE_SUPERBLOCK,
1812-
.d.name = sizeof(superblock.d.magic),
1813-
.d.len = sizeof(superblock.d),
1818+
.d.elen = sizeof(superblock.d) - sizeof(superblock.d.magic) - 4,
1819+
.d.nlen = sizeof(superblock.d.magic),
18141820
.d.version = 0x00010001,
18151821
.d.magic = {"littlefs"},
18161822
.d.block_size = lfs->cfg->block_size,
@@ -1865,8 +1871,8 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
18651871
lfs_superblock_t superblock;
18661872
err = lfs_dir_fetch(lfs, &dir, (const lfs_block_t[2]){0, 1});
18671873
if (!err) {
1868-
err = lfs_bd_read(lfs, dir.pair[0],
1869-
sizeof(dir.d), &superblock.d, sizeof(superblock.d));
1874+
err = lfs_bd_read(lfs, dir.pair[0], sizeof(dir.d),
1875+
&superblock.d, sizeof(superblock.d));
18701876

18711877
lfs->root[0] = superblock.d.root[0];
18721878
lfs->root[1] = superblock.d.root[1];
@@ -1925,7 +1931,7 @@ int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data) {
19251931
return err;
19261932
}
19271933

1928-
dir.off += entry.d.len;
1934+
dir.off += 4+entry.d.elen+entry.d.alen+entry.d.nlen;
19291935
if ((0xf & entry.d.type) == (0xf & LFS_TYPE_REG)) {
19301936
int err = lfs_index_traverse(lfs, &lfs->rcache, NULL,
19311937
entry.d.u.file.head, entry.d.u.file.size, cb, data);

lfs.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ typedef struct lfs_entry {
160160

161161
struct lfs_disk_entry {
162162
uint8_t type;
163-
uint8_t name;
164-
uint16_t len;
163+
uint8_t elen;
164+
uint8_t alen;
165+
uint8_t nlen;
165166
union {
166167
struct {
167168
lfs_block_t head;
@@ -212,8 +213,9 @@ typedef struct lfs_superblock {
212213

213214
struct lfs_disk_superblock {
214215
uint8_t type;
215-
uint8_t name;
216-
uint16_t len;
216+
uint8_t elen;
217+
uint8_t alen;
218+
uint8_t nlen;
217219
lfs_block_t root[2];
218220
uint32_t block_size;
219221
uint32_t block_count;

0 commit comments

Comments
 (0)