def read(self, name): metadata = self.read_metadata(name) data = self._entries[name].data if bytestr_hash(data) != metadata['md5']: raise IntegrityError('Checksum') return data
def write(self, name, data, metadata=None, md5=None): if md5 is None: md5 = bytestr_hash(data) if metadata is None: metadata = {} self._entries[name] = MemoryStorageItem(md5, data, metadata)
def _read_firmware(fw_data): with open(fw_data['file'], "rb") as f: firmware_binary = f.read() if len(firmware_binary) != fw_data['length']: print('Firmware file length check failed, aborting') raise IntegrityError("Firmware file length does not match") checksum = bytestr_hash(firmware_binary) if checksum != fw_data['md5']: print('Firmware file integrity check failed, aborting') raise IntegrityError("Firmware file checksum does not match") return firmware_binary
def write(self, filename, data, metadata=None, md5=None): if md5 is None: md5 = bytestr_hash(data) if metadata is None: metadata = {} metadata["md5"] = md5 metadata["length"] = len(data) with open(self._storage_file(filename), "wb") as data_file, open(self._meta_file(filename), "w") as meta_file: data_file.write(data) json.dump(metadata, meta_file)
def read(self, filename): try: data_file_path = self._storage_file(filename) meta_file_path = self._meta_file(filename) with open(data_file_path, "rb") as data_file, open(meta_file_path, "r") as meta_file: metadata = json.load(meta_file) data = data_file.read() if len(data) != metadata['length']: raise IntegrityError('Length') if bytestr_hash(data) != metadata['md5']: raise IntegrityError('Checksum') return data except IOError: raise StorageElementNotFoundError except JSONDecodeError: raise IntegrityError('Metadata')