fatfs/src/ff.c
static FRESULT dir_next (	/* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */
	DIR* dp,				/* Pointer to the directory object */
	int stretch				/* 0: Do not stretch table, 1: Stretch table if needed */
)
{
	DWORD ofs, clst;
	FATFS *fs = dp->obj.fs;


	ofs = dp->dptr + SZDIRE;	/* Next entry */
	if (ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR)) dp->sect = 0;	/* Disable it if the offset reached the max value */
	if (dp->sect == 0) return FR_NO_FILE;	/* Report EOT if it has been disabled */

	if (ofs % SS(fs) == 0) {	/* Sector changed? */
		dp->sect++;				/* Next sector */

		if (dp->clust == 0) {	/* Static table */
			if (ofs / SZDIRE >= fs->n_rootdir) {	/* Report EOT if it reached end of static table */
				dp->sect = 0; return FR_NO_FILE;
			}
		}
		else {					/* Dynamic table */
			if ((ofs / SS(fs) & (fs->csize - 1)) == 0) {	/* Cluster changed? */
				clst = get_fat(&dp->obj, dp->clust);		/* Get next cluster */
				if (clst <= 1) return FR_INT_ERR;			/* Internal error */
				if (clst == 0xFFFFFFFF) return FR_DISK_ERR;	/* Disk error */
				if (clst >= fs->n_fatent) {					/* It reached end of dynamic table */
#if !FF_FS_READONLY
					if (!stretch) {								/* If no stretch, report EOT */
						dp->sect = 0; return FR_NO_FILE;
					}
					clst = create_chain(&dp->obj, dp->clust);	/* Allocate a cluster */
					if (clst == 0) return FR_DENIED;			/* No free cluster */
					if (clst == 1) return FR_INT_ERR;			/* Internal error */
					if (clst == 0xFFFFFFFF) return FR_DISK_ERR;	/* Disk error */
					if (dir_clear(fs, clst) != FR_OK) return FR_DISK_ERR;	/* Clean up the stretched table */
					if (FF_FS_EXFAT) dp->obj.stat |= 4;			/* exFAT: The directory has been stretched */
#else
					if (!stretch) dp->sect = 0;					/* (this line is to suppress compiler warning) */
					dp->sect = 0; return FR_NO_FILE;			/* Report EOT */
#endif
				}
				dp->clust = clst;		/* Initialize data for new cluster */
				dp->sect = clst2sect(fs, clst);
			}
		}
	}
	dp->dptr = ofs;						/* Current entry */
	dp->dir = fs->win + ofs % SS(fs);	/* Pointer to the entry in the win[] */

	return FR_OK;
}
/*-----------------------------------------------------------------------*/
/* Directory handling - Move directory table index next                  */
/*-----------------------------------------------------------------------*/
Directory handling - Move directory table index next
Directory handling - Move directory table index next
static FRESULT dir_next (	
	DIR* dp,				
	int stretch				
)
{
	DWORD ofs, clst;
	FATFS *fs = dp->obj.fs;
	ofs = dp->dptr + SZDIRE;	
	if (ofs >= (DWORD)((FF_FS_EXFAT && fs->fs_type == FS_EXFAT) ? MAX_DIR_EX : MAX_DIR)) dp->sect = 0;	
	if (dp->sect == 0) return FR_NO_FILE;	
	if (ofs % SS(fs) == 0) {	
		dp->sect++;				
		if (dp->clust == 0) {	
			if (ofs / SZDIRE >= fs->n_rootdir) {	
				dp->sect = 0; return FR_NO_FILE;
			}
		}
		else {					
			if ((ofs / SS(fs) & (fs->csize - 1)) == 0) {	
				clst = get_fat(&dp->obj, dp->clust);		
				if (clst <= 1) return FR_INT_ERR;			
				if (clst == 0xFFFFFFFF) return FR_DISK_ERR;	
				if (clst >= fs->n_fatent) {					
#if !FF_FS_READONLY
					if (!stretch) {								
						dp->sect = 0; return FR_NO_FILE;
					}
					clst = create_chain(&dp->obj, dp->clust);	
					if (clst == 0) return FR_DENIED;			
					if (clst == 1) return FR_INT_ERR;			
					if (clst == 0xFFFFFFFF) return FR_DISK_ERR;	
					if (dir_clear(fs, clst) != FR_OK) return FR_DISK_ERR;	
					if (FF_FS_EXFAT) dp->obj.stat |= 4;			
#else
					if (!stretch) dp->sect = 0;					
					dp->sect = 0; return FR_NO_FILE;			
#endif
				}
				dp->clust = clst;		
				dp->sect = clst2sect(fs, clst);
			}
		}
	}
	dp->dptr = ofs;						
	dp->dir = fs->win + ofs % SS(fs);	
	return FR_OK;
}
static FRESULT dir_next ( DIR * dp , int stretch ) { DWORD ofs , clst ; FATFS * fs = dp -> obj . fs ; ofs = dp -> dptr + SZDIRE ; if ( ofs >= ( DWORD ) ( ( FF_FS_EXFAT && fs -> fs_type == FS_EXFAT ) ? MAX_DIR_EX : MAX_DIR ) ) dp -> sect = 0 ; if ( dp -> sect == 0 ) return FR_NO_FILE ; if ( ofs % SS ( fs ) == 0 ) { dp -> sect ++ ; if ( dp -> clust == 0 ) { if ( ofs / SZDIRE >= fs -> n_rootdir ) { dp -> sect = 0 ; return FR_NO_FILE ; } } else { if ( ( ofs / SS ( fs ) & ( fs -> csize - 1 ) ) == 0 ) { clst = get_fat ( & dp -> obj , dp -> clust ) ; if ( clst <= 1 ) return FR_INT_ERR ; if ( clst == 0xFFFFFFFF ) return FR_DISK_ERR ; if ( clst >= fs -> n_fatent ) { #if ! FF_FS_READONLY if ( ! stretch ) { dp -> sect = 0 ; return FR_NO_FILE ; } clst = create_chain ( & dp -> obj , dp -> clust ) ; if ( clst == 0 ) return FR_DENIED ; if ( clst == 1 ) return FR_INT_ERR ; if ( clst == 0xFFFFFFFF ) return FR_DISK_ERR ; if ( dir_clear ( fs , clst ) != FR_OK ) return FR_DISK_ERR ; if ( FF_FS_EXFAT ) dp -> obj . stat |= 4 ; #else if ( ! stretch ) dp -> sect = 0 ; dp -> sect = 0 ; return FR_NO_FILE ; #endif } dp -> clust = clst ; dp -> sect = clst2sect ( fs , clst ) ; } } } dp -> dptr = ofs ; dp -> dir = fs -> win + ofs % SS ( fs ) ; return FR_OK ; }
Directory handling - Move directory table index next
Directory handling - Move directory table index next
/* FR_OK(0):succeeded, FR_NO_FILE:End of table, FR_DENIED:Could not stretch */ /* Pointer to the directory object */ /* 0: Do not stretch table, 1: Stretch table if needed */ /* Next entry */ /* Disable it if the offset reached the max value */ /* Report EOT if it has been disabled */ /* Sector changed? */ /* Next sector */ /* Static table */ /* Report EOT if it reached end of static table */ /* Dynamic table */ /* Cluster changed? */ /* Get next cluster */ /* Internal error */ /* Disk error */ /* It reached end of dynamic table */ /* If no stretch, report EOT */ /* Allocate a cluster */ /* No free cluster */ /* Internal error */ /* Disk error */ /* Clean up the stretched table */ /* exFAT: The directory has been stretched */ /* (this line is to suppress compiler warning) */ /* Report EOT */ /* Initialize data for new cluster */ /* Current entry */ /* Pointer to the entry in the win[] */