Class: PuppetForge::Unpacker

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_forge/unpacker.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, target, tmpdir) ⇒ Unpacker

Returns a new instance of Unpacker.

Parameters:

  • filename (String)

    the file to unpack

  • target (String)

    the target directory to unpack into



32
33
34
35
36
# File 'lib/puppet_forge/unpacker.rb', line 32

def initialize(filename, target, tmpdir)
  @filename = filename
  @target = target
  @tmpdir = tmpdir
end

Class Method Details

.harmonize_ownership(source, target) ⇒ Object

Set the owner/group of the target directory to those of the source Note: don't call this function on Microsoft Windows

Parameters:

  • source (Pathname)

    source of the permissions

  • target (Pathname)

    target of the permissions change



26
27
28
# File 'lib/puppet_forge/unpacker.rb', line 26

def self.harmonize_ownership(source, target)
    FileUtils.chown_R(source.stat.uid, source.stat.gid, target)
end

.unpack(filename, target, tmpdir) ⇒ Hash{:symbol => Array<String>}

Unpack a tar file into a specified directory

Parameters:

  • filename (String)

    the file to unpack

  • target (String)

    the target directory to unpack into

Returns:

  • (Hash{:symbol => Array<String>})

    a hash with file-category keys pointing to lists of filenames. The categories are :valid, :invalid and :symlink



14
15
16
17
18
19
# File 'lib/puppet_forge/unpacker.rb', line 14

def self.unpack(filename, target, tmpdir)
  inst = self.new(filename, target, tmpdir)
  file_lists = inst.unpack
  inst.move_into(Pathname.new(target))
  file_lists
end

Instance Method Details

#move_into(dir) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



48
49
50
51
52
53
# File 'lib/puppet_forge/unpacker.rb', line 48

def move_into(dir)
  dir.rmtree if dir.exist?
  FileUtils.mv(root_dir, dir)
ensure
  FileUtils.rmtree(@tmpdir)
end

#root_dirObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/puppet_forge/unpacker.rb', line 56

def root_dir
  return @root_dir if @root_dir

  # Use Find.find instead of Dir[] for Windows long path support
   = nil
  shortest_length = Float::INFINITY
  
  begin
    Find.find(@tmpdir) do |path|
      if File.basename(path) == 'metadata.json'
        if path.length < shortest_length
           = path
          shortest_length = path.length
        end
      end
    end
  rescue Errno::ENAMETOOLONG => e
    # Even Find.find might fail, need to use Dir.each with manual recursion
    raise "Cannot traverse directory due to long paths: #{e.message}"
  end

  if 
    @root_dir = Pathname.new().dirname
  else
    raise "No valid metadata.json found!"
  end
end

#unpackObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
42
43
44
45
# File 'lib/puppet_forge/unpacker.rb', line 39

def unpack
  begin
    PuppetForge::Tar.instance.unpack(@filename, @tmpdir)
  rescue PuppetForge::ExecutionFailure => e
    raise RuntimeError, "Could not extract contents of module archive: #{e.message}"
  end
end