[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Inode



sudheer@xxxxxxxxxxxxx forced the electrons to say:
> 
> Hi,
> 
>  I have an seemly off-topic question, but any Gurus please enlighten me.
> 
>  I know only the inode number of a file. Is there anyway i can fetch
>  the contents of the file( preferably through a C program) . Assume
>  the access permissions are OK with the file.
> 
>  I'd be grateful for any pointers .

Sudhir Parasuram forced the electrons to say:
> 
> The easiest way wud b to :- 
> 
> Use find with "-inum" option as follows:
> 
> find <dir> -inum -exec cat {} \;
> 
> It shudn't be extremely difficult to simulate find (though it might take
> some time). 
> 

In a shell script, find is fine. But in a C program, implementing find will be
faster than using find in the program. I suggest you go to the beginning of
the directory tree from where you want to search from (the syscall chdir()),
use opendir() and readdir() recursively, and use stat() on all the entries in
the directory. Compare stat_buf.st_ino with the inode number you have. Then
fopen() the file and do whatever you want with it. Don't forget to closedir()
the directory.

This is going to be a nice exercise in recursive programming using C.

The required headers are sys/types.h, sys/stat.h, dirent.h and stdio.h and
possibly other headers.

Please followup to linux-india-programmers, as I feel this is more on topic
there. I have crossposted this reply there.

Regards,

Binand
- --------------------------------------------------------------------
For more information on Linux in India visit http://www.linux-india.org/
Linux India is NOT a forum for Microsoft/India/Pakistan/US/UK bashing.
Flame baits will not be tolerated.  If you can appreciate satire read
http://www.templetons.com/brad/emily.html

------------------------------