Exemplo n.º 1
0
    def __init__(self, path):
        self.path = s2u(os.path.abspath(path))
        self.tree = DirectoryTree(self.path, self)

        with LibArchive.open(self.path) as larchive:
            self.archive = larchive
            a = larchive._a
            while True:
                try:
                    e = _libarchive.archive_entry_new()
                    r = _libarchive.archive_read_next_header2(a, e)
                    if r != _libarchive.ARCHIVE_OK:
                        break

                    name = _libarchive.archive_entry_pathname(e)
                    pathname = s2u(name)

                    if pathname[-1] == '/':
                        pathname = pathname[:-1]

                    self.tree.add(os.path.join(self.path, pathname))
                except UnicodeDecodeError:
                    logging.info("Unreadable file name: {0} (in '{1}')".format(
                        name, self.path
                    ))
                finally:
                    _libarchive.archive_entry_free(e)
Exemplo n.º 2
0
    def __init__(self, path):
        self.path = s2u(os.path.abspath(path))
        self.tree = DirectoryTree(self.path, self)

        with LibArchive.open(self.path) as larchive:
            self.archive = larchive
            a = larchive._a
            while True:
                try:
                    e = _libarchive.archive_entry_new()
                    r = _libarchive.archive_read_next_header2(a, e)
                    if r != _libarchive.ARCHIVE_OK:
                        break

                    name = _libarchive.archive_entry_pathname(e)
                    pathname = s2u(name)

                    if pathname[-1] == '/':
                        pathname = pathname[:-1]

                    self.tree.add(os.path.join(self.path, pathname))
                except UnicodeDecodeError:
                    logging.info("Unreadable file name: {0} (in '{1}')".format(
                        name, self.path))
                finally:
                    _libarchive.archive_entry_free(e)
Exemplo n.º 3
0
    def create2(container, archivepath, checked):
        if not isinstance(container, FileSystem):
            logging.info("container '{0}' is not FileSystem".format(container))
            return False

        archivepath = os.path.abspath(archivepath)

        with libarchive.Archive(archivepath, 'w') as a:
            logging.info(u"START create selective '{0}'".format(archivepath))
            for node in checked:
                path = s2u(node.get_path())
                pathname = s2u(os.path.join(*node.get_data_array()[1:]))
                logging.info(u"from '{0}' to '{1}'".format(path, pathname))
                a.writepath(path, pathname)
            logging.info(u"END create selective '{0}'".format(archivepath))

        return True
Exemplo n.º 4
0
    def create2(container, archivepath, checked):
        if not isinstance(container, FileSystem):
            logging.info("container '{0}' is not FileSystem".format(container))
            return False

        archivepath = os.path.abspath(archivepath)

        with libarchive.Archive(archivepath, 'w') as a:
            logging.info(u"START create selective '{0}'".format(
                archivepath
            ))
            for node in checked:
                path = s2u(node.get_path())
                pathname = s2u(os.path.join(*node.get_data_array()[1:]))
                logging.info(u"from '{0}' to '{1}'".format(
                    path, pathname
                ))
                a.writepath(path, pathname)
            logging.info(u"END create selective '{0}'".format(archivepath))

        return True
Exemplo n.º 5
0
    def create(container, archivepath, checked):
        if not isinstance(container, FileSystem):
            logging.info("container '{0}' is not FileSystem".format(container))
            return False

        archivepath = os.path.abspath(archivepath)
        BUFFER_SIZE = 2048

        with libarchive.Archive(archivepath, 'w') as larchive:
            a = larchive._a
            for node in checked:
                if node.is_dir():
                    continue
                path = s2u(node.get_path())
                pathname = os.path.join(*node.get_data_array()[1:])
                if isinstance(pathname, unicode):
                    pathname = pathname.encode('utf8', errors='replace')
                st = os.stat(path)
                entry = _libarchive.archive_entry_new()
                _libarchive.archive_entry_set_pathname(entry, pathname)
                _libarchive.archive_entry_set_size(entry, st.st_size)
                _libarchive.archive_entry_set_filetype(
                    entry, stat.S_IFMT(st.st_mode)
                )
                _libarchive.archive_entry_set_mtime(entry, st.st_mtime, 0)
                _libarchive.archive_entry_set_perm(entry, stat.S_IMODE(st.st_mode))
                _libarchive.archive_write_header(a, entry)

                f = open(path, 'r')

                data = f.read(BUFFER_SIZE)
                count = len(data)

                while count > 0:
                    _libarchive.archive_write_data_from_str(a, data)
                    data = f.read(BUFFER_SIZE)
                    count = len(data)

                f.close()

                _libarchive.archive_entry_free(entry)

            _libarchive.archive_write_close(a)
