Tarfile and usernames

lars at gustaebel.de lars at gustaebel.de
Mon Dec 31 09:47:30 CET 2012


On Sun, Dec 30, 2012 at 03:07:05PM -0500, Albert Hopkins wrote:
> > I can't see documented anywhere what this library does with userids and
> > groupids.  I can't guarantee that the computers involved will have the
> > same users and groups, and would like the archives to be extracted so
> > that the files are all owned by the extracting user.

> However, it should be stated that by default (on *nix anyway) if the
> user is not root then user/groups are assigned to the user exctracting
> the file (because only root can assign userids/non-member-groups).
> The TarFile extract*() methods pretty much inherit the same behavior as
> the *nix tar command.  So if you are extracting as a non-root user, you
> should expect the same behavoir.  If you are extracting as root but
> don't want to change user/groups may have to extract it manually or
> create your own class by inheriting TarFile and overriding the .chown()
> method.

Please take a look at the second example in the Examples section of the tarfile
docs:

http://docs.python.org/2.7/library/tarfile.html#examples

It shows how to extract a subset of an archive using a generator as some kind
of filter for the extractall() method. Just rewrite the example so that every
tarinfo is patched with the required user and group name information before
being yielded:

def filter(members):
    for tarinfo in members:
        tarinfo.uname = "root"
        tarinfo.gname = "root"
        yield tarinfo

That's it.

-- 
Lars Gustäbel
lars at gustaebel.de



More information about the Python-list mailing list