Question

inode -> file name

Asked by: stefan73

Hi,

When i have an inode (like via stat()), is it possible to get the file name associated with that inode?

In case a generic Unix answer isn't possible, a Linux or Solaris solution will do.

Thanks!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-05-05 at 01:49:02ID20978422
Tags

inode

,

file

Topic

Unix Systems Programming

Participating Experts
7
Points
500
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. inode
    I'm getting a Warning: Attempt to open already open device 120 with a duplicate inode. Each time I get this message it displays a different device number. How can I correct this and is there a command that will list each individual inode and it's associated device?
  2. inode
    What is meant by 'inode'-- a rocord type in some OS

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: sunnycoderPosted on 2004-05-05 at 01:56:44ID: 10994203

Hi stefan73,

use fstat to get file info .. this has inode information too ... (including inode number)
extract inode from the file info
open root dir /
traverse tree recursively using readdir and comparing the inode number until you get a match
when you get the match, you get the full path too

readdir returns struct dirent which has inode number too ...

clearly the search is exhaustive and will take up a lot of time

Sunnycoder

 

by: stefan73Posted on 2004-05-05 at 02:32:44ID: 10994374

sunnycoder,
Doing a full scan of the FS won't do... No other choices?

Stefan

 

by: sunnycoderPosted on 2004-05-05 at 02:41:28ID: 10994409

Nope ... None as far as I know

The issue first came up in http:Q_20658408.html and then perhaps you picked it up from http:Q_20976945.html

btw, if you did a stat then you have fd ... If you have fd then you can get the information directly on linux (and only linux)

use getpid to get current process pid and then

ls -l /proc/pid/fd

this will give all fd's and their associated files ... which can be parsed to find the file associated with your fd

 

by: manish_regmiPosted on 2004-05-05 at 02:57:03ID: 10994469

But you need a filename to stat.

if you randomly choose an inode.
Sunny's idea is good but time consuming.

CAn we get the inode num of the parent directory from the file's inode. If it is then
One readdir will do.

regards manish

 

by: sunnycoderPosted on 2004-05-05 at 03:25:08ID: 10994601

Manish, that is almost always true ... but there are occasions when all you have is an fd and all you can do is an fstat (I did write stat in my previous post which was a mistake)

The two links I posted above had that exceptional situation.

 

by: timbauerPosted on 2004-05-05 at 03:52:13ID: 10994715

Folks, we have a fundamental problem. i-nodes are only unique within
their filesystem. If one file system is mounted on another, all bets are off.
This is why you can't hard link across file systems.
Therefore, two stat structures point to the same file only if the
device field (st_dev) and i-node (st_ino) match.

I once got burned trying to write a version of "getcwd".
I recursed from the current directory upwards adding onto
the path. I detected the root if the ".." i-node was equal to the
"." inode. I hit a mount point and the two i-nodes equaled
thus terminating the algorithm prematurely.

That said, isn't an inode an index into a file system table? I know us
"mortal" users are not supposed to know or meddle with such things,
but given enough info, it seems there has to be an efficient way for
looking up the filename.

stefan:
Where are you getting your inode from?
As sunny suggests if you have the file descriptor for the stat to get the inode.
You can fetch it out the the "/proc" sub-directories. I rather like that approach.
If you are running on something with the "/proc" sub-directories, you would
have to read kernel memory (OUCH).

Regards,
- Tim

 

 

by: timbauerPosted on 2004-05-05 at 04:12:53ID: 10994801

I said,
>That said, isn't an inode an index into a file system table? I know us
>"mortal" users are not supposed to know or meddle with such things,
>but given enough info, it seems there has to be an efficient way for
>looking up the filename.

But, I now think this is a false lead. The actual inode structure has no
reference to any names at all. It really only contains:
 - size information
 - physical addresses of the blocks the file uses.
 - modification times.

The only place that a name comes into play is within directory structures.
A directory file isn't much more than just a list of:
 file name --> i-number for that file.

Hence, I would expect sunny's brute force search to be the only practical way
to find this mapping within a file system.

 

by: sunnycoderPosted on 2004-05-05 at 04:13:57ID: 10994814

