def rewrite_hashed_files(self, destination_path, files):
        """
        Rewrites files from the actual_path to the correct file inside destination_path.
        """
        if not os.path.isdir(destination_path):
            os.makedirs(destination_path)
        
        for f in files:
            if not f['completed'] and 'postprocessing' in f:
                destination = os.path.join(destination_path, *f['path'])
                
                file_path = os.path.dirname(destination)
                if not os.path.isdir(file_path):
                    logger.debug('Folder %r does not exist, creating' % file_path)
                    os.makedirs(file_path)
    
                logger.debug('Rewriting file from %r to %r' % (f['actual_path'], destination))
                
                _, modification_action, modification_point = f['postprocessing']
                current_size = os.path.getsize(f['actual_path'])
                expected_size = f['length']
                diff = abs(current_size - expected_size)
                
                # write until modification_point, do action, write rest of file
                
                modified = False
                bytes_written = 0
                with open(destination, 'wb') as output_fp:
                    with open(f['actual_path'], 'rb') as input_fp:
                        logger.debug('Opened file %s and writing its data to %s - The breakpoint is %i' % (f['actual_path'], destination, modification_point))
                        while True:
                            if not modified and bytes_written == modification_point:
                                logger.debug('Time to modify with action %s and bytes %i' % (modification_action, diff))
                                modified = True
                                if modification_action == 'remove':
                                    seek_point = bytes_written + diff
                                    logger.debug('Have to shrink compared to original file, seeking to %i' % (seek_point, ))
                                    input_fp.seek(seek_point)
                                elif modification_action == 'add':
                                    logger.debug('Need to add data, writing %i empty bytes' % diff)
                                    while diff > 0:
                                        write_bytes = min(CHUNK_SIZE, diff)
                                        output_fp.write(b'\x00' * write_bytes)
                                        diff -= write_bytes
                            
                            read_bytes = CHUNK_SIZE
                            if not modified:
                                read_bytes = min(read_bytes, modification_point-bytes_written)
                            
                            logger.debug('Reading %i bytes' % (read_bytes, ))
                            data = input_fp.read(read_bytes)
                            if not data:
                                break
                            output_fp.write(data)
                            bytes_written += read_bytes
                logger.debug('Done rewriting file')
dataset/ETHPy150Open JohnDoee/autotorrent/autotorrent/at.py/AutoTorrent.rewrite_hashed_files/original