mirror of
https://github.com/FreeRTOS/Lab-Project-FreeRTOS-FAT.git
synced 2025-10-17 15:41:34 +08:00
Protect FF_Open in create mode with a new semaphore v2 (#38)
Co-authored-by: Hein Tibosch <hein@htibosch.net>
This commit is contained in:
18
ff_file.c
18
ff_file.c
@@ -243,6 +243,15 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager,
|
||||
char pcFileName[ ffconfigMAX_FILENAME ];
|
||||
#endif
|
||||
|
||||
#if ( ffconfigPROTECT_FF_FOPEN_WITH_SEMAPHORE == 1 )
|
||||
{
|
||||
if( ( ucMode & FF_MODE_CREATE ) != 0U )
|
||||
{
|
||||
FF_PendSemaphore( pxIOManager->pvSemaphoreOpen );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
memset( &xFindParams, '\0', sizeof( xFindParams ) );
|
||||
|
||||
/* Inform the functions that the entry will be created if not found. */
|
||||
@@ -464,6 +473,15 @@ static FF_FILE * prvAllocFileHandle( FF_IOManager_t * pxIOManager,
|
||||
pxFile = NULL;
|
||||
}
|
||||
|
||||
#if ( ffconfigPROTECT_FF_FOPEN_WITH_SEMAPHORE == 1 )
|
||||
{
|
||||
if( ( ucMode & FF_MODE_CREATE ) != 0U )
|
||||
{
|
||||
FF_ReleaseSemaphore( pxIOManager->pvSemaphoreOpen );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if( pxError != NULL )
|
||||
{
|
||||
*pxError = xError;
|
||||
|
39
ff_ioman.c
39
ff_ioman.c
@@ -189,14 +189,26 @@ FF_IOManager_t * FF_CreateIOManger( FF_CreationParameters_t * pxParameters,
|
||||
/* Finally store the semaphore for Buffer Description modifications. */
|
||||
pxIOManager->pvSemaphore = pxParameters->pvSemaphore;
|
||||
|
||||
if( pxParameters->xBlockDeviceIsReentrant != pdFALSE )
|
||||
{
|
||||
pxIOManager->ucFlags |= FF_IOMAN_BLOCK_DEVICE_IS_REENTRANT;
|
||||
}
|
||||
#if ( ffconfigPROTECT_FF_FOPEN_WITH_SEMAPHORE == 1 )
|
||||
pxIOManager->pvSemaphoreOpen = xSemaphoreCreateRecursiveMutex();
|
||||
|
||||
pxIOManager->xBlkDevice.fnpReadBlocks = pxParameters->fnReadBlocks;
|
||||
pxIOManager->xBlkDevice.fnpWriteBlocks = pxParameters->fnWriteBlocks;
|
||||
pxIOManager->xBlkDevice.pxDisk = pxParameters->pxDisk;
|
||||
if( pxIOManager->pvSemaphoreOpen == NULL )
|
||||
{
|
||||
/* Tell the user that there was not enough mmory. */
|
||||
xError = FF_ERR_NOT_ENOUGH_MEMORY | FF_CREATEIOMAN;
|
||||
}
|
||||
else
|
||||
#endif /* ffconfigPROTECT_FF_FOPEN_WITH_SEMAPHORE */
|
||||
{
|
||||
if( pxParameters->xBlockDeviceIsReentrant != pdFALSE )
|
||||
{
|
||||
pxIOManager->ucFlags |= FF_IOMAN_BLOCK_DEVICE_IS_REENTRANT;
|
||||
}
|
||||
|
||||
pxIOManager->xBlkDevice.fnpReadBlocks = pxParameters->fnReadBlocks;
|
||||
pxIOManager->xBlkDevice.fnpWriteBlocks = pxParameters->fnWriteBlocks;
|
||||
pxIOManager->xBlkDevice.pxDisk = pxParameters->pxDisk;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -256,6 +268,15 @@ FF_Error_t FF_DeleteIOManager( FF_IOManager_t * pxIOManager )
|
||||
ffconfigFREE( pxIOManager->pucCacheMem );
|
||||
}
|
||||
|
||||
#if ( ffconfigPROTECT_FF_FOPEN_WITH_SEMAPHORE == 1 )
|
||||
{
|
||||
if( pxIOManager->pvSemaphoreOpen != NULL )
|
||||
{
|
||||
vSemaphoreDelete( pxIOManager->pvSemaphoreOpen );
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Delete the event group object within the IO manager before deleting
|
||||
* the manager. */
|
||||
FF_DeleteEvents( pxIOManager );
|
||||
@@ -641,7 +662,7 @@ int32_t FF_BlockWrite( FF_IOManager_t * pxIOManager,
|
||||
if( ( slRetVal == 0ul ) && ( pxIOManager->xBlkDevice.fnpWriteBlocks != NULL ) )
|
||||
{
|
||||
do
|
||||
{ /* Make sure we don't execute a NULL. */
|
||||
{ /* Make sure we don't execute a NULL. */
|
||||
if( ( xSemLocked == pdFALSE ) &&
|
||||
( ( pxIOManager->ucFlags & FF_IOMAN_BLOCK_DEVICE_IS_REENTRANT ) == pdFALSE ) )
|
||||
{
|
||||
@@ -1497,7 +1518,7 @@ FF_Error_t FF_Mount( FF_Disk_t * pxDisk,
|
||||
}
|
||||
|
||||
if( pxPartition->ulSectorsPerFAT == 0 )
|
||||
{ /* FAT32 */
|
||||
{ /* FAT32 */
|
||||
pxPartition->ulSectorsPerFAT = FF_getLong( pxBuffer->pucBuffer, FF_FAT_32_SECTORS_PER_FAT );
|
||||
pxPartition->ulRootDirCluster = FF_getLong( pxBuffer->pucBuffer, FF_FAT_ROOT_DIR_CLUSTER );
|
||||
memcpy( pxPartition->pcVolumeLabel, pxBuffer->pucBuffer + FF_FAT_32_VOL_LABEL, sizeof( pxPartition->pcVolumeLabel ) - 1 );
|
||||
|
@@ -289,6 +289,9 @@
|
||||
FF_Partition_t xPartition; /* A partition description. */
|
||||
FF_Buffer_t * pxBuffers; /* Pointer to an array of buffer descriptors. */
|
||||
void * pvSemaphore; /* Pointer to a Semaphore object. (For buffer description modifications only!). */
|
||||
#if ( ffconfigPROTECT_FF_FOPEN_WITH_SEMAPHORE == 1 )
|
||||
void * pvSemaphoreOpen; /* A semaphore to protect FF_Open() against race conditions. */
|
||||
#endif
|
||||
void * FirstFile; /* Pointer to the first File object. */
|
||||
void * xEventGroup; /* An event group, used for locking FAT, DIR and Buffers. Replaces ucLocks. */
|
||||
uint8_t * pucCacheMem; /* Pointer to a block of memory for the cache. */
|
||||
|
Reference in New Issue
Block a user