fatfs/src/ff.c
static FRESULT dir_find (	/* FR_OK(0):succeeded, !=0:error */
	DIR* dp					/* Pointer to the directory object with the file name */
)
{
	FRESULT res;
	FATFS *fs = dp->obj.fs;
	BYTE c;
#if FF_USE_LFN
	BYTE a, ord, sum;
#endif

	res = dir_sdi(dp, 0);			/* Rewind directory object */
	if (res != FR_OK) return res;
#if FF_FS_EXFAT
	if (fs->fs_type == FS_EXFAT) {	/* On the exFAT volume */
		BYTE nc;
		UINT di, ni;
		WORD hash = xname_sum(fs->lfnbuf);		/* Hash value of the name to find */

		while ((res = DIR_READ_FILE(dp)) == FR_OK) {	/* Read an item */
#if FF_MAX_LFN < 255
			if (fs->dirbuf[XDIR_NumName] > FF_MAX_LFN) continue;			/* Skip comparison if inaccessible object name */
#endif
			if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue;	/* Skip comparison if hash mismatched */
			for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) {	/* Compare the name */
				if ((di % SZDIRE) == 0) di += 2;
				if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni])) break;
			}
			if (nc == 0 && !fs->lfnbuf[ni]) break;	/* Name matched? */
		}
		return res;
	}
#endif
	/* On the FAT/FAT32 volume */
#if FF_USE_LFN
	ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF;	/* Reset LFN sequence */
#endif
	do {
		res = move_window(fs, dp->sect);
		if (res != FR_OK) break;
		c = dp->dir[DIR_Name];
		if (c == 0) { res = FR_NO_FILE; break; }	/* Reached to end of table */
#if FF_USE_LFN		/* LFN configuration */
		dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK;
		if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) {	/* An entry without valid data */
			ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;	/* Reset LFN sequence */
		} else {
			if (a == AM_LFN) {			/* An LFN entry is found */
				if (!(dp->fn[NSFLAG] & NS_NOLFN)) {
					if (c & LLEF) {		/* Is it start of LFN sequence? */
						sum = dp->dir[LDIR_Chksum];
						c &= (BYTE)~LLEF; ord = c;	/* LFN start order */
						dp->blk_ofs = dp->dptr;	/* Start offset of LFN */
					}
					/* Check validity of the LFN entry and compare it with given name */
					ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
				}
			} else {					/* An SFN entry is found */
				if (ord == 0 && sum == sum_sfn(dp->dir)) break;	/* LFN matched? */
				if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11)) break;	/* SFN matched? */
				ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;	/* Reset LFN sequence */
			}
		}
#else		/* Non LFN configuration */
		dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;
		if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11)) break;	/* Is it a valid entry? */
