def _sanitize_dirnames(filename, restore=False):
    """
    Remove (or add) leading _ in the directories names in `filename`
    The `restore` param means that the path name should be restored from the queryname,
    i.e. conversion should be done in the opposite direction
    """
    parts = filename.split('/')
    newparts = []
    for part in parts[:-1]:
        if restore:
            newparts.append('_'+part)
            continue
        if part.startswith('_'):
            newparts.append(part[1:])
        else:
            newparts.append(part)
    newparts.append(parts[-1])

    return "/".join(newparts)