开发者

Getting the inode number of an unopen file from path

开发者 https://www.devze.com 2023-02-16 04:08 出处:网络
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

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号