Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


+FAT: ff_findfirst, ff_findnext, and ff_remove

Posted by pascal-vehigy on January 27, 2016

To whom it may concern,

I am cycling through all files in a given directory, processing them, and afterwards deleting them. Is it possible, that the ff_findnext function does not work if a file in the current working directory is being deleted in between iterations?

My sequence of events is something like that: 1. change working directory with ffchdir 2. allocate some memory and get the pointer for the FFFindDatat struct 3. RetVal = fffindfirst( "", pxFindStruct ) 4. if RetVal != 0 -> abort 5. if it is a dot (. or ..) entry or another directory skip it 6. else open the file ffopen(pxFindStruct->pcFileName, "r"), send it via TCP, finally ffclose(...) 7. if the TCP server on the other end responded positively, delete the file: ffremove( pxFindStruct->pcFileName ) 8. RetVal = fffindnext( pxFindStruct ) 9. loop back to 4 while RetVal == 0

Am I missing something? Does the FindStruct get messed up because I am deleting files while I iterate through them?

If there is only one file in the directory, it works perfectly fine. If a few files have been accumulated, the first one is being sent and removed, but then ff_findnext breaks the loop. However, I can access the very same FAT through FTP and fetch the remainng files. They are perfectly readable, the size and content is exactly as expected.

Thanks for any hints on what might be going on here.

Sincerely, Pascal

+FAT: ff_findfirst, ff_findnext, and ff_remove

Posted by rtel on January 27, 2016

Hi Pascal, thank you for your valuable description. We will check it out and report back here.

+FAT: ff_findfirst, ff_findnext, and ff_remove

Posted by heinbali01 on January 27, 2016

Hi Pascal,

I'm not sure how W32 or Linux will respond if you try to do the same: delete the current file while iterating through a directory.

Here is a solution, although it will use ffconfigMAX_FILENAME bytes on the stack:

~~~~~ BaseTypet xFindResult = fffindfirst( pcPath, &xFindData ); while( xFindResult >= 0 ) { char pcFileName[ ffconfigMAX_FILENAME ];

    snprintf( pcFileName, sizeof( pcFileName ),
        xFindData.xDirectoryEntry.pcFileName );
    /* Move to the next file before the
    current entry gets deleted. */
    xFindResult = ff_findnext( &xFindData );
    SendFile( pcFileName );
    ff_remove( pcFileName );


PS. If you need to access all properties of "the current file", you would have to make a copy of xFindData.xDirectoryEntry which has the type FF_DirEnt_t.


+FAT: ff_findfirst, ff_findnext, and ff_remove

Posted by pascal-vehigy on January 27, 2016

Hi Hein,

thank you for the quick reply. Yep, your way does make more sense, I guess I was too greedy with stack usage. But in my case the files, which obviously are a backlog as you undoubtetly have figured, have a fixed naming scheme, so it is no problem to buffer the "current" filename on the stack.

I haven't actually gotten to try it out yet, but I am sure this approach will work. Otherwise I will be posting back.

Have a nice evening (or whatever time of day it is for you), Pascal

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists