예제 #1
0
def _clean_info(
    root: Optional[str], epoch: int, info: tarfile.TarInfo
) -> tarfile.TarInfo:
    """
    Remove variable data from an archive entry.

    :param root: absolute path to the root directory from which the
        entry was added, or None to disable turning the name into a
        relative path
    :param epoch: fixed modification time to set
    :param info: tarinfo object to set
    :returns: changed tarinfo
    """
    if root is not None:
        info.name = os.path.relpath("/" + info.name, root)

    if not info.name.startswith("."):
        info.name = "./" + info.name

    info.uid = 0
    info.gid = 0
    info.uname = ""
    info.gname = ""
    info.mtime = epoch

    return info
예제 #2
0
def tar_strip_file_attributes(tar_info: tarfile.TarInfo) -> tarfile.TarInfo:
    # set time to epoch timestamp 0, aka 00:00:00 UTC on 1 January 1970
    # note that when extracting this tarfile, this time will be shown as the modified date
    tar_info.mtime = 0

    # user/group info
    tar_info.uid = 0
    tar_info.uname = ""
    tar_info.gid = 0
    tar_info.gname = ""

    # stripping paxheaders may not be required
    # see https://stackoverflow.com/questions/34688392/paxheaders-in-tarball
    tar_info.pax_headers = {}

    return tar_info
예제 #3
0
    def file_write(self,
                   path,
                   content,
                   mode=None,
                   owner=None,
                   group=None,
                   append=False,
                   hide=False,
                   sudo=False):
        """
        Writes a file to the container

        @param path: path of the file
        @param content: content to be put in the file
        @param mode: file mode
        @param owner: owner of the file
        @param group: group of the file
        @param append: append content to the file
        @param hide: hide (debug) logs
        @raise runtimeError: path for file couldn't be created
        """
        if append and self.exists(path):
            content = self.file_read(path) + content
        file_name = os.path.basename(path)
        dir_name = os.path.dirname(path)
        buf = BytesIO()
        with TarFile("write_file", mode='w', fileobj=buf) as tarf:
            f = BytesIO()
            length = f.write(content.encode('utf8'))
            f.seek(0)
            tari = TarInfo(name=file_name)
            tari.size = length
            if not mode is None:
                tari.mode = mode
            if not owner is None:
                tari.uname = owner
            if not group is None:
                tari.gname = group
            tarf.addfile(tari, f)
        if not self.exists(dir_name):
            result = self.container.exec_run("mkdir -p %s" % dir_name)
            if result.exit_code != 0:
                raise RuntimeError("Could not create path %s!\n%s" %
                                   (dir_name, result.output))
        self.container.put_archive(dir_name, buf.getvalue())
예제 #4
0
    def _reset_tarinfo(
        self,
        info: TarInfo,
        predicate: Optional[ArchiveAdapter.FileFilter],
        mtime: Optional[int],
    ) -> Optional[TarInfo]:
        if predicate is not None and not predicate(info.name):
            return None

        # remove user and group IDs as they are irrelevant for distribution and
        # may require subsequent `chown`ing on multi-tenant systems
        info.uid = 0
        info.uname = ""
        info.gid = 0
        info.gname = ""

        if mtime is not None:
            info.mtime = mtime

        return info
예제 #5
0
def reset_tar_info(info: tarfile.TarInfo) -> tarfile.TarInfo:
    info.gid = 0
    info.uid = 0
    info.uname = 'root'
    info.gname = 'root'
    return info