为什么即使不传递 `-p`、`--preserve-permissions` 或 `--same-permissions` 开关,Tar 也会保留原始文件的权限?

kos*_*kos 5 tar

man 1 tar

[...]
     -p, --preserve-permissions, --same-permissions
           extract information about file permissions (default for superuser)
[...]
     --no-same-permissions
           apply the user's umask when extracting permissions from the archive
           (default for ordinary users)
[...]
Run Code Online (Sandbox Code Playgroud)

由此我了解到,默认情况下提取的文件的权限是根据用户的 umask 设置的,除非用户是 root:

% umask
002
Run Code Online (Sandbox Code Playgroud)

所以我提取的文件应该有权限664666- 002)。

然而:

% touch foo
% chmod +x foo
% ls -l
total 0
-rwxrwxr-x 1 user user 0 nov  3 19:36 foo
% tar cf foo.tar foo
% rm foo
% tar xf foo.tar 
% ls -l
total 12
-rwxrwxr-x 1 user user     0 nov  3 19:36 foo
-rw-rw-r-- 1 user user 10240 nov  3 19:36 foo.tar
Run Code Online (Sandbox Code Playgroud)

即,即使我没有通过-p,--preserve-permissions--same-permissions开关,Tar 仍保留原始文件的权限。

尽管如此,如果我通过--no-same-permissions开关:

% tar xf foo.tar --no-same-permissions
% ls -l
total 12
-rwxrwxr-x 1 user user     0 nov  3 19:36 foo
-rw-rw-r-- 1 user user 10240 nov  3 19:36 foo.tar
Run Code Online (Sandbox Code Playgroud)

Tar 仍然保留原始文件的权限。

有人可以解释一下这是为什么吗?

kos*_*kos 2

由于阿多尼斯(发现问题的人)尚未发布答案,我将自己发布答案。

[...]
     --no-same-permissions
           apply the user's umask when extracting permissions from the archive
           (default for ordinary users)
[...]
Run Code Online (Sandbox Code Playgroud)

这与我的想法相反,意味着 umask 应用于存档中的文件夹/文件的权限,而不是像我想象的那样应用于新创建的文件夹/文件(777/ 666)的常规权限。

-p即,在未通过,--preserve-permissions--same-permissions开关的情况下提取的文件夹/文件不会将权限设置为777 & ~umask/ 666 & ~umask,而是设置为folder's/file's_archived_permissions & ~umask.

002在这种特定情况下,我还被这样一个事实所愚弄:对具有权限的文件应用 umask775不会改变任何内容,因为775 & ~002 = 775.

因此,简而言之,775使用用户 umask提取具有权限的文件002会正确地生成具有权限的文件775,结果为775 & ~002