2 require 'puppet/util/execution'
10 @file_path = Shellwords.shellescape file
14 return nil if type == :none
16 digest = Digest.const_get(type.to_s.upcase)
17 digest.file(@file).hexdigest
19 raise $ERROR_INFO, "invalid checksum type #{type}. #{$ERROR_INFO}", $ERROR_INFO.backtrace
23 if Facter.value(:osfamily) == 'windows'
30 def extract(path = root_dir, opts = {})
38 custom_command = opts.fetch(:custom_command, nil)
39 options = opts.fetch(:options)
41 cmd = if custom_command && custom_command =~ %r{%s}
42 custom_command % @file_path
44 "#{custom_command} #{options} #{@file_path}"
49 Puppet.debug("Archive extracting #{@file} in #{path}: #{cmd}")
50 File.chmod(0o644, @file) if opts[:uid] || opts[:gid]
51 Puppet::Util::Execution.execute(cmd, uid: opts[:uid], gid: opts[:gid], failonfail: true, squelch: false, combine: true)
58 if ENV['path'].include?('7-Zip')
60 elsif File.directory?('C:\\Program Files\\7-Zip')
61 'C:\\Program Files\\7-Zip\\7z.exe'
62 elsif File.directory?('C:\\Program Files (x86)\\7-zip')
63 'C:\\Program Files (x86)\\7-Zip\\7z.exe'
65 raise Exception, '7z.exe not available'
70 if Facter.value(:osfamily) == 'windows'
71 opt = parse_flags('x -aoa', options, '7z')
72 "#{win_7zip} #{opt} #{@file_path}"
76 opt = parse_flags('xf', options, 'tar')
77 "tar #{opt} #{@file_path}"
78 when %r{(\.tgz|\.tar\.gz)$}
79 case Facter.value(:osfamily)
81 gunzip_opt = parse_flags('-dc', options, 'gunzip')
82 tar_opt = parse_flags('xf', options, 'tar')
83 "gunzip #{gunzip_opt} #{@file_path} | tar #{tar_opt} -"
85 opt = parse_flags('xzf', options, 'tar')
86 "tar #{opt} #{@file_path}"
88 when %r{(\.tbz|\.tar\.bz2)$}
89 case Facter.value(:osfamily)
91 bunzip_opt = parse_flags('-dc', options, 'bunzip')
92 tar_opt = parse_flags('xf', options, 'tar')
93 "bunzip2 #{bunzip_opt} #{@file_path} | tar #{tar_opt} -"
95 opt = parse_flags('xjf', options, 'tar')
96 "tar #{opt} #{@file_path}"
98 when %r{(\.txz|\.tar\.xz)$}
99 unxz_opt = parse_flags('-dc', options, 'unxz')
100 tar_opt = parse_flags('xf', options, 'tar')
101 "unxz #{unxz_opt} #{@file_path} | tar #{tar_opt} -"
103 opt = parse_flags('-d', options, 'gunzip')
104 "gunzip #{opt} #{@file_path}"
105 when %r{(\.zip|\.war|\.jar)$}
106 opt = parse_flags('-o', options, 'zip')
107 "unzip #{opt} #{@file_path}"
109 raise NotImplementedError, "Unknown filetype: #{@file}"
114 def parse_flags(default, options, command = nil)
123 raise ArgumentError, "Invalid options for command #{command}: #{options.inspect}"