ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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.
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.
@ 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.