cfileunixinode

Is there any way that I can search for a file or a filename using a given inode number?


I am taking in an inode number from a user and I have to search the file system for that file. How do I search through inode numbers. I have to do this using C and unix.

Here is my code so far:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(){

    int inodeNumber;
    printf("Please enter the inode you wish to view:\n");
    scanf("%d",&inodeNumber);

    struct stat fileStat;
    int temp_file;
    temp_file = system("find/fs/root -inum inodeNumber");
    fstat(temp_file, &fileStat);
    //printf("Information for %s\n",argv[1]);
    printf("---------------------------\n");
    printf("File Size: \t\t%d bytes\n",(int)fileStat.st_size);
    printf("Number of Links: \t%d\n",(int)fileStat.st_nlink);
    printf("File inode: \t\t%d\n",(int)fileStat.st_ino);
}  

Thanks


Solution

  • Traverse your filesystem's directories recursively, doing stat (or probably lstat) on each file, comparing st_ino member of struct stat to the inode number you're searching.

    If you didn't have to do it in C, I would recommend find /fs/root -inum N instead.