def unserialize_blocks(file): blocks = [] while True: digest = file.read(Digest.dataDigestSize()) if digest == "": break size = Format.read_int(file) code = Format.read_int(file) blocks.append((digest, size, code)) return blocks
def _load_header(self, header_file): logging.debug("****************************** loading header") magic = header_file.read(len(MAGIC)) if MAGIC != magic: raise Exception("Manent: magic number not found") version = Format.read_int(header_file) if version != VERSION: raise Exception("Container %d has unsupported version" % self.index) index = Format.read_int(header_file) if index != self.index: raise Exception( "Manent: wrong container file index. Expected %s, found %s" % (str(self.index), str(index))) header_table_size = Format.read_int(header_file) header_table_digest = header_file.read(Digest.dataDigestSize()) header_table_str = header_file.read(header_table_size) if Digest.dataDigest(header_table_str) != header_table_digest: raise Exception("Manent: header of container file corrupted") header_dump_len = Format.read_int(header_file) header_dump_str = header_file.read(header_dump_len) header_table_io = StringIO.StringIO(header_table_str) header_blocks = unserialize_blocks(header_table_io) class BlockTableListener: def __init__(self): self.body_table_str = None def is_requested(self, digest, code): return code == CODE_BLOCK_TABLE def loaded(self, digest, code, data): assert code == CODE_BLOCK_TABLE self.body_table_str = data listener = BlockTableListener() header_dump_str_io = StringIO.StringIO(header_dump_str) header_dump_loader = DataDumpLoader(header_dump_str_io, header_blocks, password=self.storage.get_encryption_key()) header_dump_loader.load_blocks(listener) body_table_io = StringIO.StringIO(listener.body_table_str) blocks = unserialize_blocks(body_table_io) return blocks
def finish_dump(self): if self.compression_active: self.body_dumper.stop_compression() self.compression_active = False if self.encryption_active: self.body_dumper.stop_encryption() # # Serialize the body block table # body_table_io = StringIO.StringIO() body_blocks = self.body_dumper.get_blocks() serialize_blocks(body_table_io, body_blocks) body_table_str = body_table_io.getvalue() # # Serialize the header table # message = "Manent container %d of sequence '%s'" % ( self.index, base64.urlsafe_b64encode(self.sequence_id)) self.header_dumper.add_block(Digest.dataDigest(message), CODE_CONTAINER_DESCRIPTOR, message) self.header_dumper.add_block(Digest.dataDigest(body_table_str), CODE_BLOCK_TABLE, body_table_str) if self.encryption_active: self.header_dumper.stop_encryption() self.encryption_active = False header_blocks = self.header_dumper.get_blocks() header_table_io = StringIO.StringIO() serialize_blocks(header_table_io, header_blocks) header_table_str = header_table_io.getvalue() # # Write the header # assert self.header_file.tell() == 0 self.header_file.write(MAGIC) Format.write_int(self.header_file, VERSION) Format.write_int(self.header_file, self.index) Format.write_int(self.header_file, len(header_table_str)) self.header_file.write(Digest.dataDigest(header_table_str)) self.header_file.write(header_table_str) header_dump_str = self.header_dump_os.getvalue() Format.write_int(self.header_file, len(header_dump_str)) self.header_file.write(header_dump_str) logging.debug("Container %d has header of size %d" % (self.index, self.header_file.tell()))
def serialize_blocks(file, blocks): for (digest,size,code) in blocks: file.write(digest) Format.write_int(file,size) Format.write_int(file,code)