Search code examples
clinuxunixinode

C programming : How to get directory-name if I know the inode-number


How can I get directory name if I know it's inode number? Need the code. Thanks.

the below code passed dir_name by .. , then I got it's i-node number, but what I also want is the directory name.

/*child stat*/
stat(dir_name, &sb);
if (stat(dir_name, &sb) == -1) {
    perror("stat");
    exit(EXIT_FAILURE);
}
childIno = (long) sb.st_ino;

Solution

  • Unless you have an index mapping inodes to names, you will have to recursively walk the directory structure until you find a name with the inode you're looking for (which you may not find).

    If one part of your program already knows the name of the directory, you should find a way to pass it into your code.