0

In a C program, I have got the inode number of a file, and I need to get file name for this inode number.

I have a hint that gets the directory entry for this inode number, and that will, of course, have the file name. But I am unable to figure out how to get the directory entry for a file from its inode number. I need to do this in the C program and Ubuntu. Any solutions?

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63
Muhammad Zubair
  • 466
  • 5
  • 17
  • You mean file names - because there can be several files with the same inode number. – Erki Aring Mar 19 '19 at 06:57
  • There can be only file against an inode number. But in case the two files are in different partitions, in that case the two files can have same inode number. – Muhammad Zubair Mar 19 '19 at 07:13

1 Answers1

2

In a C program, I have got the inode number of a file, and I need to get file name for this inode number.

A single file, identified by its inode number, may have any number of links (a.k.a. "directory entries" a.k.a. "names") associated with it. (It is a bit more complicated than that, because files that are open have additional links that may or may not be directory entries, but that's not important for our purposes). One can add and remove links that refer to the same file (inode) freely (unless that file is actually a directory). The file doesn't keep track of links associated with it. It only keeps track of their number (the reference count). As soon as the number of links goes down to zero, the file gets deleted.

Given just an inode number, there's absolutely positively no way whatsoever to find any or all of its associated directory entries, short of checking all directory entries of a filesystem.

N.B. Links above are hard links. Soft links are something else entirely.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243