def write(self, src, ip, container, storage_medium, block_size=DEFAULT_BLOCK_SIZE): if isinstance(src, str): src = [src] dst = storage_medium.storage_target.target logger.debug('Writing {src} to {dst}'.format(src=', '.join(src), dst=dst)) if not os.path.isdir(dst): msg = "{dst} is not a directory".format(dst=dst) logger.error(msg) raise ValueError(msg) if not container: dst = os.path.join(dst, ip.object_identifier_value) for idx, f in enumerate(src): new = copy(f, dst, block_size=block_size) if idx == 0: content_location_value = new return StorageObject.objects.create( content_location_value=content_location_value, content_location_type=DISK, ip=ip, storage_medium=storage_medium, container=container, )
def read(self, storage_object, dst, extract=False, include_xml=True, block_size=65536): medium = storage_object.storage_medium target = medium.storage_target ip = storage_object.ip src = storage_object.get_full_path() if storage_object.container: src_tar = src src_xml = os.path.splitext(src)[0] + '.xml' src_aic_xml = os.path.join(target.target, str(ip.aic.pk)) + '.xml' if include_xml: copy(src_xml, dst, block_size=block_size) copy(src_aic_xml, dst, block_size=block_size) if extract: return self._extract(storage_object, dst) else: return copy(src_tar, dst, block_size=block_size) else: return copy(src, dst, block_size=block_size)
def read(self, storage_object, dst, extract=False, include_xml=True, block_size=DEFAULT_TAPE_BLOCK_SIZE): tape_pos = int(storage_object.content_location_value) medium = storage_object.storage_medium ip = storage_object.ip block_size = medium.block_size * 512 # TODO: Create temp dir inside configured temp directory tmp_path = tempfile.mkdtemp() try: drive = TapeDrive.objects.get(storage_medium=medium) except TapeDrive.DoesNotExist: raise ValueError("Tape not mounted") set_tape_file_number(drive.device, tape_pos) read_tape(drive.device, path=tmp_path, block_size=block_size) drive.last_change = timezone.now() drive.save(update_fields=['last_change']) src = os.path.join(tmp_path, ip.object_identifier_value) if storage_object.container: src_tar = src + '.tar' src_xml = src + '.xml' src_aic_xml = os.path.join(tmp_path, str(ip.aic.pk)) + '.xml' if include_xml: copy(src_xml, dst, block_size=block_size) copy(src_aic_xml, dst, block_size=block_size) if extract: with tarfile.open(src_tar) as t: root = os.path.commonprefix(t.getnames()) t.extractall(dst) new = os.path.join(dst, root) else: new = copy(src_tar, dst, block_size=block_size) else: new = copy(src, dst, block_size=block_size) try: shutil.rmtree(tmp_path) except OSError as e: if e.errno != errno.ENOENT: raise return new