Module: FileUtils::FastCopy

Defined in:
lib/vex/base/filesystem/fast_copy.rb

Instance Method Summary collapse

Instance Method Details

#fast_copy(src, dest) ⇒ Object

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.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vex/base/filesystem/fast_copy.rb', line 5

def fast_copy(src, dest)
  src_stat = File.stat(src)
  invalid_argument!(src, "This is not a file") 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