⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat32_access.c

📁 FAT32代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	int x=0;
	FAT32_ShortEntry *directoryEntry;

	// Main cluster following loop
	while (TRUE)
	{
		// Read sector
		if (FAT32_SectorReader(Cluster, x++)) // If sector read was successfull
		{
			// Analyse Sector
			for (item=0; item<=15;item++)
			{
				// Create the multiplier for sector access
				recordoffset = (32*item);

				// Overlay directory entry over buffer
				directoryEntry = (FAT32_ShortEntry*)(FATFS_Internal.currentsector+recordoffset);

				// Long File Name Text Found
				if (FATMisc_If_LFN_TextOnly(directoryEntry) ) 
					;

				// If Invalid record found delete any long file name information collated
				else if (FATMisc_If_LFN_Invalid(directoryEntry) ) 
					;

				// Normal Entry, only 8.3 Text		 
				else if (FATMisc_If_noLFN_SFN_Only(directoryEntry) )
				{
					if (strncmp((const char*)directoryEntry->Name, shortname, 11)==0)
						return TRUE;
				}
			} // End of if
		} 
		else
			break;
	} // End of while loop

	return FALSE;
}
#endif
//-------------------------------------------------------------
// FAT32_UpdateFileLength: Find a SFN entry and update it 
// NOTE: shortname is XXXXXXXXYYY not XXXXXXXX.YYY
//-------------------------------------------------------------
#ifdef INCLUDE_WRITE_SUPPORT
BOOL FAT32_UpdateFileLength(UINT32 Cluster, char *shortname, UINT32 fileLength)
{
	BYTE item=0;
	UINT16 recordoffset = 0;
	int x=0;
	FAT32_ShortEntry *directoryEntry;

	// Main cluster following loop
	while (TRUE)
	{
		// Read sector
		if (FAT32_SectorReader(Cluster, x++)) // If sector read was successfull
		{
			// Analyse Sector
			for (item=0; item<=15;item++)
			{
				// Create the multiplier for sector access
				recordoffset = (32*item);

				// Overlay directory entry over buffer
				directoryEntry = (FAT32_ShortEntry*)(FATFS_Internal.currentsector+recordoffset);

				// Long File Name Text Found
				if (FATMisc_If_LFN_TextOnly(directoryEntry) ) 
					;

				// If Invalid record found delete any long file name information collated
				else if (FATMisc_If_LFN_Invalid(directoryEntry) ) 
					;

				// Normal Entry, only 8.3 Text		 
				else if (FATMisc_If_noLFN_SFN_Only(directoryEntry) )
				{
					if (strncmp((const char*)directoryEntry->Name, shortname, 11)==0)
					{
						directoryEntry->FileSize = fileLength;
						// TODO: Update last write time

						// Update sfn entry
						memcpy((BYTE*)(FATFS_Internal.currentsector+recordoffset), (BYTE*)directoryEntry, sizeof(FAT32_ShortEntry));					

						// Write sector back
						return FAT_WriteSector(FATFS_Internal.SectorCurrentlyLoaded, FATFS_Internal.currentsector);
					}
				}
			} // End of if
		} 
		else
			break;
	} // End of while loop

	return FALSE;
}
#endif
//-------------------------------------------------------------
// FAT32_MarkFileDeleted: Find a SFN entry and mark if as deleted 
// NOTE: shortname is XXXXXXXXYYY not XXXXXXXX.YYY
//-------------------------------------------------------------
#ifdef INCLUDE_WRITE_SUPPORT
BOOL FAT32_MarkFileDeleted(UINT32 Cluster, char *shortname)
{
	BYTE item=0;
	UINT16 recordoffset = 0;
	int x=0;
	FAT32_ShortEntry *directoryEntry;

	// Main cluster following loop
	while (TRUE)
	{
		// Read sector
		if (FAT32_SectorReader(Cluster, x++)) // If sector read was successfull
		{
			// Analyse Sector
			for (item=0; item<=15;item++)
			{
				// Create the multiplier for sector access
				recordoffset = (32*item);

				// Overlay directory entry over buffer
				directoryEntry = (FAT32_ShortEntry*)(FATFS_Internal.currentsector+recordoffset);

				// Long File Name Text Found
				if (FATMisc_If_LFN_TextOnly(directoryEntry) ) 
					;

				// If Invalid record found delete any long file name information collated
				else if (FATMisc_If_LFN_Invalid(directoryEntry) ) 
					;

				// Normal Entry, only 8.3 Text		 
				else if (FATMisc_If_noLFN_SFN_Only(directoryEntry) )
				{
					if (strncmp((const char *)directoryEntry->Name, shortname, 11)==0)
					{
						// Mark as deleted
						directoryEntry->Name[0] = 0xE5; 

						// Update sfn entry
						memcpy((BYTE*)(FATFS_Internal.currentsector+recordoffset), (BYTE*)directoryEntry, sizeof(FAT32_ShortEntry));					

						// Write sector back
						return FAT_WriteSector(FATFS_Internal.SectorCurrentlyLoaded, FATFS_Internal.currentsector);
					}
				}
			} // End of if
		} 
		else
			break;
	} // End of while loop

	return FALSE;
}
#endif
//-----------------------------------------------------------------------------
// ListDirectory: Using starting cluster number of a directory and the FAT,
//				  list all directories and files 
//-----------------------------------------------------------------------------
void ListDirectory(UINT32 StartCluster)
{
	BYTE i,item;
	UINT16 recordoffset;
	BYTE LFNIndex=0;
	UINT32 x=0;
	FAT32_ShortEntry *directoryEntry;
	char LongFilename[MAX_LONG_FILENAME];
	char ShortFilename[13];
 
	FAT32.filenumber=0;
	printf("\r\nNo.             Filename\r\n");

	FATMisc_ClearLFN(TRUE);
	
	while (TRUE)
	{
		// If data read OK
		if (FAT32_SectorReader(StartCluster, x++))
		{
			LFNIndex=0;

			// Maximum of 15 directory entries
			for (item=0; item<=15;item++)
			{
				// Increase directory offset 
				recordoffset = (32*item);

				// Overlay directory entry over buffer
				directoryEntry = (FAT32_ShortEntry*)(FATFS_Internal.currentsector+recordoffset);
		 
				// Long File Name Text Found
				if ( FATMisc_If_LFN_TextOnly(directoryEntry) )   
					FATMisc_CacheLFN(FATFS_Internal.currentsector+recordoffset);
				 	 
				// If Invalid record found delete any long file name information collated
				else if ( FATMisc_If_LFN_Invalid(directoryEntry) ) 	
					FATMisc_ClearLFN(FALSE);

				// Normal SFN Entry and Long text exists 
				else if (FATMisc_If_LFN_Exists(directoryEntry) ) 
				{
					FAT32.filenumber++; //File / Dir Count

					// Get text
					FATMisc_GetLFNCache((BYTE*)LongFilename);

		 			if (FATMisc_If_dir_entry(directoryEntry)) printf("\r\nDirectory ");
    				if (FATMisc_If_file_entry(directoryEntry)) printf("\r\nFile ");

					// Print Filename
					printf("%d - %s [%d bytes] (0x%08lx)",FAT32.filenumber, LongFilename, directoryEntry->FileSize, (directoryEntry->FstClusHI<<16)|directoryEntry->FstClusLO);

		 			FATMisc_ClearLFN(FALSE);
				}
				 
				// Normal Entry, only 8.3 Text		 
				else if ( FATMisc_If_noLFN_SFN_Only(directoryEntry) )
				{
       				FATMisc_ClearLFN(FALSE);
					FAT32.filenumber++; //File / Dir Count
					
		 			if (FATMisc_If_dir_entry(directoryEntry)) printf("\r\nDirectory ");
    				if (FATMisc_If_file_entry(directoryEntry)) printf("\r\nFile ");

					memset(ShortFilename, 0, sizeof(ShortFilename));

					// Copy name to string
					for (i=0; i<8; i++) 
						ShortFilename[i] = directoryEntry->Name[i];

					// If not . or .. entry
					if (ShortFilename[0]!='.')
                        ShortFilename[8] = '.';
					else
						ShortFilename[8] = ' ';

					// Extension
					for (i=8; i<11; i++) 
						ShortFilename[i+1] = directoryEntry->Name[i];
		  			
					// Print Filename
					printf("%d - %s",FAT32.filenumber, ShortFilename);
					 					
				}
			}// end of for
		}
		else
			break;
	}
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -