Gzip compressed file ownership and permissions

gzippermissionsunix

I'm trying to find out how gzipping a file changes its permissions.
The gzip manual (man gzip) says:

Whenever possible, each file is
replaced by one with the extension
.gz, while keeping the same ownership
modes, access and modification times.

Now, I have a file 'myfile.txt' whose owner is me (myuser). When I gzip this file as another user (that user (say 'otheruser') has read and write permissions in this directory and file), the owner of the gzip file becomes this otheruser. That is, whoever gzip's the file becomes the owner of the .gz file.

Also, whoever gunzip's this file becomes the owner of the uncompressed file. Does this mean gzip does nothing at all about ownerships? If so, what does the above sentence in the manual page mean?

I don't believe the 'Whenever possible' clause might be a problem here since I'm in Unix (Solaris) where permissions retention is (AFAIK) possible.

A related question: if a user has read permission on a .gz file, is it still possible that he cannot read the contents of the file? Does gzip maintain a separate set of permissions 'within' the archive which can limit access? This does not seem probable, but I'd like to be sure.

I need to write code depending on the behaviour of gzip, so finding out the exact behaviour is important for me. Any help would be appreciated.

Thanks.

Best Answer

Changing file ownership can only be done by root. Ordinary user_a simply cannot create a file owned by ordinary user_b, whether file creation is done by gzipping or by gunzipping. If you need to preserve ownership, unzip as root.

Related Question