After tar extract, Changing Permissions
If you want to keep the permissions on files then you have to add the -p (or --preserve-permissions or --same-permissions) switch when extracting 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 PHP code should be :
exec("tar -xzfp foo.tar.gz");
Edit: --delay-directory-restore
solved the problem below about being unable to untar a file. The permissions of pwd are still altered, so the problem of the original poster might not be solved.
Not really an answer, but a way to reproduce the error.
First create some files and directories. Remove write access to the directories:
mkdir hellomkdir hello/worldecho "bar" > hello/world/foo.txtchmod -w hello/worldchmod -w hello
Next, create the tar file from within the directory, preserving permissions.
cd hellotar -cpf ../hw.tar --no-recursion ./ world world/foo.txtcd ..
Listing the archive:
tar -tvf hw.tar# dr-xr-xr-x ./# dr-xr-xr-x world/# -rw-r--r-- world/foo.txt
So far, I've been unable to untar the archive as a normal user due to the "Permission denied"-error. The archive can't be untarred naively. The permissions of the local directory change as well.
mkdir untarcd untarls -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 errorsls -ld .# dr-xr-xr-x ./
Experimenting with umask
and/or -p
did not help. However, adding --delay-directory-restore
does help untarring:
tar -xv --delay-directory-restore -f ../hw.tar# ./# world/# world/foo.txtls -ld .# dr-xr-xr-x ./chmod +w .
It is also possible to untar the file as root. What suprised me most is that tar apparently can change the permissions of pwd, which is still unsolved.
By the way, I originally 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 untarring it as a normal user to create a linux container.