From 5c13e08e76ae1d75240e1e8e46d1e9c3a8ba50cd Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Sun, 29 Jun 2014 16:15:46 +0200 Subject: [PATCH] added gfileSync() and autosync feature --- docs/releases.txt | 1 + gfxconf.example.h | 1 + src/gfile/gfile.c | 7 +++++++ src/gfile/inc_chibiosfs.c | 3 ++- src/gfile/inc_fatfs.c | 24 +++++++++++++++++++++++- src/gfile/inc_memfs.c | 3 ++- src/gfile/inc_nativefs.c | 3 ++- src/gfile/inc_romfs.c | 3 ++- src/gfile/sys_defs.h | 13 +++++++++++++ src/gfile/sys_options.h | 13 +++++++++++++ 10 files changed, 66 insertions(+), 5 deletions(-) diff --git a/docs/releases.txt b/docs/releases.txt index 31c4f55e..e0c47c4a 100644 --- a/docs/releases.txt +++ b/docs/releases.txt @@ -9,6 +9,7 @@ FEATURE: Added generic framebuffer driver FEATURE: Added Linux-Framebuffer board definition FEATURE: Added FatFS support for GFILE FEATURE: Added gfileMount() and gfileUnmount() +FEATURE: Added gfileSync() *** Release 2.1 *** diff --git a/gfxconf.example.h b/gfxconf.example.h index 8170d2e1..bc9964e6 100644 --- a/gfxconf.example.h +++ b/gfxconf.example.h @@ -220,6 +220,7 @@ // #define GFILE_ALLOW_DEVICESPECIFIC FALSE // #define GFILE_MAX_GFILES 3 //#define GFILE_NEED_NOAUTOMOUNT FALSE +//#define GFILE_NEED_NOAUTOSYNC FALSE //#define GFILE_NEED_MEMFS FALSE //#define GFILE_NEED_ROMFS FALSE diff --git a/src/gfile/gfile.c b/src/gfile/gfile.c index ba1b2dba..6aadda09 100644 --- a/src/gfile/gfile.c +++ b/src/gfile/gfile.c @@ -58,6 +58,7 @@ typedef struct GFILEVMT { bool_t (*eof) (GFILE *f); bool_t (*mount) (const char *drive); bool_t (*unmount) (const char *drive); + bool_t (*sync) (GFILE *f); } GFILEVMT; // The chain of FileSystems @@ -505,6 +506,12 @@ bool_t gfileUnmount(char fs, const char* drive) { return FALSE; } +bool_t gfileSync(GFILE *f) { + if (!f->vmt->sync) + return FALSE; + return f->vmt->sync(f); +} + /******************************************************** * String VMT routines ********************************************************/ diff --git a/src/gfile/inc_chibiosfs.c b/src/gfile/inc_chibiosfs.c index 92664288..8d321b33 100644 --- a/src/gfile/inc_chibiosfs.c +++ b/src/gfile/inc_chibiosfs.c @@ -27,7 +27,8 @@ static const GFILEVMT FsCHIBIOSVMT = { 0, 0, 0, 0, 0, ChibiOSBFSClose, ChibiOSBFSRead, ChibiOSBFSWrite, ChibiOSBFSSetpos, ChibiOSBFSGetsize, ChibiOSBFSEof, - 0, 0 + 0, 0, + 0 }; static void ChibiOSBFSClose(GFILE *f) { diff --git a/src/gfile/inc_fatfs.c b/src/gfile/inc_fatfs.c index a9066bf9..447b8a71 100644 --- a/src/gfile/inc_fatfs.c +++ b/src/gfile/inc_fatfs.c @@ -29,6 +29,7 @@ static long int fatfsGetSize(GFILE* f); static bool_t fatfsEOF(GFILE* f); static bool_t fatfsMount(const char* drive); static bool_t fatfsUnmount(const char* drive); +static bool_t fatfsSync(GFILE* f); static const GFILEVMT FsFatFSVMT = { GFILE_CHAINHEAD, @@ -46,7 +47,8 @@ static const GFILEVMT FsFatFSVMT = { fatfsGetSize, fatfsEOF, fatfsMount, - fatfsUnmount + fatfsUnmount, + fatfsSync }; #undef GFILE_CHAINHEAD @@ -140,6 +142,13 @@ static bool_t fatfsOpen(GFILE* f, const char* fname) f->obj = (void*)fd; + #if !GFILE_NEED_NOAUTOSYNC + // no need to sync when not opening for write + if (f->flags & GFILEFLG_WRITE) { + f_sync( (FIL*)f->obj ); + } + #endif + return TRUE; } @@ -165,6 +174,7 @@ static int fatfsWrite(GFILE* f, const void* buf, int size) int wr; f_write( (FIL*)f->obj, buf, size, (UINT*)&wr); + f_sync( (FIL*)f->obj ); return wr; } @@ -221,3 +231,15 @@ static bool_t fatfsUnmount(const char* drive) return FALSE; } +static bool_t fatfsSync(GFILE *f) +{ + FRESULT ferr; + + ferr = f_sync( (FIL*)f->obj ); + if (ferr != FR_OK) { + return FALSE; + } + + return TRUE; +} + diff --git a/src/gfile/inc_memfs.c b/src/gfile/inc_memfs.c index c7322686..baeb0e97 100644 --- a/src/gfile/inc_memfs.c +++ b/src/gfile/inc_memfs.c @@ -26,7 +26,8 @@ static const GFILEVMT FsMemVMT = { 0, 0, 0, 0, 0, 0, MEMRead, MEMWrite, MEMSetpos, 0, 0, - 0, 0 + 0, 0, + 0 }; static int MEMRead(GFILE *f, void *buf, int size) { diff --git a/src/gfile/inc_nativefs.c b/src/gfile/inc_nativefs.c index 98e0ab10..6845cb71 100644 --- a/src/gfile/inc_nativefs.c +++ b/src/gfile/inc_nativefs.c @@ -46,7 +46,8 @@ static const GFILEVMT FsNativeVMT = { NativeDel, NativeExists, NativeFilesize, NativeRen, NativeOpen, NativeClose, NativeRead, NativeWrite, NativeSetpos, NativeGetsize, NativeEof, - 0, 0 + 0, 0, + 0 }; #undef GFILE_CHAINHEAD #define GFILE_CHAINHEAD &FsNativeVMT diff --git a/src/gfile/inc_romfs.c b/src/gfile/inc_romfs.c index 9f7faf7c..167430ce 100644 --- a/src/gfile/inc_romfs.c +++ b/src/gfile/inc_romfs.c @@ -52,7 +52,8 @@ static const GFILEVMT FsROMVMT = { 0, ROMExists, ROMFilesize, 0, ROMOpen, ROMClose, ROMRead, 0, ROMSetpos, ROMGetsize, ROMEof, - 0, 0 + 0, 0, + 0 }; #undef GFILE_CHAINHEAD #define GFILE_CHAINHEAD &FsROMVMT diff --git a/src/gfile/sys_defs.h b/src/gfile/sys_defs.h index 6b1d27ff..4280f7fe 100644 --- a/src/gfile/sys_defs.h +++ b/src/gfile/sys_defs.h @@ -226,6 +226,19 @@ extern "C" { */ bool_t gfileUnmount(char fs, const char *drive); + /** + * @brief Syncs the file object (flushes the buffer) + * + * @details Not supported by every file system + * + * @param[in] f The file + * + * @return TRUE on success, FALSE otherwise + * + * @api + */ + bool_t gfileSync(GFILE *f); + #if GFILE_NEED_CHIBIOSFS && GFX_USE_OS_CHIBIOS GFILE * gfileOpenBaseFileStream(void *BaseFileStreamPtr, const char *mode); #endif diff --git a/src/gfile/sys_options.h b/src/gfile/sys_options.h index 41cf6a63..ee52298c 100644 --- a/src/gfile/sys_options.h +++ b/src/gfile/sys_options.h @@ -30,6 +30,19 @@ #ifndef GFILE_NEED_NOAUTOMOUNT #define GFILE_NEED_NOAUTOMOUNT FALSE #endif + /** + * @brief Should the filesystem be synced automatically + * @details The filesystem will automatically be synced after an open() or + * write() call unless this feature is disabled. + * @details If this feature is disabled, the user should sync the filesystem + * himself using @p gfileSync() + * @details Not all filesystems implement the syncing feature. This feature will + * have no effect in such a case. + * @details Defaults to FALSE + */ + #ifndef GFILE_NEED_NOAUTOSYNC + #define GFILE_NEED_NOAUTOSYNC FALSE + #endif /** * @brief Include printg, fprintg etc functions * @details Defaults to FALSE