?? tff.c
字號:
if (clust < 2 || clust >= fp->fs->max_clust) goto fr_error;
fp->curr_clust = clust; /* Update current cluster */
fp->csect = 0; /* Reset sector address in the cluster */
}
sect = clust2sect(fp->curr_clust) + fp->csect; /* Get current sector */
cc = btr / 512U; /* When remaining bytes >= sector size, */
if (cc) { /* Read maximum contiguous sectors directly */
if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
cc = fp->fs->csize - fp->csect;
if (disk_read(0, rbuff, sect, (BYTE)cc) != RES_OK)
goto fr_error;
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
rcnt = 512U * cc; /* Number of bytes transferred */
continue;
}
fp->csect++; /* Next sector address in the cluster */
}
sect = clust2sect(fp->curr_clust) + fp->csect - 1; /* Get current sector */
if (!move_window(sect)) goto fr_error; /* Move sector window */
rcnt = 512U - (fp->fptr % 512U); /* Get partial sector from sector window */
if (rcnt > btr) rcnt = btr;
memcpy(rbuff, &fp->fs->win[fp->fptr % 512U], rcnt);
}
return FR_OK;
fr_error: /* Abort this file due to an unrecoverable error */
fp->flag |= FA__ERROR;
return FR_RW_ERROR;
}
#if !_FS_READONLY
/*-----------------------------------------------------------------------*/
/* Write File */
/*-----------------------------------------------------------------------*/
FRESULT f_write (
FIL *fp, /* Pointer to the file object */
const void *buff, /* Pointer to the data to be written */
UINT btw, /* Number of bytes to write */
UINT *bw /* Pointer to number of bytes written */
)
{
FRESULT res;
DWORD sect;
UINT wcnt, cc;
CLUST clust;
const BYTE *wbuff = buff;
*bw = 0;
res = validate(fp->fs, fp->id); /* Check validity of the object */
if (res != FR_OK) return res;
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
if (fp->fsize + btw < fp->fsize) return FR_OK; /* File size cannot reach 4GB */
for ( ; btw; /* Repeat until all data transferred */
wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
if ((fp->fptr % 512U) == 0) { /* On the sector boundary? */
if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
if (fp->fptr == 0) { /* On the top of the file? */
clust = fp->org_clust; /* Follow from the origin */
if (clust == 0) /* When there is no cluster chain, */
fp->org_clust = clust = create_chain(0); /* Create a new cluster chain */
} else { /* Middle or end of the file */
clust = create_chain(fp->curr_clust); /* Trace or streach cluster chain */
}
if (clust == 0) break; /* Could not allocate a new cluster (disk full) */
if (clust == 1 || clust >= fp->fs->max_clust) goto fw_error;
fp->curr_clust = clust; /* Update current cluster */
fp->csect = 0; /* Reset sector address in the cluster */
}
sect = clust2sect(fp->curr_clust) + fp->csect; /* Get current sector */
cc = btw / 512U; /* When remaining bytes >= sector size, */
if (cc) { /* Write maximum contiguous sectors directly */
if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
cc = fp->fs->csize - fp->csect;
if (disk_write(0, wbuff, sect, (BYTE)cc) != RES_OK)
goto fw_error;
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
wcnt = 512U * cc; /* Number of bytes transferred */
continue;
}
if (fp->fptr >= fp->fsize) { /* Flush R/W window without reading if needed */
if (!move_window(0)) goto fw_error;
fp->fs->winsect = sect;
}
fp->csect++; /* Next sector address in the cluster */
}
sect = clust2sect(fp->curr_clust) + fp->csect - 1; /* Get current sector */
if (!move_window(sect)) goto fw_error; /* Move sector window */
wcnt = 512U - (fp->fptr % 512U); /* Put partial sector into sector window */
if (wcnt > btw) wcnt = btw;
memcpy(&fp->fs->win[fp->fptr % 512U], wbuff, wcnt);
fp->fs->winflag = 1;
}
if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
fp->flag |= FA__WRITTEN; /* Set file changed flag */
return res;
fw_error: /* Abort this file due to an unrecoverable error */
fp->flag |= FA__ERROR;
return FR_RW_ERROR;
}
/*-----------------------------------------------------------------------*/
/* Synchronize the file object */
/*-----------------------------------------------------------------------*/
FRESULT f_sync (
FIL *fp /* Pointer to the file object */
)
{
FRESULT res;
DWORD tim;
BYTE *dir;
res = validate(fp->fs, fp->id); /* Check validity of the object */
if (res == FR_OK) {
if (fp->flag & FA__WRITTEN) { /* Has the file been written? */
/* Update the directory entry */
if (!move_window(fp->dir_sect))
return FR_RW_ERROR;
dir = fp->dir_ptr;
dir[DIR_Attr] |= AM_ARC; /* Set archive bit */
ST_DWORD(&dir[DIR_FileSize], fp->fsize); /* Update file size */
ST_WORD(&dir[DIR_FstClusLO], fp->org_clust); /* Update start cluster */
#if _FAT32
ST_WORD(&dir[DIR_FstClusHI], fp->org_clust >> 16);
#endif
tim = get_fattime(); /* Updated time */
ST_DWORD(&dir[DIR_WrtTime], tim);
fp->flag &= (BYTE)~FA__WRITTEN;
res = sync();
}
}
return res;
}
#endif /* !_FS_READONLY */
/*-----------------------------------------------------------------------*/
/* Close File */
/*-----------------------------------------------------------------------*/
FRESULT f_close (
FIL *fp /* Pointer to the file object to be closed */
)
{
FRESULT res;
#if !_FS_READONLY
res = f_sync(fp);
#else
res = validate(fp->fs, fp->id);
#endif
if (res == FR_OK) fp->fs = NULL;
return res;
}
#if _FS_MINIMIZE <= 2
/*-----------------------------------------------------------------------*/
/* Seek File R/W Pointer */
/*-----------------------------------------------------------------------*/
FRESULT f_lseek (
FIL *fp, /* Pointer to the file object */
DWORD ofs /* File pointer from top of file */
)
{
FRESULT res;
CLUST clust;
DWORD csize, ifptr;
res = validate(fp->fs, fp->id); /* Check validity of the object */
if (res != FR_OK) return res;
if (fp->flag & FA__ERROR) return FR_RW_ERROR;
if (ofs > fp->fsize /* In read-only mode, clip offset with the file size */
#if !_FS_READONLY
&& !(fp->flag & FA_WRITE)
#endif
) ofs = fp->fsize;
ifptr = fp->fptr;
fp->fptr = 0; fp->csect = 255;
if (ofs > 0) {
csize = (DWORD)fp->fs->csize * 512U; /* Cluster size (byte) */
if (ifptr > 0 &&
(ofs - 1) / csize >= (ifptr - 1) / csize) {/* When seek to same or following cluster, */
fp->fptr = (ifptr - 1) & ~(csize - 1); /* start from the current cluster */
ofs -= fp->fptr;
clust = fp->curr_clust;
} else { /* When seek to back cluster, */
clust = fp->org_clust; /* start from the first cluster */
#if !_FS_READONLY
if (clust == 0) { /* If no cluster chain, create a new chain */
clust = create_chain(0);
if (clust == 1) goto fk_error;
fp->org_clust = clust;
}
#endif
fp->curr_clust = clust;
}
if (clust != 0) {
while (ofs > csize) { /* Cluster following loop */
#if !_FS_READONLY
if (fp->flag & FA_WRITE) { /* Check if in write mode or not */
clust = create_chain(clust); /* Force streached if in write mode */
if (clust == 0) { /* When disk gets full, clip file size */
ofs = csize; break;
}
} else
#endif
clust = get_cluster(clust); /* Follow cluster chain if not in write mode */
if (clust < 2 || clust >= fp->fs->max_clust) goto fk_error;
fp->curr_clust = clust;
fp->fptr += csize;
ofs -= csize;
}
fp->fptr += ofs;
fp->csect = (BYTE)(ofs / 512U); /* Sector offset in the cluster */
if (ofs % 512U) fp->csect++;
}
}
#if !_FS_READONLY
if (fp->fptr > fp->fsize) { /* Set changed flag if the file was extended */
fp->fsize = fp->fptr;
fp->flag |= FA__WRITTEN;
}
#endif
return FR_OK;
fk_error: /* Abort this file due to an unrecoverable error */
fp->flag |= FA__ERROR;
return FR_RW_ERROR;
}
#if _FS_MINIMIZE <= 1
/*-----------------------------------------------------------------------*/
/* Create a directroy object */
/*-----------------------------------------------------------------------*/
FRESULT f_opendir (
DIR *dj, /* Pointer to directory object to create */
const char *path /* Pointer to the directory path */
)
{
FRESULT res;
BYTE *dir;
char fn[8+3+1];
res = auto_mount(&path, 0);
if (res == FR_OK) {
res = trace_path(dj, fn, path, &dir); /* Trace the directory path */
if (res == FR_OK) { /* Trace completed */
if (dir) { /* It is not the root dir */
if (dir[DIR_Attr] & AM_DIR) { /* The entry is a directory */
dj->clust =
#if _FAT32
((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) |
#endif
LD_WORD(&dir[DIR_FstClusLO]);
dj->sect = clust2sect(dj->clust);
dj->index = 2;
} else { /* The entry is not a directory */
res = FR_NO_FILE;
}
}
dj->id = dj->fs->id;
}
}
return res;
}
/*-----------------------------------------------------------------------*/
/* Read Directory Entry in Sequense */
/*-----------------------------------------------------------------------*/
FRESULT f_readdir (
DIR *dj, /* Pointer to the directory object */
FILINFO *finfo /* Pointer to file information to return */
)
{
FRESULT res;
BYTE *dir, c;
res = validate(dj->fs, dj->id); /* Check validity of the object */
if (res != FR_OK) return res;
finfo->fname[0] = 0;
while (dj->sect) {
if (!move_window(dj->sect))
return FR_RW_ERROR;
dir = &dj->fs->win[(dj->index & 15) * 32]; /* pointer to the directory entry */
c = dir[DIR_Name];
if (c == 0) break; /* Has it reached to end of dir? */
if (c != 0xE5 && !(dir[DIR_Attr] & AM_VOL)) /* Is it a valid entry? */
get_fileinfo(finfo, dir);
if (!next_dir_entry(dj)) dj->sect = 0; /* Next entry */
if (finfo->fname[0]) break; /* Found valid entry */
}
return FR_OK;
}
#if _FS_MINIMIZE == 0
/*-----------------------------------------------------------------------*/
/* Get File Status */
/*-----------------------------------------------------------------------*/
FRESULT f_stat (
const char *path, /* Pointer to the file path */
FILINFO *finfo /* Pointer to file information to return */
)
{
FRESULT res;
DIR dj;
BYTE *dir;
char fn[8+3+1];
res = auto_mount(&path, 0);
if (res == FR_OK) {
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
if (res == FR_OK) { /* Trace completed */
if (dir) /* Found an object */
get_fileinfo(finfo, dir);
else /* It is root dir */
res = FR_INVALID_NAME;
}
}
return res;
}
#if !_FS_READONLY
/*-----------------------------------------------------------------------*/
/* Truncate File */
/*-----------------------------------------------------------------------*/
FRESULT f_truncate (
FIL *fp /* Pointer to the file object */
)
{
FRESULT res;
CLUST ncl;
res = validate(fp->fs, fp->id); /* Check validity of the object */
if (res != FR_OK) return res;
if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
if (!(fp->flag & FA_WRITE)) return FR_DENIED; /* Check access mode */
if (fp->fsize > fp->fptr) {
fp->fsize = fp->fptr; /* Set file size to current R/W point */
fp->flag |= FA__WRITTEN;
if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */
if (!remove_chain(fp->org_clust)) goto ft_error;
fp->org_clust = 0;
} else { /* When truncate a part of the file, remove remaining clusters */
ncl = get_cluster(fp->curr_clust);
if (ncl < 2) goto ft_error;
if (ncl < fp->fs->max_clust) {
if (!put_cluster(fp->curr_clust, (CLUST)0x0FFFFFFF)) goto ft_error;
if (!remove_chain(ncl)) goto ft_error;
}
}
}
return FR_OK;
ft_error: /* Abort this file due to an unrecoverable error */
fp->flag |= FA__ERROR;
return FR_RW_ERROR;
}
/*-----------------------------------------------------------------------*/
/* Get Number of Free Clusters */
/*-----------------------------------------------------------------------*/
FRESULT f_getfree (
const char *drv, /* Pointer to the logical drive number (root dir) */
DWORD *nclust, /* Pointer to the variable to return number of free clusters */
FATFS **fatfs /* Pointer to pointer to corresponding file system object to return */
)
{
FRESULT res;
FATFS *fs;
DWORD n, sect;
CLUST clust;
BYTE fat, f, *p;
/* Get drive number */
res = auto_mount(&drv, 0);
if (res != FR_OK) return res;
*fatfs = fs = FatFs;
/* If number of free cluster is valid, return it without cluster scan. */
if (fs->free_clust <= fs->max_clust - 2) {
*nclust = fs->free_clust;
return FR_OK;
}
/* Get number of free clusters */
fat = fs->fs_type;
n = 0;
if (fat == FS_FAT12) {
clust = 2;
do {
if ((WORD)get_cluster(clust) == 0) n++;
} while (++clust < fs->max_clust);
} else {
clust = fs->max_clust;
sect = fs->fatbase;
f = 0; p = 0;
do {
if (!f) {
if (!move_window(sect++)) return FR_RW_ERROR;
p = fs->win;
}
if (!_FAT32 || fat == FS_FAT16) {
if (LD_WORD(p) == 0) n++;
p += 2; f += 1;
} else {
if (LD_DWORD(p) == 0) n++;
p += 4; f += 2;
}
} while (--clust);
}
fs->free_clust = n;
#if _USE_FSINFO
if (fat == FS_FAT32) fs->fsi_flag = 1;
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -