简体   繁体   English

如何获取符号链接的文件描述符?

[英]How to get the file desciptor of a symbolic link?

I know that I can read a symbolic link with readlink , and operate upon it with llistxattr etc, but I'm looking to get the file descriptor of it so that I can use it with flistxattr etc. 我知道我可以使用readlink读取符号链接,并使用llistxattr等对其进行操作,但是我正在寻找获取它的文件描述符,以便可以与flistxattr等一起flistxattr

I've also checked manual of open and I know O_NO_FOLLOW won't open a symbolic link (but will fail so that some applications can be safer). 我还检查了open手册,并且我知道O_NO_FOLLOW不会打开符号链接(但是会失败,以便某些应用程序更安全)。

So is there any way to get the file descriptor of a symbolic link? 那么有什么方法可以获取符号链接的文件描述符?

is there any way to get the file descriptor of a symbolic link? 有什么办法来获取符号链接的文件描述符?

From the symlink(7) man page: symlink(7)手册页中:

Obtaining a file descriptor that refers to a symbolic link 获取引用符号链接的文件描述符

Using the combination of the O_PATH and O_NOFOLLOW flags to open(2) yields a file descriptor that can be passed as the dirfd argument in system calls such as fstatat(2), fchownat(2), fchmodat(2), linkat(2), and readlinkat(2), in order to operate on the symbolic link itself (rather than the file to which it refers). 使用O_PATH和O_NOFOLLOW标志的组合来打开(2)会产生一个文件描述符,该文件描述符可以作为dirfd参数传递给系统调用,例如fstatat(2),fchownat(2),fchmodat(2),linkat(2)和readlinkat(2),以便对符号链接本身(而不是它所引用的文件)进行操作。

By default (ie, if the AT_SYMLINK_FOLLOW flag is not specified), if name_to_handle_at(2) is applied to a symbolic link, it yields a handle for the symbolic link (rather than the file to which it refers). 默认情况下(即,如果未指定AT_SYMLINK_FOLLOW标志),如果将name_to_handle_at(2)应用于符号链接,它将产生符号链接的句柄(而不是它所引用的文件)。 One can then obtain a file descriptor for the symbolic link (rather than the file to which it refers) by specifying the O_PATH flag in a subsequent call to open_by_handle_at(2). 然后,可以通过在对open_by_handle_at(2)的后续调用中指定O_PATH标志,来获得符号链接的文件描述符(而不是符号引用的文件)。 Again, that file descriptor can be used in the aforementioned system calls to operate on the symbolic link itself. 同样,该文件描述符可用于上述系统调用中,以对符号链接本身进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM