mirror of
https://github.com/eledio-devices/thirdparty-littlefs.git
synced 2025-11-01 16:14:13 +01:00
WIP additional cleanup around attributes
This commit is contained in:
478
lfs.c
478
lfs.c
@@ -429,10 +429,8 @@ static inline bool lfs_pairsync(
|
|||||||
#define LFS_MKTAG(type, id, size) \
|
#define LFS_MKTAG(type, id, size) \
|
||||||
(((uint32_t)(type) << 22) | ((uint32_t)(id) << 12) | (uint32_t)(size))
|
(((uint32_t)(type) << 22) | ((uint32_t)(id) << 12) | (uint32_t)(size))
|
||||||
|
|
||||||
#define LFS_MKATTR(type, id, buffer_, size, next) \
|
#define LFS_MKATTR(type, id, buffer, size, next) \
|
||||||
&(lfs_mattrlist_t){ \
|
&(lfs_mattr_t){(next), LFS_MKTAG(type, id, size), (buffer)}
|
||||||
(next), \
|
|
||||||
{LFS_MKTAG(type, id, size), .u.buffer=(void*)(buffer_)}}
|
|
||||||
|
|
||||||
static inline bool lfs_tagisvalid(uint32_t tag) {
|
static inline bool lfs_tagisvalid(uint32_t tag) {
|
||||||
return !(tag & 0x80000000);
|
return !(tag & 0x80000000);
|
||||||
@@ -489,64 +487,69 @@ struct lfs_commit {
|
|||||||
} filter;
|
} filter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct lfs_diskoff {
|
||||||
|
lfs_block_t block;
|
||||||
|
lfs_off_t off;
|
||||||
|
};
|
||||||
|
|
||||||
// TODO predelcare
|
// TODO predelcare
|
||||||
static int lfs_commit_move(lfs_t *lfs, struct lfs_commit *commit,
|
static int lfs_commit_move(lfs_t *lfs, struct lfs_commit *commit,
|
||||||
uint16_t fromid, uint16_t toid,
|
uint16_t fromid, uint16_t toid,
|
||||||
lfs_mdir_t *dir, lfs_mattrlist_t *list);
|
const lfs_mdir_t *dir, const lfs_mattr_t *attrs);
|
||||||
|
|
||||||
static int lfs_commit_commit(lfs_t *lfs,
|
static int lfs_commit_attr(lfs_t *lfs, struct lfs_commit *commit,
|
||||||
struct lfs_commit *commit, lfs_mattr_t attr) {
|
uint32_t tag, const void *buffer) {
|
||||||
// filter out ids
|
// filter out ids
|
||||||
if (lfs_tagid(attr.tag) < 0x3ff && (
|
if (lfs_tagid(tag) < 0x3ff && (
|
||||||
lfs_tagid(attr.tag) < commit->filter.begin ||
|
lfs_tagid(tag) < commit->filter.begin ||
|
||||||
lfs_tagid(attr.tag) >= commit->filter.end)) {
|
lfs_tagid(tag) >= commit->filter.end)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// special cases
|
// special cases
|
||||||
if (lfs_tagtype(attr.tag) == LFS_FROM_DIR) {
|
if (lfs_tagtype(tag) == LFS_FROM_MOVE) {
|
||||||
return lfs_commit_move(lfs, commit,
|
return lfs_commit_move(lfs, commit,
|
||||||
lfs_tagsize(attr.tag), lfs_tagid(attr.tag),
|
lfs_tagsize(tag), lfs_tagid(tag),
|
||||||
attr.u.dir, NULL);
|
buffer, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lfs_tagid(attr.tag) != 0x3ff) {
|
if (lfs_tagid(tag) != 0x3ff) {
|
||||||
// TODO eh?
|
// TODO eh?
|
||||||
uint16_t id = lfs_tagid(attr.tag) - commit->filter.begin;
|
uint16_t id = lfs_tagid(tag) - commit->filter.begin;
|
||||||
attr.tag = LFS_MKTAG(0, id, 0) | (attr.tag & 0xffc00fff);
|
tag = LFS_MKTAG(0, id, 0) | (tag & 0xffc00fff);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if we fit
|
// check if we fit
|
||||||
lfs_size_t size = lfs_tagsize(attr.tag);
|
lfs_size_t size = lfs_tagsize(tag);
|
||||||
if (commit->off + sizeof(uint32_t)+size > commit->end) {
|
if (commit->off + sizeof(uint32_t)+size > commit->end) {
|
||||||
return LFS_ERR_NOSPC;
|
return LFS_ERR_NOSPC;
|
||||||
}
|
}
|
||||||
|
|
||||||
// write out tag
|
// write out tag
|
||||||
// TODO rm me
|
// TODO rm me
|
||||||
//printf("tag w %#010x (%x:%x %03x %03x %03x)\n", attr.tag, commit->block, commit->off+sizeof(lfs_tag_t), lfs_tagtype(attr.tag), lfs_tagid(attr.tag), lfs_tagsize(attr.tag));
|
//printf("tag w %#010x (%x:%x %03x %03x %03x)\n", tag, commit->block, commit->off+sizeof(lfs_tag_t), lfs_tagtype(tag), lfs_tagid(tag), lfs_tagsize(tag));
|
||||||
uint32_t tag = lfs_tole32((attr.tag & 0x7fffffff) ^ commit->ptag);
|
uint32_t ntag = lfs_tole32((tag & 0x7fffffff) ^ commit->ptag);
|
||||||
lfs_crc(&commit->crc, &tag, sizeof(tag));
|
lfs_crc(&commit->crc, &ntag, sizeof(ntag));
|
||||||
int err = lfs_bd_prog(lfs, commit->block, commit->off, &tag, sizeof(tag));
|
int err = lfs_bd_prog(lfs, commit->block, commit->off,
|
||||||
|
&ntag, sizeof(ntag));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
commit->off += sizeof(tag);
|
commit->off += sizeof(ntag);
|
||||||
|
|
||||||
if (!(attr.tag & 0x80000000)) {
|
if (!(tag & 0x80000000)) {
|
||||||
// from memory
|
// from memory
|
||||||
lfs_crc(&commit->crc, attr.u.buffer, size);
|
lfs_crc(&commit->crc, buffer, size);
|
||||||
err = lfs_bd_prog(lfs, commit->block, commit->off,
|
err = lfs_bd_prog(lfs, commit->block, commit->off, buffer, size);
|
||||||
attr.u.buffer, size);
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// from disk
|
// from disk
|
||||||
|
const struct lfs_diskoff *disk = buffer;
|
||||||
for (lfs_off_t i = 0; i < size; i++) {
|
for (lfs_off_t i = 0; i < size; i++) {
|
||||||
uint8_t dat;
|
uint8_t dat;
|
||||||
int err = lfs_bd_read(lfs,
|
int err = lfs_bd_read(lfs, disk->block, disk->off+i, &dat, 1);
|
||||||
attr.u.d.block, attr.u.d.off+i, &dat, 1);
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -559,7 +562,7 @@ static int lfs_commit_commit(lfs_t *lfs,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
commit->off += size;
|
commit->off += size;
|
||||||
commit->ptag = attr.tag & 0x7fffffff; // TODO do this once
|
commit->ptag = tag & 0x7fffffff; // TODO do this once
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -623,6 +626,13 @@ static int32_t lfs_commit_get(lfs_t *lfs, lfs_block_t block, lfs_off_t off,
|
|||||||
uint16_t id = lfs_tagid(gettag);
|
uint16_t id = lfs_tagid(gettag);
|
||||||
lfs_size_t size = lfs_tagsize(gettag);
|
lfs_size_t size = lfs_tagsize(gettag);
|
||||||
|
|
||||||
|
// synthetic moves
|
||||||
|
if ((block == lfs->globals.move.pair[0] ||
|
||||||
|
block == lfs->globals.move.pair[1])
|
||||||
|
&& lfs_tagid(gettag) <= lfs->globals.move.id) {
|
||||||
|
gettag += LFS_MKTAG(0, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
// iterate over dir block backwards (for faster lookups)
|
// iterate over dir block backwards (for faster lookups)
|
||||||
off += sizeof(uint32_t); // TODO nah
|
off += sizeof(uint32_t); // TODO nah
|
||||||
uint32_t tag = etag;
|
uint32_t tag = etag;
|
||||||
@@ -668,23 +678,27 @@ static int32_t lfs_dir_get(lfs_t *lfs, lfs_mdir_t *dir,
|
|||||||
|
|
||||||
static int lfs_commit_move(lfs_t *lfs, struct lfs_commit *commit,
|
static int lfs_commit_move(lfs_t *lfs, struct lfs_commit *commit,
|
||||||
uint16_t fromid, uint16_t toid,
|
uint16_t fromid, uint16_t toid,
|
||||||
lfs_mdir_t *dir, lfs_mattrlist_t *list) {
|
const lfs_mdir_t *dir, const lfs_mattr_t *attrs) {
|
||||||
// Iterate through list and commits, only committing unique entries
|
// Iterate through list and commits, only committing unique entries
|
||||||
lfs_off_t off = dir->off + sizeof(uint32_t);
|
lfs_off_t off = dir->off + sizeof(uint32_t);
|
||||||
uint32_t ntag = dir->etag;
|
uint32_t ntag = dir->etag;
|
||||||
|
|
||||||
while (list || off > 2*sizeof(uint32_t)) {
|
while (attrs || off > 2*sizeof(uint32_t)) {
|
||||||
lfs_mattr_t attr;
|
struct lfs_diskoff disk;
|
||||||
if (list) {
|
uint32_t tag;
|
||||||
attr = list->e;
|
const void *buffer;
|
||||||
list = list->next;
|
if (attrs) {
|
||||||
|
tag = attrs->tag;
|
||||||
|
buffer = attrs->buffer;
|
||||||
|
attrs = attrs->next;
|
||||||
} else {
|
} else {
|
||||||
LFS_ASSERT(off > 2*sizeof(ntag)+lfs_tagsize(ntag));
|
LFS_ASSERT(off > 2*sizeof(ntag)+lfs_tagsize(ntag));
|
||||||
off -= sizeof(ntag)+lfs_tagsize(ntag);
|
off -= sizeof(ntag)+lfs_tagsize(ntag);
|
||||||
|
|
||||||
attr.tag = ntag;
|
tag = ntag;
|
||||||
attr.u.d.block = dir->pair[0];
|
disk.block = dir->pair[0];
|
||||||
attr.u.d.off = off;
|
disk.off = off;
|
||||||
|
buffer = &disk;
|
||||||
|
|
||||||
int err = lfs_bd_read(lfs, dir->pair[0],
|
int err = lfs_bd_read(lfs, dir->pair[0],
|
||||||
off-sizeof(ntag), &ntag, sizeof(ntag));
|
off-sizeof(ntag), &ntag, sizeof(ntag));
|
||||||
@@ -693,36 +707,32 @@ static int lfs_commit_move(lfs_t *lfs, struct lfs_commit *commit,
|
|||||||
}
|
}
|
||||||
|
|
||||||
ntag = lfs_fromle32(ntag);
|
ntag = lfs_fromle32(ntag);
|
||||||
ntag ^= attr.tag;
|
ntag ^= tag;
|
||||||
attr.tag |= 0x80000000;
|
tag |= 0x80000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lfs_tagtype(attr.tag) == LFS_TYPE_DELETE &&
|
if (lfs_tagtype(tag) == LFS_TYPE_DELETE && lfs_tagid(tag) <= fromid) {
|
||||||
lfs_tagid(attr.tag) <= fromid) {
|
|
||||||
// something was deleted, we need to move around it
|
// something was deleted, we need to move around it
|
||||||
fromid += 1;
|
fromid += 1;
|
||||||
} else if (lfs_tagid(attr.tag) != fromid) {
|
} else if (lfs_tagid(tag) != fromid) {
|
||||||
// ignore non-matching ids
|
// ignore non-matching ids
|
||||||
} else {
|
} else {
|
||||||
// check if type has already been committed
|
// check if type has already been committed
|
||||||
int32_t res = lfs_dir_get(lfs,
|
int32_t res = lfs_commit_get(lfs, commit->block,
|
||||||
&(lfs_mdir_t){
|
commit->off, commit->ptag,
|
||||||
.pair[0]=commit->block,
|
lfs_tagisuser(tag) ? 0x7ffff000 : 0x7c3ff000,
|
||||||
.off=commit->off,
|
LFS_MKTAG(lfs_tagtype(tag), toid-commit->filter.begin, 0),
|
||||||
.etag=commit->ptag,
|
// TODO can all these filter adjustments be consolidated?
|
||||||
.stop_at_commit=true},
|
NULL, true);
|
||||||
lfs_tagisuser(attr.tag) ? 0x7ffff000 : 0x7c3ff000,
|
|
||||||
LFS_MKTAG(lfs_tagtype(attr.tag),
|
|
||||||
toid - commit->filter.begin, 0), // TODO can all these filter adjustments be consolidated?
|
|
||||||
NULL);
|
|
||||||
if (res < 0 && res != LFS_ERR_NOENT) {
|
if (res < 0 && res != LFS_ERR_NOENT) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res == LFS_ERR_NOENT) {
|
if (res == LFS_ERR_NOENT) {
|
||||||
// update id and commit, as we are currently unique
|
// update id and commit, as we are currently unique
|
||||||
attr.tag = LFS_MKTAG(0, toid, 0) | (attr.tag & 0xffc00fff);
|
int err = lfs_commit_attr(lfs, commit,
|
||||||
int err = lfs_commit_commit(lfs, commit, attr);
|
LFS_MKTAG(0, toid, 0) | (tag & 0xffc00fff),
|
||||||
|
buffer);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -741,9 +751,8 @@ static int lfs_commit_globals(lfs_t *lfs, struct lfs_commit *commit,
|
|||||||
|
|
||||||
// TODO check performance/complexity of different strategies here
|
// TODO check performance/complexity of different strategies here
|
||||||
lfs_globals_t res = lfs_globals_xor(source, diff);
|
lfs_globals_t res = lfs_globals_xor(source, diff);
|
||||||
int err = lfs_commit_commit(lfs, commit, (lfs_mattr_t){
|
int err = lfs_commit_attr(lfs, commit,
|
||||||
LFS_MKTAG(LFS_TYPE_GLOBALS, 0x3ff, sizeof(res)),
|
LFS_MKTAG(LFS_TYPE_GLOBALS, 0x3ff, sizeof(res)), &res);
|
||||||
.u.buffer=&res});
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -789,7 +798,6 @@ static int32_t lfs_dir_find(lfs_t *lfs,
|
|||||||
const void *findbuffer) {
|
const void *findbuffer) {
|
||||||
dir->pair[0] = pair[0];
|
dir->pair[0] = pair[0];
|
||||||
dir->pair[1] = pair[1];
|
dir->pair[1] = pair[1];
|
||||||
dir->stop_at_commit = false;
|
|
||||||
int32_t foundtag = LFS_ERR_NOENT;
|
int32_t foundtag = LFS_ERR_NOENT;
|
||||||
|
|
||||||
// find the block with the most recent revision
|
// find the block with the most recent revision
|
||||||
@@ -957,7 +965,7 @@ static int lfs_dir_fetch(lfs_t *lfs,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lfs_dir_compact(lfs_t *lfs, lfs_mdir_t *dir, lfs_mattrlist_t *list,
|
static int lfs_dir_compact(lfs_t *lfs, lfs_mdir_t *dir, lfs_mattr_t *attrs,
|
||||||
lfs_mdir_t *source, uint16_t begin, uint16_t end) {
|
lfs_mdir_t *source, uint16_t begin, uint16_t end) {
|
||||||
// save some state in case block is bad
|
// save some state in case block is bad
|
||||||
const lfs_block_t oldpair[2] = {dir->pair[1], dir->pair[0]};
|
const lfs_block_t oldpair[2] = {dir->pair[1], dir->pair[0]};
|
||||||
@@ -1036,7 +1044,7 @@ static int lfs_dir_compact(lfs_t *lfs, lfs_mdir_t *dir, lfs_mattrlist_t *list,
|
|||||||
|
|
||||||
// commit with a move
|
// commit with a move
|
||||||
for (uint16_t id = begin; id < end; id++) {
|
for (uint16_t id = begin; id < end; id++) {
|
||||||
err = lfs_commit_move(lfs, &commit, id, id, source, list);
|
err = lfs_commit_move(lfs, &commit, id, id, source, attrs);
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err == LFS_ERR_NOSPC) {
|
if (err == LFS_ERR_NOSPC) {
|
||||||
goto split;
|
goto split;
|
||||||
@@ -1055,10 +1063,9 @@ static int lfs_dir_compact(lfs_t *lfs, lfs_mdir_t *dir, lfs_mattrlist_t *list,
|
|||||||
if (!lfs_pairisnull(dir->tail)) {
|
if (!lfs_pairisnull(dir->tail)) {
|
||||||
// commit tail, which may be new after last size check
|
// commit tail, which may be new after last size check
|
||||||
// TODO le32
|
// TODO le32
|
||||||
err = lfs_commit_commit(lfs, &commit, (lfs_mattr_t){
|
err = lfs_commit_attr(lfs, &commit,
|
||||||
LFS_MKTAG(LFS_TYPE_TAIL + dir->split,
|
LFS_MKTAG(LFS_TYPE_TAIL + dir->split,
|
||||||
0x3ff, sizeof(dir->tail)),
|
0x3ff, sizeof(dir->tail)), dir->tail);
|
||||||
.u.buffer=dir->tail});
|
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err == LFS_ERR_CORRUPT) {
|
if (err == LFS_ERR_CORRUPT) {
|
||||||
goto relocate;
|
goto relocate;
|
||||||
@@ -1096,7 +1103,7 @@ split:
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = lfs_dir_compact(lfs, &tail, list, dir, ack+1, end);
|
err = lfs_dir_compact(lfs, &tail, attrs, dir, ack+1, end);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -1148,7 +1155,7 @@ relocate:
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, lfs_mattrlist_t *list) {
|
static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, lfs_mattr_t *attrs) {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!dir->erased) {
|
if (!dir->erased) {
|
||||||
// not erased, must compact
|
// not erased, must compact
|
||||||
@@ -1166,8 +1173,8 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, lfs_mattrlist_t *list) {
|
|||||||
.filter.end = 0x3ff,
|
.filter.end = 0x3ff,
|
||||||
};
|
};
|
||||||
|
|
||||||
for (lfs_mattrlist_t *a = list; a; a = a->next) {
|
for (lfs_mattr_t *a = attrs; a; a = a->next) {
|
||||||
int err = lfs_commit_commit(lfs, &commit, a->e);
|
int err = lfs_commit_attr(lfs, &commit, a->tag, a->buffer);
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err == LFS_ERR_NOSPC || err == LFS_ERR_CORRUPT) {
|
if (err == LFS_ERR_NOSPC || err == LFS_ERR_CORRUPT) {
|
||||||
goto compact;
|
goto compact;
|
||||||
@@ -1203,7 +1210,7 @@ static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir, lfs_mattrlist_t *list) {
|
|||||||
|
|
||||||
compact:
|
compact:
|
||||||
lfs->pcache.block = 0xffffffff;
|
lfs->pcache.block = 0xffffffff;
|
||||||
err = lfs_dir_compact(lfs, dir, list, dir, 0, dir->count);
|
err = lfs_dir_compact(lfs, dir, attrs, dir, 0, dir->count);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -1249,12 +1256,14 @@ static int lfs_dir_delete(lfs_t *lfs, lfs_mdir_t *dir, uint16_t id) {
|
|||||||
|
|
||||||
return lfs_dir_commit(lfs, &pdir,
|
return lfs_dir_commit(lfs, &pdir,
|
||||||
LFS_MKATTR(LFS_TYPE_TAIL + pdir.split, 0x3ff,
|
LFS_MKATTR(LFS_TYPE_TAIL + pdir.split, 0x3ff,
|
||||||
pdir.tail, sizeof(pdir.tail), NULL));
|
pdir.tail, sizeof(pdir.tail),
|
||||||
|
NULL));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int err = lfs_dir_commit(lfs, dir,
|
int err = lfs_dir_commit(lfs, dir,
|
||||||
LFS_MKATTR(LFS_TYPE_DELETE, id, NULL, 0, NULL));
|
LFS_MKATTR(LFS_TYPE_DELETE, id, NULL, 0,
|
||||||
|
NULL));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -1285,81 +1294,34 @@ static int lfs_dir_delete(lfs_t *lfs, lfs_mdir_t *dir, uint16_t id) {
|
|||||||
|
|
||||||
static int32_t lfs_dir_get(lfs_t *lfs, lfs_mdir_t *dir,
|
static int32_t lfs_dir_get(lfs_t *lfs, lfs_mdir_t *dir,
|
||||||
uint32_t getmask, uint32_t gettag, void *buffer) {
|
uint32_t getmask, uint32_t gettag, void *buffer) {
|
||||||
uint16_t id = lfs_tagid(gettag);
|
return lfs_commit_get(lfs, dir->pair[0], dir->off, dir->etag,
|
||||||
lfs_size_t size = lfs_tagsize(gettag);
|
getmask, gettag, buffer, false);
|
||||||
|
|
||||||
// synthetic moves
|
|
||||||
if (lfs_paircmp(dir->pair, lfs->globals.move.pair) == 0
|
|
||||||
&& lfs_tagid(gettag) <= lfs->globals.move.id) {
|
|
||||||
gettag += LFS_MKTAG(0, 1, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// iterate over dir block backwards (for faster lookups)
|
|
||||||
lfs_off_t off = dir->off + sizeof(uint32_t);
|
|
||||||
uint32_t tag = dir->etag;
|
|
||||||
|
|
||||||
while (off > 2*sizeof(tag)) {
|
|
||||||
LFS_ASSERT(off > 2*sizeof(tag)+lfs_tagsize(tag));
|
|
||||||
off -= sizeof(tag)+lfs_tagsize(tag);
|
|
||||||
|
|
||||||
if (lfs_tagtype(tag) == LFS_TYPE_CRC) {
|
|
||||||
if (dir->stop_at_commit) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else if (lfs_tagtype(tag) == LFS_TYPE_DELETE) {
|
|
||||||
if (lfs_tagid(tag) <= lfs_tagid(gettag)) {
|
|
||||||
gettag += LFS_MKTAG(0, 1, 0);
|
|
||||||
}
|
|
||||||
} else if ((tag & getmask) == (gettag & getmask)) {
|
|
||||||
if (buffer) {
|
|
||||||
lfs_size_t diff = lfs_min(size, lfs_tagsize(tag));
|
|
||||||
int err = lfs_bd_read(lfs, dir->pair[0], off, buffer, diff);
|
|
||||||
if (err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset((uint8_t*)buffer + diff, 0, size - diff);
|
|
||||||
}
|
|
||||||
|
|
||||||
return LFS_MKTAG(0, id, 0) | (tag & 0xffc00fff);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t ntag;
|
|
||||||
int err = lfs_bd_read(lfs, dir->pair[0],
|
|
||||||
off-sizeof(ntag), &ntag, sizeof(ntag));
|
|
||||||
if (err) {
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
tag ^= lfs_fromle32(ntag);
|
|
||||||
}
|
|
||||||
|
|
||||||
return LFS_ERR_NOENT;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int lfs_dir_getinfo(lfs_t *lfs, lfs_mdir_t *dir,
|
static int lfs_dir_getinfo(lfs_t *lfs, lfs_mdir_t *dir,
|
||||||
int16_t id, struct lfs_info *info) {
|
int16_t id, struct lfs_info *info) {
|
||||||
lfs_mattr_t attr;
|
int32_t tag = lfs_dir_get(lfs, dir, 0x7c3ff000,
|
||||||
attr.tag = lfs_dir_get(lfs, dir, 0x7c3ff000,
|
|
||||||
LFS_MKTAG(LFS_TYPE_NAME, id, lfs->name_size+1), info->name);
|
LFS_MKTAG(LFS_TYPE_NAME, id, lfs->name_size+1), info->name);
|
||||||
if (attr.tag < 0) {
|
if (tag < 0) {
|
||||||
return attr.tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
info->type = lfs_tagtype(attr.tag);
|
info->type = lfs_tagtype(tag);
|
||||||
if (lfs_tagsize(attr.tag) > lfs->name_size) {
|
if (lfs_tagsize(tag) > lfs->name_size) {
|
||||||
return LFS_ERR_RANGE;
|
return LFS_ERR_RANGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
attr.tag = lfs_dir_get(lfs, dir, 0x7c3ff000,
|
struct lfs_ctz ctz;
|
||||||
LFS_MKTAG(LFS_TYPE_STRUCT, id, 8), &attr.u);
|
tag = lfs_dir_get(lfs, dir, 0x7c3ff000,
|
||||||
if (attr.tag < 0) {
|
LFS_MKTAG(LFS_TYPE_STRUCT, id, sizeof(ctz)), &ctz);
|
||||||
return attr.tag;
|
if (tag < 0) {
|
||||||
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lfs_tagtype(attr.tag) == LFS_TYPE_CTZSTRUCT) {
|
if (lfs_tagtype(tag) == LFS_TYPE_CTZSTRUCT) {
|
||||||
info->size = attr.u.ctz.size;
|
info->size = ctz.size;
|
||||||
} else if (lfs_tagtype(attr.tag) == LFS_TYPE_INLINESTRUCT) {
|
} else if (lfs_tagtype(tag) == LFS_TYPE_INLINESTRUCT) {
|
||||||
info->size = lfs_tagsize(attr.tag);
|
info->size = lfs_tagsize(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -1367,11 +1329,7 @@ static int lfs_dir_getinfo(lfs_t *lfs, lfs_mdir_t *dir,
|
|||||||
|
|
||||||
|
|
||||||
static int32_t lfs_dir_lookup(lfs_t *lfs, lfs_mdir_t *dir, const char **path) {
|
static int32_t lfs_dir_lookup(lfs_t *lfs, lfs_mdir_t *dir, const char **path) {
|
||||||
lfs_mattr_t attr = {
|
lfs_block_t pair[2] = {lfs->root[0], lfs->root[1]};
|
||||||
.u.pair[0] = lfs->root[0],
|
|
||||||
.u.pair[1] = lfs->root[1],
|
|
||||||
};
|
|
||||||
|
|
||||||
const char *name = *path;
|
const char *name = *path;
|
||||||
lfs_size_t namelen;
|
lfs_size_t namelen;
|
||||||
int32_t tag;
|
int32_t tag;
|
||||||
@@ -1423,8 +1381,8 @@ static int32_t lfs_dir_lookup(lfs_t *lfs, lfs_mdir_t *dir, const char **path) {
|
|||||||
|
|
||||||
// find path
|
// find path
|
||||||
while (true) {
|
while (true) {
|
||||||
tag = lfs_dir_find(lfs, dir, attr.u.pair,
|
tag = lfs_dir_find(lfs, dir, pair, 0x7c000fff,
|
||||||
0x7c000fff, LFS_MKTAG(LFS_TYPE_NAME, 0, namelen), name);
|
LFS_MKTAG(LFS_TYPE_NAME, 0, namelen), name);
|
||||||
if (tag < 0 && tag != LFS_ERR_NOENT) {
|
if (tag < 0 && tag != LFS_ERR_NOENT) {
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
@@ -1438,8 +1396,8 @@ static int32_t lfs_dir_lookup(lfs_t *lfs, lfs_mdir_t *dir, const char **path) {
|
|||||||
return LFS_ERR_NOENT;
|
return LFS_ERR_NOENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
attr.u.pair[0] = dir->tail[0];
|
pair[0] = dir->tail[0];
|
||||||
attr.u.pair[1] = dir->tail[1];
|
pair[1] = dir->tail[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
name += namelen;
|
name += namelen;
|
||||||
@@ -1457,10 +1415,10 @@ static int32_t lfs_dir_lookup(lfs_t *lfs, lfs_mdir_t *dir, const char **path) {
|
|||||||
// TODO optimize grab for inline files and like?
|
// TODO optimize grab for inline files and like?
|
||||||
// TODO would this mean more code?
|
// TODO would this mean more code?
|
||||||
// grab the entry data
|
// grab the entry data
|
||||||
attr.tag = lfs_dir_get(lfs, dir, 0x7c3ff000,
|
int32_t res = lfs_dir_get(lfs, dir, 0x7c3ff000,
|
||||||
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tagid(tag), 8), &attr.u);
|
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tagid(tag), 8), pair);
|
||||||
if (attr.tag < 0) {
|
if (res < 0) {
|
||||||
return attr.tag;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1537,22 +1495,22 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
|
|||||||
return LFS_ERR_NOTDIR;
|
return LFS_ERR_NOTDIR;
|
||||||
}
|
}
|
||||||
|
|
||||||
lfs_mattr_t attr;
|
lfs_block_t pair[2];
|
||||||
if (lfs_tagid(tag) == 0x3ff) {
|
if (lfs_tagid(tag) == 0x3ff) {
|
||||||
// handle root dir separately
|
// handle root dir separately
|
||||||
attr.u.pair[0] = lfs->root[0];
|
pair[0] = lfs->root[0];
|
||||||
attr.u.pair[1] = lfs->root[1];
|
pair[1] = lfs->root[1];
|
||||||
} else {
|
} else {
|
||||||
// get dir pair from parent
|
// get dir pair from parent
|
||||||
attr.tag = lfs_dir_get(lfs, &dir->m, 0x7c3ff000,
|
int32_t res = lfs_dir_get(lfs, &dir->m, 0x7c3ff000,
|
||||||
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tagid(tag), 8), &attr.u);
|
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tagid(tag), 8), pair);
|
||||||
if (attr.tag < 0) {
|
if (res < 0) {
|
||||||
return attr.tag;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch first pair
|
// fetch first pair
|
||||||
int err = lfs_dir_fetch(lfs, &dir->m, attr.u.pair);
|
int err = lfs_dir_fetch(lfs, &dir->m, pair);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -1883,10 +1841,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
|||||||
if (tag < 0 && (tag != LFS_ERR_NOENT || strchr(path, '/') != NULL)) {
|
if (tag < 0 && (tag != LFS_ERR_NOENT || strchr(path, '/') != NULL)) {
|
||||||
return tag;
|
return tag;
|
||||||
}
|
}
|
||||||
uint16_t id = lfs_tagid(tag);
|
|
||||||
uint8_t type = lfs_tagtype(tag);
|
|
||||||
|
|
||||||
lfs_mattr_t attr; // TODO stop copying things (attr, id, type, tag)
|
|
||||||
if (tag == LFS_ERR_NOENT) {
|
if (tag == LFS_ERR_NOENT) {
|
||||||
if (!(flags & LFS_O_CREAT)) {
|
if (!(flags & LFS_O_CREAT)) {
|
||||||
return LFS_ERR_NOENT;
|
return LFS_ERR_NOENT;
|
||||||
@@ -1899,20 +1854,23 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get next slot and create entry to remember name
|
// get next slot and create entry to remember name
|
||||||
|
uint16_t id;
|
||||||
int err = lfs_dir_append(lfs, &cwd, &id);
|
int err = lfs_dir_append(lfs, &cwd, &id);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO do we need to make file registered to list to catch updates from this commit? ie if id/cwd change
|
// TODO do we need to make file registered to list to catch updates from this commit? ie if id/cwd change
|
||||||
|
// TODO don't use inline struct? just leave it out?
|
||||||
err = lfs_dir_commit(lfs, &cwd,
|
err = lfs_dir_commit(lfs, &cwd,
|
||||||
LFS_MKATTR(LFS_TYPE_REG, id, path, nlen,
|
LFS_MKATTR(LFS_TYPE_REG, id, path, nlen,
|
||||||
LFS_MKATTR(LFS_TYPE_INLINESTRUCT, id, NULL, 0, NULL)));
|
LFS_MKATTR(LFS_TYPE_INLINESTRUCT, id, NULL, 0,
|
||||||
|
NULL)));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO eh
|
// TODO eh AHHHHHHHHHHHHHH
|
||||||
if (id >= cwd.count) {
|
if (id >= cwd.count) {
|
||||||
// catch updates from a compact in the above commit
|
// catch updates from a compact in the above commit
|
||||||
id -= cwd.count;
|
id -= cwd.count;
|
||||||
@@ -1920,27 +1878,28 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
|||||||
cwd.pair[1] = cwd.tail[1];
|
cwd.pair[1] = cwd.tail[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
attr.tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, id, 0);
|
tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, id, 0);
|
||||||
} else {
|
|
||||||
if (type != LFS_TYPE_REG) {
|
|
||||||
return LFS_ERR_ISDIR;
|
|
||||||
} else if (flags & LFS_O_EXCL) {
|
} else if (flags & LFS_O_EXCL) {
|
||||||
return LFS_ERR_EXIST;
|
return LFS_ERR_EXIST;
|
||||||
}
|
} else if (lfs_tagtype(tag) != LFS_TYPE_REG) {
|
||||||
|
return LFS_ERR_ISDIR;
|
||||||
// TODO allow no entry?
|
} else if (flags & LFS_O_TRUNC) {
|
||||||
// TODO move this into one load? If cache >= 8 would work
|
// truncate if requested
|
||||||
attr.tag = lfs_dir_get(lfs, &cwd, 0x7c3ff000,
|
tag = LFS_MKTAG(LFS_TYPE_INLINESTRUCT, lfs_tagid(tag), 0);
|
||||||
LFS_MKTAG(LFS_TYPE_STRUCT, id, 8), &file->head);
|
flags |= LFS_F_DIRTY;
|
||||||
if (attr.tag < 0) {
|
} else {
|
||||||
return attr.tag;
|
// try to load what's on disk, if it's inlined we'll fix it later
|
||||||
|
tag = lfs_dir_get(lfs, &cwd, 0x7c3ff000,
|
||||||
|
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tagid(tag), 8), &file->ctz);
|
||||||
|
if (tag < 0) {
|
||||||
|
return tag;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup file struct
|
// setup file struct
|
||||||
file->pair[0] = cwd.pair[0];
|
file->pair[0] = cwd.pair[0];
|
||||||
file->pair[1] = cwd.pair[1];
|
file->pair[1] = cwd.pair[1];
|
||||||
file->id = id;
|
file->id = lfs_tagid(tag);
|
||||||
file->flags = flags;
|
file->flags = flags;
|
||||||
file->pos = 0;
|
file->pos = 0;
|
||||||
file->attrs = NULL;
|
file->attrs = NULL;
|
||||||
@@ -1961,39 +1920,26 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lfs_tagtype(attr.tag) == LFS_TYPE_INLINESTRUCT) {
|
if (lfs_tagtype(tag) == LFS_TYPE_INLINESTRUCT) {
|
||||||
// TODO make inline the better path?
|
|
||||||
// TODO can inline and trunc be combined?
|
|
||||||
// load inline files
|
// load inline files
|
||||||
file->head = 0xfffffffe;
|
file->ctz.head = 0xfffffffe;
|
||||||
file->size = lfs_tagsize(attr.tag);
|
file->ctz.size = lfs_tagsize(tag);
|
||||||
file->flags |= LFS_F_INLINE;
|
file->flags |= LFS_F_INLINE;
|
||||||
file->cache.block = file->head;
|
file->cache.block = file->ctz.head;
|
||||||
file->cache.off = 0;
|
file->cache.off = 0;
|
||||||
// don't always read (may be new file)
|
|
||||||
if (file->size > 0) {
|
// don't always read (may be new/trunc file)
|
||||||
int err = lfs_bd_read(lfs, attr.u.d.block, attr.u.d.off,
|
if (file->ctz.size > 0) {
|
||||||
file->cache.buffer, file->size);
|
int32_t res = lfs_dir_get(lfs, &cwd, 0x7c3ff000,
|
||||||
if (err) {
|
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tagid(tag), file->ctz.size),
|
||||||
|
file->cache.buffer);
|
||||||
|
if (res < 0) {
|
||||||
lfs_free(file->cache.buffer);
|
lfs_free(file->cache.buffer);
|
||||||
return err;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// truncate if requested
|
|
||||||
if (flags & LFS_O_TRUNC) {
|
|
||||||
if (file->size != 0) {
|
|
||||||
file->flags |= LFS_F_DIRTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
file->head = 0xfffffffe;
|
|
||||||
file->size = 0;
|
|
||||||
file->flags |= LFS_F_INLINE;
|
|
||||||
file->cache.block = 0xfffffffe;
|
|
||||||
file->cache.off = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add to list of files
|
// add to list of files
|
||||||
file->next = lfs->files;
|
file->next = lfs->files;
|
||||||
lfs->files = file;
|
lfs->files = file;
|
||||||
@@ -2077,15 +2023,15 @@ static int lfs_file_flush(lfs_t *lfs, lfs_file_t *file) {
|
|||||||
if (!(file->flags & LFS_F_INLINE)) {
|
if (!(file->flags & LFS_F_INLINE)) {
|
||||||
// copy over anything after current branch
|
// copy over anything after current branch
|
||||||
lfs_file_t orig = {
|
lfs_file_t orig = {
|
||||||
.head = file->head,
|
.ctz.head = file->ctz.head,
|
||||||
.size = file->size,
|
.ctz.size = file->ctz.size,
|
||||||
.flags = LFS_O_RDONLY,
|
.flags = LFS_O_RDONLY,
|
||||||
.pos = file->pos,
|
.pos = file->pos,
|
||||||
.cache = lfs->rcache,
|
.cache = lfs->rcache,
|
||||||
};
|
};
|
||||||
lfs->rcache.block = 0xffffffff;
|
lfs->rcache.block = 0xffffffff;
|
||||||
|
|
||||||
while (file->pos < file->size) {
|
while (file->pos < file->ctz.size) {
|
||||||
// copy over a byte at a time, leave it up to caching
|
// copy over a byte at a time, leave it up to caching
|
||||||
// to make this efficient
|
// to make this efficient
|
||||||
uint8_t data;
|
uint8_t data;
|
||||||
@@ -2125,12 +2071,12 @@ relocate:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
file->size = lfs_max(file->pos, file->size);
|
file->ctz.size = lfs_max(file->pos, file->ctz.size);
|
||||||
}
|
}
|
||||||
|
|
||||||
// actual file updates
|
// actual file updates
|
||||||
file->head = file->block;
|
file->ctz.head = file->block;
|
||||||
file->size = file->pos;
|
file->ctz.size = file->pos;
|
||||||
file->flags &= ~LFS_F_WRITING;
|
file->flags &= ~LFS_F_WRITING;
|
||||||
file->flags |= LFS_F_DIRTY;
|
file->flags |= LFS_F_DIRTY;
|
||||||
|
|
||||||
@@ -2162,14 +2108,16 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
|
|||||||
if (!(file->flags & LFS_F_INLINE)) {
|
if (!(file->flags & LFS_F_INLINE)) {
|
||||||
int err = lfs_dir_commit(lfs, &cwd,
|
int err = lfs_dir_commit(lfs, &cwd,
|
||||||
LFS_MKATTR(LFS_TYPE_CTZSTRUCT, file->id,
|
LFS_MKATTR(LFS_TYPE_CTZSTRUCT, file->id,
|
||||||
&file->head, 2*sizeof(uint32_t), file->attrs));
|
&file->ctz.head, 2*sizeof(uint32_t),
|
||||||
|
file->attrs));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
int err = lfs_dir_commit(lfs, &cwd,
|
int err = lfs_dir_commit(lfs, &cwd,
|
||||||
LFS_MKATTR(LFS_TYPE_INLINESTRUCT, file->id,
|
LFS_MKATTR(LFS_TYPE_INLINESTRUCT, file->id,
|
||||||
file->cache.buffer, file->size, file->attrs));
|
file->cache.buffer, file->ctz.size,
|
||||||
|
file->attrs));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -2198,12 +2146,12 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file->pos >= file->size) {
|
if (file->pos >= file->ctz.size) {
|
||||||
// eof if past end
|
// eof if past end
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size = lfs_min(size, file->size - file->pos);
|
size = lfs_min(size, file->ctz.size - file->pos);
|
||||||
nsize = size;
|
nsize = size;
|
||||||
|
|
||||||
while (nsize > 0) {
|
while (nsize > 0) {
|
||||||
@@ -2212,7 +2160,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
|
|||||||
file->off == lfs->cfg->block_size) {
|
file->off == lfs->cfg->block_size) {
|
||||||
if (!(file->flags & LFS_F_INLINE)) {
|
if (!(file->flags & LFS_F_INLINE)) {
|
||||||
int err = lfs_ctz_find(lfs, &file->cache, NULL,
|
int err = lfs_ctz_find(lfs, &file->cache, NULL,
|
||||||
file->head, file->size,
|
file->ctz.head, file->ctz.size,
|
||||||
file->pos, &file->block, &file->off);
|
file->pos, &file->block, &file->off);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
@@ -2259,14 +2207,14 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((file->flags & LFS_O_APPEND) && file->pos < file->size) {
|
if ((file->flags & LFS_O_APPEND) && file->pos < file->ctz.size) {
|
||||||
file->pos = file->size;
|
file->pos = file->ctz.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(file->flags & LFS_F_WRITING) && file->pos > file->size) {
|
if (!(file->flags & LFS_F_WRITING) && file->pos > file->ctz.size) {
|
||||||
// fill with zeros
|
// fill with zeros
|
||||||
lfs_off_t pos = file->pos;
|
lfs_off_t pos = file->pos;
|
||||||
file->pos = file->size;
|
file->pos = file->ctz.size;
|
||||||
|
|
||||||
while (file->pos < pos) {
|
while (file->pos < pos) {
|
||||||
lfs_ssize_t res = lfs_file_write(lfs, file, &(uint8_t){0}, 1);
|
lfs_ssize_t res = lfs_file_write(lfs, file, &(uint8_t){0}, 1);
|
||||||
@@ -2301,7 +2249,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
|
|||||||
if (!(file->flags & LFS_F_WRITING) && file->pos > 0) {
|
if (!(file->flags & LFS_F_WRITING) && file->pos > 0) {
|
||||||
// find out which block we're extending from
|
// find out which block we're extending from
|
||||||
int err = lfs_ctz_find(lfs, &file->cache, NULL,
|
int err = lfs_ctz_find(lfs, &file->cache, NULL,
|
||||||
file->head, file->size,
|
file->ctz.head, file->ctz.size,
|
||||||
file->pos-1, &file->block, &file->off);
|
file->pos-1, &file->block, &file->off);
|
||||||
if (err) {
|
if (err) {
|
||||||
file->flags |= LFS_F_ERRED;
|
file->flags |= LFS_F_ERRED;
|
||||||
@@ -2381,11 +2329,11 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
|
|||||||
|
|
||||||
file->pos = file->pos + off;
|
file->pos = file->pos + off;
|
||||||
} else if (whence == LFS_SEEK_END) {
|
} else if (whence == LFS_SEEK_END) {
|
||||||
if (off < 0 && (lfs_off_t)-off > file->size) {
|
if (off < 0 && (lfs_off_t)-off > file->ctz.size) {
|
||||||
return LFS_ERR_INVAL;
|
return LFS_ERR_INVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
file->pos = file->size + off;
|
file->pos = file->ctz.size + off;
|
||||||
}
|
}
|
||||||
|
|
||||||
return file->pos;
|
return file->pos;
|
||||||
@@ -2406,13 +2354,13 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
|
|||||||
|
|
||||||
// lookup new head in ctz skip list
|
// lookup new head in ctz skip list
|
||||||
err = lfs_ctz_find(lfs, &file->cache, NULL,
|
err = lfs_ctz_find(lfs, &file->cache, NULL,
|
||||||
file->head, file->size,
|
file->ctz.head, file->ctz.size,
|
||||||
size, &file->head, &(lfs_off_t){0});
|
size, &file->ctz.head, &(lfs_off_t){0});
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
file->size = size;
|
file->ctz.size = size;
|
||||||
file->flags |= LFS_F_DIRTY;
|
file->flags |= LFS_F_DIRTY;
|
||||||
} else if (size > oldsize) {
|
} else if (size > oldsize) {
|
||||||
lfs_off_t pos = file->pos;
|
lfs_off_t pos = file->pos;
|
||||||
@@ -2460,9 +2408,9 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file) {
|
|||||||
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
|
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
|
||||||
(void)lfs;
|
(void)lfs;
|
||||||
if (file->flags & LFS_F_WRITING) {
|
if (file->flags & LFS_F_WRITING) {
|
||||||
return lfs_max(file->pos, file->size);
|
return lfs_max(file->pos, file->ctz.size);
|
||||||
} else {
|
} else {
|
||||||
return file->size;
|
return file->ctz.size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2590,14 +2538,14 @@ int lfs_remove(lfs_t *lfs, const char *path) {
|
|||||||
lfs_mdir_t dir;
|
lfs_mdir_t dir;
|
||||||
if (lfs_tagtype(tag) == LFS_TYPE_DIR) {
|
if (lfs_tagtype(tag) == LFS_TYPE_DIR) {
|
||||||
// must be empty before removal
|
// must be empty before removal
|
||||||
lfs_mattr_t attr;
|
lfs_block_t pair[2];
|
||||||
attr.tag = lfs_dir_get(lfs, &cwd, 0x7c3ff000,
|
int32_t res = lfs_dir_get(lfs, &cwd, 0x7c3ff000,
|
||||||
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tagid(tag), 8), &attr.u);
|
LFS_MKTAG(LFS_TYPE_STRUCT, lfs_tagid(tag), 8), pair);
|
||||||
if (attr.tag < 0) {
|
if (res < 0) {
|
||||||
return attr.tag;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int err = lfs_dir_fetch(lfs, &dir, attr.u.pair);
|
int err = lfs_dir_fetch(lfs, &dir, pair);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -2624,7 +2572,8 @@ int lfs_remove(lfs_t *lfs, const char *path) {
|
|||||||
cwd.tail[1] = dir.tail[1];
|
cwd.tail[1] = dir.tail[1];
|
||||||
err = lfs_dir_commit(lfs, &cwd,
|
err = lfs_dir_commit(lfs, &cwd,
|
||||||
LFS_MKATTR(LFS_TYPE_SOFTTAIL, 0x3ff,
|
LFS_MKATTR(LFS_TYPE_SOFTTAIL, 0x3ff,
|
||||||
cwd.tail, sizeof(cwd.tail), NULL));
|
cwd.tail, sizeof(cwd.tail),
|
||||||
|
NULL));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -2669,15 +2618,15 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
|||||||
|
|
||||||
if (lfs_tagtype(prevtag) == LFS_TYPE_DIR) {
|
if (lfs_tagtype(prevtag) == LFS_TYPE_DIR) {
|
||||||
// must be empty before removal
|
// must be empty before removal
|
||||||
lfs_mattr_t prevattr;
|
lfs_block_t prevpair[2];
|
||||||
prevattr.tag = lfs_dir_get(lfs, &newcwd, 0x7c3ff000,
|
int32_t res = lfs_dir_get(lfs, &newcwd, 0x7c3ff000,
|
||||||
LFS_MKTAG(LFS_TYPE_STRUCT, newid, 8), &prevattr.u);
|
LFS_MKTAG(LFS_TYPE_STRUCT, newid, 8), prevpair);
|
||||||
if (prevattr.tag < 0) {
|
if (res < 0) {
|
||||||
return prevattr.tag;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// must be empty before removal
|
// must be empty before removal
|
||||||
int err = lfs_dir_fetch(lfs, &prevdir, prevattr.u.pair);
|
int err = lfs_dir_fetch(lfs, &prevdir, prevpair);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -2708,7 +2657,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
|||||||
// move over all attributes
|
// move over all attributes
|
||||||
int err = lfs_dir_commit(lfs, &newcwd,
|
int err = lfs_dir_commit(lfs, &newcwd,
|
||||||
LFS_MKATTR(lfs_tagtype(oldtag), newid, newpath, strlen(newpath),
|
LFS_MKATTR(lfs_tagtype(oldtag), newid, newpath, strlen(newpath),
|
||||||
LFS_MKATTR(LFS_FROM_DIR, newid, &oldcwd, lfs_tagid(oldtag),
|
LFS_MKATTR(LFS_FROM_MOVE, newid, &oldcwd, lfs_tagid(oldtag),
|
||||||
NULL)));
|
NULL)));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
@@ -2734,7 +2683,8 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
|
|||||||
newcwd.tail[1] = prevdir.tail[1];
|
newcwd.tail[1] = prevdir.tail[1];
|
||||||
err = lfs_dir_commit(lfs, &newcwd,
|
err = lfs_dir_commit(lfs, &newcwd,
|
||||||
LFS_MKATTR(LFS_TYPE_SOFTTAIL, 0x3ff,
|
LFS_MKATTR(LFS_TYPE_SOFTTAIL, 0x3ff,
|
||||||
newcwd.tail, sizeof(newcwd.tail), NULL));
|
newcwd.tail, sizeof(newcwd.tail),
|
||||||
|
NULL));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -3094,19 +3044,19 @@ int lfs_fs_traverse(lfs_t *lfs,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (uint16_t id = 0; id < dir.count; id++) {
|
for (uint16_t id = 0; id < dir.count; id++) {
|
||||||
lfs_mattr_t attr;
|
struct lfs_ctz ctz;
|
||||||
attr.tag = lfs_dir_get(lfs, &dir, 0x7c3ff000,
|
int32_t tag = lfs_dir_get(lfs, &dir, 0x7c3ff000,
|
||||||
LFS_MKTAG(LFS_TYPE_STRUCT, id, 8), &attr.u);
|
LFS_MKTAG(LFS_TYPE_STRUCT, id, sizeof(ctz)), &ctz);
|
||||||
if (attr.tag < 0) {
|
if (tag < 0) {
|
||||||
if (attr.tag == LFS_ERR_NOENT) {
|
if (tag == LFS_ERR_NOENT) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
return attr.tag;
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lfs_tagtype(attr.tag) == LFS_TYPE_CTZSTRUCT) {
|
if (lfs_tagtype(tag) == LFS_TYPE_CTZSTRUCT) {
|
||||||
int err = lfs_ctz_traverse(lfs, &lfs->rcache, NULL,
|
int err = lfs_ctz_traverse(lfs, &lfs->rcache, NULL,
|
||||||
attr.u.ctz.head, attr.u.ctz.size, cb, data);
|
ctz.head, ctz.size, cb, data);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -3118,7 +3068,7 @@ int lfs_fs_traverse(lfs_t *lfs,
|
|||||||
for (lfs_file_t *f = lfs->files; f; f = f->next) {
|
for (lfs_file_t *f = lfs->files; f; f = f->next) {
|
||||||
if ((f->flags & LFS_F_DIRTY) && !(f->flags & LFS_F_INLINE)) {
|
if ((f->flags & LFS_F_DIRTY) && !(f->flags & LFS_F_INLINE)) {
|
||||||
int err = lfs_ctz_traverse(lfs, &lfs->rcache, &f->cache,
|
int err = lfs_ctz_traverse(lfs, &lfs->rcache, &f->cache,
|
||||||
f->head, f->size, cb, data);
|
f->ctz.head, f->ctz.size, cb, data);
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -3264,9 +3214,7 @@ static int lfs_relocate(lfs_t *lfs,
|
|||||||
if (tag != LFS_ERR_NOENT) {
|
if (tag != LFS_ERR_NOENT) {
|
||||||
// update disk, this creates a desync
|
// update disk, this creates a desync
|
||||||
int err = lfs_dir_commit(lfs, &parent,
|
int err = lfs_dir_commit(lfs, &parent,
|
||||||
LFS_MKATTR(lfs_tagtype(tag), lfs_tagid(tag),
|
&(lfs_mattr_t){.tag=tag, .buffer=newpair});
|
||||||
newpair, lfs_tagsize(tag), NULL));
|
|
||||||
// TODO don't recreate tag?
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -3296,7 +3244,8 @@ static int lfs_relocate(lfs_t *lfs,
|
|||||||
parent.tail[1] = newpair[1];
|
parent.tail[1] = newpair[1];
|
||||||
int err = lfs_dir_commit(lfs, &parent,
|
int err = lfs_dir_commit(lfs, &parent,
|
||||||
LFS_MKATTR(LFS_TYPE_TAIL + parent.split, 0x3ff,
|
LFS_MKATTR(LFS_TYPE_TAIL + parent.split, 0x3ff,
|
||||||
newpair, sizeof(lfs_block_t[2]), NULL));
|
newpair, sizeof(lfs_block_t[2]),
|
||||||
|
NULL));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -3403,22 +3352,21 @@ int lfs_deorphan(lfs_t *lfs) {
|
|||||||
if (!pdir.split) {
|
if (!pdir.split) {
|
||||||
// check if we have a parent
|
// check if we have a parent
|
||||||
lfs_mdir_t parent;
|
lfs_mdir_t parent;
|
||||||
lfs_mattr_t attr;
|
int32_t tag = lfs_parent(lfs, pdir.tail, &parent);
|
||||||
attr.tag = lfs_parent(lfs, pdir.tail, &parent);
|
if (tag < 0 && tag != LFS_ERR_NOENT) {
|
||||||
if (attr.tag < 0&& attr.tag != LFS_ERR_NOENT) {
|
return tag;
|
||||||
return attr.tag;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attr.tag == LFS_ERR_NOENT) {
|
if (tag == LFS_ERR_NOENT) {
|
||||||
// we are an orphan
|
// we are an orphan
|
||||||
LFS_DEBUG("Found orphan %d %d",
|
LFS_DEBUG("Found orphan %d %d", pdir.tail[0], pdir.tail[1]);
|
||||||
pdir.tail[0], pdir.tail[1]);
|
|
||||||
|
|
||||||
pdir.tail[0] = dir.tail[0];
|
pdir.tail[0] = dir.tail[0];
|
||||||
pdir.tail[1] = dir.tail[1];
|
pdir.tail[1] = dir.tail[1];
|
||||||
err = lfs_dir_commit(lfs, &pdir,
|
err = lfs_dir_commit(lfs, &pdir,
|
||||||
LFS_MKATTR(LFS_TYPE_SOFTTAIL, 0x3ff,
|
LFS_MKATTR(LFS_TYPE_SOFTTAIL, 0x3ff,
|
||||||
pdir.tail, sizeof(pdir.tail), NULL));
|
pdir.tail, sizeof(pdir.tail),
|
||||||
|
NULL));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
@@ -3426,22 +3374,22 @@ int lfs_deorphan(lfs_t *lfs) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t res = lfs_dir_get(lfs, &parent,
|
lfs_block_t pair[2];
|
||||||
0x7ffff000, attr.tag, &attr.u);
|
int32_t res = lfs_dir_get(lfs, &parent, 0x7ffff000, tag, pair);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lfs_pairsync(attr.u.pair, pdir.tail)) {
|
if (!lfs_pairsync(pair, pdir.tail)) {
|
||||||
// we have desynced
|
// we have desynced
|
||||||
LFS_DEBUG("Found half-orphan %d %d",
|
LFS_DEBUG("Found half-orphan %d %d", pair[0], pair[1]);
|
||||||
attr.u.pair[0], attr.u.pair[1]);
|
|
||||||
|
|
||||||
pdir.tail[0] = attr.u.pair[0];
|
pdir.tail[0] = pair[0];
|
||||||
pdir.tail[1] = attr.u.pair[1];
|
pdir.tail[1] = pair[1];
|
||||||
err = lfs_dir_commit(lfs, &pdir,
|
err = lfs_dir_commit(lfs, &pdir,
|
||||||
LFS_MKATTR(LFS_TYPE_SOFTTAIL, 0x3ff,
|
LFS_MKATTR(LFS_TYPE_SOFTTAIL, 0x3ff,
|
||||||
pdir.tail, sizeof(pdir.tail), NULL));
|
pdir.tail, sizeof(pdir.tail),
|
||||||
|
NULL));
|
||||||
if (err) {
|
if (err) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|||||||
32
lfs.h
32
lfs.h
@@ -118,7 +118,7 @@ enum lfs_type {
|
|||||||
// internal chip sources
|
// internal chip sources
|
||||||
LFS_FROM_REGION = 0x000,
|
LFS_FROM_REGION = 0x000,
|
||||||
LFS_FROM_DISK = 0x200,
|
LFS_FROM_DISK = 0x200,
|
||||||
LFS_FROM_DIR = 0x030,
|
LFS_FROM_MOVE = 0x030,
|
||||||
};
|
};
|
||||||
|
|
||||||
// File open flags
|
// File open flags
|
||||||
@@ -263,32 +263,11 @@ struct lfs_attr {
|
|||||||
|
|
||||||
/// littlefs data structures ///
|
/// littlefs data structures ///
|
||||||
typedef struct lfs_mattr {
|
typedef struct lfs_mattr {
|
||||||
|
struct lfs_mattr *next;
|
||||||
int32_t tag;
|
int32_t tag;
|
||||||
union {
|
const void *buffer;
|
||||||
void *buffer;
|
|
||||||
lfs_block_t pair[2];
|
|
||||||
struct {
|
|
||||||
lfs_block_t head;
|
|
||||||
lfs_size_t size;
|
|
||||||
} ctz;
|
|
||||||
struct {
|
|
||||||
lfs_block_t block;
|
|
||||||
lfs_off_t off;
|
|
||||||
} d;
|
|
||||||
struct lfs_mdir *dir;
|
|
||||||
} u;
|
|
||||||
} lfs_mattr_t;
|
} lfs_mattr_t;
|
||||||
|
|
||||||
typedef struct lfs_mattrlist {
|
|
||||||
struct lfs_mattrlist *next;
|
|
||||||
lfs_mattr_t e;
|
|
||||||
} lfs_mattrlist_t;
|
|
||||||
|
|
||||||
//typedef struct lfs_entry {
|
|
||||||
// lfs_block_t pair[2];
|
|
||||||
// uint16_t id;
|
|
||||||
//} lfs_entry_t;
|
|
||||||
|
|
||||||
typedef struct lfs_globals {
|
typedef struct lfs_globals {
|
||||||
struct lfs_move {
|
struct lfs_move {
|
||||||
lfs_block_t pair[2];
|
lfs_block_t pair[2];
|
||||||
@@ -305,7 +284,6 @@ typedef struct lfs_mdir {
|
|||||||
uint16_t count;
|
uint16_t count;
|
||||||
bool erased;
|
bool erased;
|
||||||
bool split;
|
bool split;
|
||||||
bool stop_at_commit; // TODO hmmm
|
|
||||||
lfs_globals_t globals;
|
lfs_globals_t globals;
|
||||||
} lfs_mdir_t;
|
} lfs_mdir_t;
|
||||||
|
|
||||||
@@ -319,8 +297,10 @@ typedef struct lfs_file {
|
|||||||
struct lfs_file *next;
|
struct lfs_file *next;
|
||||||
lfs_block_t pair[2];
|
lfs_block_t pair[2];
|
||||||
uint16_t id;
|
uint16_t id;
|
||||||
|
struct lfs_ctz {
|
||||||
lfs_block_t head;
|
lfs_block_t head;
|
||||||
lfs_size_t size;
|
lfs_size_t size;
|
||||||
|
} ctz;
|
||||||
|
|
||||||
uint32_t flags;
|
uint32_t flags;
|
||||||
lfs_off_t pos;
|
lfs_off_t pos;
|
||||||
@@ -328,7 +308,7 @@ typedef struct lfs_file {
|
|||||||
lfs_off_t off;
|
lfs_off_t off;
|
||||||
lfs_cache_t cache;
|
lfs_cache_t cache;
|
||||||
|
|
||||||
lfs_mattrlist_t *attrs;
|
lfs_mattr_t *attrs;
|
||||||
} lfs_file_t;
|
} lfs_file_t;
|
||||||
|
|
||||||
typedef struct lfs_dir {
|
typedef struct lfs_dir {
|
||||||
|
|||||||
Reference in New Issue
Block a user