鉴于在 Linux 中 utimes(2)
是一个系统调用,而 futimes(3)
是一个库函数,我认为 futimes
是根据 utimes
实现的。但是,utimes
采用路径名,而 futimes
采用文件描述符。
因为,“不可能”从文件描述符或i节点号确定路径名,我想知道如何做到这一点? “真正的”系统调用总是对i-node 编号起作用吗?
最佳答案
首先,您可能错误地提到了 Posix,因为后者没有不同的系统调用和库函数。将 futimes() 放入库调用是 Linux 特定的。在glibc(文件sysdeps/unix/sysv/linux/futimes.c)中,有这样的注释:
/* Change the access time of the file associated with FD to TVP[0] and the modification time of FILE to TVP[1]. Starting with 2.6.22 the Linux kernel has the utimensat syscall which can be used to implement futimes. Earlier kernels have no futimes() syscall so we use the /proc filesystem. */
因此,这是使用 utimensat() 来完成的,并将指定的描述符作为所有 *at() 调用的引用描述符。以前,这对路径/proc/${pid}/fd/${fd} 使用 utimes() 起作用(太麻烦,并且只有在挂载了/proc 时)。这是对第二个问题的答复:尽管通常不可能从文件描述符中检测到文件名,但仍然可以单独访问该文件。 (顺便说一句,有时会存储用于打开文件的初始路径;有关 Linux 进程,请参阅/proc/$pid/{cwd,exe}。)
相比之下,FreeBSD 提供了显式的 futimes() 和 futimesat() 系统调用(但我想知道为什么后者不命名为“utimesat”)。
关于linux - 如何用utimes来实现futimes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20719957/