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 *