3

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

NinjaK
  • 33
  • 1
  • 4
  • 2
    What have you tried? What didn't work? Why do you need to do this in the first place? – Carl Norum Feb 11 '13 at 23:07
  • 1
    See http://stackoverflow.com/questions/4606774/why-cant-files-be-manipulated-by-inode – nos Feb 11 '13 at 23:08
  • I tried to use the system("find - inum inodeNumer"); but it doesn't seem to work. It just gives me errors. I need this for a class assignment. – NinjaK Feb 12 '13 at 00:42

2 Answers2

2

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.

Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • What if I just wanted to search through the /dev files and not the whole file system. Would that be easier to do? – NinjaK Feb 12 '13 at 00:45
  • How do you traverse the file system? – NinjaK Feb 12 '13 at 00:54
  • You traverse the file system with [`nftw()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/nftw.html) or [`ftw()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/ftw.html). Restricting the search to `/dev` is simply a question of where you start from. There are fewer files in `/dev` than in the system as a whole, so it will be quicker, but no easier or more difficult (but there are few plain files in `/dev` whereas there are few character or block special devices outside `/dev`). – Jonathan Leffler Feb 12 '13 at 01:14
  • So, my code runs but it just keeps giving me the same file over and over again. What am I doing wrong? – NinjaK Feb 12 '13 at 02:40
  • `system()` runs the command without any connection to your process beyond it waits for `system()` to complete. You maybe need to think of using `popen()`? – Jonathan Leffler Feb 12 '13 at 02:54
  • I just tried popen, this is what it gives me `Please enter the inode you wish to view: 123456 sh: find/dev/: No such file or directory --------------------------- FILE SIZE: 0 BYTES NUMBER OF LINKS: 191 FILE INODE: 15774463 File Permissions: -rwxr-xr-- ` – NinjaK Feb 12 '13 at 03:08
  • 1
    `find/dev/` lacks a necessary space character, should be `find /dev ...` – Anton Kovalenko Feb 12 '13 at 04:52
0

Anton is right in general if you need to support all OS, but if you are using ext4 there is a faster way:

function fd_to_inode() { cat /proc/self/fdinfo/"$1" | grep ino: | awk '{print $2}'; }
function path_to_device() { df --output=source "$1" | tail -n 1; }
sudo debugfs -R "ncheck $(fd_to_inode 4)" "$(path_to_device .)"
debugfs 1.46.5 (30-Dec-2021)
Inode   Pathname
67771103    /home/fred/Downloads/bash-5.2.9/example

Internally, it uses these functions:

errcode_t ext2fs_open_inode_scan (ext2_filsys fs, int buffer_blocks, ext2_inode_scan *scan)

Initialize the iteration variable scan. This variable is used by ext2fs_get_next_inode. The buffer_blocks parameter controls how many blocks of the inode table are read in at a time. A large number of blocks requires more memory, but reduces the overhead in seeking and reading from the disk. If buffer_blocks is zero, a suitable default value will be used.

void ext2fs_close_inode_scan (ext2_inode_scan scan)

Release the memory associated with scan and invalidate it.

errcode_t ext2fs_get_next_inode (ext2_inode_scan scan, ext2_ino_t *ino, struct ext2_inode *inode)

This function returns the next inode from the filesystem; the inode number of the inode is stored in ino, and the inode is stored in inode.

If the inode is located in a block that has been marked as bad, ext2fs_get_next_inode will return the error EXT2_ET_BAD_BLOCK_IN_INODE_TABLE.

Fredrick Brennan
  • 7,079
  • 2
  • 30
  • 61