Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow setting uid and gid when creating tar files #51105

Closed
tarekziade mannequin opened this issue Sep 7, 2009 · 6 comments
Closed

allow setting uid and gid when creating tar files #51105

tarekziade mannequin opened this issue Sep 7, 2009 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@tarekziade
Copy link
Mannequin

tarekziade mannequin commented Sep 7, 2009

BPO 6856
Nosy @gustaebel, @tarekziade
Files
  • issue6856.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/gustaebel'
    closed_at = <Date 2009-09-12.10:52:29.458>
    created_at = <Date 2009-09-07.07:47:29.529>
    labels = ['type-feature', 'library']
    title = 'allow setting uid and gid when creating tar files'
    updated_at = <Date 2009-09-12.10:52:29.456>
    user = 'https://github.com/tarekziade'

    bugs.python.org fields:

    activity = <Date 2009-09-12.10:52:29.456>
    actor = 'lars.gustaebel'
    assignee = 'lars.gustaebel'
    closed = True
    closed_date = <Date 2009-09-12.10:52:29.458>
    closer = 'lars.gustaebel'
    components = ['Library (Lib)']
    creation = <Date 2009-09-07.07:47:29.529>
    creator = 'tarek'
    dependencies = []
    files = ['14853']
    hgrepos = []
    issue_num = 6856
    keywords = ['patch']
    message_count = 6.0
    messages = ['92348', '92361', '92363', '92373', '92375', '92539']
    nosy_count = 2.0
    nosy_names = ['lars.gustaebel', 'tarek']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue6856'
    versions = ['Python 2.7', 'Python 3.2']

    @tarekziade
    Copy link
    Mannequin Author

    tarekziade mannequin commented Sep 7, 2009

    I am proposing this feature for an issue we have in Distutils: being
    able to set the uid/gid of files added in a tar archive using tarfile.

    Here's what I am proposing:

    • adding two methods to TarInfo: set_uid and set_gid, that are able to
      take a user and group name *or* a uid and gid number

    • adding in TarFile a new filter option to add() called include. If
      given, it's a callable that receives the tarinfo object right before
      it's added, so its uid/gid can be tweaked. This callable must return the
      object. If it returns None, the object is not added to the tar file.

    @tarekziade tarekziade mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 7, 2009
    @tarekziade tarekziade mannequin changed the title allow settong uid and gid when creating tar files allow setting uid and gid when creating tar files Sep 7, 2009
    @loewis loewis mannequin assigned gustaebel Sep 7, 2009
    @gustaebel
    Copy link
    Mannequin

    gustaebel mannequin commented Sep 7, 2009

    TarInfo does not need set_uid() or set_gid() methods, both can be set
    using the uid and gid attributes.
    If the list of files to add to the archive is known you can do this:

    tar = tarfile.open("foo.tar.gz", "w:gz")
    for filename in filenames:
      tarinfo = tar.gettarinfo(filename)
      if tarinfo.isreg():
        fobj = open(filename)
      else:
        fobj = None
      tarinfo.uid = 0
      tarinfo.gid = 0
      tar.addfile(tarinfo, fobj)
    tar.close()

    I am not against adding a new option. Although, it's too bad that I
    added the exclude option in 2.6 not long ago, which does something very
    similar and would again be replaced by this more general "include"
    option. BTW, I think calling the option "filter" would be more suitable
    for what it's doing.

    @tarekziade
    Copy link
    Mannequin Author

    tarekziade mannequin commented Sep 7, 2009

    TarInfo does not need set_uid() or set_gid() methods,
    both can be set using the uid and gid attributes.

    I was thinking about the set_ methods to be able to use
    "root" (str) instead of "0" (int) for example, like
    what the tar command seems to allow with --uid and --gid.

    I am not against adding a new option. Although, it's too bad that
    I added the exclude option in 2.6 not long ago, which does
    something very similar and would again be replaced by this
    more general "include" option. BTW, I think calling
    the option "filter" would be more suitable for what it's doing.

    Maybe we could add the "filter" option for 2.7/3.2 together with the
    exclude option? And add a deprecation warning for "exclude" when it's
    used, since it would then become *one* use case for "filter".

    We could also add an exclude callable in the module, as an example
    usage of the filter option, exactly like I did for shutil.copytree
    (look for ignore_patterns).

    @gustaebel
    Copy link
    Mannequin

    gustaebel mannequin commented Sep 7, 2009

    I do not quite see the benefit from the set_* methods. Although the
    attribute access I proposed may be slightly more complicated (because
    you might need the pwd and grp modules) it offers the most freedom.
    Let's take the set_uid() method as an example: Its purpose would be to
    set both the uid and uname field in the tar header. That is fine as long
    as its argument is a uid or username that actually exists. If set_uid()
    gets a username that does not exist, what are we going to do? Only set
    the uname field and leave the uid field alone or raise an exception? If
    the user wants to set a non-existant username on purpose, he cannot use
    the set_uid() method. And what are we going to do on Windows? Is there
    anything comparable to pwd/grp we could use?
    I expect the common use case for these both methods will be to *reset*
    the owner information to a default, and this is done by setting uname to
    "root" and uid to 0.

    The filter argument is actually a nice idea. I have attached a patch
    that outlines my idea of how it is supposed to be. Comments welcome.

    @tarekziade
    Copy link
    Mannequin Author

    tarekziade mannequin commented Sep 7, 2009

    I do not quite see the benefit from the set_* methods.
    .. some explanations of the underlying complexity...

    The only benefit I can see for the set_* method is to hide
    the underlying complexity you've explained.

    In Distutils, I'd like to provide a uid and gid option
    to the sdist command where the user can set "root" for instance
    and see the lib taking care of creating a tarfile with everything
    set to the right value (and ignore the flags under windows etc)

    So it seems that working per TarInfo is the wrong approach,
    and a global function to create an archive would be better.

    The filter argument is actually a nice idea. I have attached a
    patch that outlines my idea of how it is supposed to be.
    Comments welcome.

    The patch looks nice to me

    small typo in the doc :

    How create

    should be "How to create"

    @gustaebel
    Copy link
    Mannequin

    gustaebel mannequin commented Sep 12, 2009

    I applied the patch with some more small fixes to the trunk (r74750) and
    the py3k branch (r74751).

    @gustaebel gustaebel mannequin closed this as completed Sep 12, 2009
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants