You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/10/13 14:21:43 UTC

[GitHub] [incubator-nuttx] fjpanag opened a new issue, #7306: Failure of rename() may cause delete files.

fjpanag opened a new issue, #7306:
URL: https://github.com/apache/incubator-nuttx/issues/7306

   There is a case were `rename()` may fail, but also delete files in the process that it shouldn't.
   
   This can happen when `oldpath` refers to a non-existent file, while `newpath` refers to a file that is actually there.  
   The code will first delete `newpath`, and then try to rename the first file. If this fails, `newpath` is lost.
   
   The following snippet demonstrates this:
   
   ```c
   #define TEST_FILE1 "/mnt/sdcard0/file1"
   #define TEST_FILE2 "/mnt/sdcard0/file2"
   
   int fd1 = open(TEST_FILE1, O_WRONLY | O_CREAT, 0666);
   write(fd1, "file1", sizeof("file1") - 1);
   close(fd1);
   
   int fd2 = open(TEST_FILE2, O_WRONLY | O_CREAT, 0666);
   write(fd2, "file2", sizeof("file2") - 1);
   close(fd2);
   
   /* Rename file 1 to file 2. File 2 should be deleted
   * and file 1 should take its place.
   */
   
   rename(TEST_FILE1, TEST_FILE2);
   
   /* Now only test file 2 should exist. */
   
   /* Since there is no file 1, renaming bellow should fail. */
   
   assert(rename(TEST_FILE1, TEST_FILE2) < 0);
   
   /* ERROR! File 2 has been deleted! */
   
   struct stat f_stat;
   assert(stat(TEST_FILE2, &f_stat) == 0);
   
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-nuttx] xiaoxiang781216 closed issue #7306: Failure of rename() may cause delete files.

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 closed issue #7306: Failure of rename() may cause delete files.
URL: https://github.com/apache/incubator-nuttx/issues/7306


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org