Thread: Odd behavor with rename() function on Raspberry Pi writting to SD Card

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    5

    Odd behavor with rename() function on Raspberry Pi writting to SD Card

    Temporarily, I am writing files to SD card. Perhaps this is the source of the issue?

    My client software receives real-time streamed data via tcpip and writes this data to files based on data size. The file naming convection is basically a '0' padded integer, e.g. 000001.tmp, 000002.tmp, etc.
    Once a file is completed (writing done), the file is closed and then renamed using the rename function. I am closely checking both the returned values from file closing (fclose) and file renaming and both are returning the correct values (0== success).

    Code:
    // close file function
    BOOL closeDataFile()
    {
     int ret;
     if( pFile != NULL ) 
     {
      ret=fclose(pFile);
      if(ret==0) 
      {
       pFile = NULL;
       return TRUE;
      }
      else return FALSE;
     }
     return TRUE;
    }
    
    // rename file function
    BOOL renameTempFile()
    {
    ...
     ret = rename(old_fnp, new_fnp);
     if( ret != 0 ) 
     {
      printf("ERROR: Failed to Rename Data File %s",old_fnp);
      perror( old_fnp );
      return FALSE;
     }
    
     return TRUE;
    }
    The issue I am having is that occasionally the old file (in the renaming function) remains in the directory list after the renaming function has successfully completed. The matching newly renamed file is there also. The old files remaining in the folder are not valid files and cannot be manually deleted. On rebooting the RPi these old files are cleared in the reboot process. One further note is that the free space on the SD card is not an issue as I only write a few MBs before stopping the program while I have multi GBs free.

    I have not found others reporting this issue and would like to know if someone has any ideals.

    Thanks
    Jim
    Last edited by JimT; 02-18-2020 at 04:29 PM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    manpages and gdb are your friends... use them...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    sync(2): commit buffer cache to disk - Linux man page - code
    sync(1): flush file system buffers - Linux man page - command line

    That the bogus files disappear on reboot could just be down to an inconsistency in file metadata.
    The old name is truly gone, because you can't do anything with it after the rename.

    Try a sync when you see one, and see if that resolves the issue.

    Also, do these old names persist 'indefinitely' up to the next reboot, or do they disappear anyway because you're carrying on writing further files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2020
    Posts
    5
    Salem

    The old files persist even when I close the RPi's file manager and open a new one. When I examine the properties of the old file it shows that the size of the file is 0 bytes, however the size shown in the file manager is NOT 0. When I attempt to move one of these files to the trash an error message is shown stating that the file does not exist. I also tried the sync command but the old files remained.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078

  6. #6
    Registered User
    Join Date
    Feb 2020
    Posts
    5
    The code below, called just before issuing the rename() command, seems to have fixed the issue:

    file_no = fileno(pFile);
    fsync(file_no);

    Thanks!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writting to file in callback function
    By Median in forum C++ Programming
    Replies: 10
    Last Post: 04-02-2013, 05:18 PM
  2. Problem with rename function
    By strokebow in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2012, 01:10 PM
  3. writting swap function
    By Pulock2009 in forum C Programming
    Replies: 7
    Last Post: 04-07-2010, 10:36 AM
  4. Replies: 6
    Last Post: 01-26-2009, 08:01 PM
  5. renaming file without rename() function?
    By subodh_dg in forum C Programming
    Replies: 3
    Last Post: 03-26-2005, 09:40 AM

Tags for this Thread