LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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: 356Reputation: 356Reputation: 356Reputation: 356
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: slackware 14.2 kernel 4.19.43
Posts: 462

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: slackware 14.2 kernel 4.19.43
Posts: 462

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.
 
Old 11-16-2018, 09:39 AM   #7
Too0ld2c0de
LQ Newbie
 
Registered: Nov 2018
Posts: 2

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, 10:35 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That would flink(2) -- sadly, there is no such thing.
 
Old 11-16-2018, 10:50 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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 10:58 AM.
 
Old 11-16-2018, 02:19 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
@ 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, 03:57 PM   #11
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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



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 04:22 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
Open Source Consulting | Domain Registration