The Crystal Programming Language Forum

File.copy("afile", "afile") clobbers "afile"

Does anyone else think a friendlier default would be to leave the file intact, without clobbering its contents? Suggestion:

  def File.copy(src : String | Path, dst : String | Path, preserve = true)
    return if preserve && (src.to_s == dst.to_s)
    open(src) do |s|
      open(dst, "wb") do |d|
        # TODO use sendfile or copy_file_range syscall. See #8926, #8919
        IO.copy(s, d)
      end

      # Set the permissions after the content is written in case src permissions is read-only
      chmod(dst, s.info.permissions)
    end
  end

In Ruby it throws an error:

ArgumentError (same file: bar.cr and bar.cr)

We should do the same thing.

6 Likes