import six


def copy_file_to_fs(f, fs, path, encoding=None, errors=None, progress_callback=None, chunk_size=64 * 1024):
    """Copy an open file to a path on an FS"""
    if progress_callback is None:
        progress_callback = lambda bytes_written: None
    read = f.read
    chunk = read(chunk_size)
    if isinstance(chunk, six.text_type):
        f = fs.open(path, 'wt', encoding=encoding, errors=errors)
    else:
        f = fs.open(path, 'wb')
    write = f.write
    bytes_written = 0
    try:
        while chunk:
            write(chunk)
            bytes_written += len(chunk)
            progress_callback(bytes_written)
            chunk = read(chunk_size)
    finally:
        f.close()
    return bytes_written