linux - 如何用utimes来实现futimes?

标签 linux system-calls libc

鉴于在 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/

相关文章:

linux - 使用 Qt4 在后台运行 gnuplot

c++ - 在 C++ 中使用 ioprio_set

centos - 获取cp : error while loading shared libraries: libc. so.6:无法打开共享对象文件:没有这样的文件或目录错误

debugging - 在 Mac OS X 上,如何获得 System/LibC 的调试版本以进行源代码级调试?

linux - 无法从安装的驱动器克隆存储库

c - 如何拦截 gtk 窗口关闭按钮单击?

linux - 一般颜色列表

linux - 模拟内核socket编程中select()和poll()的作用

c++ - 如何找出依赖项中存在某个库的特定版本的原因?

android - android中的pidstat命令?