📄 ff.lst
字号:
829 1 fp->fs = NULL; /* Clear file object */
830 1 #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
834 1 mode &= FA_READ;
835 1 res = auto_mount(&path, &dj.fs, 0);
836 1 #endif
837 1 if (res != FR_OK) return res;
838 1 res = trace_path(&dj, fn, path, &dir); /* Trace the file path */
839 1
840 1 #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) */
C51 COMPILER V8.08 FF 10/31/2008 14:44:16 PAGE 15
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 */
881 1 if (res != FR_OK) return res; /* Trace failed */
882 1 if (!dir || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */
883 1 return FR_NO_FILE;
884 1 #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
891 1 fp->flag = mode; /* File access mode */
892 1 fp->org_clust = /* File start cluster */
893 1 ((DWORD)LD_WORD(&dir[DIR_FstClusHI]) << 16) | LD_WORD(&dir[DIR_FstClusLO]);
894 1 fp->fsize = LD_DWORD(&dir[DIR_FileSize]); /* File size */
895 1 fp->fptr = 0; fp->csect = 255; /* File pointer */
896 1 fp->curr_sect = 0;
897 1 fp->fs = dj.fs; fp->id = dj.fs->id; /* Owner file system object of the file */
898 1
899 1 return FR_OK;
900 1 }
901
902
903
904
905 /*-----------------------------------------------------------------------*/
906 /* Read File */
907 /*-----------------------------------------------------------------------*/
908
909 FRESULT f_read (
910 FIL *fp, /* Pointer to the file object */
911 void *buff, /* Pointer to data buffer */
912 UINT btr, /* Number of bytes to read */
913 UINT *br /* Pointer to number of bytes read */
914 )
915 {
916 1 FRESULT res;
917 1 DWORD clust, sect, remain;
C51 COMPILER V8.08 FF 10/31/2008 14:44:16 PAGE 16
918 1 UINT rcnt, cc;
919 1 BYTE *rbuff = buff;
920 1
921 1
922 1 *br = 0;
923 1 res = validate(fp->fs, fp->id); /* Check validity of the object */
924 1 if (res != FR_OK) return res;
925 1 if (fp->flag & FA__ERROR) return FR_RW_ERROR; /* Check error flag */
926 1 if (!(fp->flag & FA_READ)) return FR_DENIED; /* Check access mode */
927 1 remain = fp->fsize - fp->fptr;
928 1 if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */
929 1
930 1 for ( ; btr; /* Repeat until all data transferred */
931 1 rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
932 2 if ((fp->fptr % SS(fp->fs)) == 0) { /* On the sector boundary? */
933 3 if (fp->csect >= fp->fs->csize) { /* On the cluster boundary? */
934 4 clust = (fp->fptr == 0) ? /* On the top of the file? */
935 4 fp->org_clust : get_cluster(fp->fs, fp->curr_clust);
936 4 if (clust < 2 || clust >= fp->fs->max_clust) goto fr_error;
937 4 fp->curr_clust = clust; /* Update current cluster */
938 4 fp->csect = 0; /* Reset sector address in the cluster */
939 4 }
940 3 sect = clust2sect(fp->fs, fp->curr_clust) + fp->csect; /* Get current sector */
941 3 cc = btr / SS(fp->fs); /* When remaining bytes >= sector size, */
942 3 if (cc) { /* Read maximum contiguous sectors directly */
943 4 if (fp->csect + cc > fp->fs->csize) /* Clip at cluster boundary */
944 4 cc = fp->fs->csize - fp->csect;
945 4 if (disk_read(fp->fs->drive, rbuff, sect, (BYTE)cc) != RES_OK)
946 4 goto fr_error;
947 4 fp->csect += (BYTE)cc; /* Next sector address in the cluster */
948 4 rcnt = SS(fp->fs) * cc; /* Number of bytes transferred */
949 4 continue;
950 4 }
951 3 if (sect != fp->curr_sect) { /* Is window offset changed? */
952 4 #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
959 4 if (disk_read(fp->fs->drive, fp->buffer, sect, 1) != RES_OK) /* Fill file I/O buffer with file data */
960 4 goto fr_error;
961 4 fp->curr_sect = sect;
962 4 }
963 3 fp->csect++; /* Next sector address in the cluster */
964 3 }
965 2 rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs)); /* Get partial sector from file I/O buffer */
966 2 if (rcnt > btr) rcnt = btr;
967 2 memcpy(rbuff, &fp->buffer[fp->fptr % SS(fp->fs)], rcnt);
968 2 }
969 1
970 1 return FR_OK;
971 1
972 1 fr_error: /* Abort this file due to an unrecoverable error */
973 1 fp->flag |= FA__ERROR;
974 1 return FR_RW_ERROR;
975 1 }
976
977
978
979
C51 COMPILER V8.08 FF 10/31/2008 14:44:16 PAGE 17
980 #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) != RE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -