/* Get logical drive number */ res = find_volume(&fs, &path, 0); if (res == FR_OK) { dp->fs = fs; INIT_BUF(*dp); res = follow_path(dp, path); /* Follow the path to the directory */ FREE_BUF(); if (res == FR_OK) { /* Follow completed */ if (dp->dir) { /* It is not the origin directory itself */ if (dp->dir[DIR_Attr] & AM_DIR) /* The object is a sub directory */ dp->sclust = ld_clust(fs, dp->dir); else /* The object is a file */ res = FR_NO_PATH; } if (res == FR_OK) { dp->id = fs->id; res = dir_sdi(dp, 0); /* Rewind directory *///调试发现dir_sdir函数的返回值不为FR_OK #if _FS_LOCK if (res == FR_OK) { if (dp->sclust) { dp->lockid = inc_lock(dp, 0); /* Lock the sub directory */ if (!dp->lockid) res = FR_TOO_MANY_OPEN_FILES; } else { dp->lockid = 0; /* Root directory need not to be locked */ } } #endif } } if (res == FR_NO_FILE) res = FR_NO_PATH; } if (res != FR_OK) dp->fs = 0; /* Invalidate the directory object if function faild */ LEAVE_FF(fs, res); }
/*-----------------------------------------------------------------------*/
/* Directory handling - Set directory index */
/*-----------------------------------------------------------------------*/
//去掉static方便其他地方调用
FRESULT dir_sdi (
DIR* dp, /* Pointer to directory object */
UINT idx /* Index of directory table */
)
{
DWORD clst, sect;
UINT ic;
dp->index = (WORD)idx; /* Current index */
clst = dp->sclust; /* Table start cluster (0:root) */
if (clst == 1 || clst >= dp->fs->n_fatent) /* Check start cluster range */
return FR_INT_ERR;
if (!clst && dp->fs->fs_type == FS_FAT32) /* Replace cluster# 0 with root cluster# if in FAT32 */
clst = dp->fs->dirbase;
if (clst == 0) { /* Static table (root-directory in FAT12/16) */
if (idx >= dp->fs->n_rootdir) /* Is index out of range? */
return FR_INT_ERR;
sect = dp->fs->dirbase;
}
else { /* Dynamic table (root-directory in FAT32 or sub-directory) */
ic = SS(dp->fs) / SZ_DIR * dp->fs->csize; /* Entries per cluster */
while (idx >= ic) { /* Follow cluster chain */
clst = get_fat(dp->fs, clst); /* Get next cluster */
if (clst == 0xFFFFFFFF) return FR_DISK_ERR; /* Disk error */
if (clst < 2 || clst >= dp->fs->n_fatent) /* Reached to end of table or internal error */
return FR_INT_ERR;
idx -= ic;
}
sect = clust2sect(dp->fs, clst);
}
dp->clust = clst; /* Current cluster# */
if (!sect) return FR_INT_ERR;
dp->sect = sect + idx / (SS(dp->fs) / SZ_DIR); /* Sector# of the directory entry */
dp->dir = dp->fs->win + (idx % (SS(dp->fs) / SZ_DIR)) * SZ_DIR; /* Ptr to the entry in the sector */