Skip to content

Commit 8a9b9ba

Browse files
committed
Modified entry head to include name length
This provides a path for adding inlined files in the future, which requires multiple lengths to distinguish between the file data and name. As an extra bonus, the directory can now be iterated over even if the types are unknown, since the name's representation is consistent on all entry types. This does come at the cost of reducing types from 16-bits to 8-bits, but I doubt this will become a problem.
1 parent 931442a commit 8a9b9ba

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

lfs.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -711,9 +711,9 @@ static int lfs_dir_find(lfs_t *lfs, lfs_dir_t *dir,
711711
return err;
712712
}
713713

714-
if (((0xff & entry->d.type) != LFS_TYPE_REG &&
715-
(0xff & entry->d.type) != LFS_TYPE_DIR) ||
716-
entry->d.len - sizeof(entry->d) != pathlen) {
714+
if ((entry->d.type != LFS_TYPE_REG &&
715+
entry->d.type != LFS_TYPE_DIR) ||
716+
entry->d.name != pathlen) {
717717
continue;
718718
}
719719

@@ -784,7 +784,8 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
784784
}
785785

786786
entry.d.type = LFS_TYPE_DIR;
787-
entry.d.len = sizeof(entry.d) + strlen(path);
787+
entry.d.name = strlen(path);
788+
entry.d.len = sizeof(entry.d) + entry.d.name;
788789
entry.d.u.dir[0] = dir.pair[0];
789790
entry.d.u.dir[1] = dir.pair[1];
790791

@@ -868,19 +869,19 @@ int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) {
868869
return (err == LFS_ERR_NOENT) ? 0 : err;
869870
}
870871

871-
if ((0xff & entry.d.type) == LFS_TYPE_REG ||
872-
(0xff & entry.d.type) == LFS_TYPE_DIR) {
872+
if (entry.d.type == LFS_TYPE_REG ||
873+
entry.d.type == LFS_TYPE_DIR) {
873874
break;
874875
}
875876
}
876877

877-
info->type = entry.d.type & 0xff;
878+
info->type = entry.d.type;
878879
if (info->type == LFS_TYPE_REG) {
879880
info->size = entry.d.u.file.size;
880881
}
881882

882883
int err = lfs_bd_read(lfs, dir->pair[0], entry.off + sizeof(entry.d),
883-
info->name, entry.d.len - sizeof(entry.d));
884+
info->name, entry.d.name);
884885
if (err) {
885886
return err;
886887
}
@@ -1116,7 +1117,8 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
11161117

11171118
// create entry to remember name
11181119
entry.d.type = LFS_TYPE_REG;
1119-
entry.d.len = sizeof(entry.d) + strlen(path);
1120+
entry.d.name = strlen(path);
1121+
entry.d.len = sizeof(entry.d) + entry.d.name;
11201122
entry.d.u.file.head = -1;
11211123
entry.d.u.file.size = 0;
11221124
err = lfs_dir_append(lfs, &cwd, &entry, path);
@@ -1530,13 +1532,13 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
15301532
}
15311533

15321534
memset(info, 0, sizeof(*info));
1533-
info->type = entry.d.type & 0xff;
1535+
info->type = entry.d.type;
15341536
if (info->type == LFS_TYPE_REG) {
15351537
info->size = entry.d.u.file.size;
15361538
}
15371539

15381540
err = lfs_bd_read(lfs, cwd.pair[0], entry.off + sizeof(entry.d),
1539-
info->name, entry.d.len - sizeof(entry.d));
1541+
info->name, entry.d.name);
15401542
if (err) {
15411543
return err;
15421544
}
@@ -1649,7 +1651,8 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
16491651
// move to new location
16501652
lfs_entry_t newentry = preventry;
16511653
newentry.d = oldentry.d;
1652-
newentry.d.len = sizeof(newentry.d) + strlen(newpath);
1654+
newentry.d.name = strlen(newpath);
1655+
newentry.d.len = sizeof(newentry.d) + newentry.d.name;
16531656

16541657
if (prevexists) {
16551658
int err = lfs_dir_update(lfs, &newcwd, &newentry, newpath);
@@ -1806,8 +1809,9 @@ int lfs_format(lfs_t *lfs, const struct lfs_config *cfg) {
18061809
lfs_superblock_t superblock = {
18071810
.off = sizeof(superdir.d),
18081811
.d.type = LFS_TYPE_SUPERBLOCK,
1812+
.d.name = sizeof(superblock.d.magic),
18091813
.d.len = sizeof(superblock.d),
1810-
.d.version = 0x00000001,
1814+
.d.version = 0x00010001,
18111815
.d.magic = {"littlefs"},
18121816
.d.block_size = lfs->cfg->block_size,
18131817
.d.block_count = lfs->cfg->block_count,
@@ -1874,7 +1878,7 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
18741878
return LFS_ERR_CORRUPT;
18751879
}
18761880

1877-
if (superblock.d.version > 0x0000ffff) {
1881+
if (superblock.d.version > (0x00010001 | 0x0000ffff)) {
18781882
LFS_ERROR("Invalid version %d.%d\n",
18791883
0xffff & (superblock.d.version >> 16),
18801884
0xffff & (superblock.d.version >> 0));
@@ -1922,7 +1926,7 @@ int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data) {
19221926
}
19231927

19241928
dir.off += entry.d.len;
1925-
if ((0xf & entry.d.type) == LFS_TYPE_REG) {
1929+
if ((0xf & entry.d.type) == (0xf & LFS_TYPE_REG)) {
19261930
int err = lfs_index_traverse(lfs, &lfs->rcache, NULL,
19271931
entry.d.u.file.head, entry.d.u.file.size, cb, data);
19281932
if (err) {
@@ -2012,7 +2016,7 @@ static int lfs_parent(lfs_t *lfs, const lfs_block_t dir[2],
20122016
break;
20132017
}
20142018

2015-
if (((0xf & entry->d.type) == LFS_TYPE_DIR) &&
2019+
if (((0xf & entry->d.type) == (0xf & LFS_TYPE_DIR)) &&
20162020
lfs_paircmp(entry->d.u.dir, dir) == 0) {
20172021
return true;
20182022
}

lfs.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ enum lfs_error {
4444

4545
// File types
4646
enum lfs_type {
47-
LFS_TYPE_REG = 0x01,
48-
LFS_TYPE_DIR = 0x02,
49-
LFS_TYPE_SUPERBLOCK = 0x12,
47+
LFS_TYPE_REG = 0x11,
48+
LFS_TYPE_DIR = 0x22,
49+
LFS_TYPE_SUPERBLOCK = 0xe2,
5050
};
5151

5252
// File open flags
@@ -159,7 +159,8 @@ typedef struct lfs_entry {
159159
lfs_off_t off;
160160

161161
struct lfs_disk_entry {
162-
uint16_t type;
162+
uint8_t type;
163+
uint8_t name;
163164
uint16_t len;
164165
union {
165166
struct {
@@ -210,13 +211,14 @@ typedef struct lfs_superblock {
210211
lfs_off_t off;
211212

212213
struct lfs_disk_superblock {
213-
uint16_t type;
214+
uint8_t type;
215+
uint8_t name;
214216
uint16_t len;
215217
lfs_block_t root[2];
216-
uint32_t version;
217-
char magic[8];
218218
uint32_t block_size;
219219
uint32_t block_count;
220+
uint32_t version;
221+
char magic[8];
220222
} d;
221223
} lfs_superblock_t;
222224

0 commit comments

Comments
 (0)