Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 38 additions & 22 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,31 @@ static inline void lfs_superblock_tole32(lfs_superblock_t *superblock) {
superblock->attr_max = lfs_tole32(superblock->attr_max);
}

static inline bool lfs_mlist_isopen(struct lfs_mlist *head,
struct lfs_mlist *node) {
for (struct lfs_mlist **p = &head; *p; p = &(*p)->next) {
if (*p == (struct lfs_mlist*)node) {
return true;
}
}

return false;
}

static inline void lfs_mlist_remove(lfs_t *lfs, struct lfs_mlist *mlist) {
for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) {
if (*p == mlist) {
*p = (*p)->next;
break;
}
}
}

static inline void lfs_mlist_append(lfs_t *lfs, struct lfs_mlist *mlist) {
mlist->next = lfs->mlist;
lfs->mlist = mlist;
}


/// Internal operations predeclared here ///
static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir,
Expand Down Expand Up @@ -2007,6 +2032,8 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {

int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
LFS_TRACE("lfs_dir_open(%p, %p, \"%s\")", (void*)lfs, (void*)dir, path);
LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)dir));

lfs_stag_t tag = lfs_dir_find(lfs, &dir->m, &path, NULL);
if (tag < 0) {
LFS_TRACE("lfs_dir_open -> %"PRId32, tag);
Expand Down Expand Up @@ -2049,8 +2076,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {

// add to list of mdirs
dir->type = LFS_TYPE_DIR;
dir->next = (lfs_dir_t*)lfs->mlist;
lfs->mlist = (struct lfs_mlist*)dir;
lfs_mlist_append(lfs, (struct lfs_mlist *)dir);

LFS_TRACE("lfs_dir_open -> %d", 0);
return 0;
Expand All @@ -2059,12 +2085,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir) {
LFS_TRACE("lfs_dir_close(%p, %p)", (void*)lfs, (void*)dir);
// remove from list of mdirs
for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) {
if (*p == (struct lfs_mlist*)dir) {
*p = (*p)->next;
break;
}
}
lfs_mlist_remove(lfs, (struct lfs_mlist *)dir);

LFS_TRACE("lfs_dir_close -> %d", 0);
return 0;
Expand Down Expand Up @@ -2387,9 +2408,10 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
".buffer=%p, .attrs=%p, .attr_count=%"PRIu32"})",
(void*)lfs, (void*)file, path, flags,
(void*)cfg, cfg->buffer, (void*)cfg->attrs, cfg->attr_count);
LFS_ASSERT(!lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));

// deorphan if we haven't yet, needed at most once after poweron
if ((flags & 3) != LFS_O_RDONLY) {
if ((flags & LFS_O_RDWR) != LFS_O_RDONLY) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed in all places now 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I misread this. Well, I don't really have an opinion on this change. It is a curious mask.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is better than a magic num

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't argue with that

int err = lfs_fs_forceconsistency(lfs);
if (err) {
LFS_TRACE("lfs_file_opencfg -> %d", err);
Expand All @@ -2414,8 +2436,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,

// get id, add to list of mdirs to catch update changes
file->type = LFS_TYPE_REG;
file->next = (lfs_file_t*)lfs->mlist;
lfs->mlist = (struct lfs_mlist*)file;
lfs_mlist_append(lfs, (struct lfs_mlist *)file);

if (tag == LFS_ERR_NOENT) {
if (!(flags & LFS_O_CREAT)) {
Expand Down Expand Up @@ -2464,7 +2485,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,

// fetch attrs
for (unsigned i = 0; i < file->cfg->attr_count; i++) {
if ((file->flags & 3) != LFS_O_WRONLY) {
if ((file->flags & LFS_O_RDWR) != LFS_O_WRONLY) {
lfs_stag_t res = lfs_dir_get(lfs, &file->m,
LFS_MKTAG(0x7ff, 0x3ff, 0),
LFS_MKTAG(LFS_TYPE_USERATTR + file->cfg->attrs[i].type,
Expand All @@ -2476,7 +2497,7 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
}
}

if ((file->flags & 3) != LFS_O_RDONLY) {
if ((file->flags & LFS_O_RDWR) != LFS_O_RDONLY) {
if (file->cfg->attrs[i].size > lfs->attr_max) {
err = LFS_ERR_NOSPC;
goto cleanup;
Expand Down Expand Up @@ -2551,12 +2572,7 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
int err = lfs_file_sync(lfs, file);

// remove from list of mdirs
for (struct lfs_mlist **p = &lfs->mlist; *p; p = &(*p)->next) {
if (*p == (struct lfs_mlist*)file) {
*p = (*p)->next;
break;
}
}
lfs_mlist_remove(lfs, (struct lfs_mlist*)file);

// clean up memory
if (!file->cfg->buffer) {
Expand Down Expand Up @@ -2793,7 +2809,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
LFS_TRACE("lfs_file_read(%p, %p, %p, %"PRIu32")",
(void*)lfs, (void*)file, buffer, size);
LFS_ASSERT(file->flags & LFS_F_OPENED);
LFS_ASSERT((file->flags & 3) != LFS_O_WRONLY);
LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_WRONLY);

uint8_t *data = buffer;
lfs_size_t nsize = size;
Expand Down Expand Up @@ -2873,7 +2889,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
LFS_TRACE("lfs_file_write(%p, %p, %p, %"PRIu32")",
(void*)lfs, (void*)file, buffer, size);
LFS_ASSERT(file->flags & LFS_F_OPENED);
LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY);
LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_RDONLY);

const uint8_t *data = buffer;
lfs_size_t nsize = size;
Expand Down Expand Up @@ -3038,7 +3054,7 @@ int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {
LFS_TRACE("lfs_file_truncate(%p, %p, %"PRIu32")",
(void*)lfs, (void*)file, size);
LFS_ASSERT(file->flags & LFS_F_OPENED);
LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY);
LFS_ASSERT((file->flags & LFS_O_RDWR) != LFS_O_RDONLY);

if (size > LFS_FILE_MAX) {
LFS_TRACE("lfs_file_truncate -> %d", LFS_ERR_INVAL);
Expand Down