Reputation: 426
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
Upvotes: 2
Views: 370
Reputation: 5279
Removing a file means removing a directory entry. Removing (as well as creating) a directory entry affects the containing directory's blocks on the disk, while blocks used by file being removed are not affected. In fact, file blocks might not be even freed when there are more than one references to the file (hard links). So it is containing directory's write permissions that are checked, not the file's.
Upvotes: 1
Reputation: 182829
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.
Upvotes: 2
Reputation: 1059
remove() works on directories, so it won't work only if you don't have write permission for the folder, not the file.
Upvotes: 2