我想从C代码触摸我的文件来修改他们的访问日期.这似乎不起作用:
struct stat fileSt;
lstat(path, &fileSt);
fileSt.st_mtime = time(NULL);
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
sig*_*ice 11
utimes()可能是怎么做的.utime()已经过时了.
像strace这样的工具确定这样的事情是微不足道的.
strace touch -t 01010911 xxx
.
.
open("xxx", O_WRONLY|O_NONBLOCK|O_CREAT|O_NOCTTY|O_LARGEFILE, 0666) = 0
utimes("/proc/self/fd/0", {1230829860, 0}) = 0
Run Code Online (Sandbox Code Playgroud)
我想你想要的utime(2)
.这应该足够了:
utime(filename, NULL);
Run Code Online (Sandbox Code Playgroud)
文档说:
Run Code Online (Sandbox Code Playgroud)int utime(const char *filename, const struct utimbuf *times);
[...]
utime()系统调用将filename指定的inode的访问和修改时间分别更改为actime和modtime字段.
如果是
NULL
,则将文件的访问和修改时间设置为当前时间.
旧的utime()
和utimes()
对于许多用例来说都可以,但是要以现代系统上需要的纳秒分辨率更新& ,请使用atime
:mtime
utimensat(0, path, NULL, 0);
Run Code Online (Sandbox Code Playgroud)
这与较新的结合非常有用,stat()
后者也返回纳秒分辨率的struct timespec
st_mtim
字段。struct stat