Jay Q.
Jay Q.

Reputation: 4999

Ruby FileUtils.mv with force flag is not working

I'm not a Ruby guy but I was assigned to modify our build script. The script is trying to move (rename) a file but I'm not sure why I'm getting errors. I have added :force => true which I assume would overwrite the file if it existed. I'm running this script on OSX.

FileUtils.mv(var1, var2, {:force => true, :verbose => true})

Result:

ERROR -- : same file: filename1.txt and Filename1.txt
ERROR -- : ["/opt/local/lib/ruby/1.8/fileutils.rb:1396:in `fu_each_src_dest'",
"/opt/local/lib/ruby/1.8/fileutils.rb:1413:in `fu_each_src_dest0'",
"/opt/local/lib/ruby/1.8/fileutils.rb:1395:in `fu_each_src_dest'", 
"/opt/local/lib/ruby/1.8/fileutils.rb:495:in `mv'"

So I did some digging and found where this error is being thrown:

# File lib/fileutils.rb, line 1512
  def fu_each_src_dest(src, dest)   #:nodoc:
    fu_each_src_dest0(src, dest) do |s, d|
      raise ArgumentError, "same file: #{s} and #{d}" if fu_same?(s, d)
      yield s, d, File.stat(s)
    end
  end

Does this line throw the ArgumentError when if fu_same?(s, d) returns true? Or does this mean the opposite?

UPDATE The problem was the filesystem is NOT case sensitive so it treats both filenames as the "same file". On a side note, OSX filesystem can be set to be case sensitive.

Upvotes: 2

Views: 2454

Answers (1)

Jeremy
Jeremy

Reputation: 4930

You are correct that FileUtils.mv with the :force => true does seem to do what you expect, however I think you're misreading the error message. It seems to be telling you that var1 == var2 whereas var1 should be the existing path, and var2 should be the (different) destination

Upvotes: 4

Related Questions