Getting the inode number of an unopen file from path
Is there a way I could get the inode number of a file which has not yet been opened. I would like开发者_Go百科 to do this from inside a c function
Regards, Lipika
Use stat(2) (which takes a file path), and check the st_ino field. Do note that it's possible for someone to move or remove the file between the time you call stat
and whenever you manage to do anything with the information.
Essentially, the answer can be found in this question:
How do I read a directory as a file in Unix?
You have to read the directory which contains the file entry. That entry contains the inode number.
Unless you have very tight control over the permissions on every element of the path to the file, what you're trying to do is almost surely wrong. Between any two operations involving accessing a file by pathname, there is a race condition, i.e. the inode number you get is potentially incorrect even before the call (stat
or readdir
) that gives it to you returns. Why don't you want to open the file?
精彩评论