Module: Dockly::Util::Tar

Extended by:
Tar
Included in:
Tar
Defined in:
lib/dockly/util/tar.rb

Instance Method Summary collapse

Instance Method Details

#is_gzip?(path) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dockly/util/tar.rb', line 18

def is_gzip?(path)
  if File.size(path) < 2
    return false
  end
  magic = nil
  File.open(path, "r") do |f|
    magic = f.read(2)
  end
  magic = magic.unpack('H*')[0]
  magic == "1f8b"
end

#is_tar?(path) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dockly/util/tar.rb', line 6

def is_tar?(path)
  if File.size(path) < 262
    return false
  end
  magic = nil
  File.open(path, "r") do |f|
    f.read(257)
    magic = f.read(5)
  end
  magic == "ustar"
end

#tar(path, output) ⇒ Object

Creates a tar file in memory recursively from the given path.

Returns a StringIO whose underlying String is the contents of the tar file.



35
36
37
38
39
40
41
42
43
44
# File 'lib/dockly/util/tar.rb', line 35

def tar(path, output)
  FileUtils.mkdir_p(File.dirname(output))
  puts "tarring #{path} to #{output}"
  tar_command = "tar -cf #{output} -C #{File.dirname(path)} #{File.basename(path)}"
  puts "Tar Command: #{tar_command}"
  IO.popen(tar_command) do |io|
    puts io.read
  end
  File.open(output, 'rb+')
end

#untar(input_io, destination) ⇒ Object

untars the given IO into the specified directory



48
49
50
51
52
53
54
55
56
57
# File 'lib/dockly/util/tar.rb', line 48

def untar(input_io, destination)
  puts "untarring #{input_io.path} to #{destination}"
  FileUtils.mkdir_p(destination)
  untar_command = "tar -xf #{input_io.path} -C #{destination}"
  puts "Untar command: #{untar_command}"
  IO.popen(untar_command) do |io|
    puts io.read
  end
  input_io
end