コード例 #1
0
ファイル: containers.py プロジェクト: pombredanne/tarman
    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)
コード例 #2
0
ファイル: containers.py プロジェクト: rongmu/tarman
    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)
コード例 #3
0
    def from_archive(cls, archive, encoding=ENCODING):
        '''Instantiates an Entry class and sets all the properties from an archive header.'''
        e = _libarchive.archive_entry_new()
        try:
            call_and_check(_libarchive.archive_read_next_header2, archive._a,
                           archive._a, e)
            mode = _libarchive.archive_entry_filetype(e)
            mode |= _libarchive.archive_entry_perm(e)

            if PY3:
                pathname = _libarchive.archive_entry_pathname(e)
            else:
                pathname = _libarchive.archive_entry_pathname(e).decode(
                    encoding)

            entry = cls(
                pathname=pathname,
                size=_libarchive.archive_entry_size(e),
                mtime=_libarchive.archive_entry_mtime(e),
                mode=mode,
                hpos=archive.header_position,
            )
        finally:
            _libarchive.archive_entry_free(e)
        return entry
コード例 #4
0
 def to_archive(self, archive):
     '''Creates an archive header and writes it to the given archive.'''
     e = _libarchive.archive_entry_new()
     try:
         _libarchive.archive_entry_set_pathname(e, self.pathname.encode(self.encoding))
         _libarchive.archive_entry_set_filetype(e, stat.S_IFMT(self.mode))
         _libarchive.archive_entry_set_perm(e, stat.S_IMODE(self.mode))
         _libarchive.archive_entry_set_size(e, self.size)
         _libarchive.archive_entry_set_mtime(e, self.mtime, 0)
         call_and_check(_libarchive.archive_write_header, archive._a, archive._a, e)
         #self.hpos = archive.header_position
     finally:
         _libarchive.archive_entry_free(e)
コード例 #5
0
 def to_archive(self, archive):
     '''Creates an archive header and writes it to the given archive.'''
     e = _libarchive.archive_entry_new()
     try:
         _libarchive.archive_entry_set_pathname(e, self.pathname.encode(self.encoding))
         _libarchive.archive_entry_set_filetype(e, stat.S_IFMT(self.mode))
         _libarchive.archive_entry_set_perm(e, stat.S_IMODE(self.mode))
         _libarchive.archive_entry_set_size(e, self.size)
         _libarchive.archive_entry_set_mtime(e, self.mtime, 0)
         call_and_check(_libarchive.archive_write_header, archive._a, archive._a, e)
         #self.hpos = archive.header_position
     finally:
         _libarchive.archive_entry_free(e)
コード例 #6
0
 def from_archive(cls, archive, encoding=ENCODING):
     '''Instantiates an Entry class and sets all the properties from an archive header.'''
     e = _libarchive.archive_entry_new()
     try:
         call_and_check(_libarchive.archive_read_next_header2, archive._a, archive._a, e)
         mode = _libarchive.archive_entry_filetype(e)
         mode |= _libarchive.archive_entry_perm(e)
         entry = cls(
             pathname=_libarchive.archive_entry_pathname(e),
             size=_libarchive.archive_entry_size(e),
             mtime=_libarchive.archive_entry_mtime(e),
             mode=mode,
             hpos=archive.header_position,
         )
     finally:
         _libarchive.archive_entry_free(e)
     return entry
コード例 #7
0
ファイル: containers.py プロジェクト: pombredanne/tarman
    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)
コード例 #8
0
ファイル: containers.py プロジェクト: rongmu/tarman
    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(u2s(archivepath))
        BUFFER_SIZE = 2048

        with libarchive.Archive(archivepath, 'w') as larchive:
            a = larchive._a
            for node in checked:
                if node.is_dir():
                    continue
                path = u2s(node.get_path())
                pathname = u2s(os.path.join(*node.get_data_array()[1:]))
                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)
コード例 #9
0
ファイル: containers.py プロジェクト: pombredanne/tarman
    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()
コード例 #10
0
ファイル: containers.py プロジェクト: rongmu/tarman
    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()