After extracting tar, change permissions

Just a question regarding unix and PHP today.

What I am doing in my PHP uses a Unix system to decompress a tarred file.

exec("tar -xzf foo.tar.gz");

As a rule, everything works fine until I came across this particular foo.tar.gz file, which has a file system as follows:

 Applications/ Library/ Systems/ 

After running the tar command, it seems that the file permissions are changed to 644 (instead of 755).

This calls Permission denied (errno 13) and therefore disables most of my code. (I guess due to lack of privileges)

Anyway, can I stop this tar command from completely breaking my permissions?

Thanks.

Oh, and this only seems to happen when I have a foo.tar.gz file that has this particular file system. Anything else and I'm fine.

+4
source share
2 answers

If you want to retain file permissions, you need to add the -p switch (or --preserve-permissions or -same-permissions) when retrieving the tarball. From the tar man pages:

 --preserve-permissions --same-permissions -p When `tar' is extracting an archive, it normally subtracts the users' umask from the permissions specified in the archive and uses that number as the permissions to create the destination file. Specifying this option instructs `tar' that it should use the permissions directly from the archive. 

So, the PHP code should be:

 exec("tar -xzfp foo.tar.gz"); 
+2
source

Edit: --delay-directory-restore solve the problem below that you cannot unlock the file. Pwd permissions are still changed, so the problem with the original poster may not be resolved.

Not exactly the answer, but a way to reproduce the error.

First create some files and directories. Remove write access to directories:

 mkdir hello mkdir hello/world echo "bar" > hello/world/foo.txt chmod -w hello/world chmod -w hello 

Then create the tar file from the directory, keeping the permissions.

 cd hello tar -cpf ../hw.tar --no-recursion ./ world world/foo.txt cd .. 

List of archives:

 tar -tvf hw.tar # dr-xr-xr-x ./ # dr-xr-xr-x world/ # -rw-r--r-- world/foo.txt 

So far I have not been able to unzip the archive as a regular user due to an "Allowed Failure" -error. Unable to quickly unzip the archive. The permissions of the local directory also change.

 mkdir untar cd untar ls -ld . # drwxr-xr-x ./ tar -xvf ../hw.tar # ./ # world/ # tar: world: Cannot mkdir: Permission denied # world/foo.txt # tar: world/foo.txt: Cannot open: No such file or directory # tar: Exiting with failure status due to previous errors ls -ld . # dr-xr-xr-x ./ 

The experiment with umask and / or -p did not help. However, adding --delay-directory-restore helps to expand:

 tar -xv --delay-directory-restore -f ../hw.tar # ./ # world/ # world/foo.txt ls -ld . # dr-xr-xr-x ./ chmod +w . 

You can also unzip the file with root privileges. What surprises me the most is that tar seems to be able to change pwd permissions, which is still not resolved.

By the way, I initially got into this problem by creating a tarball for / with

 tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system / 

as root (pwd = /) and cancel it as a normal user to create a linux container.

0
source

Source: https://habr.com/ru/post/1310656/


All Articles