@storage_format_upgrade('Convert inode blocks list to treetree',
                       upgrade_from={'inode_format': None},
                       upgrade_to={'inode_format': 'treetree'})
def convert_fs_to_treetree_inodes(eg):
    """
    Convert an existing filesystem from the "inode with flat list of blocks"
    format to the "inode with treetree of blocks" format.
    """

    inode_index = eg.root['inodes']

    class DummyStorage(object):
        def _autocommit(self): pass
    s = DummyStorage()

    for inode_name in inode_index:
        upgrade_log.debug('Reorganizing inode %r', inode_name)
        inode = StorageInode(inode_name, inode_index[inode_name], s)

        block_offsets = set()
        for old_block_name in inode.tree:
            if old_block_name.startswith('b'):
                if not old_block_name.startswith('bt'):
                    block_offsets.add(int(old_block_name[1:]))

        for block_offset in sorted(block_offsets):
            old_block_name = 'b%d' % block_offset
            new_block_name = str(block_offset / StorageInode.blocksize)
            old_block = inode.tree[old_block_name]
            new_block = inode.tt.clone(old_block, new_block_name)
            del inode.tree[old_block_name]

        inode['size'] = block_offset + len(new_block.data)
        inode.tree._commit()
dataset/ETHPy150Open mgax/SpaghettiFS/spaghettifs/storage.py/convert_fs_to_treetree_inodes/original