+FAT: ff_findfirst, ff_findnext, and ff_remove

pascal-vehigy wrote on Wednesday, 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 ff_chdir
  2. allocate some memory and get the pointer for the FF_FindData_t struct
  3. RetVal = ff_findfirst( “”, px_FindStruct )
  4. if RetVal != 0 -> abort
  5. if it is a dot (. or …) entry or another directory skip it
  6. else open the file ff_open(px_FindStruct->pcFileName, “r”), send it via TCP, finally ff_close(…)
  7. if the TCP server on the other end responded positively, delete the file: ff_remove( px_FindStruct->pcFileName )
  8. RetVal = ff_findnext( px_FindStruct )
  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.


rtel wrote on Wednesday, January 27, 2016:

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

heinbali01 wrote on Wednesday, 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:

    BaseType_t xFindResult = ff_findfirst( 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.


pascal-vehigy wrote on Wednesday, 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),