Dear Lazyweb. Open by inode.
Jan. 8th, 2008 01:38 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
For the past twenty years, I've always assumed that, given knowledge of a file's inode number, there was some efficient way to open(2) that file by inode instead of name, and I've just never felt the need to use it.
Now I feel the desire to do that, and cannot find how. Searching google for "open by inode" just gives me references to stuff on how to implement Linux VFS, and/or how to open a file from inside kernel space, not from user space.
The openat(2) looks like a half-assed way to get some but not all of the same optimization.
So, is there a way to "open by inode" from user space, and how do I do it?
Now I feel the desire to do that, and cannot find how. Searching google for "open by inode" just gives me references to stuff on how to implement Linux VFS, and/or how to open a file from inside kernel space, not from user space.
The openat(2) looks like a half-assed way to get some but not all of the same optimization.
So, is there a way to "open by inode" from user space, and how do I do it?
no subject
Date: 2008-01-08 10:18 pm (UTC)no subject
Date: 2008-01-08 10:28 pm (UTC)no subject
Date: 2008-01-14 11:45 pm (UTC)Answer 1: No, because inodes are specific to a file system (ie are handled differently in different file systsms). The way open in Unix is designed is that it transforms a name into a handle. An inode may or may not have a name. (You open a file, write to it, and then delete it. It's still on the disk. It still has an inode. It does not have any name.)
There are ways to do close things depending on what you need to do really. Do you want to bypass the time it takes to do name resolving? Do you want to make sure you access the same handle you opened before?
To use the same handle you can use dup(2). For bypassing the time it takes to do name resolving, it would depend on your intended end and the filesystem you are using.
Answer 2: There are some kernel hacks which will allow you to open by inode for specific file systems, but because different file systems deal with inodes differently, there is no one-size-fits-all solution to this issue. These kernel patches may give you what you want, but at a cost of causing possible other problems.
M recommends looking at what your goal is and finding another way to deal with it.