Skip to content

Instantly share code, notes, and snippets.

@venables
Created December 4, 2012 16:09
Show Gist options
  • Select an option

  • Save venables/4205617 to your computer and use it in GitHub Desktop.

Select an option

Save venables/4205617 to your computer and use it in GitHub Desktop.
FileUtils.cp_r using preserve fails if there are symlinks
# FileUtils.cp_r using "preserve: true" fails if there's a symlink that points to a file that has not yet
# been copied over. This is because "preserve" updates the metadata for the files (including symlink files)
# and the symlink will not be valid when it is saved (causing a failure).
require "fileutils"
base = "./fileutils_cp_r_base"
dest = "./fileutils_cp_r_dest"
{ "A" => "B", "B" => "A" }.each do |symlink, file|
puts "Testing FileUtils.cp_r(preserve: true) with a symlink from #{symlink} pointing to a real file #{file}"
`mkdir #{base}`
`touch #{base}/#{file}`
`ln -s #{base}/#{file} #{base}/#{symlink}`
begin
FileUtils.cp_r(base, dest, preserve: true)
puts "We never get here.\n"
rescue Errno::ENOENT => e
puts "ERROR: #{e.to_s}\n"
ensure
`rm -rf #{base}`
`rm -rf #{dest}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment