📄 ff.c
字号:
fn[NS] = 0; dj->lfn = 0; /* Find only SFN */
for (n = 1; n < 100; n++) {
gen_numname(fn, sn, lfn, n); /* Generate a numbered name */
res = dir_find(dj); /* Check if the name collides with existing SFN */
if (res != FR_OK) break;
}
if (n == 100) return FR_DENIED; /* Abort if too many collisions */
if (res != FR_NO_FILE) return res; /* Abort if the result is other than 'not collided' */
fn[NS] = sn[NS]; dj->lfn = lfn;
}
if (sn[NS] & NS_LFN) { /* When LFN is to be created, reserve an SFN + LFN entries. */
for (ne = 0; lfn[ne]; ne++) ;
ne = (ne + 25) / 13;
} else { /* Otherwise reserve only an SFN entry. */
ne = 1;
}
/* Reserve contiguous entries */
res = dir_sdi(dj, 0);
if (res != FR_OK) return res;
n = is = 0;
do {
res = move_window(dj->fs, dj->sect);
if (res != FR_OK) break;
c = *dj->dir; /* Check the entry status */
if (c == 0xE5 || c == 0) { /* Is it a blank entry? */
if (n == 0) is = dj->index; /* First index of the contiguous entry */
if (++n == ne) break; /* A contiguous entry that required count is found */
} else {
n = 0; /* Not a blank entry. Restart to search */
}
res = dir_next(dj, 1); /* Next entry with table stretch */
} while (res == FR_OK);
if (res == FR_OK && ne > 1) { /* Initialize LFN entry if needed */
res = dir_sdi(dj, is);
if (res == FR_OK) {
sum = sum_sfn(dj->fn); /* Sum of the SFN tied to the LFN */
ne--;
do { /* Store LFN entries in bottom first */
res = move_window(dj->fs, dj->sect);
if (res != FR_OK) break;
fit_lfn(dj->lfn, dj->dir, (BYTE)ne, sum);
dj->fs->wflag = 1;
res = dir_next(dj, 0); /* Next entry */
} while (res == FR_OK && --ne);
}
}
#else /* Non LFN configuration */
res = dir_sdi(dj, 0);
if (res == FR_OK) {
do { /* Find a blank entry for the SFN */
res = move_window(dj->fs, dj->sect);
if (res != FR_OK) break;
c = *dj->dir;
if (c == 0xE5 || c == 0) break; /* Is it a blank entry? */
res = dir_next(dj, 1); /* Next entry with table stretch */
} while (res == FR_OK);
}
#endif
if (res == FR_OK) { /* Initialize the SFN entry */
res = move_window(dj->fs, dj->sect);
if (res == FR_OK) {
dir = dj->dir;
mem_set(dir, 0, 32); /* Clean the entry */
mem_cpy(dir, dj->fn, 11); /* Put SFN */
#if _USE_LFN
dir[DIR_NTres] = *(dj->fn+NS) & (NS_BODY | NS_EXT); /* Put NT flag */
#endif
dj->fs->wflag = 1;
}
}
return res;
}
#endif /* !_FS_READONLY */
/*-----------------------------------------------------------------------*/
/* Remove an object from the directory */
/*-----------------------------------------------------------------------*/
#if !_FS_READONLY && !_FS_MINIMIZE
static
FRESULT dir_remove ( /* FR_OK: Successful, FR_DISK_ERR: A disk error */
DIR *dj /* Directory object pointing the entry to be removed */
)
{
FRESULT res;
#if _USE_LFN /* LFN configuration */
WORD i;
i = dj->index; /* SFN index */
res = dir_sdi(dj, (WORD)((dj->lfn_idx == 0xFFFF) ? i : dj->lfn_idx)); /* Goto the SFN or top of the LFN entries */
if (res == FR_OK) {
do {
res = move_window(dj->fs, dj->sect);
if (res != FR_OK) break;
*dj->dir = 0xE5; /* Mark the entry "deleted" */
dj->fs->wflag = 1;
if (dj->index >= i) break; /* When reached SFN, all entries of the object has been deleted. */
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;
UINT 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)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
b = (BYTE)p[si++]; /* Get 2nd byte */
if (!IsDBCS2(b))
return FR_INVALID_NAME; /* Reject invalid sequence */
w = (w << 8) + b; /* Create a DBC */
}
w = ff_convert(w, 1); /* Convert ANSI/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 (always false on SBCS cfg) */
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;
UINT 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? */
b |= 3; /* Eliminate NT flag */
#ifdef _EXCVT
c = excvt[c-0x80]; /* Upper conversion (SBCS) */
#else
#if !_DF1S /* ASCII only cfg */
return FR_INVALID_NAME;
#endif
#endif
}
if (IsDBCS1(c)) { /* Check if it is a DBC 1st byte (always false on SBCS cfg) */
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 */
)
{
UINT 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; /* Terminate SFN str by a \0 */
#if _USE_LFN
if (fno->lfname && fno->lfsize) {
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 (always false on SBCS cfg) */
tp[i++] = (TCHAR)(w >> 8);
#endif
if (i >= fno->lfsize - 1) { i = 0; break; } /* Buffer overflow, no LFN */
tp[i++] = (TCHAR)w;
}
}
tp[i] = 0; /* Terminate the LFN str by a \0 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -