fatfs/src/ff.c
static FRESULT dir_sdi (	/* FR_OK(0):succeeded, !=0:error */
	DIR* dp,		/* Pointer to directory object */
	DWORD ofs		/* Offset of directory table */
)
{
	DWORD csz, clst;
	FATFS *fs = dp->obj.fs;


	if (ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR) || ofs % SZDIRE) {	/* Check range of offset and alignment */
		return FR_INT_ERR;
	}
	dp->dptr = ofs;				/* Set current offset */
	clst = dp->obj.sclust;		/* Table start cluster (0:root) */
	if (clst == 0 && fs->fs_type >= FS_FAT32) {	/* Replace cluster# 0 with root cluster# */
		clst = fs->dirbase;
		if (FF_FS_EXFAT) dp->obj.stat = 0;	/* exFAT: Root dir has an FAT chain */
	}

	if (clst == 0) {	/* Static table (root-directory on the FAT volume) */
		if (ofs / SZDIRE >= fs->n_rootdir) return FR_INT_ERR;	/* Is index out of range? */
		dp->sect = fs->dirbase;

	} else {			/* Dynamic table (sub-directory or root-directory on the FAT32/exFAT volume) */
		csz = (DWORD)fs->csize * SS(fs);	/* Bytes per cluster */
		while (ofs >= csz) {				/* Follow cluster chain */
			clst = get_fat(&dp->obj, clst);				/* Get next cluster */
			if (clst == 0xFFFFFFFF) return FR_DISK_ERR;	/* Disk error */
			if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR;	/* Reached to end of table or internal error */
			ofs -= csz;
		}
		dp->sect = clst2sect(fs, clst);
	}
	dp->clust = clst;					/* Current cluster# */
	if (dp->sect == 0) return FR_INT_ERR;
	dp->sect += ofs / SS(fs);			/* Sector# of the directory entry */
	dp->dir = fs->win + (ofs % SS(fs));	/* Pointer to the entry in the win[] */

	return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* Directory handling - Set directory index                              */
/*-----------------------------------------------------------------------*/
Directory handling - Set directory index
Directory handling - Set directory index
static FRESULT dir_sdi (	
	DIR* dp,		
	DWORD ofs		
)
{
	DWORD csz, clst;
	FATFS *fs = dp->obj.fs;
	if (ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR) || ofs % SZDIRE) {	
		return FR_INT_ERR;
	}
	dp->dptr = ofs;				
	clst = dp->obj.sclust;		
	if (clst == 0 && fs->fs_type >= FS_FAT32) {	
		clst = fs->dirbase;
		if (FF_FS_EXFAT) dp->obj.stat = 0;	
	}
	if (clst == 0) {	
		if (ofs / SZDIRE >= fs->n_rootdir) return FR_INT_ERR;	
		dp->sect = fs->dirbase;
	} else {			
		csz = (DWORD)fs->csize * SS(fs);	
		while (ofs >= csz) {				
			clst = get_fat(&dp->obj, clst);				
			if (clst == 0xFFFFFFFF) return FR_DISK_ERR;	
			if (clst < 2 || clst >= fs->n_fatent) return FR_INT_ERR;	
			ofs -= csz;
		}
		dp->sect = clst2sect(fs, clst);
	}
	dp->clust = clst;					
	if (dp->sect == 0) return FR_INT_ERR;
	dp->sect += ofs / SS(fs);			
	dp->dir = fs->win + (ofs % SS(fs));	
	return FR_OK;
}
static FRESULT dir_sdi ( DIR * dp , DWORD ofs ) { DWORD csz , clst ; FATFS * fs = dp -> obj . fs ; if ( ofs >= ( DWORD ) ( ( FF_FS_EXFAT && fs -> fs_type == FS_EXFAT ) ? MAX_DIR_EX : MAX_DIR ) || ofs % SZDIRE ) { return FR_INT_ERR ; } dp -> dptr = ofs ; clst = dp -> obj . sclust ; if ( clst == 0 && fs -> fs_type >= FS_FAT32 ) { clst = fs -> dirbase ; if ( FF_FS_EXFAT ) dp -> obj . stat = 0 ; } if ( clst == 0 ) { if ( ofs / SZDIRE >= fs -> n_rootdir ) return FR_INT_ERR ; dp -> sect = fs -> dirbase ; } else { csz = ( DWORD ) fs -> csize * SS ( fs ) ; while ( ofs >= csz ) { clst = get_fat ( & dp -> obj , clst ) ; if ( clst == 0xFFFFFFFF ) return FR_DISK_ERR ; if ( clst < 2 || clst >= fs -> n_fatent ) return FR_INT_ERR ; ofs -= csz ; } dp -> sect = clst2sect ( fs , clst ) ; } dp -> clust = clst ; if ( dp -> sect == 0 ) return FR_INT_ERR ; dp -> sect += ofs / SS ( fs ) ; dp -> dir = fs -> win + ( ofs % SS ( fs ) ) ; return FR_OK ; }
Directory handling - Set directory index
Directory handling - Set directory index
/* FR_OK(0):succeeded, !=0:error */ /* Pointer to directory object */ /* Offset of directory table */ /* Check range of offset and alignment */ /* Set current offset */ /* Table start cluster (0:root) */ /* Replace cluster# 0 with root cluster# */ /* exFAT: Root dir has an FAT chain */ /* Static table (root-directory on the FAT volume) */ /* Is index out of range? */ /* Dynamic table (sub-directory or root-directory on the FAT32/exFAT volume) */ /* Bytes per cluster */ /* Follow cluster chain */ /* Get next cluster */ /* Disk error */ /* Reached to end of table or internal error */ /* Current cluster# */ /* Sector# of the directory entry */ /* Pointer to the entry in the win[] */