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

*爱你&永不变心* 提交于 2019-12-19 09:24:16

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14822611/is-there-any-way-that-i-can-search-for-a-file-or-a-filename-using-a-given-inode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!