![touch -t 内部如何工作?](https://linux22.com/image/48901/touch%20-t%20%E5%86%85%E9%83%A8%E5%A6%82%E4%BD%95%E5%B7%A5%E4%BD%9C%EF%BC%9F.png)
命令在内部是如何touch -t
准确工作的(我试图找到它的源代码但找不到)?
答案1
有时您甚至不需要源代码。使用strace
。
$ strace touch -t 201212121212 foobar
execve("/usr/bin/touch", ["touch", "-t", "201212121212", "foobar"], [/* 61 vars */]) = 0
[...] lots of noise [...]
open("foobar", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
dup2(3, 0) = 0
close(3) = 0
utimensat(0, NULL, {{1355310720, 0}, {1355310720, 0}}, 0) = 0
close(0) = 0
close(1) = 0
close(2) = 0
exit_group(0) = ?
+++ exited with 0 +++
为什么你好,utimensat()
.你是做什么的?
$ man utimensat
NAME
utimensat, futimens - change file timestamps with nanosecond precision
因此,有一个函数可以更改文件时间戳,并touch
使用它来更新文件的时间戳。这就是它的内部运作方式。
答案2
答案3
以下是它在 Solaris 上的工作方式。在这里truss
使用的strace
是一个完全不同的命令。
就像在 Gnu/Linux 下一样,utimensat
是使用的系统调用。
$ truss -vall -u a.out -f touch -t 1306080000 z
4160: execve("/usr/bin/touch", 0xF0770FC0, 0xF0770FD4) argc = 4
...
4160/1@1: -> main(0x4, 0xf0770fc0, 0xf0770fd4, 0xf0770f7c)
...
4160/1@1: -> atoi_for2(0xf0771131, 0x0, 0x24, 0xebc95be0)
4160/1@1: <- atoi_for2() = 13
4160/1@1: -> atoi_for2(0xf0771133, 0x0, 0x24, 0xebc95be0)
4160/1@1: <- atoi_for2() = 6
4160/1@1: -> atoi_for2(0xf0771135, 0x0, 0x24, 0xebc95be0)
4160/1@1: <- atoi_for2() = 8
4160/1@1: -> atoi_for2(0xf0771137, 0x0, 0x24, 0xebc95be0)
4160/1@1: <- atoi_for2() = 0
4160/1@1: -> atoi_for2(0xf0771139, 0x0, 0x24, 0xebc95be0)
4160/1@1: <- atoi_for2() = 0
4160/1@1: <- parse_time() = 0x51b257e0
4160/1: stat64("z", 0xF0770ED0) = 0
4160/1: d=0x08A00002 i=75783706 m=0100644 l=1 u=100 g=10 sz=0
4160/1: at = Jun 8 01:48:08 CEST 2013 [ 1370648888.022270973 ]
4160/1: mt = Jun 8 01:48:08 CEST 2013 [ 1370648888.022270973 ]
4160/1: ct = Jun 8 01:48:08 CEST 2013 [ 1370648888.022273810 ]
4160/1: bsz=4096 blks=0 fs=tmpfs
4160/1: utimensat(AT_FDCWD, "z", 0xF0770F60, 0) = 0
4160/1: at = Jun 8 00:00:00 CEST 2013 [ 1370642400.000000000 ]
4160/1: mt = Jun 8 00:00:00 CEST 2013 [ 1370642400.000000000 ]
4160/1@1: <- main() = 0
4160/1@1: -> _fini()
4160/1@1: <- _fini() = 0xebcc0140
4160/1: _exit(0)