Class: Dockly::TarDiff

Inherits:
Object
  • Object
show all
Includes:
Util::Logger::Mixin
Defined in:
lib/dockly/tar_diff.rb

Constant Summary collapse

HEADER_UNPACK_FORMAT =

Tar header format for a ustar tar

"Z100A8A8A8A12A12A8aZ100A6A2Z32Z32A8A8Z155"
PAX_FILE_FORMAT_REGEX =
/\d+ path=(.*)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, target, output) ⇒ TarDiff

Returns a new instance of TarDiff.



12
13
14
15
16
17
# File 'lib/dockly/tar_diff.rb', line 12

def initialize(base, target, output)
  @base, @target, @output = base, target, output

  @base_enum = to_enum(:read_header, base)
  @target_enum = to_enum(:read_header, target)
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



10
11
12
# File 'lib/dockly/tar_diff.rb', line 10

def base
  @base
end

#base_enumObject (readonly)

Returns the value of attribute base_enum.



10
11
12
# File 'lib/dockly/tar_diff.rb', line 10

def base_enum
  @base_enum
end

#outputObject (readonly)

Returns the value of attribute output.



10
11
12
# File 'lib/dockly/tar_diff.rb', line 10

def output
  @output
end

#targetObject (readonly)

Returns the value of attribute target.



10
11
12
# File 'lib/dockly/tar_diff.rb', line 10

def target
  @target
end

#target_enumObject (readonly)

Returns the value of attribute target_enum.



10
11
12
# File 'lib/dockly/tar_diff.rb', line 10

def target_enum
  @target_enum
end

Instance Method Details

#processObject



54
55
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/dockly/tar_diff.rb', line 54

def process
  debug "Started processing tar diff"
  target_data = nil
  base_data = nil
  loop do
    begin

      target_header, target_name,  \
      target_prefix, target_mtime,  \
      target_typeflag,               \
      target_size, target_remainder,  \
      target_empty                     = target_enum.peek
    rescue StopIteration
      debug "Finished target file"
      break
    end

    if target_empty
      debug "End of target file/Empty"
      break
    end

    begin
      _, base_name, base_prefix, base_mtime, base_typeflag, base_size, _, base_empty = base_enum.peek
    rescue StopIteration
      target_data ||= target.read(target_size)
      write_tar_section(target_header, target_data, target_remainder)
      target_data = nil
      target_enum.next
      next
    end

    if base_empty
      target_data ||= target.read(target_size)
      write_tar_section(target_header, target_data, target_remainder)
      target_data = nil
      target_enum.next
      next
    end

    target_full_name = File.join(target_prefix, target_name)
    base_full_name = File.join(base_prefix, base_name)

    target_full_name = target_full_name[1..-1] if target_full_name[0] == '/'
    base_full_name = base_full_name[1..-1] if base_full_name[0] == '/'

    if target_typeflag == 'x'
      target_file = File.basename(target_full_name)
      target_dir  = File.dirname(File.dirname(target_full_name))
      target_full_name = File.join(target_dir, target_file)
    end

    if base_typeflag == 'x'
      base_file = File.basename(base_full_name)
      base_dir  = File.dirname(File.dirname(base_full_name))
      base_full_name = File.join(base_dir, base_file)
    end

    # Remove the PaxHeader.PID from the file
    # Format: /base/directory/PaxHeader.1234/file.ext
    # After:  /base/directory/file.ext
    if (target_typeflag == 'x' && base_typeflag == 'x')
      target_data = target.read(target_size)
      base_data = base.read(base_size)

      if target_match = target_data.match(PAX_FILE_FORMAT_REGEX) && \
          base_match = base_data.match(PAX_FILE_FORMAT_REGEX)
        target_full_name = target_match[1]
        base_full_name   = base_match[1]
      end
    end

    if (target_full_name < base_full_name)
      target_data ||= target.read(target_size)
      write_tar_section(target_header, target_data, target_remainder)
      target_data = nil
      target_enum.next
    elsif (base_full_name < target_full_name)
      base.read(base_size) unless base_data
      base_data = nil
      base_enum.next
    elsif (target_mtime != base_mtime) || (target_size != base_size)
      target_data ||= target.read(target_size)
      write_tar_section(target_header, target_data, target_remainder)
      target_data = nil
      target_enum.next
    else
      target.read(target_size) unless target_data
      target_data = nil
      target_enum.next
      base.read(base_size) unless base_data
      base_data = nil
      base_enum.next
    end
  end
end

#quick_write(size) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/dockly/tar_diff.rb', line 25

def quick_write(size)
  while size > 0
    bread = target.read([size, 4096].min)
    output.write(bread)
    size -= bread.to_s.size
  end
end

#read_header(io) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dockly/tar_diff.rb', line 33

def read_header(io)
  loop do
    return if io.eof?
    # Tar header is 512 bytes large
    data = io.read(512)
    fields = data.unpack(HEADER_UNPACK_FORMAT)
    name = fields[0]
    size = fields[4].oct
    mtime = fields[5].oct
    typeflag = fields[7]
    prefix = fields[15]

    empty = (data == "\0" * 512)
    remainder = (512 - (size % 512)) % 512

    yield data, name, prefix, mtime, typeflag, size, remainder, empty

    io.read(remainder)
  end
end

#write_tar_section(header, data, remainder) ⇒ Object



19
20
21
22
23
# File 'lib/dockly/tar_diff.rb', line 19

def write_tar_section(header, data, remainder)
  output.write(header)
  output.write(data)
  output.write("\0" * remainder)
end