?? ff.c
字號:
}
fp->fptr += ofs;
fp->csect = (BYTE)(ofs / SS(fp->fs)); /* Sector offset in the cluster */
if (ofs & (SS(fp->fs) - 1)) {
nsect = clust2sect(fp->fs, clust) + fp->csect; /* Current sector */
fp->csect++;
}
}
}
if (nsect && nsect != fp->curr_sect) {
#if !_FS_READONLY
if (fp->flag & FA__DIRTY) { /* Write-back dirty buffer if needed */
if (disk_write(fp->fs->drive, fp->buffer, fp->curr_sect, 1) != RES_OK)
goto fk_error;
fp->flag &= (BYTE)~FA__DIRTY;
}
#endif
if (disk_read(fp->fs->drive, fp->buffer, nsect, 1) != RES_OK)
goto fk_error;
fp->curr_sect = nsect;
}
#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, &dj->fs, 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 = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
dj->sect = clust2sect(dj->fs, 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 */
)
{
BYTE *dir, c, res;
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->fs, dj->sect))
return FR_RW_ERROR;
dir = &dj->fs->win[(dj->index & ((SS(dj->fs) - 1) >> 5)) * 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, &dj.fs, 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;
DWORD 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->fs, 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->fs, fp->curr_clust);
if (ncl < 2) goto ft_error;
if (ncl < fp->fs->max_clust) {
if (!put_cluster(fp->fs, fp->curr_clust, 0x0FFFFFFF)) goto ft_error;
if (!remove_chain(fp->fs, 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;
DWORD n, clust, sect;
BYTE fat, f, *p;
/* Get drive number */
res = auto_mount(&drv, fatfs, 0);
if (res != FR_OK) return res;
/* If number of free cluster is valid, return it without cluster scan. */
if ((*fatfs)->free_clust <= (*fatfs)->max_clust - 2) {
*nclust = (*fatfs)->free_clust;
return FR_OK;
}
/* Get number of free clusters */
fat = (*fatfs)->fs_type;
n = 0;
if (fat == FS_FAT12) {
clust = 2;
do {
if ((WORD)get_cluster(*fatfs, clust) == 0) n++;
} while (++clust < (*fatfs)->max_clust);
} else {
clust = (*fatfs)->max_clust;
sect = (*fatfs)->fatbase;
f = 0; p = 0;
do {
if (!f) {
if (!move_window(*fatfs, sect++)) return FR_RW_ERROR;
p = (*fatfs)->win;
}
if (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);
}
(*fatfs)->free_clust = n;
#if _USE_FSINFO
if (fat == FS_FAT32) (*fatfs)->fsi_flag = 1;
#endif
*nclust = n;
return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* Delete a File or Directory */
/*-----------------------------------------------------------------------*/
FRESULT f_unlink (
const char *path /* Pointer to the file or directory path */
)
{
FRESULT res;
DIR dj;
BYTE *dir, *sdir;
DWORD dclust, dsect;
char fn[8+3+1];
res = auto_mount(&path, &dj.fs, 1);
if (res != FR_OK) return res;
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
if (res != FR_OK) return res; /* Trace failed */
if (!dir) return FR_INVALID_NAME; /* It is the root directory */
if (dir[DIR_Attr] & AM_RDO) return FR_DENIED; /* It is a R/O object */
dsect = dj.fs->winsect;
dclust = ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
if (dir[DIR_Attr] & AM_DIR) { /* It is a sub-directory */
dj.clust = dclust; /* Check if the sub-dir is empty or not */
dj.sect = clust2sect(dj.fs, dclust);
dj.index = 2;
do {
if (!move_window(dj.fs, dj.sect)) return FR_RW_ERROR;
sdir = &dj.fs->win[(dj.index & ((SS(dj.fs) - 1) >> 5)) * 32];
if (sdir[DIR_Name] == 0) break;
if (sdir[DIR_Name] != 0xE5 && !(sdir[DIR_Attr] & AM_VOL))
return FR_DENIED; /* The directory is not empty */
} while (next_dir_entry(&dj));
}
if (!move_window(dj.fs, dsect)) return FR_RW_ERROR; /* Mark the directory entry 'deleted' */
dir[DIR_Name] = 0xE5;
dj.fs->winflag = 1;
if (!remove_chain(dj.fs, dclust)) return FR_RW_ERROR; /* Remove the cluster chain */
return sync(dj.fs);
}
/*-----------------------------------------------------------------------*/
/* Create a Directory */
/*-----------------------------------------------------------------------*/
FRESULT f_mkdir (
const char *path /* Pointer to the directory path */
)
{
FRESULT res;
DIR dj;
BYTE *dir, *fw, n;
char fn[8+3+1];
DWORD sect, dsect, dclust, pclust, tim;
res = auto_mount(&path, &dj.fs, 1);
if (res != FR_OK) return res;
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
if (res == FR_OK) return FR_EXIST; /* Any file or directory is already existing */
if (res != FR_NO_FILE) return res;
res = reserve_direntry(&dj, &dir); /* Reserve a directory entry */
if (res != FR_OK) return res;
sect = dj.fs->winsect;
dclust = create_chain(dj.fs, 0); /* Allocate a cluster for new directory table */
if (dclust == 1) return FR_RW_ERROR;
dsect = clust2sect(dj.fs, dclust);
if (!dsect) return FR_DENIED;
if (!move_window(dj.fs, dsect)) return FR_RW_ERROR;
fw = dj.fs->win;
memset(fw, 0, SS(dj.fs)); /* Clear the new directory table */
for (n = 1; n < dj.fs->csize; n++) {
if (disk_write(dj.fs->drive, fw, ++dsect, 1) != RES_OK)
return FR_RW_ERROR;
}
memset(&fw[DIR_Name], ' ', 8+3); /* Create "." entry */
fw[DIR_Name] = '.';
fw[DIR_Attr] = AM_DIR;
tim = get_fattime();
ST_DWORD(&fw[DIR_WrtTime], tim);
memcpy(&fw[32], &fw[0], 32); fw[33] = '.'; /* Create ".." entry */
ST_WORD(&fw[ DIR_FstClusLO], dclust);
ST_WORD(&fw[ DIR_FstClusHI], dclust >> 16);
pclust = dj.sclust;
if (dj.fs->fs_type == FS_FAT32 && pclust == dj.fs->dirbase) pclust = 0;
ST_WORD(&fw[32+DIR_FstClusLO], pclust);
ST_WORD(&fw[32+DIR_FstClusHI], pclust >> 16);
dj.fs->winflag = 1;
if (!move_window(dj.fs, sect)) return FR_RW_ERROR;
memset(&dir[0], 0, 32); /* Initialize the new entry */
memcpy(&dir[DIR_Name], fn, 8+3); /* Name */
dir[DIR_NTres] = fn[11];
dir[DIR_Attr] = AM_DIR; /* Attribute */
ST_DWORD(&dir[DIR_WrtTime], tim); /* Crated time */
ST_WORD(&dir[DIR_FstClusLO], dclust); /* Table start cluster */
ST_WORD(&dir[DIR_FstClusHI], dclust >> 16);
return sync(dj.fs);
}
/*-----------------------------------------------------------------------*/
/* Change File Attribute */
/*-----------------------------------------------------------------------*/
FRESULT f_chmod (
const char *path, /* Pointer to the file path */
BYTE value, /* Attribute bits */
BYTE mask /* Attribute mask to change */
)
{
FRESULT res;
DIR dj;
BYTE *dir;
char fn[8+3+1];
res = auto_mount(&path, &dj.fs, 1);
if (res == FR_OK) {
res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
if (res == FR_OK) { /* Trace completed */
if (!dir) {
res = FR_INVALID_NAME; /* Root directory */
} else {
mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC; /* Valid attribute mask */
dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask); /* Apply attribute change */
res = sync(dj.fs);
}
}
}
return res;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -