Example #1
0
    def do_extract(self, args):
        """Extract archive contents
        """
        repository = self.open_repository(args.archive)
        manifest, key = Manifest.load(repository)
        archive = Archive(repository,
                          key,
                          manifest,
                          args.archive.archive,
                          numeric_owner=args.numeric_owner)
        patterns = adjust_patterns(args.paths, args.excludes)
        dirs = []
        for item, peek in archive.iter_items(
                lambda item: not exclude_path(item[b'path'], patterns)):
            while dirs and not item[b'path'].startswith(dirs[-1][b'path']):
                archive.extract_item(dirs.pop(-1))
            self.print_verbose(remove_surrogates(item[b'path']))
            try:
                if stat.S_ISDIR(item[b'mode']):
                    dirs.append(item)
                    archive.extract_item(item, restore_attrs=False)
                else:
                    archive.extract_item(item, peek=peek)
            except IOError as e:
                self.print_error('%s: %s', remove_surrogates(item[b'path']), e)

        while dirs:
            archive.extract_item(dirs.pop(-1))
        return self.exit_code
Example #2
0
    def do_extract(self, args):
        """Extract archive contents
        """
        repository = self.open_repository(args.archive)
        manifest, key = Manifest.load(repository)
        archive = Archive(repository, key, manifest, args.archive.archive,
                          numeric_owner=args.numeric_owner)
        patterns = adjust_patterns(args.paths, args.excludes)
        dirs = []
        for item, peek in archive.iter_items(lambda item: not exclude_path(item[b'path'], patterns)):
            while dirs and not item[b'path'].startswith(dirs[-1][b'path']):
                archive.extract_item(dirs.pop(-1))
            self.print_verbose(remove_surrogates(item[b'path']))
            try:
                if stat.S_ISDIR(item[b'mode']):
                    dirs.append(item)
                    archive.extract_item(item, restore_attrs=False)
                else:
                    archive.extract_item(item, peek=peek)
            except IOError as e:
                self.print_error('%s: %s', remove_surrogates(item[b'path']), e)

        while dirs:
            archive.extract_item(dirs.pop(-1))
        return self.exit_code
Example #3
0
    def do_verify(self, args):
        """Verify archive consistency
        """
        repository = self.open_repository(args.archive)
        manifest, key = Manifest.load(repository)
        archive = Archive(repository, key, manifest, args.archive.archive)
        patterns = adjust_patterns(args.paths, args.excludes)

        def start_cb(item):
            self.print_verbose('%s ...',
                               remove_surrogates(item[b'path']),
                               newline=False)

        def result_cb(item, success):
            if success:
                self.print_verbose('OK')
            else:
                self.print_verbose('ERROR')
                self.print_error('%s: verification failed' %
                                 remove_surrogates(item[b'path']))

        for item, peek in archive.iter_items(
                lambda item: not exclude_path(item[b'path'], patterns)):
            if stat.S_ISREG(item[b'mode']) and b'chunks' in item:
                archive.verify_file(item, start_cb, result_cb, peek=peek)
        return self.exit_code
Example #4
0
 def _process(self, archive, cache, excludes, exclude_caches, skip_inodes, path, restrict_dev):
     if exclude_path(path, excludes):
         return
     try:
         st = os.lstat(path)
     except OSError as e:
         self.print_error('%s: %s', path, e)
         return
     if (st.st_ino, st.st_dev) in skip_inodes:
         return
     # Entering a new filesystem?
     if restrict_dev and st.st_dev != restrict_dev:
         return
     # Ignore unix sockets
     if stat.S_ISSOCK(st.st_mode):
         return
     status = None
     if stat.S_ISREG(st.st_mode):
         try:
             status = archive.process_file(path, st, cache)
         except IOError as e:
             self.print_error('%s: %s', path, e)
     elif stat.S_ISDIR(st.st_mode):
         if exclude_caches and is_cachedir(path):
             return
         status = archive.process_dir(path, st)
         try:
             entries = os.listdir(path)
         except OSError as e:
             self.print_error('%s: %s', path, e)
         else:
             for filename in sorted(entries):
                 entry_path = os.path.normpath(os.path.join(path, filename))
                 self._process(archive, cache, excludes, exclude_caches, skip_inodes,
                               entry_path, restrict_dev)
     elif stat.S_ISLNK(st.st_mode):
         status = archive.process_symlink(path, st)
     elif stat.S_ISFIFO(st.st_mode):
         status = archive.process_fifo(path, st)
     elif stat.S_ISCHR(st.st_mode) or stat.S_ISBLK(st.st_mode):
         status = archive.process_dev(path, st)
     else:
         self.print_error('Unknown file type: %s', path)
         return
     # Status output
     # A lowercase character means a file type other than a regular file,
     # borg usually just stores them. E.g. (d)irectory.
     # Hardlinks to already seen content are indicated by (h).
     # A uppercase character means a regular file that was (A)dded,
     # (M)odified or was (U)nchanged.
     # Note: A/M/U is relative to the "files" cache, not to the repo.
     # This would be an issue if the files cache is not used.
     if status is None:
         status = '?'  # need to add a status code somewhere
     # output ALL the stuff - it can be easily filtered using grep.
     # even stuff considered unchanged might be interesting.
     self.print_verbose("%1s %s", status, remove_surrogates(path))
Example #5
0
    def do_extract(self, args):
        """Extract archive contents"""
        # be restrictive when restoring files, restore permissions later
        if sys.getfilesystemencoding() == 'ascii':
            print(
                'Warning: File system encoding is "ascii", extracting non-ascii filenames will not be supported.'
            )
        os.umask(0o077)
        repository = self.open_repository(args.archive)
        manifest, key = Manifest.load(repository)
        archive = Archive(repository,
                          key,
                          manifest,
                          args.archive.archive,
                          numeric_owner=args.numeric_owner)
        patterns = adjust_include_patterns(args.paths, args.excludes)
        dry_run = args.dry_run
        strip_components = args.strip_components
        dirs = []
        for item in archive.iter_items(
                lambda item: not exclude_path(item[b'path'], patterns),
                preload=True):
            orig_path = item[b'path']
            if strip_components:
                item[b'path'] = os.sep.join(
                    orig_path.split(os.sep)[strip_components:])
                if not item[b'path']:
                    continue
            if not args.dry_run:
                while dirs and not item[b'path'].startswith(dirs[-1][b'path']):
                    archive.extract_item(dirs.pop(-1))
            self.print_verbose(remove_surrogates(orig_path))
            try:
                if dry_run:
                    archive.extract_item(item, dry_run=True)
                else:
                    if stat.S_ISDIR(item[b'mode']):
                        dirs.append(item)
                        archive.extract_item(item, restore_attrs=False)
                    else:
                        archive.extract_item(item)
            except IOError as e:
                self.print_error('%s: %s', remove_surrogates(orig_path), e)

        if not args.dry_run:
            while dirs:
                archive.extract_item(dirs.pop(-1))
        return self.exit_code
Example #6
0
 def _process(self, archive, cache, excludes, exclude_caches, skip_inodes,
              path, restrict_dev):
     if exclude_path(path, excludes):
         return
     try:
         st = os.lstat(path)
     except OSError as e:
         self.print_error('%s: %s', path, e)
         return
     if (st.st_ino, st.st_dev) in skip_inodes:
         return
     # Entering a new filesystem?
     if restrict_dev and st.st_dev != restrict_dev:
         return
     # Ignore unix sockets
     if stat.S_ISSOCK(st.st_mode):
         return
     # Ignore if nodump flag set
     if has_lchflags and (st.st_flags & stat.UF_NODUMP):
         return
     self.print_verbose(remove_surrogates(path))
     if stat.S_ISREG(st.st_mode):
         try:
             archive.process_file(path, st, cache)
         except IOError as e:
             self.print_error('%s: %s', path, e)
     elif stat.S_ISDIR(st.st_mode):
         if exclude_caches and is_cachedir(path):
             return
         archive.process_item(path, st)
         try:
             entries = os.listdir(path)
         except OSError as e:
             self.print_error('%s: %s', path, e)
         else:
             for filename in sorted(entries):
                 self._process(archive, cache, excludes,
                               exclude_caches, skip_inodes,
                               os.path.join(path, filename), restrict_dev)
     elif stat.S_ISLNK(st.st_mode):
         archive.process_symlink(path, st)
     elif stat.S_ISFIFO(st.st_mode):
         archive.process_item(path, st)
     elif stat.S_ISCHR(st.st_mode) or stat.S_ISBLK(st.st_mode):
         archive.process_dev(path, st)
     else:
         self.print_error('Unknown file type: %s', path)
 def _process(self, archive, cache, excludes, exclude_caches, skip_inodes, path, restrict_dev):
     if exclude_path(path, excludes):
         return
     try:
         st = os.lstat(path)
     except OSError as e:
         self.print_error('%s: %s', path, e)
         return
     if (st.st_ino, st.st_dev) in skip_inodes:
         return
     # Entering a new filesystem?
     if restrict_dev and st.st_dev != restrict_dev:
         return
     # Ignore unix sockets
     if stat.S_ISSOCK(st.st_mode):
         return
     self.print_verbose(remove_surrogates(path))
     if stat.S_ISREG(st.st_mode):
         try:
             archive.process_file(path, st, cache)
         except IOError as e:
             self.print_error('%s: %s', path, e)
     elif stat.S_ISDIR(st.st_mode):
         if exclude_caches and is_cachedir(path):
             return
         archive.process_item(path, st)
         try:
             entries = os.listdir(path)
         except OSError as e:
             self.print_error('%s: %s', path, e)
         else:
             for filename in sorted(entries):
                 self._process(archive, cache, excludes, exclude_caches, skip_inodes,
                               os.path.join(path, filename), restrict_dev)
     elif stat.S_ISLNK(st.st_mode):
         archive.process_symlink(path, st)
     elif stat.S_ISFIFO(st.st_mode):
         archive.process_item(path, st)
     elif stat.S_ISCHR(st.st_mode) or stat.S_ISBLK(st.st_mode):
         archive.process_dev(path, st)
     else:
         self.print_error('Unknown file type: %s', path)
Example #8
0
    def do_extract(self, args):
        """Extract archive contents"""
        repository = self.open_repository(args.archive)
        manifest, key = Manifest.load(repository)
        archive = Archive(repository,
                          key,
                          manifest,
                          args.archive.archive,
                          numeric_owner=args.numeric_owner)
        patterns = adjust_patterns(args.paths, args.excludes)
        dry_run = args.dry_run
        strip_components = args.strip_components
        dirs = []
        for item in archive.iter_items(
                lambda item: not exclude_path(item[b'path'], patterns),
                preload=True):
            orig_path = item[b'path']
            if strip_components:
                item[b'path'] = os.sep.join(
                    orig_path.split(os.sep)[strip_components:])
                if not item[b'path']:
                    continue
            if not args.dry_run:
                while dirs and not item[b'path'].startswith(dirs[-1][b'path']):
                    archive.extract_item(dirs.pop(-1))
            self.print_verbose(remove_surrogates(orig_path))
            try:
                if dry_run:
                    archive.extract_item(item, dry_run=True)
                else:
                    if stat.S_ISDIR(item[b'mode']):
                        dirs.append(item)
                        archive.extract_item(item, restore_attrs=False)
                    else:
                        archive.extract_item(item)
            except IOError as e:
                self.print_error('%s: %s', remove_surrogates(orig_path), e)

        if not args.dry_run:
            while dirs:
                archive.extract_item(dirs.pop(-1))
        return self.exit_code
Example #9
0
    def do_extract(self, args):
        """Extract archive contents"""
        # be restrictive when restoring files, restore permissions later
        if sys.getfilesystemencoding() == 'ascii':
            print('Warning: File system encoding is "ascii", extracting non-ascii filenames will not be supported.')
        os.umask(0o077)
        repository = self.open_repository(args.archive)
        manifest, key = Manifest.load(repository)
        archive = Archive(repository, key, manifest, args.archive.archive,
                          numeric_owner=args.numeric_owner)
        patterns = adjust_patterns(args.paths, args.excludes)
        dry_run = args.dry_run
        stdout = args.stdout
        sparse = args.sparse
        strip_components = args.strip_components
        dirs = []
        for item in archive.iter_items(lambda item: not exclude_path(item[b'path'], patterns), preload=True):
            orig_path = item[b'path']
            if strip_components:
                item[b'path'] = os.sep.join(orig_path.split(os.sep)[strip_components:])
                if not item[b'path']:
                    continue
            if not args.dry_run:
                while dirs and not item[b'path'].startswith(dirs[-1][b'path']):
                    archive.extract_item(dirs.pop(-1), stdout=stdout)
            self.print_verbose(remove_surrogates(orig_path))
            try:
                if dry_run:
                    archive.extract_item(item, dry_run=True)
                else:
                    if stat.S_ISDIR(item[b'mode']):
                        dirs.append(item)
                        archive.extract_item(item, restore_attrs=False)
                    else:
                        archive.extract_item(item, stdout=stdout, sparse=sparse)
            except IOError as e:
                self.print_error('%s: %s', remove_surrogates(orig_path), e)

        if not args.dry_run:
            while dirs:
                archive.extract_item(dirs.pop(-1))
        return self.exit_code
Example #10
0
    def do_verify(self, args):
        """Verify archive consistency
        """
        repository = self.open_repository(args.archive)
        manifest, key = Manifest.load(repository)
        archive = Archive(repository, key, manifest, args.archive.archive)
        patterns = adjust_patterns(args.paths, args.excludes)

        def start_cb(item):
            self.print_verbose('%s ...', remove_surrogates(item[b'path']), newline=False)

        def result_cb(item, success):
            if success:
                self.print_verbose('OK')
            else:
                self.print_verbose('ERROR')
                self.print_error('%s: verification failed' % remove_surrogates(item[b'path']))
        for item, peek in archive.iter_items(lambda item: not exclude_path(item[b'path'], patterns)):
            if stat.S_ISREG(item[b'mode']) and b'chunks' in item:
                archive.verify_file(item, start_cb, result_cb, peek=peek)
        return self.exit_code
Example #11
0
    def do_extract(self, args):
        """Extract archive contents"""
        repository = self.open_repository(args.archive)
        manifest, key = Manifest.load(repository)
        archive = Archive(repository, key, manifest, args.archive.archive,
                          numeric_owner=args.numeric_owner)
        patterns = adjust_patterns(args.paths, args.excludes)
        dry_run = args.dry_run
        strip_components = args.strip_components
        dirs = []
        for item in archive.iter_items(lambda item: not exclude_path(item[b'path'], patterns), preload=True):
            orig_path = item[b'path']
            if strip_components:
                item[b'path'] = os.sep.join(orig_path.split(os.sep)[strip_components:])
                if not item[b'path']:
                    continue
            if not args.dry_run:
                while dirs and not item[b'path'].startswith(dirs[-1][b'path']):
                    archive.extract_item(dirs.pop(-1))
            self.print_verbose(remove_surrogates(orig_path))
            try:
                if dry_run:
                    archive.extract_item(item, dry_run=True)
                else:
                    if stat.S_ISDIR(item[b'mode']):
                        dirs.append(item)
                        archive.extract_item(item, restore_attrs=False)
                    else:
                        archive.extract_item(item)
            except IOError as e:
                self.print_error('%s: %s', remove_surrogates(orig_path), e)

        if not args.dry_run:
            while dirs:
                archive.extract_item(dirs.pop(-1))
        return self.exit_code
Example #12
0
 def evaluate(self, paths, excludes):
     patterns = adjust_patterns(paths, [ExcludePattern(p) for p in excludes])
     return [path for path in self.files if not exclude_path(path, patterns)]
Example #13
0
 def evaluate(self, paths, excludes):
     patterns = adjust_patterns(paths,
                                [ExcludePattern(p) for p in excludes])
     return [
         path for path in self.files if not exclude_path(path, patterns)
     ]