LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-24-2007, 12:20 PM   #1
kalamaraki
LQ Newbie
 
Registered: Oct 2006
Location: Athens - Greece
Distribution: Ubuntu 6.06 LTS - the Dapper Drake
Posts: 13

Rep: Reputation: 0
Getting file name from inode number


Hello again! I have a question. I am writing a program in C. If I have a filename, I can find its inode number by using struct stat. Is there a function that works the other way around? I mean, if I have the inode number, can I find the filename? Or, at least, ONE of the filenames? I know that there can be multiple filenames that have the same inode number, but they actually refer to the same file in the file system.

What I want to do is create hard links to inode numbers. For example, if there is a file file1.txt with inode number 123456, I want to create another file called file2.txt with the same inode number. I have a variable that holds the initial inode number. Searching the man pages, I found 'link' command:

link(const char *oldpath, const char *newpath)

I have the newpath, but I want to find the oldpath. I have the inode number. Any ideas?

Thanks in advance.
 
Old 02-24-2007, 01:10 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
if I have the inode number, can I find the filename?
You can do it from a console with the ls -i command. Add the -R (recursive) to list subdirectories. ls -i lists inode numbers and filenames. ls -i | grep '<inode number>' will find the inode number, and show you the file name. So, I'd say it can be done.

Suggestion: look at the source code for ls to see how it's done.
 
Old 02-24-2007, 01:31 PM   #3
kalamaraki
LQ Newbie
 
Registered: Oct 2006
Location: Athens - Greece
Distribution: Ubuntu 6.06 LTS - the Dapper Drake
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks for the reply, but I want to do this in the source code. I mean, I don't want to do it in the terminal window, because it is something I want to use further in my code.
 
Old 02-24-2007, 02:57 PM   #4
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
here ya go

http://www.die.net/doc/linux/man/man2/stat.2.html

enjoy
 
Old 02-24-2007, 03:00 PM   #5
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 452

Rep: Reputation: 30
oops,sorry ignore last post.I didn't read your question properly..
doh.
 
Old 02-24-2007, 09:17 PM   #6
KenJennings
LQ Newbie
 
Registered: Mar 2005
Location: FL, USA
Distribution: SuSE
Posts: 28

Rep: Reputation: 15
As far as I know the only direct association between filename and inode occurs inside the directory structure. So, if you need to actually implement this in C internally, then you would have to re-invent the wheel by duplicating what the find command does to recursively traverse directories and examine all the directory entries.

Alternatively, if you can use system() to call the find command try system("find -inum your_inum_here"); and capture the output to a temporary file, then read the contents of the temporary file.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can two files have the same inode number? sambyte Linux - Newbie 5 02-04-2009 06:27 AM
accessing file using inode number namusin Linux - Kernel 1 01-30-2007 06:31 PM
access file using inode number? allasso Linux - General 1 09-26-2006 09:14 PM
iNode number, how does it change? saudoi Solaris / OpenSolaris 4 05-22-2005 04:38 AM
What can change an inode number? nc101 Linux - General 2 01-03-2005 10:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration