def atomic_write(filename, content, overwrite=True, permissions=0o0644, encoding='utf-8'):
    '''
    Write a file atomically by writing the file content to a
    temporary location first, then renaming the file. 
    
    TODO: this relies pretty heavily on os.rename to ensure atomicity, but 
    os.rename does not silently overwrite files that already exist on 
    Windows natively. For now, the functionality provided here can only be 
    supported under Windows Subsystem for Linux on Windows 10 version 1607 
    and later.

    :param filename: Filename
    :type filename: str
    :param content: File content
    :type content: str
    :param overwrite: Overwrite
    :type overwrite: bool
    :param permissions: Octal permissions
    :type permissions: octal
    '''
    filename = os.path.expanduser(filename)
    if not overwrite and os.path.exists(filename):
        raise WriteError('file already exists: {0}'.format(filename))
    dirname = os.path.dirname(filename)
    with tf.NamedTemporaryFile(dir=dirname, prefix='.', delete=False) as tmp:
        if isinstance(content, six.string_types):
            tmp.write(content.decode(encoding))
        else:
            tmp.write(content)
    os.chmod(tmp.name, permissions)
    os.rename(tmp.name, filename)