C++ for QNX: delete corrupted file using remove(fileName) vs system("rm -rf fileName")?

60 Views Asked by At

What is the difference in removing a file using the c function remove(fileName) or a system call with the "rm -rf fileName") command? If the remove(fileName) fails to remove a file (when the file is corrupted) - is the system("rm -rf fileName") kind of "stronger" and will remove the corrupted file, that remove() function failed to remove?

1

There are 1 best solutions below

0
Poder Psittacus On

What is the difference?

The difference is that remove needs to meet some conditions to remove a file. The QNX docu states: The remove() function removes a link to a file:

  • If the filename names a symbolic link, remove() removes the link, but doesn't affect the file or directory that the link goes to.
  • If the filename isn't a symbolic link, remove() removes the link and decrements the link count of the file that the link refers to.
  • If the link count of the file becomes zero, and no process has the file open, then the space that the file occupies is freed, and no one can access the file anymore. If one or more processes have the file open when the last link is removed, the link is removed, but the removal of the file is delayed until all references to it have been closed.

Especially point 3 is interesting in your case.

Here is the Docu for remove() in QNX Docu remoce()

rm "fileName", specifically rm -f "fileName" attempts to remove the files without prompting for confirmation, regardless of the file’s permissions.

By the way the -r flag is for directories. If you just want to delete files you don't need that.

Here is the docu for rm in QNX Docu rm

Is rm -f "stronger" than remove

Depending on how the file was corrupted rm -f can be "stronger" then remove, but both are not 100 % sure to remove a corrupted file. To handle corrupted file you have to look at the error messages thrown and maybe use an memory analysis tool.