删除硬链接的尾部文件会更改头部的更改时间,反之亦然。为什么?

Him*_*man 8 hard-link stat inode timestamps links

注意:问题虽然说反之亦然,但它实际上没有任何意义,因为它们都指向同一个 inode,并且不可能说出哪个是头,哪个是尾。

假设我有一个文件 hlh.txt

[root@FREL ~]# fallocate -l 100 hlh.txt
Run Code Online (Sandbox Code Playgroud)

现在如果我看到 hlh.txt 的更改时间

[root@FREL ~]# stat hlh.txt
  File: hlh.txt
  Size: 100             Blocks: 8          IO Block: 4096   regular file
Device: fc00h/64512d    Inode: 994         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2023-01-11 01:43:05.469703330 -0500
Modify: 2023-01-11 01:43:05.469703330 -0500
Change: 2023-01-11 01:43:05.469703330 -0500
 Birth: 2023-01-11 01:43:05.469703330 -0500
Run Code Online (Sandbox Code Playgroud)

创建硬链接

[root@FREL ~]# ln hlh.txt hlt.txt
Run Code Online (Sandbox Code Playgroud)

由于hlh.txt和hlt.txt都指向相同的inode,因此更改时间将是可以理解的硬链接尾文件的ctime。

[root@FREL ~]# stat hlt.txt
  File: hlt.txt
  Size: 100             Blocks: 8          IO Block: 4096   regular file
Device: fc00h/64512d    Inode: 994         Links: 2
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2023-01-11 01:43:05.469703330 -0500
Modify: 2023-01-11 01:43:05.469703330 -0500
Change: 2023-01-11 01:44:05.316842644 -0500
 Birth: 2023-01-11 01:43:05.469703330 -0500
Run Code Online (Sandbox Code Playgroud)

但如果我取消链接头文件,也会更改文件的 ctime。为什么?我的意思是我们所做的只是删除了头部,改变时间在内部有什么意义。为什么需要改变?

[root@FREL ~]# unlink hlh.txt
[root@FREL ~]#
[root@FREL ~]# stat hlt.txt
  File: hlt.txt
  Size: 100             Blocks: 8          IO Block: 4096   regular file
Device: fc00h/64512d    Inode: 994         Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2023-01-11 01:43:05.469703330 -0500
Modify: 2023-01-11 01:43:05.469703330 -0500
Change: 2023-01-11 01:47:49.588364704 -0500
 Birth: 2023-01-11 01:43:05.469703330 -0500
Run Code Online (Sandbox Code Playgroud)

Kus*_*nda 13

这是POSIX对库函数unlink()要求:

成功完成后,unlink()应标记更新父目录的上次数据修改和上次文件状态更改时间戳。另外,如果文件的链接计数不为0,则应将文件的最后一次文件状态更改时间戳标记为更新。

标准文档没有对此要求进行扩展。由于链接计数减少了 1,我假设 ctime 时间戳(“上次文件状态更改时间戳”)已更新以反映文件状态更改的事实。

  • 你的假设几乎肯定是正确的。链接计数是“stat()”返回的数据的一部分,并且作为一般规则,如果时间戳以外的任何数据发生变化,则 ctime 也应该发生变化。 (4认同)