?? ff.c
字號:
/*-----------------------------------------------------------------------*/
FRESULT f_mount (
BYTE drv, /* Logical drive number to be mounted/unmounted */
FATFS *fs /* Pointer to new file system object (NULL for unmount)*/
)
{
if (drv >= _DRIVES) return FR_INVALID_DRIVE;
if (FatFs[drv]) FatFs[drv]->fs_type = 0; /* Clear old object */
FatFs[drv] = fs; /* Register and clear new object */
if (fs) fs->fs_type = 0;
return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* Open or Create a File */
/*-----------------------------------------------------------------------*/
FRESULT f_open (
FIL *fp, /* Pointer to the blank file object */
const char *path, /* Pointer to the file name */
BYTE mode /* Access mode and file open mode flags */
)
{
FRESULT res;
DIR dj;
BYTE *dir;
char fn[8+3+1];
fp->fs = NULL; /* Clear file object */
#if !_FS_READONLY
mode &= (FA_READ|FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW);
res = auto_mount(&path, &dj.fs, (BYTE)(mode & (FA_WRITE|FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)));
#else
mode &= FA_READ;
res = auto_mount(&path, &dj.fs, 0);
#endif
if (res != FR_OK) return res;
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
#if !_FS_READONLY
/* Create or Open a file */
if (mode & (FA_CREATE_ALWAYS|FA_OPEN_ALWAYS|FA_CREATE_NEW)) {
DWORD ps, rs;
if (res != FR_OK) { /* No file, create new */
if (res != FR_NO_FILE) return res;
res = reserve_direntry(&dj, &dir);
if (res != FR_OK) return res;
memset(dir, 0, 32); /* Initialize the new entry with open name */
memcpy(&dir[DIR_Name], fn, 8+3);
dir[DIR_NTres] = fn[11];
mode |= FA_CREATE_ALWAYS;
}
else { /* Any object is already existing */
if (mode & FA_CREATE_NEW) /* Cannot create new */
return FR_EXIST;
if (!dir || (dir[DIR_Attr] & (AM_RDO|AM_DIR))) /* Cannot overwrite it (R/O or DIR) */
return FR_DENIED;
if (mode & FA_CREATE_ALWAYS) { /* Resize it to zero if needed */
rs = ((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->winflag = 1;
ps = dj.fs->winsect; /* Remove the cluster chain */
if (!remove_chain(dj.fs, rs) || !move_window(dj.fs, ps))
return FR_RW_ERROR;
dj.fs->last_clust = rs - 1; /* Reuse the cluster hole */
}
}
if (mode & FA_CREATE_ALWAYS) {
dir[DIR_Attr] = 0; /* Reset attribute */
ps = get_fattime();
ST_DWORD(&dir[DIR_CrtTime], ps); /* Created time */
dj.fs->winflag = 1;
mode |= FA__WRITTEN; /* Set file changed flag */
}
}
/* Open an existing file */
else {
#endif /* !_FS_READONLY */
if (res != FR_OK) return res; /* Trace failed */
if (!dir || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
return FR_NO_FILE;
#if !_FS_READONLY
if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
return FR_DENIED;
}
fp->dir_sect = dj.fs->winsect; /* Pointer to the directory entry */
fp->dir_ptr = 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->curr_sect = 0;
fp->fs = dj.fs; fp->id = dj.fs->id; /* Owner file system object of the file */
return 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 clust, sect, remain;
UINT rcnt, cc;
BYTE *rbuff = buff;
*br = 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_READ)) return FR_DENIED; /* Check access mode */
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? */
clust = (fp->fptr == 0) ? /* On the top of the file? */
fp->org_clust : get_cluster(fp->fs, fp->curr_clust);
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->fs, fp->curr_clust) + fp->csect; /* Get current sector */
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)
goto fr_error;
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
continue;
}
if (sect != fp->curr_sect) { /* Is window offset changed? */
#if !_FS_READONLY
if (fp->flag & FA__DIRTY) { /* Write back file I/O buffer if needed */
if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
goto fr_error;
fp->flag &= (BYTE)~FA__DIRTY;
}
#endif
if (disk_read(fp->fs->drive, fp->buffer, sect, 1) != RES_OK) /* Fill file I/O buffer with file data */
goto fr_error;
fp->curr_sect = sect;
}
fp->csect++; /* Next sector address in the cluster */
}
rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector from file I/O buffer */
if (rcnt > btr) rcnt = btr;
memcpy(rbuff, &fp->buffer[fp->fptr % SS(fp->fs)], 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 clust, sect;
UINT wcnt, cc;
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 % 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? */
clust = fp->org_clust; /* Follow from the origin */
if (clust == 0) /* When there is no cluster chain, */
fp->org_clust = clust = create_chain(fp->fs, 0); /* Create a new cluster chain */
} else { /* Middle or end of the file */
clust = create_chain(fp->fs, 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->fs, fp->curr_clust) + fp->csect; /* Get current sector */
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)
goto fw_error;
fp->csect += (BYTE)cc; /* Next sector address in the cluster */
wcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
continue;
}
if (sect != fp->curr_sect) { /* Is window offset changed? */
if (fp->flag & FA__DIRTY) { /* Write back file I/O buffer if needed */
if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
goto fw_error;
fp->flag &= (BYTE)~FA__DIRTY;
}
if (fp->fptr < fp->fsize && /* Fill file I/O buffer with file data */
disk_read(fp->fs->drive, fp->buffer, sect, 1) != RES_OK)
goto fw_error;
fp->curr_sect = 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;
memcpy(&fp->buffer[fp->fptr % SS(fp->fs)], wbuff, wcnt);
fp->flag |= FA__DIRTY;
}
if (fp->fptr > fp->fsize) fp->fsize = fp->fptr; /* Update file size if needed */
fp->flag |= FA__WRITTEN; /* Set file changed flag */
return FR_OK;
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? */
/* Write back data buffer if needed */
if (fp->flag & FA__DIRTY) {
if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
return FR_RW_ERROR;
fp->flag &= (BYTE)~FA__DIRTY;
}
/* Update the directory entry */
if (!move_window(fp->fs, 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 */
ST_WORD(&dir[DIR_FstClusHI], fp->org_clust >> 16);
tim = get_fattime(); /* Updated time */
ST_DWORD(&dir[DIR_WrtTime], tim);
fp->flag &= (BYTE)~FA__WRITTEN;
res = sync(fp->fs);
}
}
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;
DWORD clust, csize, nsect, 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;
nsect = 0;
if (ofs > 0) {
csize = (DWORD)fp->fs->csize * SS(fp->fs); /* 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(fp->fs, 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(fp->fs, 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(fp->fs, 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;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -