Monday, April 06, 2015

Finding Hardlinks

Well, this may be something very primitive but may  be useful to someone.

How do you now how many  hardlinks you have to a file ? There is a system call: fstat for open files and stat for close files. the structure returned by this system call is as follows:

           struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

And it is very interesting that one of the parameters of the fstat is the number of hard links. Just refer to "man 2 fstat" for more details.

The utility in shell to read these values is simply the stat command:

user@localhost ~]$ stat ./scr.sh
  File: ‘./scr.sh’
  Size: 72            Blocks: 8          IO Block: 4096   regular file
Device: fd02h/64770d    Inode: 137155      Links: 2
Access: (0755/-rwxr-xr-x)  Uid: ( 1000/    user)   Gid: ( 1000/    user)
Context: unconfined_u:object_r:user_home_t:s0
Access: 2015-04-06 19:32:44.410014623 +1000
Modify: 2015-04-06 19:32:42.703024143 +1000
Change: 2015-04-06 20:12:21.379767352 +1000
 Birth: -
[user@localhost ~]$

and ls -ial  shows you the inode number. Two files with the same inode number means they are the same file and referring to the same inode on the filesystem.

To find all the hard links on the mount point, you should simply find the files with the same inode number. Find has made it simeple. Since hardlinks are only possible on the same mount point,  to make life easier, we can use -xdev option with the find comand:

find ./ -xdev -samefile ./scr.sh


[user@localhost ~]$ find ./ -xdev -samefile ./scr.sh
./scr-1.sh
./scr.sh

or use the inode number:
[user@localhost ~]$ find ./ -xdev -inum 137155
./scr-1.sh
./scr.sh



That's all falks :)