class Pathutil

def safe_copy_directory(to, root: nil, ignore: [])

def safe_copy_directory(to, root: nil, ignore: [])
  ignore = [ignore].flatten.uniq
  if !in_path?(root)
    raise Errno::EPERM, "#{self} not in #{
      root
    }"
  else
    to.mkdir_p unless to.exist?
    children do |file|
      unless ignore.any? { |path| file.in_path?(path) }
        if !file.in_path?(root)
          raise Errno::EPERM, "#{file} not in #{
            root
          }"
        elsif file.file?
          FileUtils.cp(file, to, {
            :preserve => true
          })
        else
          path = file.realpath
          path.safe_copy(to.join(file.basename), {
            :root => root, :ignore => ignore
          })
        end
      end
    end
  end
end