Skip to content

Commit

Permalink
feat: add uncommitted close which will withdrawn previous writes
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Stauss <[email protected]>
  • Loading branch information
Pascal Stauss committed Apr 2, 2024
1 parent 4dd30c1 commit cd1cd27
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ static lfs_ssize_t lfs_file_flushedread(lfs_t *lfs, lfs_file_t *file,
static lfs_ssize_t lfs_file_read_(lfs_t *lfs, lfs_file_t *file,
void *buffer, lfs_size_t size);
static int lfs_file_close_(lfs_t *lfs, lfs_file_t *file);
static int lfs_file_uncommitted_close_(lfs_t *lfs, lfs_file_t *file);
static lfs_soff_t lfs_file_size_(lfs_t *lfs, lfs_file_t *file);

static lfs_ssize_t lfs_fs_size_(lfs_t *lfs);
Expand Down Expand Up @@ -3222,6 +3223,18 @@ static int lfs_file_close_(lfs_t *lfs, lfs_file_t *file) {
return err;
}

static int lfs_file_uncommitted_close_(lfs_t *lfs, lfs_file_t *file) {
// remove from list of mdirs
lfs_mlist_remove(lfs, (struct lfs_mlist*)file);

// clean up memory
if (!file->cfg->buffer) {
lfs_free(file->cache.buffer);
}

return 0;
}


#ifndef LFS_READONLY
static int lfs_file_relocate(lfs_t *lfs, lfs_file_t *file) {
Expand Down Expand Up @@ -6098,6 +6111,21 @@ int lfs_file_close(lfs_t *lfs, lfs_file_t *file) {
return err;
}

int lfs_file_uncommitted_close(lfs_t *lfs, lfs_file_t *file) {
int err = LFS_LOCK(lfs->cfg);
if (err) {
return err;
}
LFS_TRACE("lfs_file_uncommitted_close(%p, %p)", (void*)lfs, (void*)file);
LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));

err = lfs_file_uncommitted_close_(lfs, file);

LFS_TRACE("lfs_file_uncommitted_close -> %d", err);
LFS_UNLOCK(lfs->cfg);
return err;
}

#ifndef LFS_READONLY
int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) {
int err = LFS_LOCK(lfs->cfg);
Expand Down
9 changes: 9 additions & 0 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,15 @@ int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
// Returns a negative error code on failure.
int lfs_file_close(lfs_t *lfs, lfs_file_t *file);

// Close a file
//
// Any pending writes are withdrawn.
// If desired a previous sync call is necesarry to write pending writes out to the storage.
// Releases any allocated resources.
//
// Returns a negative error code on failure.
int lfs_file_uncommitted_close(lfs_t *lfs, lfs_file_t *file);

// Synchronize a file on storage
//
// Any pending writes are written out to storage.
Expand Down

0 comments on commit cd1cd27

Please sign in to comment.