Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing symbolic links for a original file in C

Tags:

c

linux

unix

I am writing a C program, that uses *NIX System calls. Now, when the user calls for deleting a particular file, I want to remove all the symbolic links created to the file, also to be removed. How can this be achieved?

like image 945
Amrith Krishna Avatar asked Dec 09 '25 22:12

Amrith Krishna


2 Answers

You can't, unless you search the whole directory tree, or you have some other means of knowing where these symbolic links are. A file doesn't "know" which symlinks point to it. You have to locate each symlink on your own and unlink() it.

like image 121
Guntram Blohm Avatar answered Dec 11 '25 13:12

Guntram Blohm


There is no way to get a list of symbolic links that point to a particular file save 'the hard way', IE go through the entire file system recursively (or whatever subset of the file system you know the symbolic links to be in) and determine whether any resolve to that file. You would probably be best outsourcing that to the find utility.

like image 42
abligh Avatar answered Dec 11 '25 13:12

abligh