f_sync解决fatfs文件掉电数据丢失问题

来源:互联网 发布:尤克里里调音器软件 编辑:程序博客网 时间:2024/06/08 10:20
临界段

When write operation to the FAT file system is interrupted due to any accidental failure, such as sudden blackout, incorrect disk removal and unrecoverable disk error, the FAT structure can be collapted. Following images shows the critical section on the FatFs module.

当对FAT文件系统的写操作由于默写意外而中断,如突然断电,不正确的磁盘移除或不可恢复的磁盘错误,FAT结构可以被毁坏。下面的图片显示了FatFs的临界段。

 

f_sync解决fatfs文件掉电数据丢失问题

An interruption in the red section can cause a cross link; as a result, the file/directory being changed may be lost. There is one or more possibility listed below when an interruption in the yellow section is occured.

红色区域的中断会导致一个交叉链接,结果,正在修改的文件/目录可能会丢失。而黄色区域中断可能导致的效果在下面列出:

 

  • File data being rewrited is collapted. 正在重写的文件数据被毁坏
  • A file being appended returns initial state. 正在添加内容的文件回到初始状态
  • A file created as new is gone. 丢失新建的文件
  • A file created as new or in overwritten remains with length of zero. 一个新建或覆盖的文件保持长度为0
  • Efficiency of disk use gets worse due to lost chain. 因为丢失关联,磁盘的使用效率变坏。

Each case does not affect the files that not in write mode open. To minimize risk of data loss, the critical section can be minimized like shown in Figure 5 by minimizing the time that file is opened in write mode or using f_sync function properly.

在文件不是用写模式打开时,这些情况不会发生。为了最小化磁盘数据的丢失,临界段可以像图表5显示的那样最小化,通过最小化文件处于写模式打开的时间或者适当的使用f_sync函数。


f_sync

冲洗一个写文件的缓存信息

FRESULT f_sync (

 FIL* FileObject          /* 文件对象结构的指针 */

);

参数

FileObject


待冲洗的打开的文件对象的指针。

返回值

FR_OK (0)


函数成功。

FR_DISK_ERR


由于底层磁盘I/O函数中的错误,而导致该函数失败。

FR_INT_ERR


由于一个错误的FAT结构或一个内部错误,而导致该函数失败。

FR_NOT_READY


由于驱动器中没有存储介质或任何其他原因,而导致磁盘驱动器无法工作。

FR_INVALID_OBJECT 文件对象无效。


描述

f_sync函数当_FS_READONLY == 0时可用。

f_sync函数和f_close函数执行同样的过程,但是文件仍处于打开状态,并且可以继续对文件执行读/写/移动指针操作。这适用于以写模式长时间打开文件,比如数据记录器。定期的或f_write后立即执行f_sync可以将由于突然断电或移去磁盘而导致数据丢失的风险最小化。在f_close前立即执行f_sync没有作用,因为在f_close中执行了f_sync。换句话说,这两个函数的差异就是文件对象是不是无效的。


原创粉丝点击