예제 #1
0
파일: compressed.py 프로젝트: gmoro/carbono
class CompressedReader(GenericReader):

    def __init__(self, path, compressor_level):
        GenericReader.__init__(self, path)
        self.compressor = Compressor(compressor_level)

    def next(self):
        self.open()
        if not self._check_fd():
            header = self._fd.read(self.compressor.get_header_size())
            if not len(header):
                self.close()
                raise StopIteration
            size = self.compressor.read_block_header(header)
            cdata = self._fd.read(size)
            data = self.compressor.extract(cdata)
            return data
예제 #2
0
class CompressedReader(GenericReader):
    def __init__(self, image_path, pattern, volumes, compressor_level,
                 notify_status):
        GenericReader.__init__(self, image_path, pattern, volumes,
                               notify_status)
        self.compressor = Compressor(compressor_level)

    def read_block(self):
        header_size = self.compressor.get_header_size()
        if not self._check_fd():
            header = self._fd.read(header_size)
            if not len(header):
                self.close()
                if self.current_volume < self.volumes:
                    self.current_volume += 1
                    self.open()
                    data = self.read_block()
                    return data
                else:
                    return None
            size = self.compressor.read_block_header(header)
            data = self._fd.read(size)
            return data
예제 #3
0
class CompressedReader(GenericReader):

    def __init__(self, image_path, pattern, volumes,
                 compressor_level, notify_status):
        GenericReader.__init__(self, image_path, pattern,
                               volumes, notify_status)
        self.compressor = Compressor(compressor_level)

    def read_block(self):
        header_size = self.compressor.get_header_size()
        if not self._check_fd():
            header = self._fd.read(header_size)
            if not len(header):
                self.close()
                if self.current_volume < self.volumes:
                    self.current_volume += 1
                    self.open()
                    data = self.read_block()
                    return data
                else:
                    return None
            size = self.compressor.read_block_header(header)
            data = self._fd.read(size)
            return data
예제 #4
0
    def restore_image(self):
        """ """
        information = Information(self.image_path)
        information.load()
        image_name = information.get_image_name()
        compressor_level = information.get_image_compressor_level()
        total_bytes = information.get_image_total_bytes()
        total_blocks = long(math.ceil(total_bytes / float(BLOCK_SIZE)))

        compressor = Compressor(compressor_level)

        device = Device(self.target_device)

        if device.is_disk() != \
           information.get_image_is_disk():
            raise ErrorRestoringImage("Invalid target dispositive")

        try:
            disk = Disk(device)
        except _ped.DiskLabelException:
            try:
                device.fix_disk_label()
                disk = Disk(device)
            except:
                raise ErrorRestoringImage("Unrecognized disk label")

        if information.get_image_is_disk():
            mbr = Mbr(self.image_path)
            mbr.restore_from_file(self.target_device)
            dlm = DiskLayoutManager(self.image_path)
            dlm.restore_from_file(disk)

        self._print_informations(total_bytes, image_name)
        progress = Progress(total_blocks)
        progress.start()
        partitions = information.get_partitions()
        for p in partitions:
            if information.get_image_is_disk():
                partition = disk.get_partition_by_number(p.number, p.type)
            else:
                parent_path = get_parent_path(self.target_device)
                parent_device = Device(parent_path)
                parent_disk = Disk(parent_device)
                partition = parent_disk.get_partition_by_path(
                    self.target_device, p.type)

            if partition is None:
                raise ErrorRestoringImage("No valid partitions found")

            if p.uuid is not None:
                partition.filesystem.open_to_write(p.uuid)
            else:
                partition.filesystem.open_to_write()

            if partition.filesystem.is_swap():
                continue

            file_name = FILE_PATTERN % (image_name, p.number)
            with open(self.image_path + file_name, 'rb') as f:
                # TODO: Work with parallelism
                while True:  # Ugly
                    header = f.read(compressor.get_header_size())
                    if not len(header):
                        break
                    size = compressor.read_block_header(header)
                    cdata = f.read(size)
                    data = compressor.extract(cdata)
                    partition.filesystem.write(data)
                    progress.increment(1)
            partition.filesystem.close()

        progress.stop()
        print "completed."
예제 #5
0
    def restore_image(self):
        """ """
        information = Information(self.image_path)
        information.load()
        image_name = information.get_image_name()
        compressor_level = information.get_image_compressor_level()
        total_bytes = information.get_image_total_bytes()
        total_blocks = long(math.ceil(total_bytes/float(BLOCK_SIZE)))

        compressor = Compressor(compressor_level)

        device = Device(self.target_device)

        if device.is_disk() != \
           information.get_image_is_disk():
            raise ErrorRestoringImage("Invalid target dispositive")

        try:
            disk = Disk(device)
        except _ped.DiskLabelException:
            try:
                device.fix_disk_label()
                disk = Disk(device)
            except:
                raise ErrorRestoringImage("Unrecognized disk label")

        if information.get_image_is_disk():
            mbr = Mbr(self.image_path)
            mbr.restore_from_file(self.target_device)
            dlm = DiskLayoutManager(self.image_path)
            dlm.restore_from_file(disk)

        self._print_informations(total_bytes, image_name)
        progress = Progress(total_blocks)
        progress.start()
        partitions = information.get_partitions()
        for p in partitions:
            if information.get_image_is_disk():
                partition = disk.get_partition_by_number(p.number, p.type)
            else:
                parent_path = get_parent_path(self.target_device)
                parent_device = Device(parent_path)
                parent_disk = Disk(parent_device)
                partition = parent_disk.get_partition_by_path(
                                            self.target_device,
                                            p.type)

            if partition is None:
                raise ErrorRestoringImage("No valid partitions found")

            if p.uuid is not None:
                partition.filesystem.open_to_write(p.uuid)
            else:
                partition.filesystem.open_to_write()

            if partition.filesystem.is_swap():
                continue

            file_name = FILE_PATTERN % (image_name, p.number)
            with open(self.image_path + file_name, 'rb') as f:
                # TODO: Work with parallelism
                while True: # Ugly
                    header = f.read(compressor.get_header_size())
                    if not len(header):
                        break
                    size = compressor.read_block_header(header)
                    cdata = f.read(size)
                    data = compressor.extract(cdata)
                    partition.filesystem.write(data)
                    progress.increment(1)
            partition.filesystem.close()

        progress.stop()
        print "completed."