5

From 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)
[...]

From this I understand that by default extracted files' permissions are set based on the user's umask, unless the user is root:

% umask
002

So files extracted by me should have permissions 664 (666-002).

However:

% 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

I.e., Tar is preserving the permissions of the original file even though I didn't pass the -p, --preserve-permissions or --same-permissions switch.

Nonetheless if I pass the --no-same-permissions switch:

% 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

Still Tar is preserving the permissions of the original file.

Can someone please explain why is that?

kos
  • 35,891
  • 1
    as I understand it, umask is applied to the permissions, i.e. 775 ^ 002 = 775. Try testing it with a file with 777 permissions. –  Nov 03 '15 at 20:20
  • 1
    @adonis Indeed, that's correct. I missunderstood the description and I was fooled by the fact that that an umask of 002 applied on a file with permissions 775 doesn't change anything. I knew it was a silly thing, thanks. Do you mind posting that as answer? – kos Nov 03 '15 at 20:35

1 Answers1

2

Since adonis (who spotted the problem) didn't post an answer yet, I'll post an answer myself.

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

This, contrarily to what I thought, means that the umask is applied to the permissions of the folders / files in the archive, and not to the conventional permissions of newly created folders / files (777 / 666) like I thought.

I.e. a folder / file extracted without passing the -p, --preserve-permissions or --same-permissions switch won't have the permissions set to 777 & ~umask / 666 & ~umask, but to folder's/file's_archived_permissions & ~umask.

In this specific case I was additionally fooled by the fact that applying an umask of 002 to a file with permissions 775 doesn't change anything, since 775 & ~002 = 775.

So, in short, extracting a file with permissions 775 with an user umask of 002 produces, correctly, a file with permissions 775, result of 775 & ~002.

Random832
  • 1,155
kos
  • 35,891