Exemplo n.º 6
0
    def extract(container, archive, target_path, checked=None):
        target_path = os.path.abspath(target_path).encode('utf8')
        archive_path = s2u(archive.filename)

        # reopen archive file
        archive.denit()
        archive.f.seek(0)
        archive.init()

        a = archive._a
        try:
            if checked:
                logging.info(u"START extract selective '{0}'".format(
                    archive_path
                ))
                while True:
                    try:
                        e = _libarchive.archive_entry_new()
                        r = _libarchive.archive_read_next_header2(a, e)
                        if r != _libarchive.ARCHIVE_OK:
                            break

                        pathname = _libarchive.archive_entry_pathname(e) \
                            .decode('utf8', 'replace')
                        if pathname[-1] == '/':
                            pathname = pathname[:-1]

                        path = s2u(os.path.join(
                            target_path, pathname
                        ))

                        logging.info(u"from '{0}' to '{1}'".format(
                            pathname, path
                        ))

                        if LibArchive.verify(archive_path, pathname, checked):
                            ftype = _libarchive.archive_entry_filetype(e)
                            if stat.S_ISDIR(ftype):
                                makepath(path)
                            else:
                                makepath(os.path.dirname(path))
                                with open(path, 'wb') as f:
                                    _libarchive.archive_read_data_into_fd(
                                        a, f.fileno()
                                    )
                    finally:
                        _libarchive.archive_entry_free(e)

                logging.info(u"END extract selective '{0}'".format(
                    archive_path
                ))

            else:
                logging.info(u"START extract all '{0}'".format(archive_path))
                while True:
                    try:
                        e = _libarchive.archive_entry_new()
                        r = _libarchive.archive_read_next_header2(a, e)
                        if r != _libarchive.ARCHIVE_OK:
                            break
                        pathname = _libarchive.archive_entry_pathname(e) \
                            .decode('utf8', 'replace')
                        path = s2u(os.path.join(target_path, pathname))

                        logging.info(u"from '{0}' to '{1}'".format(
                            pathname, path
                        ))

                        if stat.S_ISDIR(_libarchive.archive_entry_filetype(e)):
                            makepath(path)
                        else:
                            makepath(os.path.dirname(path))
                            with open(path, 'wb') as f:
                                _libarchive.archive_read_data_into_fd(
                                    a, f.fileno()
                                )
                    finally:
                        _libarchive.archive_entry_free(e)
                logging.info(u"END extract all '{0}'".format(archive_path))
        finally:
            archive.close()
Exemplo n.º 7
0
    def extract(container, archive, target_path, checked=None):
        target_path = os.path.abspath(target_path)
        archive_path = s2u(archive.filename)

        # reopen archive file
        archive.denit()
        archive.f.seek(0)
        archive.init()

        a = archive._a
        try:
            if checked:
                logging.info(
                    u"START extract selective '{0}'".format(archive_path))
                while True:
                    try:
                        e = _libarchive.archive_entry_new()
                        r = _libarchive.archive_read_next_header2(a, e)
                        if r != _libarchive.ARCHIVE_OK:
                            break

                        pathname = _libarchive.archive_entry_pathname(e)
                        if pathname[-1] == '/':
                            pathname = pathname[:-1]

                        path = os.path.join(target_path, s2u(pathname))

                        logging.info(u"from '{0}' to '{1}'".format(
                            s2u(pathname), s2u(path)))

                        if LibArchive.verify(archive_path, pathname, checked):
                            ftype = _libarchive.archive_entry_filetype(e)
                            if stat.S_ISDIR(ftype):
                                makepath(path)
                            else:
                                makepath(os.path.dirname(path))
                                with open(path, 'wb') as f:
                                    _libarchive.archive_read_data_into_fd(
                                        a, f.fileno())
                    finally:
                        _libarchive.archive_entry_free(e)

                logging.info(
                    u"END extract selective '{0}'".format(archive_path))

            else:
                logging.info(u"START extract all '{0}'".format(archive_path))
                while True:
                    try:
                        e = _libarchive.archive_entry_new()
                        r = _libarchive.archive_read_next_header2(a, e)
                        if r != _libarchive.ARCHIVE_OK:
                            break
                        pathname = _libarchive.archive_entry_pathname(e)
                        path = os.path.join(target_path, s2u(pathname))

                        logging.info(u"from '{0}' to '{1}'".format(
                            s2u(pathname), s2u(path)))

                        if stat.S_ISDIR(_libarchive.archive_entry_filetype(e)):
                            makepath(path)
                        else:
                            makepath(os.path.dirname(path))
                            with open(u2s(path), 'wb') as f:
                                _libarchive.archive_read_data_into_fd(
                                    a, f.fileno())
                    finally:
                        _libarchive.archive_entry_free(e)
                logging.info(u"END extract all '{0}'".format(archive_path))
        finally:
            archive.close()