LinuxQuestions.org
Help answer threads with 0 replies.
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, 01: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, 02: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, 02: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, 03:57 PM   #4
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slack 10.2 kde 3.4.2 kernel 2.6.15
Posts: 454

Rep: Reputation: 30
here ya go

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

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

Rep: Reputation: 30
oops,sorry ignore last post.I didn't read your question properly..
doh.
 
Old 02-24-2007, 10: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.
 
Old 11-16-2018, 10:39 AM   #7
Too0ld2c0de
LQ Newbie
 
Registered: Nov 2018
Posts: 1

Rep: Reputation: Disabled
C code to find filenames matching inode number

You can use nftw(3) or ftw(3) to recursively walk part of the file tree. These library functions will call a routine you provide which will get the stat(2) structure for each file encountered along with its filename. You can check the stat's st_ino field to see if it matches your target inode number and if so, grab the filename that was passed to it. Let me know if you'd like to see a code fragment that shows this.
 
1 members found this post helpful.
Old 11-16-2018, 11:35 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,654

Rep: Reputation: 1125Reputation: 1125Reputation: 1125Reputation: 1125Reputation: 1125Reputation: 1125Reputation: 1125Reputation: 1125Reputation: 1125
That would flink(2) -- sadly, there is no such thing.
 
Old 11-16-2018, 11:50 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 current / Linux Mint
Posts: 7,672

Rep: Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546Reputation: 1546
going back to what @bigrigdriver said, look at the source code to see how it is done, ls is written in C as well.

https://github.com/wertarbyte/coreut...aster/src/ls.c

search for inode, and whatever else you need.
https://ftp.gnu.org/pub/gnu/findutils/
for find to see how it does it.

Last edited by BW-userx; 11-16-2018 at 11:58 AM.
 
Old 11-16-2018, 03:19 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,885
Blog Entries: 7

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
@ Too0ld2c0de: You have replied to a thread which has been inactive for more than 11 years.

If you have a question or wish to share your knowledge it is always preferable to open your own thread. This will allow it to attract the attention of currently active members and allow others to see your questions and information in the context specific to your issues, rather than having to parse possibly outdated information in the older thread.

Welcome to LQ, and good luck!
 
Old 11-20-2018, 04:57 PM   #11
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 505

Rep: Reputation: 224Reputation: 224Reputation: 224
Since this thread has been dug up, I guess there’s no harm in adding one more post explaining reasons for a lack of such a system call.

One cannot create a link to an inode (or open an inode) because that would circumvent filesystem permissions. To be able to open a file, user must have permissions to that file (which are stored in its inode so that’s fine) and all directories on path its path. As long as user had a write permission on some directory, user would be able to read all files its permissions allow them to do even if normally that would be blocked by directory permissions.
 
  


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 07:27 AM
accessing file using inode number namusin Linux - Kernel 1 01-30-2007 07:31 PM
access file using inode number? allasso Linux - General 1 09-26-2006 10:14 PM
iNode number, how does it change? saudoi Solaris / OpenSolaris 4 05-22-2005 05:38 AM
What can change an inode number? nc101 Linux - General 2 01-03-2005 11:29 AM

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

All times are GMT -5. The time now is 04:33 AM.

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