Sha256: 0cd820e551b8b75f1e7e0b115c16a2932dc30ad3617b69c964e1017eb4d88057

Contents?: true

Size: 1.75 KB

Versions: 11

Compression:

Stored size: 1.75 KB

Contents

module FileUtils::FastCopy
  # fast_copy a file: This hardlinks the source file to the destfile,
  # if possible. src must refer to a file; dest must refer to a file
  # or not exist at all.
  def fast_copy(src, dest)
    src_stat = File.stat(src)
    invalid_argument!(src) unless src_stat.file?
    
    dest_stat = begin
      File.stat(dest) 
    rescue Errno::ENOENT
    end
    
    invalid_argument!(dest) unless !dest_stat || dest_stat.file?
    
    dest_dev = dest_stat ? dest_stat.dev : begin
      dest_dir = File.dirname(File.expand_path(dest))
      File.stat(dest_dir).dev
    end

    if src_stat.dev == dest_dev
      File.unlink(dest) if File.exists?(dest)
      File.link src, dest
    else
      FileUtils.copy src, dest
    end
  end
end

FileUtils.extend FileUtils::FastCopy

module FileUtils::FastCopy::Etest
  def test_fast_copy
    assert File.exist?(__FILE__)
    File.unlink("tmp/somedata.dat") if File.exist?("tmp/somedata.dat")

    assert !File.exist?("tmp/somedata.dat")
    FileUtils.fast_copy __FILE__, "tmp/somedata.dat"
    assert_equal File.read("tmp/somedata.dat"), File.read(__FILE__)

    File.unlink("tmp/somedata.dat")
    File.touch("tmp/somedata.dat")
    assert_not_equal File.size("tmp/somedata.dat"), File.size(__FILE__)
    FileUtils.fast_copy __FILE__, "tmp/somedata.dat"
    assert_equal File.size("tmp/somedata.dat"), File.size(__FILE__)
  end

  def test_fast_copy_failures
    assert_raise(Errno::ENOENT) {  
      FileUtils.fast_copy "fixtures/somedata.dat.nonexisting", "tmp/somedata.dat"
    }

    assert_raise(Errno::ENOENT) {  
      FileUtils.fast_copy "fixtures/somedata.dat.nonexisting", "tmp"
    }

    assert_raise(Errno::ENOENT) {  
      FileUtils.fast_copy __FILE__, "tmp/nonexisting/dir"
    }
  end
end if VEX_TEST == "base"

Version data entries

11 entries across 10 versions & 1 rubygems

Version Path
vex-0.4.2 lib/vex/base/filesystem/fast_copy.rb
vex-0.3.3 lib/vex/base/filesystem/fast_copy.rb
vex-0.2.9 lib/vex/base/filesystem/fast_copy.rb
vex-0.2.8 lib/vex/base/filesystem/fast_copy.rb
vex-0.2.7 lib/vex/base/filesystem/fast_copy.rb
vex-0.2.6 lib/vex/base/filesystem/fast_copy.rb
vex-0.2.5 lib/vex/base/filesystem/fast_copy.rb
vex-0.2.2 lib/vex/base/filesystem/fast_copy.rb
vex-0.2.1 lib/vex/base/filesystem/fast_copy.rb
vex-0.2 lib/vex/base/filesystem/fast_copy.rb
vex-0.2 test/tmp/somedata.dat