From 065a9e44b7446874c2fd7ae2b068dc97c0287f9b Mon Sep 17 00:00:00 2001 From: Tobias Gruen Date: Fri, 26 Jan 2024 12:12:31 +0100 Subject: [PATCH 01/15] Change the FF_MakeNameCompliant behavior Invalid path characters cause now an error. (FF_ERR_FILE_INVALID_PATH if a file should be created and FF_ERR_DIR_INVALID_PATH for directories) --- ff_dir.c | 250 ++++++++++++++++++++++++--------------------- include/ff_error.h | 1 + 2 files changed, 132 insertions(+), 119 deletions(-) diff --git a/ff_dir.c b/ff_dir.c index 04cf851..fd41652 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -135,9 +135,9 @@ static BaseType_t FF_ValidShortChar( char cChar ); #endif /* if ( ffconfigLFN_SUPPORT != 0 ) */ #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - static void FF_MakeNameCompliant( FF_T_WCHAR * pcName ); + static FF_Error_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ); #else - static void FF_MakeNameCompliant( char * pcName ); + static FF_Error_t FF_MakeNameCompliant( char * pcName ); #endif #if ( FF_NOSTRCASECMP == 0 ) @@ -2875,7 +2875,13 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, } /* FF_ExtendDirectory() */ /*-----------------------------------------------------------*/ -static const uint8_t forbiddenChrs[] = +/* *INDENT-OFF* */ +#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) + static const FF_T_WCHAR forbiddenChrs[] = +#else + static const uint8_t forbiddenChrs[] = +#endif +/* *INDENT-ON* */ { /* Windows says: don't use these characters: '\/:*?"<>|' * " * / : < > ? '\' ? | */ @@ -2884,15 +2890,16 @@ static const uint8_t forbiddenChrs[] = /* *INDENT-OFF* */ #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - static void FF_MakeNameCompliant( FF_T_WCHAR * pcName ) + static FF_Error_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ) #else - static void FF_MakeNameCompliant( char * pcName ) + static FF_Error_t FF_MakeNameCompliant( char * pcName ) #endif /* *INDENT-ON* */ { + FF_Error_t xReturn = pdTRUE; BaseType_t index; - if( ( uint8_t ) pcName[ 0 ] == FF_FAT_DELETED ) /* Support Japanese KANJI symbol0xE5. */ + if( ( uint8_t ) pcName[ 0 ] == FF_FAT_DELETED ) /* Support Japanese KANJI symbol 0xE5. */ { pcName[ 0 ] = 0x05; } @@ -2903,11 +2910,11 @@ static const uint8_t forbiddenChrs[] = { if( *pcName == forbiddenChrs[ index ] ) { - *pcName = '_'; - break; + xReturn = pdFALSE; } } } + return xReturn; } /* FF_MakeNameCompliant() */ /*-----------------------------------------------------------*/ @@ -2941,160 +2948,165 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, /* Round-up the number of LFN's needed: */ xLFNCount = ( BaseType_t ) ( ( NameLen + 12 ) / 13 ); - #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - { - FF_MakeNameCompliant( pxDirEntry->pcFileName ); /* Ensure we don't break the Dir tables. */ - } - #else + if ( FF_MakeNameCompliant( pxDirEntry->pcFileName ) == pdTRUE ) { - FF_MakeNameCompliant( pxDirEntry->pcFileName ); /* Ensure we don't break the Dir tables. */ - } - #endif - memset( pucEntryBuffer, 0, sizeof( pucEntryBuffer ) ); - - #if ( ffconfigLFN_SUPPORT != 0 ) - { - /* Create and push the LFN's. */ - /* Find enough places for the LFNs and the ShortName. */ - xEntryCount = xLFNCount + 1; - } - #else - { - xEntryCount = 1; - } - #endif - - /* Create the ShortName. */ - FF_LockDirectory( pxIOManager ); - - do - { - /* Open a do {} while( pdFALSE ) loop to allow the use of break statements. */ - /* As FF_FindShortName( ) can fail, it should be called before finding a free directory entry. */ - if( ( pxFindParams->ulFlags & FIND_FLAG_SHORTNAME_SET ) == 0 ) - { - FF_CreateShortName( pxFindParams, pxDirEntry->pcFileName ); - } - - lFitShort = FF_FindShortName( pxIOManager, pxFindParams ); + memset( pucEntryBuffer, 0, sizeof( pucEntryBuffer ) ); - memcpy( pucEntryBuffer, pxFindParams->pcEntryBuffer, sizeof( pucEntryBuffer ) ); - - if( FF_isERR( lFitShort ) ) + #if ( ffconfigLFN_SUPPORT != 0 ) { - xReturn = lFitShort; - break; + /* Create and push the LFN's. */ + /* Find enough places for the LFNs and the ShortName. */ + xEntryCount = xLFNCount + 1; } - - if( lFitShort != 0 ) + #else { - /* There is no need to create a LFN entry because the file name - * fits into a normal 32-byte entry.. */ - xLFNCount = 0; xEntryCount = 1; } + #endif - lFreeEntry = FF_FindFreeDirent( pxIOManager, pxFindParams, ( uint16_t ) xEntryCount ); + /* Create the ShortName. */ + FF_LockDirectory( pxIOManager ); - if( FF_isERR( lFreeEntry ) ) - { - xReturn = lFreeEntry; - break; - } - - #if ( ffconfigLFN_SUPPORT != 0 ) + do { - if( xLFNCount > 0 ) + /* Open a do {} while( pdFALSE ) loop to allow the use of break statements. */ + /* As FF_FindShortName( ) can fail, it should be called before finding a free directory entry. */ + if( ( pxFindParams->ulFlags & FIND_FLAG_SHORTNAME_SET ) == 0 ) { - ucCheckSum = FF_CreateChkSum( pucEntryBuffer ); - xReturn = FF_CreateLFNs( pxIOManager, ulDirCluster, pxDirEntry->pcFileName, ucCheckSum, ( uint16_t ) lFreeEntry ); + FF_CreateShortName( pxFindParams, pxDirEntry->pcFileName ); } - } - #else - { - xLFNCount = 0; - } - #endif /* ffconfigLFN_SUPPORT */ - if( FF_isERR( xReturn ) == pdFALSE ) - { - #if ( ffconfigTIME_SUPPORT != 0 ) + lFitShort = FF_FindShortName( pxIOManager, pxFindParams ); + + memcpy( pucEntryBuffer, pxFindParams->pcEntryBuffer, sizeof( pucEntryBuffer ) ); + + if( FF_isERR( lFitShort ) ) { - FF_GetSystemTime( &pxDirEntry->xCreateTime ); /* Date and Time Created. */ - pxDirEntry->xModifiedTime = pxDirEntry->xCreateTime; /* Date and Time Modified. */ - pxDirEntry->xAccessedTime = pxDirEntry->xCreateTime; /* Date of Last Access. */ - FF_PlaceTime( pucEntryBuffer, FF_FAT_DIRENT_CREATE_TIME, &pxDirEntry->xCreateTime ); - FF_PlaceDate( pucEntryBuffer, FF_FAT_DIRENT_CREATE_DATE, &pxDirEntry->xCreateTime ); - FF_PlaceTime( pucEntryBuffer, FF_FAT_DIRENT_LASTMOD_TIME, &pxDirEntry->xModifiedTime ); - FF_PlaceDate( pucEntryBuffer, FF_FAT_DIRENT_LASTMOD_DATE, &pxDirEntry->xModifiedTime ); + xReturn = lFitShort; + break; } - #endif /* ffconfigTIME_SUPPORT */ - FF_putChar( pucEntryBuffer, FF_FAT_DIRENT_ATTRIB, pxDirEntry->ucAttrib ); - #if ( ffconfigSHORTNAME_CASE != 0 ) - FF_putChar( pucEntryBuffer, FF_FAT_CASE_OFFS, ( uint32_t ) lFitShort & ( FF_FAT_CASE_ATTR_BASE | FF_FAT_CASE_ATTR_EXT ) ); - #endif - FF_putShort( pucEntryBuffer, FF_FAT_DIRENT_CLUS_HIGH, ( uint16_t ) ( pxDirEntry->ulObjectCluster >> 16 ) ); - FF_putShort( pucEntryBuffer, FF_FAT_DIRENT_CLUS_LOW, ( uint16_t ) ( pxDirEntry->ulObjectCluster ) ); - FF_putLong( pucEntryBuffer, FF_FAT_DIRENT_FILESIZE, pxDirEntry->ulFileSize ); + if( lFitShort != 0 ) + { + /* There is no need to create a LFN entry because the file name + * fits into a normal 32-byte entry.. */ + xLFNCount = 0; + xEntryCount = 1; + } - xReturn = FF_InitEntryFetch( pxIOManager, ulDirCluster, &xFetchContext ); + lFreeEntry = FF_FindFreeDirent( pxIOManager, pxFindParams, ( uint16_t ) xEntryCount ); - if( FF_isERR( xReturn ) ) + if( FF_isERR( lFreeEntry ) ) { + xReturn = lFreeEntry; break; } - xReturn = FF_PushEntryWithContext( pxIOManager, ( uint16_t ) ( lFreeEntry + xLFNCount ), &xFetchContext, pucEntryBuffer ); - + #if ( ffconfigLFN_SUPPORT != 0 ) { - FF_Error_t xTempError; - - xTempError = FF_CleanupEntryFetch( pxIOManager, &xFetchContext ); - - if( FF_isERR( xReturn ) == pdFALSE ) + if( xLFNCount > 0 ) { - xReturn = xTempError; + ucCheckSum = FF_CreateChkSum( pucEntryBuffer ); + xReturn = FF_CreateLFNs( pxIOManager, ulDirCluster, pxDirEntry->pcFileName, ucCheckSum, ( uint16_t ) lFreeEntry ); } } - - if( FF_isERR( xReturn ) ) + #else { - break; + xLFNCount = 0; } + #endif /* ffconfigLFN_SUPPORT */ - #if ( ffconfigHASH_CACHE != 0 ) + if( FF_isERR( xReturn ) == pdFALSE ) { - if( FF_DirHashed( pxIOManager, ulDirCluster ) == pdFALSE ) + #if ( ffconfigTIME_SUPPORT != 0 ) { - /* Hash the directory. */ - FF_HashDir( pxIOManager, ulDirCluster ); + FF_GetSystemTime( &pxDirEntry->xCreateTime ); /* Date and Time Created. */ + pxDirEntry->xModifiedTime = pxDirEntry->xCreateTime; /* Date and Time Modified. */ + pxDirEntry->xAccessedTime = pxDirEntry->xCreateTime; /* Date of Last Access. */ + FF_PlaceTime( pucEntryBuffer, FF_FAT_DIRENT_CREATE_TIME, &pxDirEntry->xCreateTime ); + FF_PlaceDate( pucEntryBuffer, FF_FAT_DIRENT_CREATE_DATE, &pxDirEntry->xCreateTime ); + FF_PlaceTime( pucEntryBuffer, FF_FAT_DIRENT_LASTMOD_TIME, &pxDirEntry->xModifiedTime ); + FF_PlaceDate( pucEntryBuffer, FF_FAT_DIRENT_LASTMOD_DATE, &pxDirEntry->xModifiedTime ); } + #endif /* ffconfigTIME_SUPPORT */ - memcpy( pcShortName, pucEntryBuffer, 11 ); - FF_ProcessShortName( pcShortName ); /* Format the shortname to 8.3. */ - #if ( ffconfigHASH_FUNCTION == CRC16 ) + FF_putChar( pucEntryBuffer, FF_FAT_DIRENT_ATTRIB, pxDirEntry->ucAttrib ); + #if ( ffconfigSHORTNAME_CASE != 0 ) + FF_putChar( pucEntryBuffer, FF_FAT_CASE_OFFS, ( uint32_t ) lFitShort & ( FF_FAT_CASE_ATTR_BASE | FF_FAT_CASE_ATTR_EXT ) ); + #endif + FF_putShort( pucEntryBuffer, FF_FAT_DIRENT_CLUS_HIGH, ( uint16_t ) ( pxDirEntry->ulObjectCluster >> 16 ) ); + FF_putShort( pucEntryBuffer, FF_FAT_DIRENT_CLUS_LOW, ( uint16_t ) ( pxDirEntry->ulObjectCluster ) ); + FF_putLong( pucEntryBuffer, FF_FAT_DIRENT_FILESIZE, pxDirEntry->ulFileSize ); + + xReturn = FF_InitEntryFetch( pxIOManager, ulDirCluster, &xFetchContext ); + + if( FF_isERR( xReturn ) ) { - FF_AddDirentHash( pxIOManager, ulDirCluster, ( uint32_t ) FF_GetCRC16( ( uint8_t * ) pcShortName, ( uint32_t ) strlen( pcShortName ) ) ); + break; } - #elif ( ffconfigHASH_FUNCTION == CRC8 ) + + xReturn = FF_PushEntryWithContext( pxIOManager, ( uint16_t ) ( lFreeEntry + xLFNCount ), &xFetchContext, pucEntryBuffer ); + { - FF_AddDirentHash( pxIOManager, ulDirCluster, ( uint32_t ) FF_GetCRC8( ( uint8_t * ) pcShortName, strlen( pcShortName ) ) ); + FF_Error_t xTempError; + + xTempError = FF_CleanupEntryFetch( pxIOManager, &xFetchContext ); + + if( FF_isERR( xReturn ) == pdFALSE ) + { + xReturn = xTempError; + } } - #endif /* ffconfigHASH_FUNCTION */ + + if( FF_isERR( xReturn ) ) + { + break; + } + + #if ( ffconfigHASH_CACHE != 0 ) + { + if( FF_DirHashed( pxIOManager, ulDirCluster ) == pdFALSE ) + { + /* Hash the directory. */ + FF_HashDir( pxIOManager, ulDirCluster ); + } + + memcpy( pcShortName, pucEntryBuffer, 11 ); + FF_ProcessShortName( pcShortName ); /* Format the shortname to 8.3. */ + #if ( ffconfigHASH_FUNCTION == CRC16 ) + { + FF_AddDirentHash( pxIOManager, ulDirCluster, ( uint32_t ) FF_GetCRC16( ( uint8_t * ) pcShortName, ( uint32_t ) strlen( pcShortName ) ) ); + } + #elif ( ffconfigHASH_FUNCTION == CRC8 ) + { + FF_AddDirentHash( pxIOManager, ulDirCluster, ( uint32_t ) FF_GetCRC8( ( uint8_t * ) pcShortName, strlen( pcShortName ) ) ); + } + #endif /* ffconfigHASH_FUNCTION */ + } + #endif /* ffconfigHASH_CACHE*/ } - #endif /* ffconfigHASH_CACHE*/ } - } - while( pdFALSE ); + while( pdFALSE ); - FF_UnlockDirectory( pxIOManager ); + FF_UnlockDirectory( pxIOManager ); - if( FF_isERR( xReturn ) == pdFALSE ) + if( FF_isERR( xReturn ) == pdFALSE ) + { + if( pxDirEntry != NULL ) + { + pxDirEntry->usCurrentItem = ( uint16_t ) ( lFreeEntry + xLFNCount ); + } + } + } /* if ( FF_MakeNameCompliant( pxDirEntry->pcFileName ) == pdTRUE ) */ + else { - if( pxDirEntry != NULL ) + if ( pxDirEntry->ucAttrib & FF_FAT_ATTR_DIR ) + { + xReturn = FF_createERR( FF_ERR_DIR_INVALID_PATH, FF_CREATEDIRENT ); + } + else { - pxDirEntry->usCurrentItem = ( uint16_t ) ( lFreeEntry + xLFNCount ); + xReturn = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_CREATEDIRENT ); } } diff --git a/include/ff_error.h b/include/ff_error.h index 18ff2f1..6c887f3 100644 --- a/include/ff_error.h +++ b/include/ff_error.h @@ -118,6 +118,7 @@ #define FF_MKDIR ( ( 12 << FF_FUNCTION_SHIFT ) | FF_MODULE_DIR ) #define FF_TRAVERSE ( ( 13 << FF_FUNCTION_SHIFT ) | FF_MODULE_DIR ) #define FF_FINDDIR ( ( 14 << FF_FUNCTION_SHIFT ) | FF_MODULE_DIR ) +#define FF_CREATEDIRENT ( ( 15 << FF_FUNCTION_SHIFT ) | FF_MODULE_DIR ) /*----- FF_FILE - The FreeRTOS+FAT file handling routines. */ #define FF_GETMODEBITS ( ( 1 << FF_FUNCTION_SHIFT ) | FF_MODULE_FILE ) From 61baf4e5adcba2dc28507dd1befc157e00950577 Mon Sep 17 00:00:00 2001 From: Tobias Gruen Date: Thu, 1 Feb 2024 00:39:09 +0100 Subject: [PATCH 02/15] Exclude the forbiddenChrs array from uncrustify --- ff_dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ff_dir.c b/ff_dir.c index fd41652..f608de1 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -2881,12 +2881,12 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, #else static const uint8_t forbiddenChrs[] = #endif -/* *INDENT-ON* */ { /* Windows says: don't use these characters: '\/:*?"<>|' * " * / : < > ? '\' ? | */ 0x22, 0x2A, 0x2F, 0x3A, 0x3C, 0x3E, 0x3F, 0x5C, 0x7F, 0x7C }; +/* *INDENT-ON* */ /* *INDENT-OFF* */ #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) From a7e51c1a18419f3e121b4df59a9c6a4a35bb141b Mon Sep 17 00:00:00 2001 From: Tobias Gruen Date: Thu, 1 Feb 2024 01:14:11 +0100 Subject: [PATCH 03/15] Cleanup indentation and improve consistency Fixed the indentation. And also removed the superfluous (== pdTrue) in the if condition. --- ff_dir.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ff_dir.c b/ff_dir.c index f608de1..d8c067a 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -2914,6 +2914,7 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, } } } + return xReturn; } /* FF_MakeNameCompliant() */ /*-----------------------------------------------------------*/ @@ -2948,7 +2949,7 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, /* Round-up the number of LFN's needed: */ xLFNCount = ( BaseType_t ) ( ( NameLen + 12 ) / 13 ); - if ( FF_MakeNameCompliant( pxDirEntry->pcFileName ) == pdTRUE ) + if( FF_MakeNameCompliant( pxDirEntry->pcFileName ) ) { memset( pucEntryBuffer, 0, sizeof( pucEntryBuffer ) ); @@ -2989,7 +2990,7 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, if( lFitShort != 0 ) { /* There is no need to create a LFN entry because the file name - * fits into a normal 32-byte entry.. */ + * fits into a normal 32-byte entry.. */ xLFNCount = 0; xEntryCount = 1; } @@ -3097,10 +3098,10 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, pxDirEntry->usCurrentItem = ( uint16_t ) ( lFreeEntry + xLFNCount ); } } - } /* if ( FF_MakeNameCompliant( pxDirEntry->pcFileName ) == pdTRUE ) */ + } /* if( FF_MakeNameCompliant( pxDirEntry->pcFileName ) ) */ else { - if ( pxDirEntry->ucAttrib & FF_FAT_ATTR_DIR ) + if( pxDirEntry->ucAttrib & FF_FAT_ATTR_DIR ) { xReturn = FF_createERR( FF_ERR_DIR_INVALID_PATH, FF_CREATEDIRENT ); } From e0037f3e3fb55ff97582c8e3161ec93a39c62432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Gr=C3=BCn?= Date: Wed, 7 Feb 2024 00:18:33 +0100 Subject: [PATCH 04/15] Move the happy path The happy code path is now located in the else branch which improves locality of the error handling. --- ff_dir.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ff_dir.c b/ff_dir.c index d8c067a..764b70a 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -2949,7 +2949,18 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, /* Round-up the number of LFN's needed: */ xLFNCount = ( BaseType_t ) ( ( NameLen + 12 ) / 13 ); - if( FF_MakeNameCompliant( pxDirEntry->pcFileName ) ) + if( FF_MakeNameCompliant( pxDirEntry->pcFileName ) == pdFALSE ) + { + if( pxDirEntry->ucAttrib & FF_FAT_ATTR_DIR ) + { + xReturn = FF_createERR( FF_ERR_DIR_INVALID_PATH, FF_CREATEDIRENT ); + } + else + { + xReturn = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_CREATEDIRENT ); + } + } + else { memset( pucEntryBuffer, 0, sizeof( pucEntryBuffer ) ); @@ -3098,17 +3109,6 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, pxDirEntry->usCurrentItem = ( uint16_t ) ( lFreeEntry + xLFNCount ); } } - } /* if( FF_MakeNameCompliant( pxDirEntry->pcFileName ) ) */ - else - { - if( pxDirEntry->ucAttrib & FF_FAT_ATTR_DIR ) - { - xReturn = FF_createERR( FF_ERR_DIR_INVALID_PATH, FF_CREATEDIRENT ); - } - else - { - xReturn = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_CREATEDIRENT ); - } } return xReturn; From 066d9e7b1090d7bcec127c5a8afdb4ae035ae703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Gr=C3=BCn?= Date: Fri, 9 Feb 2024 01:20:09 +0100 Subject: [PATCH 05/15] Change MakeNameCompliant return type to BaseType_t --- ff_dir.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ff_dir.c b/ff_dir.c index 764b70a..e1a7376 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -135,9 +135,9 @@ static BaseType_t FF_ValidShortChar( char cChar ); #endif /* if ( ffconfigLFN_SUPPORT != 0 ) */ #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - static FF_Error_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ); + static BaseType_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ); #else - static FF_Error_t FF_MakeNameCompliant( char * pcName ); + static BaseType_t FF_MakeNameCompliant( char * pcName ); #endif #if ( FF_NOSTRCASECMP == 0 ) @@ -2890,9 +2890,9 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, /* *INDENT-OFF* */ #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - static FF_Error_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ) + static BaseType_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ) #else - static FF_Error_t FF_MakeNameCompliant( char * pcName ) + static BaseType_t FF_MakeNameCompliant( char * pcName ) #endif /* *INDENT-ON* */ { From 4d7e214ea7597f6def495c25485d165330a7a2b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Gr=C3=BCn?= Date: Fri, 9 Feb 2024 02:03:07 +0100 Subject: [PATCH 06/15] Fix BaseType_t --- ff_dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ff_dir.c b/ff_dir.c index e1a7376..f328a5a 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -2896,7 +2896,7 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, #endif /* *INDENT-ON* */ { - FF_Error_t xReturn = pdTRUE; + BaseType_t xReturn = pdTRUE; BaseType_t index; if( ( uint8_t ) pcName[ 0 ] == FF_FAT_DELETED ) /* Support Japanese KANJI symbol 0xE5. */ From 59b1673c9d7f7164583593f0b48c53285f8b27ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Gr=C3=BCn?= Date: Fri, 9 Feb 2024 01:51:56 +0100 Subject: [PATCH 07/15] Rename chrs to chars --- ff_dir.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ff_dir.c b/ff_dir.c index f328a5a..a7bb9d0 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -2877,9 +2877,9 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, /* *INDENT-OFF* */ #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - static const FF_T_WCHAR forbiddenChrs[] = + static const FF_T_WCHAR forbiddenChars[] = #else - static const uint8_t forbiddenChrs[] = + static const uint8_t forbiddenChars[] = #endif { /* Windows says: don't use these characters: '\/:*?"<>|' @@ -2906,9 +2906,9 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, for( ; *pcName; pcName++ ) { - for( index = 0; index < ( BaseType_t ) sizeof( forbiddenChrs ); index++ ) + for( index = 0; index < ( BaseType_t ) sizeof( forbiddenChars ); index++ ) { - if( *pcName == forbiddenChrs[ index ] ) + if( *pcName == forbiddenChars[ index ] ) { xReturn = pdFALSE; } From 65f1137c4614c91cfdee35e53b54ca41de09ec9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Gr=C3=BCn?= Date: Fri, 9 Feb 2024 02:31:05 +0100 Subject: [PATCH 08/15] Fix misra compliance --- ff_dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ff_dir.c b/ff_dir.c index a7bb9d0..8061c96 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -2951,7 +2951,7 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, if( FF_MakeNameCompliant( pxDirEntry->pcFileName ) == pdFALSE ) { - if( pxDirEntry->ucAttrib & FF_FAT_ATTR_DIR ) + if( ( pxDirEntry->ucAttrib & FF_FAT_ATTR_DIR ) != 0U ) { xReturn = FF_createERR( FF_ERR_DIR_INVALID_PATH, FF_CREATEDIRENT ); } From d065004717381f764829c2babfa01c394f99b2f4 Mon Sep 17 00:00:00 2001 From: Tobias Gruen Date: Sun, 11 Feb 2024 22:21:26 +0100 Subject: [PATCH 09/15] Check the filenames earlier These changes were made by hitibosch. They move the name check upward in the call-hierarchy. --- ff_dir.c | 245 ++++++++++++++++++++++----------------------- ff_file.c | 5 + include/ff_dir.h | 10 ++ include/ff_error.h | 2 +- 4 files changed, 135 insertions(+), 127 deletions(-) diff --git a/ff_dir.c b/ff_dir.c index 8061c96..ab5f3f7 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -134,12 +134,6 @@ static BaseType_t FF_ValidShortChar( char cChar ); #endif /* if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) */ #endif /* if ( ffconfigLFN_SUPPORT != 0 ) */ -#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - static BaseType_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ); -#else - static BaseType_t FF_MakeNameCompliant( char * pcName ); -#endif - #if ( FF_NOSTRCASECMP == 0 ) static portINLINE unsigned char prvToLower( unsigned char c ) { @@ -2890,23 +2884,23 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, /* *INDENT-OFF* */ #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - static BaseType_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ) + BaseType_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ) #else - static BaseType_t FF_MakeNameCompliant( char * pcName ) + BaseType_t FF_MakeNameCompliant( char * pcName ) #endif /* *INDENT-ON* */ { BaseType_t xReturn = pdTRUE; BaseType_t index; - if( ( uint8_t ) pcName[ 0 ] == FF_FAT_DELETED ) /* Support Japanese KANJI symbol 0xE5. */ + if( ( uint8_t ) pcName[ 0 ] == FF_FAT_DELETED ) /* Do not create a "deleted" entry 0xE5. */ { - pcName[ 0 ] = 0x05; + xReturn = pdFALSE; } for( ; *pcName; pcName++ ) { - for( index = 0; index < ( BaseType_t ) sizeof( forbiddenChars ); index++ ) + for( index = 0; index < ( BaseType_t ) ( sizeof( forbiddenChars ) / sizeof ( forbiddenChars[ 0 ] ) ); index++ ) { if( *pcName == forbiddenChars[ index ] ) { @@ -2949,165 +2943,151 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, /* Round-up the number of LFN's needed: */ xLFNCount = ( BaseType_t ) ( ( NameLen + 12 ) / 13 ); - if( FF_MakeNameCompliant( pxDirEntry->pcFileName ) == pdFALSE ) + memset( pucEntryBuffer, 0, sizeof( pucEntryBuffer ) ); + + #if ( ffconfigLFN_SUPPORT != 0 ) + { + /* Create and push the LFN's. */ + /* Find enough places for the LFNs and the ShortName. */ + xEntryCount = xLFNCount + 1; + } + #else + { + xEntryCount = 1; + } + #endif + + /* Create the ShortName. */ + FF_LockDirectory( pxIOManager ); + + do { - if( ( pxDirEntry->ucAttrib & FF_FAT_ATTR_DIR ) != 0U ) + /* Open a do {} while( pdFALSE ) loop to allow the use of break statements. */ + /* As FF_FindShortName( ) can fail, it should be called before finding a free directory entry. */ + if( ( pxFindParams->ulFlags & FIND_FLAG_SHORTNAME_SET ) == 0 ) { - xReturn = FF_createERR( FF_ERR_DIR_INVALID_PATH, FF_CREATEDIRENT ); + FF_CreateShortName( pxFindParams, pxDirEntry->pcFileName ); } - else + + lFitShort = FF_FindShortName( pxIOManager, pxFindParams ); + + memcpy( pucEntryBuffer, pxFindParams->pcEntryBuffer, sizeof( pucEntryBuffer ) ); + + if( FF_isERR( lFitShort ) ) { - xReturn = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_CREATEDIRENT ); + xReturn = lFitShort; + break; + } + + if( lFitShort != 0 ) + { + /* There is no need to create a LFN entry because the file name + * fits into a normal 32-byte entry.. */ + xLFNCount = 0; + xEntryCount = 1; + } + + lFreeEntry = FF_FindFreeDirent( pxIOManager, pxFindParams, ( uint16_t ) xEntryCount ); + + if( FF_isERR( lFreeEntry ) ) + { + xReturn = lFreeEntry; + break; } - } - else - { - memset( pucEntryBuffer, 0, sizeof( pucEntryBuffer ) ); #if ( ffconfigLFN_SUPPORT != 0 ) { - /* Create and push the LFN's. */ - /* Find enough places for the LFNs and the ShortName. */ - xEntryCount = xLFNCount + 1; + if( xLFNCount > 0 ) + { + ucCheckSum = FF_CreateChkSum( pucEntryBuffer ); + xReturn = FF_CreateLFNs( pxIOManager, ulDirCluster, pxDirEntry->pcFileName, ucCheckSum, ( uint16_t ) lFreeEntry ); + } } #else { - xEntryCount = 1; + xLFNCount = 0; } - #endif - - /* Create the ShortName. */ - FF_LockDirectory( pxIOManager ); + #endif /* ffconfigLFN_SUPPORT */ - do + if( FF_isERR( xReturn ) == pdFALSE ) { - /* Open a do {} while( pdFALSE ) loop to allow the use of break statements. */ - /* As FF_FindShortName( ) can fail, it should be called before finding a free directory entry. */ - if( ( pxFindParams->ulFlags & FIND_FLAG_SHORTNAME_SET ) == 0 ) + #if ( ffconfigTIME_SUPPORT != 0 ) { - FF_CreateShortName( pxFindParams, pxDirEntry->pcFileName ); + FF_GetSystemTime( &pxDirEntry->xCreateTime ); /* Date and Time Created. */ + pxDirEntry->xModifiedTime = pxDirEntry->xCreateTime; /* Date and Time Modified. */ + pxDirEntry->xAccessedTime = pxDirEntry->xCreateTime; /* Date of Last Access. */ + FF_PlaceTime( pucEntryBuffer, FF_FAT_DIRENT_CREATE_TIME, &pxDirEntry->xCreateTime ); + FF_PlaceDate( pucEntryBuffer, FF_FAT_DIRENT_CREATE_DATE, &pxDirEntry->xCreateTime ); + FF_PlaceTime( pucEntryBuffer, FF_FAT_DIRENT_LASTMOD_TIME, &pxDirEntry->xModifiedTime ); + FF_PlaceDate( pucEntryBuffer, FF_FAT_DIRENT_LASTMOD_DATE, &pxDirEntry->xModifiedTime ); } + #endif /* ffconfigTIME_SUPPORT */ - lFitShort = FF_FindShortName( pxIOManager, pxFindParams ); + FF_putChar( pucEntryBuffer, FF_FAT_DIRENT_ATTRIB, pxDirEntry->ucAttrib ); + #if ( ffconfigSHORTNAME_CASE != 0 ) + FF_putChar( pucEntryBuffer, FF_FAT_CASE_OFFS, ( uint32_t ) lFitShort & ( FF_FAT_CASE_ATTR_BASE | FF_FAT_CASE_ATTR_EXT ) ); + #endif + FF_putShort( pucEntryBuffer, FF_FAT_DIRENT_CLUS_HIGH, ( uint16_t ) ( pxDirEntry->ulObjectCluster >> 16 ) ); + FF_putShort( pucEntryBuffer, FF_FAT_DIRENT_CLUS_LOW, ( uint16_t ) ( pxDirEntry->ulObjectCluster ) ); + FF_putLong( pucEntryBuffer, FF_FAT_DIRENT_FILESIZE, pxDirEntry->ulFileSize ); - memcpy( pucEntryBuffer, pxFindParams->pcEntryBuffer, sizeof( pucEntryBuffer ) ); + xReturn = FF_InitEntryFetch( pxIOManager, ulDirCluster, &xFetchContext ); - if( FF_isERR( lFitShort ) ) + if( FF_isERR( xReturn ) ) { - xReturn = lFitShort; break; } - if( lFitShort != 0 ) - { - /* There is no need to create a LFN entry because the file name - * fits into a normal 32-byte entry.. */ - xLFNCount = 0; - xEntryCount = 1; - } - - lFreeEntry = FF_FindFreeDirent( pxIOManager, pxFindParams, ( uint16_t ) xEntryCount ); + xReturn = FF_PushEntryWithContext( pxIOManager, ( uint16_t ) ( lFreeEntry + xLFNCount ), &xFetchContext, pucEntryBuffer ); - if( FF_isERR( lFreeEntry ) ) { - xReturn = lFreeEntry; - break; - } + FF_Error_t xTempError; - #if ( ffconfigLFN_SUPPORT != 0 ) - { - if( xLFNCount > 0 ) + xTempError = FF_CleanupEntryFetch( pxIOManager, &xFetchContext ); + + if( FF_isERR( xReturn ) == pdFALSE ) { - ucCheckSum = FF_CreateChkSum( pucEntryBuffer ); - xReturn = FF_CreateLFNs( pxIOManager, ulDirCluster, pxDirEntry->pcFileName, ucCheckSum, ( uint16_t ) lFreeEntry ); + xReturn = xTempError; } } - #else + + if( FF_isERR( xReturn ) ) { - xLFNCount = 0; + break; } - #endif /* ffconfigLFN_SUPPORT */ - if( FF_isERR( xReturn ) == pdFALSE ) + #if ( ffconfigHASH_CACHE != 0 ) { - #if ( ffconfigTIME_SUPPORT != 0 ) - { - FF_GetSystemTime( &pxDirEntry->xCreateTime ); /* Date and Time Created. */ - pxDirEntry->xModifiedTime = pxDirEntry->xCreateTime; /* Date and Time Modified. */ - pxDirEntry->xAccessedTime = pxDirEntry->xCreateTime; /* Date of Last Access. */ - FF_PlaceTime( pucEntryBuffer, FF_FAT_DIRENT_CREATE_TIME, &pxDirEntry->xCreateTime ); - FF_PlaceDate( pucEntryBuffer, FF_FAT_DIRENT_CREATE_DATE, &pxDirEntry->xCreateTime ); - FF_PlaceTime( pucEntryBuffer, FF_FAT_DIRENT_LASTMOD_TIME, &pxDirEntry->xModifiedTime ); - FF_PlaceDate( pucEntryBuffer, FF_FAT_DIRENT_LASTMOD_DATE, &pxDirEntry->xModifiedTime ); - } - #endif /* ffconfigTIME_SUPPORT */ - - FF_putChar( pucEntryBuffer, FF_FAT_DIRENT_ATTRIB, pxDirEntry->ucAttrib ); - #if ( ffconfigSHORTNAME_CASE != 0 ) - FF_putChar( pucEntryBuffer, FF_FAT_CASE_OFFS, ( uint32_t ) lFitShort & ( FF_FAT_CASE_ATTR_BASE | FF_FAT_CASE_ATTR_EXT ) ); - #endif - FF_putShort( pucEntryBuffer, FF_FAT_DIRENT_CLUS_HIGH, ( uint16_t ) ( pxDirEntry->ulObjectCluster >> 16 ) ); - FF_putShort( pucEntryBuffer, FF_FAT_DIRENT_CLUS_LOW, ( uint16_t ) ( pxDirEntry->ulObjectCluster ) ); - FF_putLong( pucEntryBuffer, FF_FAT_DIRENT_FILESIZE, pxDirEntry->ulFileSize ); - - xReturn = FF_InitEntryFetch( pxIOManager, ulDirCluster, &xFetchContext ); - - if( FF_isERR( xReturn ) ) - { - break; - } - - xReturn = FF_PushEntryWithContext( pxIOManager, ( uint16_t ) ( lFreeEntry + xLFNCount ), &xFetchContext, pucEntryBuffer ); - + if( FF_DirHashed( pxIOManager, ulDirCluster ) == pdFALSE ) { - FF_Error_t xTempError; - - xTempError = FF_CleanupEntryFetch( pxIOManager, &xFetchContext ); - - if( FF_isERR( xReturn ) == pdFALSE ) - { - xReturn = xTempError; - } + /* Hash the directory. */ + FF_HashDir( pxIOManager, ulDirCluster ); } - if( FF_isERR( xReturn ) ) + memcpy( pcShortName, pucEntryBuffer, 11 ); + FF_ProcessShortName( pcShortName ); /* Format the shortname to 8.3. */ + #if ( ffconfigHASH_FUNCTION == CRC16 ) { - break; + FF_AddDirentHash( pxIOManager, ulDirCluster, ( uint32_t ) FF_GetCRC16( ( uint8_t * ) pcShortName, ( uint32_t ) strlen( pcShortName ) ) ); } - - #if ( ffconfigHASH_CACHE != 0 ) + #elif ( ffconfigHASH_FUNCTION == CRC8 ) { - if( FF_DirHashed( pxIOManager, ulDirCluster ) == pdFALSE ) - { - /* Hash the directory. */ - FF_HashDir( pxIOManager, ulDirCluster ); - } - - memcpy( pcShortName, pucEntryBuffer, 11 ); - FF_ProcessShortName( pcShortName ); /* Format the shortname to 8.3. */ - #if ( ffconfigHASH_FUNCTION == CRC16 ) - { - FF_AddDirentHash( pxIOManager, ulDirCluster, ( uint32_t ) FF_GetCRC16( ( uint8_t * ) pcShortName, ( uint32_t ) strlen( pcShortName ) ) ); - } - #elif ( ffconfigHASH_FUNCTION == CRC8 ) - { - FF_AddDirentHash( pxIOManager, ulDirCluster, ( uint32_t ) FF_GetCRC8( ( uint8_t * ) pcShortName, strlen( pcShortName ) ) ); - } - #endif /* ffconfigHASH_FUNCTION */ + FF_AddDirentHash( pxIOManager, ulDirCluster, ( uint32_t ) FF_GetCRC8( ( uint8_t * ) pcShortName, strlen( pcShortName ) ) ); } - #endif /* ffconfigHASH_CACHE*/ + #endif /* ffconfigHASH_FUNCTION */ } + #endif /* ffconfigHASH_CACHE*/ } - while( pdFALSE ); + } + while( pdFALSE ); - FF_UnlockDirectory( pxIOManager ); + FF_UnlockDirectory( pxIOManager ); - if( FF_isERR( xReturn ) == pdFALSE ) + if( FF_isERR( xReturn ) == pdFALSE ) + { + if( pxDirEntry != NULL ) { - if( pxDirEntry != NULL ) - { - pxDirEntry->usCurrentItem = ( uint16_t ) ( lFreeEntry + xLFNCount ); - } + pxDirEntry->usCurrentItem = ( uint16_t ) ( lFreeEntry + xLFNCount ); } } @@ -3140,7 +3120,15 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, STRNCPY( xMyFile.pcFileName, pcFileName, ffconfigMAX_FILENAME - 1 ); xMyFile.pcFileName[ ffconfigMAX_FILENAME - 1 ] = 0; - xMyFile.ulObjectCluster = FF_CreateClusterChain( pxIOManager, &xError ); + if( FF_MakeNameCompliant( xMyFile.pcFileName ) == pdFALSE ) + { + xError = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_CREATEFILE ); + } + + if( FF_isERR( xError ) == pdFALSE ) + { + xMyFile.ulObjectCluster = FF_CreateClusterChain( pxIOManager, &xError ); + } if( FF_isERR( xError ) == pdFALSE ) { @@ -3239,6 +3227,11 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, xError = FF_createERR( FF_ERR_NULL_POINTER, FF_MKDIR ); break; } + if( FF_MakeNameCompliant( pcPath ) == pdFALSE ) + { + xError = FF_createERR( FF_ERR_DIR_INVALID_PATH, FF_MKDIR ); + break; + } #if ( ffconfigREMOVABLE_MEDIA != 0 ) if( ( pxIOManager->ucFlags & FF_IOMAN_DEVICE_IS_EXTRACTED ) != 0 ) diff --git a/ff_file.c b/ff_file.c index 3ad033a..c2b0a9c 100644 --- a/ff_file.c +++ b/ff_file.c @@ -905,6 +905,11 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager, xError = FF_createERR( FF_ERR_NULL_POINTER, FF_MOVE ); } + else if( FF_MakeNameCompliant( szDestinationFile ) == pdFALSE ) + { + xError = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_MOVE ); + } + #if ( ffconfigREMOVABLE_MEDIA != 0 ) else if( ( pxIOManager->ucFlags & FF_IOMAN_DEVICE_IS_EXTRACTED ) != 0 ) { diff --git a/include/ff_dir.h b/include/ff_dir.h index 7169f7d..022fe8f 100644 --- a/include/ff_dir.h +++ b/include/ff_dir.h @@ -172,6 +172,16 @@ int8_t FF_PushEntry( FF_IOManager_t * pxIOManager, uint8_t * buffer, void * pParam ); +/* + * FF_MakeNameCompliant is a function that checks if a given path name + * contains any legal characters only. + */ +#if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) + BaseType_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ); +#else + BaseType_t FF_MakeNameCompliant( char * pcName ); +#endif + static portINLINE BaseType_t FF_isEndOfDir( const uint8_t * pucEntryBuffer ) { return pucEntryBuffer[ 0 ] == ( uint8_t ) 0; diff --git a/include/ff_error.h b/include/ff_error.h index 6c887f3..c2e2286 100644 --- a/include/ff_error.h +++ b/include/ff_error.h @@ -118,7 +118,7 @@ #define FF_MKDIR ( ( 12 << FF_FUNCTION_SHIFT ) | FF_MODULE_DIR ) #define FF_TRAVERSE ( ( 13 << FF_FUNCTION_SHIFT ) | FF_MODULE_DIR ) #define FF_FINDDIR ( ( 14 << FF_FUNCTION_SHIFT ) | FF_MODULE_DIR ) -#define FF_CREATEDIRENT ( ( 15 << FF_FUNCTION_SHIFT ) | FF_MODULE_DIR ) +#define FF_CREATEFILE ( ( 15 << FF_FUNCTION_SHIFT ) | FF_MODULE_DIR ) /*----- FF_FILE - The FreeRTOS+FAT file handling routines. */ #define FF_GETMODEBITS ( ( 1 << FF_FUNCTION_SHIFT ) | FF_MODULE_FILE ) From 2d1229cd05d58fbb5fa55f779544e86e3ffe63ec Mon Sep 17 00:00:00 2001 From: Tobias Gruen Date: Mon, 12 Feb 2024 00:31:53 +0100 Subject: [PATCH 10/15] Use the mkdir pcDirName for the filename check The previously used pcPath contained slashes. This lead to an error, because slashes are invalid Dir/Filename characters. --- ff_dir.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ff_dir.c b/ff_dir.c index ab5f3f7..390bb3a 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -3227,11 +3227,6 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, xError = FF_createERR( FF_ERR_NULL_POINTER, FF_MKDIR ); break; } - if( FF_MakeNameCompliant( pcPath ) == pdFALSE ) - { - xError = FF_createERR( FF_ERR_DIR_INVALID_PATH, FF_MKDIR ); - break; - } #if ( ffconfigREMOVABLE_MEDIA != 0 ) if( ( pxIOManager->ucFlags & FF_IOMAN_DEVICE_IS_EXTRACTED ) != 0 ) @@ -3275,6 +3270,12 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, break; } + if (FF_MakeNameCompliant(pcDirName) == pdFALSE) + { + xError = FF_createERR(FF_ERR_DIR_INVALID_PATH, FF_MKDIR); + break; + } + xFindParams.ulDirCluster = FF_FindDir( pxIOManager, pcPath, ( uint16_t ) xIndex, &xError ); if( FF_isERR( xError ) ) From 01753aafe001ece1100beaadddfdafb348b8eacf Mon Sep 17 00:00:00 2001 From: Tobias Gruen Date: Mon, 12 Feb 2024 01:03:01 +0100 Subject: [PATCH 11/15] Rename function FF_MakeNameCompliant to FF_IsNameCompliant The function does only perform checks now. --- ff_dir.c | 10 +++++----- ff_file.c | 2 +- include/ff_dir.h | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/ff_dir.c b/ff_dir.c index 390bb3a..62129ff 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -2884,9 +2884,9 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, /* *INDENT-OFF* */ #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - BaseType_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ) + BaseType_t FF_IsNameCompliant( FF_T_WCHAR * pcName ) #else - BaseType_t FF_MakeNameCompliant( char * pcName ) + BaseType_t FF_IsNameCompliant( char * pcName ) #endif /* *INDENT-ON* */ { @@ -2910,7 +2910,7 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, } return xReturn; -} /* FF_MakeNameCompliant() */ +} /* FF_IsNameCompliant() */ /*-----------------------------------------------------------*/ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, @@ -3120,7 +3120,7 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, STRNCPY( xMyFile.pcFileName, pcFileName, ffconfigMAX_FILENAME - 1 ); xMyFile.pcFileName[ ffconfigMAX_FILENAME - 1 ] = 0; - if( FF_MakeNameCompliant( xMyFile.pcFileName ) == pdFALSE ) + if( FF_IsNameCompliant( xMyFile.pcFileName ) == pdFALSE ) { xError = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_CREATEFILE ); } @@ -3270,7 +3270,7 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, break; } - if (FF_MakeNameCompliant(pcDirName) == pdFALSE) + if (FF_IsNameCompliant(pcDirName) == pdFALSE) { xError = FF_createERR(FF_ERR_DIR_INVALID_PATH, FF_MKDIR); break; diff --git a/ff_file.c b/ff_file.c index c2b0a9c..13abc8b 100644 --- a/ff_file.c +++ b/ff_file.c @@ -905,7 +905,7 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager, xError = FF_createERR( FF_ERR_NULL_POINTER, FF_MOVE ); } - else if( FF_MakeNameCompliant( szDestinationFile ) == pdFALSE ) + else if( FF_IsNameCompliant( szDestinationFile ) == pdFALSE ) { xError = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_MOVE ); } diff --git a/include/ff_dir.h b/include/ff_dir.h index 022fe8f..4a23624 100644 --- a/include/ff_dir.h +++ b/include/ff_dir.h @@ -173,13 +173,13 @@ int8_t FF_PushEntry( FF_IOManager_t * pxIOManager, void * pParam ); /* - * FF_MakeNameCompliant is a function that checks if a given path name + * FF_IsNameCompliant is a function that checks if a given path name * contains any legal characters only. */ #if ( ffconfigUNICODE_UTF16_SUPPORT != 0 ) - BaseType_t FF_MakeNameCompliant( FF_T_WCHAR * pcName ); + BaseType_t FF_IsNameCompliant( FF_T_WCHAR * pcName ); #else - BaseType_t FF_MakeNameCompliant( char * pcName ); + BaseType_t FF_IsNameCompliant( char * pcName ); #endif static portINLINE BaseType_t FF_isEndOfDir( const uint8_t * pucEntryBuffer ) From d44ee939abd95c01f3c1f399ae57c299e7d09e65 Mon Sep 17 00:00:00 2001 From: Tobias Gruen Date: Tue, 13 Feb 2024 01:43:22 +0100 Subject: [PATCH 12/15] Fix the IsNameCompliant check for FF_MOVE This change was made by hitibosch. I added just a comment. --- ff_file.c | 69 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/ff_file.c b/ff_file.c index 13abc8b..472d5a1 100644 --- a/ff_file.c +++ b/ff_file.c @@ -868,6 +868,7 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager, * @retval FF_ERR_FILE_DESTINATION_EXISTS if the destination file exists. * @retval FF_ERR_FILE_COULD_NOT_CREATE_DIRENT if dirent creation failed (fatal error!). * @retval FF_ERR_FILE_DIR_NOT_FOUND if destination directory was not found. + * @retval FF_ERR_FILE_INVALID_PATH if destination path is invalid. * @retval FF_ERR_FILE_SOURCE_NOT_FOUND if the source file was not found. * **/ @@ -890,7 +891,7 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager, FF_FILE * pSrcFile, * pxDestFile; FF_DirEnt_t xMyFile; uint8_t ucEntryBuffer[ 32 ]; - UBaseType_t xIndex; + size_t uxIndex = 0U; uint32_t ulDirCluster = 0ul; FF_FetchContext_t xFetchContext; @@ -905,18 +906,47 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager, xError = FF_createERR( FF_ERR_NULL_POINTER, FF_MOVE ); } - else if( FF_IsNameCompliant( szDestinationFile ) == pdFALSE ) - { - xError = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_MOVE ); - } - #if ( ffconfigREMOVABLE_MEDIA != 0 ) - else if( ( pxIOManager->ucFlags & FF_IOMAN_DEVICE_IS_EXTRACTED ) != 0 ) + else if( ( pxIOManager->ucFlags & FF_IOMAN_DEVICE_IS_EXTRACTED ) != 0U ) { xError = FF_createERR( FF_ERR_IOMAN_DRIVER_NOMEDIUM, FF_MOVE ); } #endif /* ffconfigREMOVABLE_MEDIA */ - else + + if( FF_isERR( xError ) == pdFALSE ) + { + uxIndex = ( size_t ) STRLEN( szDestinationFile ); + + /* Find the base name. */ + while( uxIndex != 0U ) + { + if( ( szDestinationFile[ uxIndex ] == '\\' ) || ( szDestinationFile[ uxIndex ] == '/' ) ) + { + break; + } + + uxIndex--; + } + + if( uxIndex == 0U ) + { + /* Give the directory part a minimum + * length of 1, to get '/' at least. */ + uxIndex = 1U; + } + + /* Copy the base name of the destination file. */ + STRNCPY( xMyFile.pcFileName, ( szDestinationFile + uxIndex + 1 ), ffconfigMAX_FILENAME - 1 ); + xMyFile.pcFileName[ ffconfigMAX_FILENAME - 1 ] = 0; + + /* Now check if the target base name is compliant. */ + if( FF_IsNameCompliant( xMyFile.pcFileName ) == pdFALSE ) + { + xError = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_MOVE ); + } + } + + if( FF_isERR( xError ) == pdFALSE ) { /* Check destination file doesn't exist! */ pxDestFile = FF_Open( pxIOManager, szDestinationFile, FF_MODE_READ, &xError ); @@ -974,30 +1004,9 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager, xMyFile.ulObjectCluster = pSrcFile->ulObjectCluster; xMyFile.usCurrentItem = 0; - xIndex = ( UBaseType_t ) STRLEN( szDestinationFile ); - - while( xIndex != 0 ) - { - if( ( szDestinationFile[ xIndex ] == '\\' ) || ( szDestinationFile[ xIndex ] == '/' ) ) - { - break; - } - - xIndex--; - } - - /* Copy the base name of the destination file. */ - STRNCPY( xMyFile.pcFileName, ( szDestinationFile + xIndex + 1 ), ffconfigMAX_FILENAME - 1 ); - xMyFile.pcFileName[ ffconfigMAX_FILENAME - 1 ] = 0; - - if( xIndex == 0 ) - { - xIndex = 1; - } - /* Find the (cluster of the) directory in which the target file will be located. * It must exist before calling FF_Move(). */ - ulDirCluster = FF_FindDir( pxIOManager, szDestinationFile, ( uint16_t ) xIndex, &xError ); + ulDirCluster = FF_FindDir( pxIOManager, szDestinationFile, ( uint16_t ) uxIndex, &xError ); } } } From 4268c2386930d723a0adf00b504a4ced79a105f4 Mon Sep 17 00:00:00 2001 From: Tobias Gruen Date: Wed, 14 Feb 2024 01:44:17 +0100 Subject: [PATCH 13/15] Fix the filename handling in ff_Move The first character of the destination filename was omitted when a file in the root directory was supplied. This change was made by hitibosch. --- ff_file.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ff_file.c b/ff_file.c index 472d5a1..5d09746 100644 --- a/ff_file.c +++ b/ff_file.c @@ -928,13 +928,6 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager, uxIndex--; } - if( uxIndex == 0U ) - { - /* Give the directory part a minimum - * length of 1, to get '/' at least. */ - uxIndex = 1U; - } - /* Copy the base name of the destination file. */ STRNCPY( xMyFile.pcFileName, ( szDestinationFile + uxIndex + 1 ), ffconfigMAX_FILENAME - 1 ); xMyFile.pcFileName[ ffconfigMAX_FILENAME - 1 ] = 0; @@ -944,6 +937,13 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager, { xError = FF_createERR( FF_ERR_FILE_INVALID_PATH, FF_MOVE ); } + + if( uxIndex == 0U ) + { + /* Give the directory part a minimum + * length of 1, to get '/' at least. */ + uxIndex = 1U; + } } if( FF_isERR( xError ) == pdFALSE ) From 174fd4600a38a4ab92276c020b18b541d35a56c9 Mon Sep 17 00:00:00 2001 From: Tobias Gruen Date: Wed, 14 Feb 2024 01:45:39 +0100 Subject: [PATCH 14/15] Fix missing initialization of xError in FF_Move The error variable could get accessed before initialization. --- ff_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ff_file.c b/ff_file.c index 5d09746..87bb31e 100644 --- a/ff_file.c +++ b/ff_file.c @@ -887,7 +887,7 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager, #endif /* *INDENT-ON* */ { - FF_Error_t xError; + FF_Error_t xError = FF_ERR_NONE; FF_FILE * pSrcFile, * pxDestFile; FF_DirEnt_t xMyFile; uint8_t ucEntryBuffer[ 32 ]; From bebaaade7861678277d8296bdad18f0462c01e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Gr=C3=BCn?= Date: Thu, 15 Feb 2024 00:01:19 +0100 Subject: [PATCH 15/15] Run code-formatter Using uncrustify v 0.67 And the uncrustify config from the FreeRTOS main project. --- ff_dir.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ff_dir.c b/ff_dir.c index 62129ff..8ea3c5c 100644 --- a/ff_dir.c +++ b/ff_dir.c @@ -2900,7 +2900,7 @@ FF_Error_t FF_ExtendDirectory( FF_IOManager_t * pxIOManager, for( ; *pcName; pcName++ ) { - for( index = 0; index < ( BaseType_t ) ( sizeof( forbiddenChars ) / sizeof ( forbiddenChars[ 0 ] ) ); index++ ) + for( index = 0; index < ( BaseType_t ) ( sizeof( forbiddenChars ) / sizeof( forbiddenChars[ 0 ] ) ); index++ ) { if( *pcName == forbiddenChars[ index ] ) { @@ -3270,9 +3270,9 @@ FF_Error_t FF_CreateDirent( FF_IOManager_t * pxIOManager, break; } - if (FF_IsNameCompliant(pcDirName) == pdFALSE) + if( FF_IsNameCompliant( pcDirName ) == pdFALSE ) { - xError = FF_createERR(FF_ERR_DIR_INVALID_PATH, FF_MKDIR); + xError = FF_createERR( FF_ERR_DIR_INVALID_PATH, FF_MKDIR ); break; }