📄 ff.c
字号:
res = dir_next(dj, 0); /* Next entry */
} while (res == FR_OK);
if (res == FR_NO_FILE) res = FR_INT_ERR;
}
#else /* Non LFN configuration */
res = dir_sdi(dj, dj->index);
if (res == FR_OK) {
res = move_window(dj->fs, dj->sect);
if (res == FR_OK) {
*dj->dir = 0xE5; /* Mark the entry "deleted" */
dj->fs->wflag = 1;
}
}
#endif
return res;
}
#endif /* !_FS_READONLY */
/*-----------------------------------------------------------------------*/
/* Pick a segment and create the object name in directory form */
/*-----------------------------------------------------------------------*/
static
FRESULT create_name (
DIR *dj, /* Pointer to the directory object */
const TCHAR **path /* Pointer to pointer to the segment in the path string */
)
{
#ifdef _EXCVT
static const BYTE excvt[] = _EXCVT; /* Upper conversion table for extended chars */
#endif
#if _USE_LFN /* LFN configuration */
BYTE b, cf;
WCHAR w, *lfn;
int i, ni, si, di;
const TCHAR *p;
/* Create LFN in Unicode */
si = di = 0;
p = *path;
lfn = dj->lfn;
for (;;) {
w = p[si++]; /* Get a character */
if (w < ' ' || w == '/' || w == '\\') break; /* Break on end of segment */
if (di >= _MAX_LFN) /* Reject too long name */
return FR_INVALID_NAME;
#if !_LFN_UNICODE
w &= 0xFF;
if (IsDBCS1(w)) { /* If it is a DBC 1st byte */
b = p[si++]; /* Get 2nd byte */
if (!IsDBCS2(b)) /* Reject invalid code for DBC */
return FR_INVALID_NAME;
w = (w << 8) + b;
}
w = ff_convert(w, 1); /* Convert OEM to Unicode */
if (!w) return FR_INVALID_NAME; /* Reject invalid code */
#endif
if (w < 0x80 && chk_chr("\"*:<>\?|\x7F", w)) /* Reject illegal chars for LFN */
return FR_INVALID_NAME;
lfn[di++] = w; /* Store the Unicode char */
}
*path = &p[si]; /* Return pointer to the next segment */
cf = (w < ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
#if _FS_RPATH
if ((di == 1 && lfn[di - 1] == '.') || /* Is this a dot entry? */
(di == 2 && lfn[di - 1] == '.' && lfn[di - 2] == '.')) {
lfn[di] = 0;
for (i = 0; i < 11; i++)
dj->fn[i] = (i < di) ? '.' : ' ';
dj->fn[i] = cf | NS_DOT; /* This is a dot entry */
return FR_OK;
}
#endif
while (di) { /* Strip trailing spaces and dots */
w = lfn[di - 1];
if (w != ' ' && w != '.') break;
di--;
}
if (!di) return FR_INVALID_NAME; /* Reject nul string */
lfn[di] = 0; /* LFN is created */
/* Create SFN in directory form */
mem_set(dj->fn, ' ', 11);
for (si = 0; lfn[si] == ' ' || lfn[si] == '.'; si++) ; /* Strip leading spaces and dots */
if (si) cf |= NS_LOSS | NS_LFN;
while (di && lfn[di - 1] != '.') di--; /* Find extension (di<=si: no extension) */
b = i = 0; ni = 8;
for (;;) {
w = lfn[si++]; /* Get an LFN char */
if (!w) break; /* Break on end of the LFN */
if (w == ' ' || (w == '.' && si != di)) { /* Remove spaces and dots */
cf |= NS_LOSS | NS_LFN; continue;
}
if (i >= ni || si == di) { /* Extension or end of SFN */
if (ni == 11) { /* Long extension */
cf |= NS_LOSS | NS_LFN; break;
}
if (si != di) cf |= NS_LOSS | NS_LFN; /* Out of 8.3 format */
if (si > di) break; /* No extension */
si = di; i = 8; ni = 11; /* Enter extension section */
b <<= 2; continue;
}
if (w >= 0x80) { /* Non ASCII char */
#ifdef _EXCVT
w = ff_convert(w, 0); /* Unicode -> OEM code */
if (w) w = excvt[w - 0x80]; /* Convert extended char to upper (SBCS) */
#else
w = ff_convert(ff_wtoupper(w), 0); /* Upper converted Unicode -> OEM code */
#endif
cf |= NS_LFN; /* Force create LFN entry */
}
if (_DF1S && w >= 0x100) { /* Double byte char */
if (i >= ni - 1) {
cf |= NS_LOSS | NS_LFN; i = ni; continue;
}
dj->fn[i++] = (BYTE)(w >> 8);
} else { /* Single byte char */
if (!w || chk_chr("+,;=[]", w)) { /* Replace illegal chars for SFN */
w = '_'; cf |= NS_LOSS | NS_LFN; /* Lossy conversion */
} else {
if (IsUpper(w)) { /* ASCII large capital */
b |= 2;
} else {
if (IsLower(w)) { /* ASCII small capital */
b |= 1; w -= 0x20;
}
}
}
}
dj->fn[i++] = (BYTE)w;
}
if (dj->fn[0] == 0xE5) dj->fn[0] = 0x05; /* If the first char collides with deleted mark, replace it with 0x05 */
if (ni == 8) b <<= 2;
if ((b & 0x0C) == 0x0C || (b & 0x03) == 0x03) /* Create LFN entry when there are composite capitals */
cf |= NS_LFN;
if (!(cf & NS_LFN)) { /* When LFN is in 8.3 format without extended char, NT flags are created */
if ((b & 0x03) == 0x01) cf |= NS_EXT; /* NT flag (Extension has only small capital) */
if ((b & 0x0C) == 0x04) cf |= NS_BODY; /* NT flag (Filename has only small capital) */
}
dj->fn[NS] = cf; /* SFN is created */
return FR_OK;
#else /* Non-LFN configuration */
BYTE b, c, d, *sfn;
int ni, si, i;
const char *p;
/* Create file name in directory form */
sfn = dj->fn;
mem_set(sfn, ' ', 11);
si = i = b = 0; ni = 8;
p = *path;
#if _FS_RPATH
if (p[si] == '.') { /* Is this a dot entry? */
for (;;) {
c = (BYTE)p[si++];
if (c != '.' || si >= 3) break;
sfn[i++] = c;
}
if (c != '/' && c != '\\' && c > ' ') return FR_INVALID_NAME;
*path = &p[si]; /* Return pointer to the next segment */
sfn[NS] = (c <= ' ') ? NS_LAST | NS_DOT : NS_DOT; /* Set last segment flag if end of path */
return FR_OK;
}
#endif
for (;;) {
c = (BYTE)p[si++];
if (c <= ' ' || c == '/' || c == '\\') break; /* Break on end of segment */
if (c == '.' || i >= ni) {
if (ni != 8 || c != '.') return FR_INVALID_NAME;
i = 8; ni = 11;
b <<= 2; continue;
}
if (c >= 0x80) { /* Extended char */
#ifdef _EXCVT
c = excvt[c - 0x80]; /* Convert extend char (SBCS) */
#else
b |= 3; /* Eliminate NT flag if extended char is exist */
#if !_DF1S /* ASCII only cfg */
return FR_INVALID_NAME;
#endif
#endif
}
if (IsDBCS1(c)) { /* DBC 1st byte? */
d = (BYTE)p[si++]; /* Get 2nd byte */
if (!IsDBCS2(d) || i >= ni - 1) /* Reject invalid DBC */
return FR_INVALID_NAME;
sfn[i++] = c;
sfn[i++] = d;
} else { /* Single byte code */
if (chk_chr("\"*+,:<=>\?[]|\x7F", c)) /* Reject illegal chrs for SFN */
return FR_INVALID_NAME;
if (IsUpper(c)) { /* ASCII large capital? */
b |= 2;
} else {
if (IsLower(c)) { /* ASCII small capital? */
b |= 1; c -= 0x20;
}
}
sfn[i++] = c;
}
}
*path = &p[si]; /* Return pointer to the next segment */
c = (c <= ' ') ? NS_LAST : 0; /* Set last segment flag if end of path */
if (!i) return FR_INVALID_NAME; /* Reject nul string */
if (sfn[0] == 0xE5) sfn[0] = 0x05; /* When first char collides with 0xE5, replace it with 0x05 */
if (ni == 8) b <<= 2;
if ((b & 0x03) == 0x01) c |= NS_EXT; /* NT flag (Name extension has only small capital) */
if ((b & 0x0C) == 0x04) c |= NS_BODY; /* NT flag (Name body has only small capital) */
sfn[NS] = c; /* Store NT flag, File name is created */
return FR_OK;
#endif
}
/*-----------------------------------------------------------------------*/
/* Get file information from directory entry */
/*-----------------------------------------------------------------------*/
#if _FS_MINIMIZE <= 1
static
void get_fileinfo ( /* No return code */
DIR *dj, /* Pointer to the directory object */
FILINFO *fno /* Pointer to the file information to be filled */
)
{
int i;
BYTE nt, *dir;
TCHAR *p, c;
p = fno->fname;
if (dj->sect) {
dir = dj->dir;
nt = dir[DIR_NTres]; /* NT flag */
for (i = 0; i < 8; i++) { /* Copy name body */
c = dir[i];
if (c == ' ') break;
if (c == 0x05) c = (TCHAR)0xE5;
if (_USE_LFN && (nt & NS_BODY) && IsUpper(c)) c += 0x20;
#if _LFN_UNICODE
if (IsDBCS1(c) && i < 7 && IsDBCS2(dir[i + 1]))
c = (c << 8) | dir[++i];
c = ff_convert(c, 1);
if (!c) c = '?';
#endif
*p++ = c;
}
if (dir[8] != ' ') { /* Copy name extension */
*p++ = '.';
for (i = 8; i < 11; i++) {
c = dir[i];
if (c == ' ') break;
if (_USE_LFN && (nt & NS_EXT) && IsUpper(c)) c += 0x20;
#if _LFN_UNICODE
if (IsDBCS1(c) && i < 10 && IsDBCS2(dir[i + 1]))
c = (c << 8) | dir[++i];
c = ff_convert(c, 1);
if (!c) c = '?';
#endif
*p++ = c;
}
}
fno->fattrib = dir[DIR_Attr]; /* Attribute */
fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */
fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */
fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */
}
*p = 0;
#if _USE_LFN
if (fno->lfname) {
TCHAR *tp = fno->lfname;
WCHAR w, *lfn;
i = 0;
if (dj->sect && dj->lfn_idx != 0xFFFF) {/* Get LFN if available */
lfn = dj->lfn;
while ((w = *lfn++) != 0) { /* Get an LFN char */
#if !_LFN_UNICODE
w = ff_convert(w, 0); /* Unicode -> OEM conversion */
if (!w) { i = 0; break; } /* Could not convert, no LFN */
if (_DF1S && w >= 0x100) /* Put 1st byte if it is a DBC */
tp[i++] = (TCHAR)(w >> 8);
#endif
if (i >= fno->lfsize - 1) { i = 0; break; } /* Buffer overrun, no LFN */
tp[i++] = (TCHAR)w;
}
}
tp[i] = 0; /* Terminator */
}
#endif
}
#endif /* _FS_MINIMIZE <= 1 */
/*-----------------------------------------------------------------------*/
/* Follow a file path */
/*-----------------------------------------------------------------------*/
static
FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */
DIR *dj, /* Directory object to return last directory and found object */
const TCHAR *path /* Full-path string to find a file or directory */
)
{
FRESULT res;
BYTE *dir, ns;
#if _FS_RPATH
if (*path == '/' || *path == '\\') { /* There is a heading separator */
path++; dj->sclust = 0; /* Strip it and start from the root dir */
} else { /* No heading separator */
dj->sclust = dj->fs->cdir; /* Start from the current dir */
}
#else
if (*path == '/' || *path == '\\') /* Strip heading separator if exist */
path++;
dj->sclust = 0; /* Start from the root dir */
#endif
if ((UINT)*path < ' ') { /* Nul path means the start directory itself */
res = dir_sdi(dj, 0);
dj->dir = 0;
} else { /* Follow path */
for (;;) {
res = create_name(dj, &path); /* Get a segment */
if (res != FR_OK) break;
res = dir_find(dj); /* Find it */
ns = *(dj->fn+NS);
if (res != FR_OK) { /* Failed to find the object */
if (res != FR_NO_FILE) break; /* Abort if any hard error occured */
/* Object not found */
if (_FS_RPATH && (ns & NS_DOT)) { /* If dot entry is not exit */
dj->sclust = 0; dj->dir = 0; /* It is the root dir */
res = FR_OK;
if (!(ns & NS_LAST)) continue;
} else { /* Could not find the object */
if (!(ns & NS_LAST)) res = FR_NO_PATH;
}
break;
}
if (ns & NS_LAST) break; /* Last segment match. Function completed. */
dir = dj->dir; /* There is next segment. Follow the sub directory */
if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */
res = FR_NO_PATH; break;
}
dj->sclust = ((DWORD)LD_WORD(dir+DIR_FstClusHI) << 16) | LD_WORD(dir+DIR_FstClusLO);
}
}
return res;
}
/*-----------------------------------------------------------------------*/
/* Load boot record and check if it is an FAT boot record */
/*-----------------------------------------------------------------------*/
static
BYTE check_fs ( /* 0:The FAT BR, 1:Valid BR but not an FAT, 2:Not a BR, 3:Disk error */
FATFS *fs, /* File system object */
DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */
)
{
if (disk_read(fs->drv, fs->win, sect, 1) != RES_OK) /* Load boot record */
return 3;
if (LD_WORD(&fs->win[BS_55AA]) != 0xAA55) /* Check record signature (always placed at offset 510 even if the sector size is >512) */
return 2;
if ((LD_DWORD(&fs->win[BS_FilSysType]) & 0xFFFFFF) == 0x544146) /* Check "FAT" string */
return 0;
if ((LD_DWORD(&fs->win[BS_FilSysType32]) & 0xFFFFFF) == 0x544146)
return 0;
return 1;
}
/*-----------------------------------------------------------------------*/
/* Make sure that the file system is valid */
/*-----------------------------------------------------------------------*/
static
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -