Welcome, guest | Sign In | My Account | Store | Cart

Some tarballs have u-x set on directories .. this makes the program fail due to permissions. See http://bugs.python.org/issue6196

Python, 25 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def _ensure_read_access(compressedfile):
    """Ensure that the compressedfile will be readable by the user after
    extraction.

    Some tarballs have u-x set on directories. They may as well have u-r set on
    files. We reset such perms here.. so that the extracted files remain
    accessible.

    See also: http://bugs.python.org/issue6196
    """
    if isinstance(compressedfile, zipfile.ZipFile):
        return # zipfiles have no perms, I believe
    elif isinstance(compressedfile, tarfile.TarFile):
        EXECUTE = 0100
        READ = 0400
        dir_perm = EXECUTE
        file_perm = EXECUTE | READ

        # WARNING: if the tarfile had a huge of list of files, this could be a
        # potential performance bottleneck.
        for tarinfo in compressedfile.getmembers():
            tarinfo.mode |= (dir_perm if tarinfo.isdir() else file_perm)
    else:
        raise NotImplementedError, \
            "don't know how to read this of compressed file: {0}".format(compressedfile)
Created by Sridhar Ratnakumar on Thu, 4 Jun 2009 (MIT)
Python recipes (4591)
Sridhar Ratnakumar's recipes (7)

Required Modules

  • (none specified)

Other Information and Tasks