diff --git a/lfs.c b/lfs.c index a0bd76fd..14d63fa0 100644 --- a/lfs.c +++ b/lfs.c @@ -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); @@ -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) { @@ -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); diff --git a/lfs.h b/lfs.h index 99145022..6f8c90b2 100644 --- a/lfs.h +++ b/lfs.h @@ -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.