📄 ff.c[2010-03-16-09-03-03].sfb
字号:
{
if (!fs || !fs->fs_type || fs->id != id)
return FR_INVALID_OBJECT;
ENTER_FF(fs); /* Lock file system */
if (disk_status(fs->drive) & STA_NOINIT)
return FR_NOT_READY;
return FR_OK;
}
/*--------------------------------------------------------------------------
Public Functions
--------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
/* Mount/Unmount a Locical Drive */
/*-----------------------------------------------------------------------*/
FRESULT f_mount (
BYTE vol, /* Logical drive number to be mounted/unmounted */
FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
)
{
FATFS *rfs;
if (vol >= _DRIVES) /* Check if the drive number is valid */
return FR_INVALID_DRIVE;
rfs = FatFs[vol]; /* Get current fs object */
if (rfs) {
#if _FS_REENTRANT /* Discard sync object of the current volume */
if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR;
#endif
rfs->fs_type = 0; /* Clear old fs object */
}
if (fs) {
fs->fs_type = 0; /* Clear new fs object */
#if _FS_REENTRANT /* Create sync object for the new volume */
if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;
#endif
}
FatFs[vol] = fs; /* Register new fs object */
return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* Open or Create a File */
/*-----------------------------------------------------------------------*/
FRESULT f_open (
FIL *fp, /* Pointer to the blank file object */
const XCHAR *path, /* Pointer to the file name */
BYTE mode /* Access mode and file open mode flags */
)
{
FRESULT res;
DIR dj;
NAMEBUF(sfn, lfn);
BYTE *dir;
fp->fs = NULL; /* Clear file object */
#if !_FS_READONLY
mode &= (FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW);
res = chk_mounted(&path, &dj.fs, (BYTE)(mode & (FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)));
#else
mode &= FA_READ;
res = chk_mounted(&path, &dj.fs, 0);
#endif
if (res != FR_OK) LEAVE_FF(dj.fs, res);
INITBUF(dj, sfn, lfn);
res = follow_path(&dj, path); /* Follow the file path */
#if !_FS_READONLY
/* Create or Open a file */
if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
DWORD ps, cl;
if (res != FR_OK) { /* No file, create new */
if (res == FR_NO_FILE) /* There is no file to open, create a new entry */
res = dir_register(&dj);
if (res != FR_OK) LEAVE_FF(dj.fs, res);
mode |= FA_CREATE_ALWAYS;
dir = dj.dir; /* Created entry (SFN entry) */
}
else { /* Any object is already existing */
if (mode & FA_CREATE_NEW) /* Cannot create new */
LEAVE_FF(dj.fs, FR_EXIST);
dir = dj.dir;
if (!dir || (dir[DIR_Attr] & (AM_RDO | AM_DIR))) /* Cannot overwrite it (R/O or DIR) */
LEAVE_FF(dj.fs, FR_DENIED);
if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero on over write mode */
cl = ((DWORD)LD_WORD(dir+DIR_FstClusHI) << 16) | LD_WORD(dir+DIR_FstClusLO); /* Get start cluster */
ST_WORD(dir+DIR_FstClusHI, 0); /* cluster = 0 */
ST_WORD(dir+DIR_FstClusLO, 0);
ST_DWORD(dir+DIR_FileSize, 0); /* size = 0 */
dj.fs->wflag = 1;
ps = dj.fs->winsect; /* Remove the cluster chain */
if (cl) {
res = remove_chain(dj.fs, cl);
if (res) LEAVE_FF(dj.fs, res);
dj.fs->last_clust = cl - 1; /* Reuse the cluster hole */
}
res = move_window(dj.fs, ps);
if (res != FR_OK) LEAVE_FF(dj.fs, res);
}
}
if (mode & FA_CREATE_ALWAYS) {
dir[DIR_Attr] = 0; /* Reset attribute */
ps = get_fattime();
ST_DWORD(dir+DIR_CrtTime, ps); /* Created time */
dj.fs->wflag = 1;
mode |= FA__WRITTEN; /* Set file changed flag */
}
}
/* Open an existing file */
else {
#endif /* !_FS_READONLY */
if (res != FR_OK) LEAVE_FF(dj.fs, res); /* Follow failed */
dir = dj.dir;
if (!dir || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
LEAVE_FF(dj.fs, FR_NO_FILE);
#if !_FS_READONLY
if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
LEAVE_FF(dj.fs, FR_DENIED);
}
fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
fp->dir_ptr = dj.dir;
#endif
fp->flag = mode; /* File access mode */
fp->org_clust = /* File start cluster */
((DWORD)LD_WORD(dir+DIR_FstClusHI) << 16) | LD_WORD(dir+DIR_FstClusLO);
fp->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */
fp->fptr = 0; fp->csect = 255; /* File pointer */
fp->dsect = 0;
fp->fs = dj.fs; fp->id = dj.fs->id; /* Owner file system object of the file */
LEAVE_FF(dj.fs, FR_OK);
}
/*-----------------------------------------------------------------------*/
/* Read File */
/*-----------------------------------------------------------------------*/
FRESULT f_read (
FIL *fp, /* Pointer to the file object */
void *buff, /* Pointer to data buffer */
UINT btr, /* Number of bytes to read */
UINT *br /* Pointer to number of bytes read */
)
{
FRESULT res;
DWORD clst, sect, remain;
UINT rcnt, cc;
BYTE *rbuff = buff;
*br = 0; /* Initialize bytes read */
res = validate(fp->fs, fp->id); /* Check validity of the object */
if (res != FR_OK) LEAVE_FF(fp->fs, res);
if (fp->flag & FA__ERROR) /* Check abort flag */
LEAVE_FF(fp->fs, FR_INT_ERR);
if (!(fp->flag & FA_READ)) /* Check access mode */
LEAVE_FF(fp->fs, FR_DENIED);
remain = fp->fsize - fp->fptr;
if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
for ( ; btr; /* Repeat until all data transferred */
rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
clst = (fp->fptr == 0) ? /* On the top of the file? */
fp->org_clust : get_fat(fp->fs, fp->curr_clust);
if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);
if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
fp->curr_clust = clst; /* Update current cluster */
fp->csect = 0; /* Reset sector offset in the cluster */
}
sect = clust2sect(fp->fs, fp->curr_clust); /* Get current sector */
if (!sect) ABORT(fp->fs, FR_INT_ERR);
sect += fp->csect;
cc = btr / SS(fp->fs); /* 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(fp->fs->drive, rbuff, sect, (BYTE)cc) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
#if !_FS_READONLY && _FS_MINIMIZE <= 2
#if _FS_TINY
if (fp->fs->wflag && fp->fs->winsect - sect < cc) /* Replace one of the read sectors with cached data if it contains a dirty sector */
mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
#else
if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc) /* Replace one of the read sectors with cached data if it contains a dirty sector */
mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
#endif
#endif
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
continue;
}
#if !_FS_TINY
#if !_FS_READONLY
if (fp->flag & FA__DIRTY) { /* Write sector I/O buffer if needed */
if (disk_write(fp->fs->drive, fp->buf, fp->dsect, 1) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
fp->flag &= ~FA__DIRTY;
}
#endif
if (fp->dsect != sect) { /* Fill sector buffer with file data */
if (disk_read(fp->fs->drive, fp->buf, sect, 1) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
}
#endif
fp->dsect = sect;
fp->csect++; /* Next sector address in the cluster */
}
rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector data from sector buffer */
if (rcnt > btr) rcnt = btr;
#if _FS_TINY
if (move_window(fp->fs, fp->dsect)) /* Move sector window */
ABORT(fp->fs, FR_DISK_ERR);
mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
#else
mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt); /* Pick partial sector */
#endif
}
LEAVE_FF(fp->fs, FR_OK);
}
#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 clst, sect;
UINT wcnt, cc;
const BYTE *wbuff = buff;
*bw = 0; /* Initialize bytes written */
res = validate(fp->fs, fp->id); /* Check validity of the object */
if (res != FR_OK) LEAVE_FF(fp->fs, res);
if (fp->flag & FA__ERROR) /* Check abort flag */
LEAVE_FF(fp->fs, FR_INT_ERR);
if (!(fp->flag & FA_WRITE)) /* Check access mode */
LEAVE_FF(fp->fs, FR_DENIED);
if (fp->fsize + btw < fp->fsize) btw = 0; /* File size cannot reach 4GB */
for ( ; btw; /* Repeat until all data transferred */
wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
if ((fp->fptr % SS(fp->fs)) == 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? */
clst = fp->org_clust; /* Follow from the origin */
if (clst == 0) /* When there is no cluster chain, */
fp->org_clust = clst = create_chain(fp->fs, 0); /* Create a new cluster chain */
} else { /* Middle or end of the file */
clst = create_chain(fp->fs, fp->curr_clust); /* Follow or streach cluster chain */
}
if (clst == 0) break; /* Could not allocate a new cluster (disk full) */
if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
fp->curr_clust = clst; /* Update current cluster */
fp->csect = 0; /* Reset sector address in the cluster */
}
#if _FS_TINY
if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0)) /* Write back data buffer prior to following direct transfer */
ABORT(fp->fs, FR_DISK_ERR);
#else
if (fp->flag & FA__DIRTY) { /* Write back data buffer prior to following direct transfer */
if (disk_write(fp->fs->drive, fp->buf, fp->dsect, 1) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
fp->flag &= ~FA__DIRTY;
}
#endif
sect = clust2sect(fp->fs, fp->curr_clust); /* Get current sector */
if (!sect) ABORT(fp->fs, FR_INT_ERR);
sect += fp->csect;
cc = btw / SS(fp->fs); /* 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(fp->fs->drive, wbuff, sect, (BYTE)cc) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
#if _FS_TINY
if (fp->fs->winsect - sect < cc) { /* Refill sector cache if it gets dirty by the direct write */
mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
fp->fs->wflag = 0;
}
#else
if (fp->dsect - sect < cc) { /* Refill sector cache if it gets dirty by the direct write */
mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
fp->flag &= ~FA__DIRTY;
}
#endif
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
continue;
}
#if _FS_TINY
if (fp->fptr >= fp->fsize) { /* Avoid silly buffer filling at growing edge */
if (move_window(fp->fs, 0)) ABORT(fp->fs, FR_DISK_ERR);
fp->fs->winsect = sect;
}
#else
if (fp->dsect != sect) { /* Fill sector buffer with file data */
if (fp->fptr < fp->fsize &&
disk_read(fp->fs->drive, fp->buf, sect, 1) != RES_OK)
ABORT(fp->fs, FR_DISK_ERR);
}
#endif
fp->dsect = sect;
fp->csect++; /* Next sector address in the cluster */
}
wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Put partial sector into file I/O buffer */
if (wcnt > btw) wcnt = btw;
#if _FS_TINY
if (move_window(fp->fs, fp->dsect)) /* Move sector window */
ABORT(fp->fs, FR_DISK_ERR);
mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
fp->fs->wflag = 1;
#else
mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt); /* Fit partial sector */
fp->flag |= FA__DIRTY;
#endif
}
if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
fp->flag |= FA__WRITTEN; /* Set file changed flag */
LEAVE_FF(fp->fs, FR_OK);
}
/*-----------------------------------------------------------------------*/
/* 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? */
#if !_FS_TINY /* Write-back dirty buffer */
if (fp->flag & FA__DIRTY) {
if (disk_write(fp->fs->drive, fp->buf, fp->dsect, 1) != RES_OK)
LEAVE_FF(fp->fs, FR_DISK_ERR);
fp->flag &= ~FA__DIRTY;
}
#endif
/* Update the directory entry */
res = move_window(fp->fs, fp->dir_sect);
if (res == FR_OK) {
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 */
ST_WORD(dir+D
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -