Good day to all.
I have this question:

How to programmatically get the number of links to a file (by file descriptor) used in Linux? Are there any API functions that solve this problem? If not, then maybe someone has already implemented something like this on their own?

    1 answer 1

    The number of hard links can be obtained via the stat syscall (via the file descriptor - fstat ). st_nlink struct stat has a st_nlink field. And the number of soft links, I'm afraid, did. The only thing I can imagine is to bypass the entire file system and analyze the soft links encountered.

    • @eigenein, this is what you? - skegg pm
    • @mikillskegg, here was a comment from the TS asking what is a hard link. - eigenein
    • It is clear)))) - skegg
    • Not sure (based on the reference to the processes in question) that the TS meant only file system links. That’s right, but when you open a file, 1 is added to the current reference count (I don’t remember which kernel structure (not inode)), and when it is closed it is subtracted. This allows you to open the file, delete it (from the table of contents, the number of hard links will decrease) and continue working with the contents. When the total number of links is reset, the system will free up disk space. Perhaps it was these links that were meant, but I don’t know how to look at them either. - avp