Search code examples
c++cunixposixunlink

When do you have permission to use unlink()?


I'm trying to predict which files I can delete using unlink().

My understanding so far is that the file must either be writeable to others, or have the same group id or owner id as the executing program.

Are there any other cases? I'm trying to explain how these files are deletable by my program (the program is not running as root nor in the admin group).

drwxrwxr-x  4 root  admin    136 Apr 17 23:53 .
drwxrwxr-x  7 root  admin    238 Jan 27 11:49 ..
-rwxrwxr-x  1 root  admin    560 Jan 27 11:49 info.nib
-rwxrwxr-x  1 root  admin  18399 Jan 27 11:49 keyedobjects.nib

Thanks!


Solution

  • As others have mentioned, the directory must be writeable by the effective user. However, the file can have extended permissions (ACLs) applied to it that prevent it from being deleted. From the example you show, I think you're using Mac OS, right? There you can use ls -le to display the ACL. The chmod(1) manpage tells you about the various permissions. If you want to query the ACL in your program, you should probably start by reading the acl(3) manpage.