Class: Docker::Template::Util::Copy

Inherits:
Object
  • Object
show all
Defined in:
lib/docker/template/util/copy.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Copy

Returns a new instance of Copy.



9
10
11
12
13
14
# File 'lib/docker/template/util/copy.rb', line 9

def initialize(from, to)
  @root = Template.root.realpath
  @repos_root = Template.repo_is_root?? Template.root.realpath : Template.repos_root.realpath
  @from = from.to_pathname
  @to = to.to_pathname
end

Class Method Details

.directory(from, to) ⇒ Object



18
19
20
21
22
# File 'lib/docker/template/util/copy.rb', line 18

def self.directory(from, to)
  if from && to && File.exist?(from)
    new(from, to).directory
  end
end

.file(from, to) ⇒ Object



47
48
49
50
51
# File 'lib/docker/template/util/copy.rb', line 47

def self.file(from, to)
  if to && from && File.exist?(from)
    new(from, to).file
  end
end

Instance Method Details

#directoryObject

Copy a directory checking for symlinks and resolving them (only at the top level) if they are in the repos root, the root or the from path. The reason we check all three is because repos might be a symlink that resolves out of path so we need to allow symlinks from it. The same for the copy folder.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/docker/template/util/copy.rb', line 30

def directory
  FileUtils.cp_r(@from.children, @to, :dereference_root => false)
  @from.all_children.select(&:symlink?).each do |path|
    path = @to.join(path.relative_path_from(@from))
    resolved = path.realpath

    unless in_path?(resolved)
      raise Errno::EPERM, "#{path} not in #{@root}"
    end

    FileUtils.rm_r(path)
    FileUtils.cp_r(resolved, path, :dereference_root => false)
  end
end

#fileObject

Raises:

  • (Errno::EPERM)


55
56
57
58
59
60
61
62
# File 'lib/docker/template/util/copy.rb', line 55

def file
  return FileUtils.cp(@from, @to) unless @from.symlink?

  resolved = @from.realpath
  allowed = resolved.in_path?(@root)
  raise Errno::EPERM, "#{resolved} not in #{@root}." unless allowed
  FileUtils.cp(resolved, @to)
end