#endif
		res = dir_next(dp, 0);	/* Next entry */
	} while (res == FR_OK);

	return res;
}
/*-----------------------------------------------------------------------*/
/* Directory handling - Find an object in the directory                  */
/*-----------------------------------------------------------------------*/
Directory handling - Find an object in the directory
Directory handling - Find an object in the directory
static FRESULT dir_find (	
	DIR* dp					
)
{
	FRESULT res;
	FATFS *fs = dp->obj.fs;
	BYTE c;
#if FF_USE_LFN
	BYTE a, ord, sum;
#endif
	res = dir_sdi(dp, 0);			
	if (res != FR_OK) return res;
#if FF_FS_EXFAT
	if (fs->fs_type == FS_EXFAT) {	
		BYTE nc;
		UINT di, ni;
		WORD hash = xname_sum(fs->lfnbuf);		
		while ((res = DIR_READ_FILE(dp)) == FR_OK) {	
#if FF_MAX_LFN < 255
			if (fs->dirbuf[XDIR_NumName] > FF_MAX_LFN) continue;			
#endif
			if (ld_word(fs->dirbuf + XDIR_NameHash) != hash) continue;	
			for (nc = fs->dirbuf[XDIR_NumName], di = SZDIRE * 2, ni = 0; nc; nc--, di += 2, ni++) {	
				if ((di % SZDIRE) == 0) di += 2;
				if (ff_wtoupper(ld_word(fs->dirbuf + di)) != ff_wtoupper(fs->lfnbuf[ni])) break;
			}
			if (nc == 0 && !fs->lfnbuf[ni]) break;	
		}
		return res;
	}
#endif
#if FF_USE_LFN
	ord = sum = 0xFF; dp->blk_ofs = 0xFFFFFFFF;	
#endif
	do {
		res = move_window(fs, dp->sect);
		if (res != FR_OK) break;
		c = dp->dir[DIR_Name];
		if (c == 0) { res = FR_NO_FILE; break; }	
#if FF_USE_LFN		
		dp->obj.attr = a = dp->dir[DIR_Attr] & AM_MASK;
		if (c == DDEM || ((a & AM_VOL) && a != AM_LFN)) {	
			ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;	
		} else {
			if (a == AM_LFN) {			
				if (!(dp->fn[NSFLAG] & NS_NOLFN)) {
					if (c & LLEF) {		
						sum = dp->dir[LDIR_Chksum];
						c &= (BYTE)~LLEF; ord = c;	
						dp->blk_ofs = dp->dptr;	
					}
					ord = (c == ord && sum == dp->dir[LDIR_Chksum] && cmp_lfn(fs->lfnbuf, dp->dir)) ? ord - 1 : 0xFF;
				}
			} else {					
				if (ord == 0 && sum == sum_sfn(dp->dir)) break;	
				if (!(dp->fn[NSFLAG] & NS_LOSS) && !mem_cmp(dp->dir, dp->fn, 11)) break;	
				ord = 0xFF; dp->blk_ofs = 0xFFFFFFFF;	
			}
		}
#else		
		dp->obj.attr = dp->dir[DIR_Attr] & AM_MASK;
		if (!(dp->dir[DIR_Attr] & AM_VOL) && !mem_cmp(dp->dir, dp->fn, 11)) break;	
#endif
		res = dir_next(dp, 0);	
	} while (res == FR_OK);
	return res;
}
static FRESULT dir_find ( DIR * dp ) { FRESULT res ; FATFS * fs = dp -> obj . fs ; BYTE c ; #if FF_USE_LFN BYTE a , ord , sum ; #endif res = dir_sdi ( dp , 0 ) ; if ( res != FR_OK ) return res ; #if FF_FS_EXFAT if ( fs -> fs_type == FS_EXFAT ) { BYTE nc ; UINT di , ni ; WORD hash = xname_sum ( fs -> lfnbuf ) ; while ( ( res = DIR_READ_FILE ( dp ) ) == FR_OK ) { #if FF_MAX_LFN < 255 if ( fs -> dirbuf [ XDIR_NumName ] > FF_MAX_LFN ) continue ; #endif if ( ld_word ( fs -> dirbuf + XDIR_NameHash ) != hash ) continue ; for ( nc = fs -> dirbuf [ XDIR_NumName ] , di = SZDIRE * 2 , ni = 0 ; nc ; nc -- , di += 2 , ni ++ ) { if ( ( di % SZDIRE ) == 0 ) di += 2 ; if ( ff_wtoupper ( ld_word ( fs -> dirbuf + di ) ) != ff_wtoupper ( fs -> lfnbuf [ ni ] ) ) break ; } if ( nc == 0 && ! fs -> lfnbuf [ ni ] ) break ; } return res ; } #endif #if FF_USE_LFN ord = sum = 0xFF ; dp -> blk_ofs = 0xFFFFFFFF ; #endif do { res = move_window ( fs , dp -> sect ) ; if ( res != FR_OK ) break ; c = dp -> dir [ DIR_Name ] ; if ( c == 0 ) { res = FR_NO_FILE ; break ; } #if FF_USE_LFN dp -> obj . attr = a = dp -> dir [ DIR_Attr ] & AM_MASK ; if ( c == DDEM || ( ( a & AM_VOL ) && a != AM_LFN ) ) { ord = 0xFF ; dp -> blk_ofs = 0xFFFFFFFF ; } else { if ( a == AM_LFN ) { if ( ! ( dp -> fn [ NSFLAG ] & NS_NOLFN ) ) { if ( c & LLEF ) { sum = dp -> dir [ LDIR_Chksum ] ; c &= ( BYTE ) ~ LLEF ; ord = c ; dp -> blk_ofs = dp -> dptr ; } ord = ( c == ord && sum == dp -> dir [ LDIR_Chksum ] && cmp_lfn ( fs -> lfnbuf , dp -> dir ) ) ? ord - 1 : 0xFF ; } } else { if ( ord == 0 && sum == sum_sfn ( dp -> dir ) ) break ; if ( ! ( dp -> fn [ NSFLAG ] & NS_LOSS ) && ! mem_cmp ( dp -> dir , dp -> fn , 11 ) ) break ; ord = 0xFF ; dp -> blk_ofs = 0xFFFFFFFF ; } } #else dp -> obj . attr = dp -> dir [ DIR_Attr ] & AM_MASK ; if ( ! ( dp -> dir [ DIR_Attr ] & AM_VOL ) && ! mem_cmp ( dp -> dir , dp -> fn , 11 ) ) break ; #endif res = dir_next ( dp , 0 ) ; } while ( res == FR_OK ) ; return res ; }
Directory handling - Find an object in the directory
Directory handling - Find an object in the directory
/* FR_OK(0):succeeded, !=0:error */ /* Pointer to the directory object with the file name */ /* Rewind directory object */ /* On the exFAT volume */ /* Hash value of the name to find */ /* Read an item */ /* Skip comparison if inaccessible object name */ /* Skip comparison if hash mismatched */ /* Compare the name */ /* Name matched? */ /* On the FAT/FAT32 volume */ /* Reset LFN sequence */ /* Reached to end of table */ /* LFN configuration */ /* An entry without valid data */ /* Reset LFN sequence */ /* An LFN entry is found */ /* Is it start of LFN sequence? */ /* LFN start order */ /* Start offset of LFN */ /* Check validity of the LFN entry and compare it with given name */ /* An SFN entry is found */ /* LFN matched? */ /* SFN matched? */ /* Reset LFN sequence */ /* Non LFN configuration */ /* Is it a valid entry? */ /* Next entry */