You are quite right Tim ... I never thought about mounted filesystem ... That was quite a trap ;o)

>but given enough info, it seems there has to be an efficient way for
>looking up the filename.
The question troubled me for sometime ... For the efficient way, we need inode or one of its descendends to point to filename ... All fields of inode are not standardized and I could not find even a non-standard implementation which does that (To be honest I did not look too hard either ;o) )

 

by: sunnycoderPosted on 2004-05-05 at 04:18:49ID: 10994841

LOL ... I am glad that I did not look too hard ;o)

I guess the reason for this is

inodes are used by kernel for internal references and functioning ... This has nothing to do with what file the inode is associated with and inodes were never meant for user's consumption ... Hence filename field was never put there

Also one would reasonable expect that you do not guess inode number for your file from random number generator .. rather you obtain it using one of the APIs provided ... To call that API, you would have known the file name at some point of time !! If you will need it later, save it ... If you need it and haven't saved it, it is probably poor design

just my thoughts

 

by: stefan73Posted on 2004-05-05 at 04:20:30ID: 10994852

Yes, I do have a proc FS here on Solaris, but all I find in the fd subdir is just something like

ls -l /proc/$$/fd
total 0
c---------   1 root     sys       13,  2 May  5 13:11 0
c---------   1 root     sys       13,  2 May  5 13:11 1
c---------   1 csmdev4  tty       24,134 May  5 13:11 15
c---------   1 csmdev4  tty       24,134 May  5 13:11 16
c---------   1 csmdev4  tty       24,134 May  5 13:11 17
c---------   1 csmdev4  tty       24,134 May  5 13:11 18
c---------   1 csmdev4  tty       24,134 May  5 13:11 19
c---------   1 root     sys       13,  2 May  5 13:11 2

...which is not of much help :-/

 

by: sunnycoderPosted on 2004-05-05 at 04:24:36ID: 10994870

>If you have fd then you can get the information directly on linux (and only linux)
try it on a linux box ...
here is a sample output from my linux box

lrwx------    1 user  user        64 May  5 16:49 0 -> /dev/pts/1
lrwx------    1 user  user        64 May  5 16:49 1 -> /dev/pts/1
lrwx------    1 user  user        64 May  5 16:49 2 -> /dev/pts/1
lrwx------    1 user  user        64 May  5 16:49 255 -> /dev/pts/1

 

by: stefan73Posted on 2004-05-05 at 04:31:49ID: 10994917

sunnycoder,
I see. I'll have a look at the lsof sources, that might give me an answer for Solaris, too.

But lsof is suid root :-/

Stefan

 

by: sunnycoderPosted on 2004-05-05 at 04:38:04ID: 10994959

c---------   1 root     sys       13,  2 May  5 13:11 0
c---------   1 root     sys       13,  2 May  5 13:11 1
c---------   1 csmdev4  tty       24,134 May  5 13:11 15
c---------   1 csmdev4  tty       24,134 May  5 13:11 16
c---------   1 csmdev4  tty       24,134 May  5 13:11 17
c---------   1 csmdev4  tty       24,134 May  5 13:11 18
c---------   1 csmdev4  tty       24,134 May  5 13:11 19
c---------   1 root     sys       13,  2 May  5 13:11 2

Also read the proc man page for solaris ... The last column could be fd

 

by: stefan73Posted on 2004-05-05 at 04:48:14ID: 10995031

sunnycoder,
> The last column could be fd
Yes, that's the fd. But a lot less helpful than on Linux :-/

Stefan

 

by: sunnycoderPosted on 2004-05-05 at 04:56:25ID: 10995089

But atleast there is way ... 10 minutes ago there was none ;o)

 

by: stefan73Posted on 2004-05-05 at 05:29:03ID: 10995348

sunnycoder,
Assuming I start with an fd, it's not too helpful...

Stefan

 

by: timbauerPosted on 2004-05-05 at 09:06:32ID: 10997400


>inodes are used by kernel for internal references and functioning
realized this same thing right after I posted my first comment, hence, the quick retreat. : )

On Linux, it appears that the fd subdirectory stores its "files"
(whose name is the file descritor integer) as symbolic links (ls dereferences these for us).

