From 97d8fd1748d8071b07acfa12e98bc662a960ccdc Mon Sep 17 00:00:00 2001 From: Ramesh Thomas Date: Thu, 1 Sep 2016 23:15:25 -0700 Subject: [PATCH] fs: Add file system API to flush cache of an open file This API flushes the cached data of an open file to the storage media. This can be called after writes to avoid data loss if power is removed unexpectedly. Jira: ZEP-767 Change-Id: I0f99f2f34126aa8e6a43f69c7a1b6d903937de11 Signed-off-by: Ramesh Thomas --- fs/fat_fs.c | 9 +++++++++ include/fs.h | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/fs/fat_fs.c b/fs/fat_fs.c index a71e6b9bf46..414241e3b19 100644 --- a/fs/fat_fs.c +++ b/fs/fat_fs.c @@ -194,6 +194,15 @@ int fs_truncate(ZFILE *zfp, off_t length) return translate_error(res); } +int fs_sync(ZFILE *zfp) +{ + FRESULT res = FR_OK; + + res = f_sync(&zfp->fp); + + return translate_error(res); +} + int fs_mkdir(const char *path) { FRESULT res; diff --git a/include/fs.h b/include/fs.h index 24a3d3ce43b..dc4679d280b 100644 --- a/include/fs.h +++ b/include/fs.h @@ -233,6 +233,22 @@ off_t fs_tell(ZFILE *zfp); */ int fs_truncate(ZFILE *zfp, off_t length); +/** + * @brief Flushes any cached write of an open file + * + * This function can be used to flush the cache of an open file. This can + * be called to ensure data gets written to the storage media immediately. + * This may be done to avoid data loss if power is removed unexpectedly. + * Note that closing a file will cause caches to be flushed correctly so it + * need not be called if the file is being closed. + * + * @param zfp Pointer to the file object + * + * @retval 0 Success + * @retval -ERRNO errno code if error + */ +int fs_sync(ZFILE *zfp); + /** * @brief Directory create *