1 /*-----------------------------------------------------------------------*/ 2 /* Synchronize the File Object */ 3 /*-----------------------------------------------------------------------*/ 4 5 FRESULT f_sync ( 6 FIL *fp /* Pointer to the file object */ 7 ) 8 { 9 FRESULT res;10 DWORD tim;11 BYTE *dir;12 13 14 res = validate(fp->fs, fp->id); /* Check validity of the object */15 if (res == FR_OK) {16 if (fp->flag & FA__WRITTEN) { /* Has the file been written? */17 #if !_FS_TINY /* Write-back dirty buffer */18 if (fp->flag & FA__DIRTY) {19 if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)20 LEAVE_FF(fp->fs, FR_DISK_ERR);21 fp->flag &= ~FA__DIRTY;22 }23 #endif24 /* Update the directory entry */25 res = move_window(fp->fs, fp->dir_sect);26 if (res == FR_OK) {27 dir = fp->dir_ptr;28 dir[DIR_Attr] |= AM_ARC; /* Set archive bit */29 ST_DWORD(dir+DIR_FileSize, fp->fsize); /* Update file size */30 ST_CLUST(dir, fp->sclust); /* Update start cluster */31 tim = get_fattime(); /* Update updated time */32 ST_DWORD(dir+DIR_WrtTime, tim);33 fp->flag &= ~FA__WRITTEN;34 fp->fs->wflag = 1;35 res = sync(fp->fs);36 }37 }38 }39 40 LEAVE_FF(fp->fs, res);41 }View code
Function: clears the cache information of a written file.
Description:
F_sync function is available when _ fs_readonly = 0.
The f_sync function and the f_close function execute the same process, but the file is still open and can continue to read, write, and move the file.
Pointer operation. This applies to opening a file in write mode for a long time, such as a data recorder. You can execute f_sync immediately after a scheduled or f_write operation.
The risk of data loss is minimized due to sudden power outages or disk removal. Executing f_sync before f_close does not work because
F_sync is executed in f_close. In other words, the difference between the two functions is whether the file object is invalid.
---------------------------------------- I am a split line ----------------------------------------
Address: http://blog.sina.com.cn/s/blog_66ffe2770100w2p7.html
When write operations on the FAT file system are interrupted due to silent writing, such as sudden power failure, incorrect disk removal or unrecoverable disk errors, the fat structure can be destroyed. The following figure shows the critical segments of fatfs.
The interruption in the red area may lead to a cross link. As a result, the files/directories being modified may be lost. The effects of interruptions in the yellow area are listed below:
The file data being overwritten is destroyed.
The file that is being added returns to the initial state.
The new file is lost.
The length of a new or overwritten file is 0.
Disk usage efficiency deteriorated due to lost Association.
This will not happen when the file is not opened in write mode. To minimize disk data loss, the critical segment can be minimized as shown in Figure 5 by minimizing the time when the file is opened in write mode or using the f_sync function as appropriate.
---------------------------------------- I am a split line ----------------------------------------