c---------   1 root     sys       13,  2 May  5 06:58 0
Solaris is a bit more cantankerous and stores them only as character devices.

lsof does read out of /dev/kmem and that is the need for the suid bit. : (
No doubt, that will lead to a solution. Good luck on this.
- Tim

 

by: chris_calabresePosted on 2004-05-05 at 09:11:45ID: 10997449

To reiterate/summerize:

o  If you have an open file-descriptor, then you should be able to use /proc if your OS flavor support the necessary functionality (i.e., probably not if you're not on Linux). You can not walk up the file tree with the moral equivalent of 'cd ..' because open file descriptors only point you at the data-blocks for the file in question, not at the name-space/directory information - see below.

o  Otherwise, if you have a kernel device nubmer and an inode number (the thing you get from stat/fstat), you can do an exhaustive search (using stat to make sure the device number matches).

o  If you just have an inode number, you can do an exaustive search and get all the candidate files with the same inode number but with different device numbers.

Why is this so difficult, you ask? Because the inodes point only at the data-space of the filesystem, not at the name-space. This is because the name-space is not a onoe-to-one mapping to the data-space.

For example, every directory has several different places in the name-space pointing at the same data-space - the name of the directory in the parent directory, '.' in the directory itself, and '..' in any child directories.

And regular files can also have multiple names for the same files. The diffent names are referred to as 'hard links' to each other in this case. For example, here's the output of 'ls -li /usr/bin/* | grep 259254' on a Solaris 8 box:
    259254 -r-xr-xr-x   5 root     bin       226656 Oct 11  2003 /usr/bin/edit*
    259254 -r-xr-xr-x   5 root     bin       226656 Oct 11  2003 /usr/bin/ex*
    259254 -r-xr-xr-x   5 root     bin       226656 Oct 11  2003 /usr/bin/vedit*
    259254 -r-xr-xr-x   5 root     bin       226656 Oct 11  2003 /usr/bin/vi*
    259254 -r-xr-xr-x   5 root     bin       226656 Oct 11  2003 /usr/bin/view*

As you can see, these are actually all the same underlying file / inode, but with different names. And they could just as easily have been in completely differnet parts of the filesystem, say by doing
    mkdir -p /usr/foo/bar/1/2/3
    ln /usr/bin/vi /usr/foo/bar/1/2/3/vi

 

by: ahoffmannPosted on 2004-05-05 at 12:17:57ID: 10999066

find . -inum <your-inode> -print

 

by: NovaDenizenPosted on 2004-05-05 at 12:33:02ID: 10999215

0.  inode numbers are only relevant for a particular filesystem.  To identify a file, you really need the combination of inode and a filesystem identifier of some sort (like a mountpoint or device name).
1.  inodes do not have a filename associated with them.
2.  filenames are contained within directory entries, which refer to the inode number.
3.  Through hard-linking, more than one filename can refer to the same inode and file.
4.  It is possible for one program to open a file just before another deletes it.  In this situation, the file and inode continue to exist while the first program maintains the file descriptor to it, but at this time there is no directory entry in the filesystem that refers to the inode.  When the descriptor is closed, the OS deletes the file and frees up the inode.
5.  Therefore, the best you can hope for is to traverse the filesystem's entire directory structure (ala the find command above) and get a list of all filenames that refer to that particular inode.  It is possible that you will not find any directory entries that refer to the inode.

Note that there is no unix API command called "delete()", only one called "unlink()".  Its action is to remove a hard-link to an inode, without caring whether or not there are other links to that inode.

 

by: NovaDenizenPosted on 2004-05-05 at 12:39:10ID: 10999268

If you use the find command above, be sure to use the -mount option so find doesn't go wandering off onto another filesystem.

 

by: avizitPosted on 2004-05-05 at 22:40:25ID: 11002706

glibc contains a header fd_to_filename.h

the content of which is  as follows

-----------
/* In general there is no generic way to query filename for an open
   file descriptor.  */
static inline const char *
fd_to_filename (int fd)
{
  return NULL;
}
--------------

/abhijit/

 

by: stefan73Posted on 2004-05-07 at 06:59:19ID: 11014793

avizit,
Hehe, good one :-)

Stefan

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...