andy5995 andy5995 - 2 months ago 7
C Question

c program: Why does remove() delete a file with no write permissions

I've been using GNU/Linux for over 10 years now, so I consider myself pretty familiar with file permissions.

To test the following code, I used 'chmod -w' on 'undo_path'. I then verified that the file had no write permissions by using ls -al. It showed:


-r--r--r-- 1 andy andy 52 Sep 26 18:17 lastrmw


I then tried the code. Twice. Both times the remove() statement was successful.

if (remove (undo_path))
{
fprintf (stderr, "Warning: failed to remove %s\n", undo_path);
perror (__func__);
}


How could a file with no write permissions be deleted?

I'm using Debian 8/Jessie, and my kernel: Linux oceanus 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u1 (2016-09-03) x86_64 GNU/Linux

Answer

The remove function is an operation on a directory that removes an entry from it. It does not delete a file unless it happens to produce the conditions under which a file is automatically deleted by the file system. If, for example, the same file has two corresponding directory entries or is opened by a process, it will not be deleted.

Comments