Module: FileDependencies::Archive

Defined in:
lib/file-dependencies/archive.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.extract_file?(entry, extract = nil, exclude = nil, prefix) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
# File 'lib/file-dependencies/archive.rb', line 67

def extract_file?(entry, extract=nil, exclude=nil, prefix)
  return true if extract.nil? && exclude.nil?
  return false if tar_header?(entry)

  if extract
    return match?(entry, extract, prefix)
  elsif exclude
    return !match?(entry, exclude, prefix)
  end
end

.match?(entry, list, prefix) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/file-dependencies/archive.rb', line 79

def match?(entry, list, prefix)
  return false if list.nil?

  if list.is_a?(Array)
    list.each do |pattern|
      if pattern.is_a?(Regexp) or regexp_string?(pattern)
        return true if entry =~ Regexp.new(pattern)
      elsif pattern.is_a?(String)
        return true if pattern == entry.gsub(prefix, '')
      end
    end
  end
  false
end

.regexp_string?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/file-dependencies/archive.rb', line 100

def regexp_string?(pattern)
  pattern.to_s.match('^\(\?-mix:.+\)$')
end

.tar_header?(entry) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/file-dependencies/archive.rb', line 95

def tar_header?(entry)
  entry =~ /PaxHeaders/
end

.ungzip(file, outdir) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/file-dependencies/archive.rb', line 7

def ungzip(file, outdir)
  output = ::File.join(outdir, file.gsub('.gz', '').split(::File::SEPARATOR).last)
  tgz = Zlib::GzipReader.new(::File.open(file))
  begin
    ::File.open(output, "w") do |out|
      IO.copy_stream(tgz, out)
    end
    ::File.unlink(file)
  rescue
    ::File.unlink(output) if ::File.file?(output)
    raise
  end
  tgz.close
end

.untar(tarball, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/file-dependencies/archive.rb', line 23

def untar(tarball, &block)
  tgz = Zlib::GzipReader.new(::File.open(tarball))
  tar = ::Archive::Tar::Minitar::Input.open(tgz)
  tar.each do |entry|
    path = block.call(entry)
    next if path.nil?
    parent = ::File.dirname(path)

    FileUtils.mkdir_p(parent) unless ::File.directory?(parent)

    # Skip this file if the output file is the same size
    if entry.directory?
      FileUtils.mkdir_p(path) unless ::File.directory?(path)
    else
      entry_mode = entry.instance_eval { @mode } & 0777
      if ::File.exist?(path)
        stat = ::File.stat(path)
        # TODO(sissel): Submit a patch to archive-tar-minitar upstream to
        # expose headers in the entry.
        entry_size = entry.instance_eval { @size }
        # If file sizes are same, skip writing.
        next if stat.size == entry_size && (stat.mode & 0777) == entry_mode
      end
      puts "Extracting #{entry.full_name} from #{tarball} #{entry_mode.to_s(8)}" if $DEBUG
      ::File.open(path, "w") do |fd|
        # eof? check lets us skip empty files. Necessary because the API provided by
        # Archive::Tar::Minitar::Reader::EntryStream only mostly acts like an
        # IO object. Something about empty files in this EntryStream causes
        # IO.copy_stream to throw "can't convert nil into String" on JRuby
        # TODO(sissel): File a bug about this.
        until entry.eof?
          chunk = entry.read(16_384)
          fd.write(chunk)
        end
        # IO.copy_stream(entry, fd)
      end
      ::File.chmod(entry_mode, path)
    end
  end
  tar.close
  ::File.unlink(tarball) if ::File.file?(tarball)
end