📄 ff.c
字号:
dj->index = idx;
clst = dj->sclust;
if (clst == 1 || clst >= dj->fs->n_fatent) /* Check start cluster range */
return FR_INT_ERR;
if (!clst && dj->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */
clst = dj->fs->dirbase;
if (clst == 0) { /* Static table (root-dir in FAT12/16) */
dj->clust = clst;
if (idx >= dj->fs->n_rootdir) /* Index is out of range */
return FR_INT_ERR;
dj->sect = dj->fs->dirbase + idx / (SS(dj->fs) / 32); /* Sector# */
}
else { /* Dynamic table (sub-dirs or root-dir in FAT32) */
ic = SS(dj->fs) / 32 * dj->fs->csize; /* Entries per cluster */
while (idx >= ic) { /* Follow cluster chain */
clst = get_fat(dj->fs, clst); /* Get next cluster */
if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
if (clst < 2 || clst >= dj->fs->n_fatent) /* Reached to end of table or int error */
return FR_INT_ERR;
idx -= ic;
}
dj->clust = clst;
dj->sect = clust2sect(dj->fs, clst) + idx / (SS(dj->fs) / 32); /* Sector# */
}
dj->dir = dj->fs->win + (idx % (SS(dj->fs) / 32)) * 32; /* Ptr to the entry in the sector */
return FR_OK; /* Seek succeeded */
}
/*-----------------------------------------------------------------------*/
/* Directory handling - Move directory index next */
/*-----------------------------------------------------------------------*/
static
FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not stretch */
DIR *dj, /* Pointer to directory object */
int stretch /* 0: Do not stretch table, 1: Stretch table if needed */
)
{
DWORD clst;
WORD i;
i = dj->index + 1;
if (!i || !dj->sect) /* Report EOT when index has reached 65535 */
return FR_NO_FILE;
if (!(i % (SS(dj->fs) / 32))) { /* Sector changed? */
dj->sect++; /* Next sector */
if (dj->clust == 0) { /* Static table */
if (i >= dj->fs->n_rootdir) /* Report EOT when end of table */
return FR_NO_FILE;
}
else { /* Dynamic table */
if (((i / (SS(dj->fs) / 32)) & (dj->fs->csize - 1)) == 0) { /* Cluster changed? */
clst = get_fat(dj->fs, dj->clust); /* Get next cluster */
if (clst <= 1) return FR_INT_ERR;
if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
if (clst >= dj->fs->n_fatent) { /* When it reached end of dynamic table */
#if !_FS_READONLY
BYTE c;
if (!stretch) return FR_NO_FILE; /* When do not stretch, report EOT */
clst = create_chain(dj->fs, dj->clust); /* Stretch cluster chain */
if (clst == 0) return FR_DENIED; /* No free cluster */
if (clst == 1) return FR_INT_ERR;
if (clst == 0xFFFFFFFF) return FR_DISK_ERR;
/* Clean-up stretched table */
if (move_window(dj->fs, 0)) return FR_DISK_ERR; /* Flush active window */
mem_set(dj->fs->win, 0, SS(dj->fs)); /* Clear window buffer */
dj->fs->winsect = clust2sect(dj->fs, clst); /* Cluster start sector */
for (c = 0; c < dj->fs->csize; c++) { /* Fill the new cluster with 0 */
dj->fs->wflag = 1;
if (move_window(dj->fs, 0)) return FR_DISK_ERR;
dj->fs->winsect++;
}
dj->fs->winsect -= c; /* Rewind window address */
#else
return FR_NO_FILE; /* Report EOT */
#endif
}
dj->clust = clst; /* Initialize data for new cluster */
dj->sect = clust2sect(dj->fs, clst);
}
}
}
dj->index = i;
dj->dir = dj->fs->win + (i % (SS(dj->fs) / 32)) * 32;
return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* LFN handling - Test/Pick/Fit an LFN segment from/to directory entry */
/*-----------------------------------------------------------------------*/
#if _USE_LFN
static
const BYTE LfnOfs[] = {1,3,5,7,9,14,16,18,20,22,24,28,30}; /* Offset of LFN chars in the directory entry */
static
int cmp_lfn ( /* 1:Matched, 0:Not matched */
WCHAR *lfnbuf, /* Pointer to the LFN to be compared */
BYTE *dir /* Pointer to the directory entry containing a part of LFN */
)
{
UINT i, s;
WCHAR wc, uc;
i = ((dir[LDIR_Ord] & 0xBF) - 1) * 13; /* Get offset in the LFN buffer */
s = 0; wc = 1;
do {
uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
if (wc) { /* Last char has not been processed */
wc = ff_wtoupper(uc); /* Convert it to upper case */
if (i >= _MAX_LFN || wc != ff_wtoupper(lfnbuf[i++])) /* Compare it */
return 0; /* Not matched */
} else {
if (uc != 0xFFFF) return 0; /* Check filler */
}
} while (++s < 13); /* Repeat until all chars in the entry are checked */
if ((dir[LDIR_Ord] & 0x40) && wc && lfnbuf[i]) /* Last segment matched but different length */
return 0;
return 1; /* The part of LFN matched */
}
static
int pick_lfn ( /* 1:Succeeded, 0:Buffer overflow */
WCHAR *lfnbuf, /* Pointer to the Unicode-LFN buffer */
BYTE *dir /* Pointer to the directory entry */
)
{
UINT i, s;
WCHAR wc, uc;
i = ((dir[LDIR_Ord] & 0x3F) - 1) * 13; /* Offset in the LFN buffer */
s = 0; wc = 1;
do {
uc = LD_WORD(dir+LfnOfs[s]); /* Pick an LFN character from the entry */
if (wc) { /* Last char has not been processed */
if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
lfnbuf[i++] = wc = uc; /* Store it */
} else {
if (uc != 0xFFFF) return 0; /* Check filler */
}
} while (++s < 13); /* Read all character in the entry */
if (dir[LDIR_Ord] & 0x40) { /* Put terminator if it is the last LFN part */
if (i >= _MAX_LFN) return 0; /* Buffer overflow? */
lfnbuf[i] = 0;
}
return 1;
}
#if !_FS_READONLY
static
void fit_lfn (
const WCHAR *lfnbuf, /* Pointer to the LFN buffer */
BYTE *dir, /* Pointer to the directory entry */
BYTE ord, /* LFN order (1-20) */
BYTE sum /* SFN sum */
)
{
UINT i, s;
WCHAR wc;
dir[LDIR_Chksum] = sum; /* Set check sum */
dir[LDIR_Attr] = AM_LFN; /* Set attribute. LFN entry */
dir[LDIR_Type] = 0;
ST_WORD(dir+LDIR_FstClusLO, 0);
i = (ord - 1) * 13; /* Get offset in the LFN buffer */
s = wc = 0;
do {
if (wc != 0xFFFF) wc = lfnbuf[i++]; /* Get an effective char */
ST_WORD(dir+LfnOfs[s], wc); /* Put it */
if (!wc) wc = 0xFFFF; /* Padding chars following last char */
} while (++s < 13);
if (wc == 0xFFFF || !lfnbuf[i]) ord |= 0x40; /* Bottom LFN part is the start of LFN sequence */
dir[LDIR_Ord] = ord; /* Set the LFN order */
}
#endif
#endif
/*-----------------------------------------------------------------------*/
/* Create numbered name */
/*-----------------------------------------------------------------------*/
#if _USE_LFN
void gen_numname (
BYTE *dst, /* Pointer to generated SFN */
const BYTE *src, /* Pointer to source SFN to be modified */
const WCHAR *lfn, /* Pointer to LFN */
WORD seq /* Sequence number */
)
{
BYTE ns[8], c;
UINT i, j;
mem_cpy(dst, src, 11);
if (seq > 5) { /* On many collisions, generate a hash number instead of sequential number */
do seq = (seq >> 1) + (seq << 15) + (WORD)*lfn++; while (*lfn);
}
/* itoa */
i = 7;
do {
c = (seq % 16) + '0';
if (c > '9') c += 7;
ns[i--] = c;
seq /= 16;
} while (seq);
ns[i] = '~';
/* Append the number */
for (j = 0; j < i && dst[j] != ' '; j++) {
if (IsDBCS1(dst[j])) {
if (j == i - 1) break;
j++;
}
}
do {
dst[j++] = (i < 8) ? ns[i++] : ' ';
} while (j < 8);
}
#endif
/*-----------------------------------------------------------------------*/
/* Calculate sum of an SFN */
/*-----------------------------------------------------------------------*/
#if _USE_LFN
static
BYTE sum_sfn (
const BYTE *dir /* Ptr to directory entry */
)
{
BYTE sum = 0;
UINT n = 11;
do sum = (sum >> 1) + (sum << 7) + *dir++; while (--n);
return sum;
}
#endif
/*-----------------------------------------------------------------------*/
/* Directory handling - Find an object in the directory */
/*-----------------------------------------------------------------------*/
static
FRESULT dir_find (
DIR *dj /* Pointer to the directory object linked to the file name */
)
{
FRESULT res;
BYTE c, *dir;
#if _USE_LFN
BYTE a, ord, sum;
#endif
res = dir_sdi(dj, 0); /* Rewind directory object */
if (res != FR_OK) return res;
#if _USE_LFN
ord = sum = 0xFF;
#endif
do {
res = move_window(dj->fs, dj->sect);
if (res != FR_OK) break;
dir = dj->dir; /* Ptr to the directory entry of current index */
c = dir[DIR_Name];
if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
#if _USE_LFN /* LFN configuration */
a = dir[DIR_Attr] & AM_MASK;
if (c == 0xE5 || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
ord = 0xFF;
} else {
if (a == AM_LFN) { /* An LFN entry is found */
if (dj->lfn) {
if (c & 0x40) { /* Is it start of LFN sequence? */
sum = dir[LDIR_Chksum];
c &= 0xBF; ord = c; /* LFN start order */
dj->lfn_idx = dj->index;
}
/* Check validity of the LFN entry and compare it with given name */
ord = (c == ord && sum == dir[LDIR_Chksum] && cmp_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
}
} else { /* An SFN entry is found */
if (!ord && sum == sum_sfn(dir)) break; /* LFN matched? */
ord = 0xFF; dj->lfn_idx = 0xFFFF; /* Reset LFN sequence */
if (!(dj->fn[NS] & NS_LOSS) && !mem_cmp(dir, dj->fn, 11)) break; /* SFN matched? */
}
}
#else /* Non LFN configuration */
if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */
break;
#endif
res = dir_next(dj, 0); /* Next entry */
} while (res == FR_OK);
return res;
}
/*-----------------------------------------------------------------------*/
/* Read an object from the directory */
/*-----------------------------------------------------------------------*/
#if _FS_MINIMIZE <= 1
static
FRESULT dir_read (
DIR *dj /* Pointer to the directory object that pointing the entry to be read */
)
{
FRESULT res;
BYTE c, *dir;
#if _USE_LFN
BYTE a, ord = 0xFF, sum = 0xFF;
#endif
res = FR_NO_FILE;
while (dj->sect) {
res = move_window(dj->fs, dj->sect);
if (res != FR_OK) break;
dir = dj->dir; /* Ptr to the directory entry of current index */
c = dir[DIR_Name];
if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */
#if _USE_LFN /* LFN configuration */
a = dir[DIR_Attr] & AM_MASK;
if (c == 0xE5 || (!_FS_RPATH && c == '.') || ((a & AM_VOL) && a != AM_LFN)) { /* An entry without valid data */
ord = 0xFF;
} else {
if (a == AM_LFN) { /* An LFN entry is found */
if (c & 0x40) { /* Is it start of LFN sequence? */
sum = dir[LDIR_Chksum];
c &= 0xBF; ord = c;
dj->lfn_idx = dj->index;
}
/* Check LFN validity and capture it */
ord = (c == ord && sum == dir[LDIR_Chksum] && pick_lfn(dj->lfn, dir)) ? ord - 1 : 0xFF;
} else { /* An SFN entry is found */
if (ord || sum != sum_sfn(dir)) /* Is there a valid LFN? */
dj->lfn_idx = 0xFFFF; /* It has no LFN. */
break;
}
}
#else /* Non LFN configuration */
if (c != 0xE5 && (_FS_RPATH || c != '.') && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */
break;
#endif
res = dir_next(dj, 0); /* Next entry */
if (res != FR_OK) break;
}
if (res != FR_OK) dj->sect = 0;
return res;
}
#endif
/*-----------------------------------------------------------------------*/
/* Register an object to the directory */
/*-----------------------------------------------------------------------*/
#if !_FS_READONLY
static
FRESULT dir_register ( /* FR_OK:Successful, FR_DENIED:No free entry or too many SFN collision, FR_DISK_ERR:Disk error */
DIR *dj /* Target directory with object name to be created */
)
{
FRESULT res;
BYTE c, *dir;
#if _USE_LFN /* LFN configuration */
WORD n, ne, is;
BYTE sn[12], *fn, sum;
WCHAR *lfn;
fn = dj->fn; lfn = dj->lfn;
mem_cpy(sn, fn, 12);
if (_FS_RPATH && (sn[NS] & NS_DOT)) /* Cannot create dot entry */
return FR_INVALID_NAME;
if (sn[NS] & NS_LOSS) { /* When LFN is out of 8.3 format, generate a numbered name */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -