金牌会员
 
- 积分
- 2082
- 金钱
- 2082
- 注册时间
- 2014-12-19
- 在线时间
- 711 小时
|

楼主 |
发表于 2014-12-19 16:30:28
|
显示全部楼层
回复【4楼】正点原子:
---------------------------------
今天偶然在 FATFS的官网看到,原来是之前的f_unlink函数有问题,详见下面的网址
http://elm-chan.org/fsw/ff/patches.html
R0.10c版和R0.10b版均有此问题,各位要注意了,注意修改,否则使用删除功能时会导致文件系统损坏。
f_unlink function does not remove cluster chain of the file. It reads to a lost cluster of
the FAT volume. This is added at a bugfix of R0.10c.
f_unlink函数不会删除文件的簇链,它会一直读到FAT卷的一个丢失簇。
修改后的完整 f_unlink函数如下:
FRESULT f_unlink (
const TCHAR* path /*  ointer to the file or directory path */
)
{
FRESULT res;
DIR dj, sdj;
BYTE *dir;
DWORD dclst = 0;
DEF_NAMEBUF;
/* Get logical drive number */
res = find_volume(&dj.fs, &path, 1);
if (res == FR_OK) {
INIT_BUF(dj);
res = follow_path(&dj, path); /* Follow the file path */
if (_FS_RPATH && res == FR_OK && (dj.fn[NSFLAG] & NS_DOT))
res = FR_INVALID_NAME; /* Cannot remove dot entry */
#if _FS_LOCK
if (res == FR_OK) res = chk_lock(&dj, 2); /* Cannot remove open object */
#endif
if (res == FR_OK) { /* The object is accessible */
dir = dj.dir;
if (!dir) {
res = FR_INVALID_NAME; /* Cannot remove the origin directory */
} else {
if (dir[DIR_Attr] & AM_RDO)
res = FR_DENIED; /* Cannot remove R/O object */
}
if (res == FR_OK) {
dclst = ld_clust(dj.fs, dir);
if (dir[DIR_Attr] & AM_DIR) { /* Is it a sub-dir? */
if (!dclst) {
res = FR_INT_ERR;
} else { /* Make sure the sub-directory is empty */
mem_cpy(&sdj, &dj, sizeof (DIR));
sdj.sclust = dclst;
res = dir_sdi(&sdj, 2); /* Exclude dot entries */
if (res == FR_OK) {
res = dir_read(&sdj, 0); /* Read an item */
if (res == FR_OK /* Not empty directory */
#if _FS_RPATH
|| dclst == dj.fs->cdir /* or current directory */
#endif
) res = FR_DENIED;
if (res == FR_NO_FILE) res = FR_OK; /* It is empty */
}
}
}
}
if (res == FR_OK) {
res = dir_remove(&dj); /* Remove the directory entry */
if (res == FR_OK && dclst) /* Remove the cluster chain if exist */
res = remove_chain(dj.fs, dclst);
if (res == FR_OK) res = sync_fs(dj.fs);
}
}
FREE_BUF();
}
LEAVE_FF(dj.fs, res);
} |
|