Skip to content

Commit c2283a2

Browse files
committed
Extended entry tag to support attributes
Zero attributes are actually supported at the moment, but this change will allow entry attribute to be added in a backwards compatible manner. Each dir entry is now prefixed with a 32 bit tag: 4b - entry type 4b - data structure 8b - entry len 8b - attribute len 8b - name len A full entry on disk looks a bit like this: [- 8 -|- 8 -|- 8 -|- 8 -|-- elen --|-- alen --|-- nlen --] [ type | elen | alen | nlen | entry | attrs | name ] The actually contents of the attributes section is a bit handwavey until the first attributes are implemented, but to put plans in place: Each attribute will be prefixed with only a byte that indicates the type of attribute. Attributes should be sorted based on portability, since unknown attributes will force attribute parsing to stop.
1 parent 8795f0e commit c2283a2

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

lfs.c

+35-29
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

+6-4
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)