Class: Higgs::Tar::ArchiveReader

Inherits:
IOHandler show all
Includes:
Enumerable
Defined in:
lib/higgs/tar.rb

Constant Summary

Constants included from Block

Block::AREGTYPE, Block::BLKSIZ, Block::BLKTYPE, Block::CHRTYPE, Block::CONTTYPE, Block::DIRTYPE, Block::EOA, Block::FIFOTYPE, Block::HEAD_FMT, Block::LNKTYPE, Block::MAGIC, Block::MAX_LEN, Block::REGTYPE, Block::SYMTYPE, Block::VERSION

Instance Method Summary collapse

Methods inherited from IOHandler

#initialize, #to_io

Methods included from Block

blocked_size, padding_size, tar?

Constructor Details

This class inherits a constructor from Higgs::Tar::IOHandler

Instance Method Details

#each(skip_body = false) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/higgs/tar.rb', line 265

def each(skip_body=false)
  if (skip_body) then
    while (head = read_header(true))
      yield(head)
    end
  else
    while (head_and_body = fetch)
      yield(head_and_body)
    end
  end
  self
end

#fetchObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/higgs/tar.rb', line 242

def fetch
  head_and_body = read_header or return
  if (head_and_body[:size] > 0) then
    blocked_size = blocked_size(head_and_body[:size])
    head_and_body[:body] = @io.read(blocked_size)
    unless (head_and_body) then
      raise FormatError, 'unexpected EOF'
    end
    if (head_and_body[:body].size != blocked_size) then
      raise FormatError, 'mismatch body size'
    end
    head_and_body[:body][head_and_body[:size]...blocked_size] = ''
  else
    case (head_and_body[:typeflag])
    when REGTYPE, CONTTYPE
      head_and_body[:body] = ''
    else
      head_and_body[:body] = nil
    end
  end
  head_and_body
end

#read_header(skip_body = false) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/higgs/tar.rb', line 187

def read_header(skip_body=false)
  head_data = @io.read(BLKSIZ)
  unless (head_data) then
    raise FormatError, 'unexpected EOF'
  end
  if (head_data.length != BLKSIZ) then
    raise FormatError, 'too short header'
  end
  if (head_data == EOA) then
    next_head_data = @io.read(BLKSIZ)
    if (next_head_data && next_head_data == EOA) then
      return nil
    else
      raise FormatError, "not of EOA: #{head_data.inspect}, #{next_head_data.inspect}"
    end
  end
  chksum = (head_data[0...148] + ' ' * 8 + head_data[156...BLKSIZ]).sum(20)
  head_list = head_data.unpack(HEAD_FMT)
  head = {}
  [ :name, :mode, :uid, :gid, :size, :mtime,
    :chksum, :typeflag, :linkname, :magic, :version,
    :uname, :gname, :devmajor, :devminor, :prefix
  ].each_with_index do |k, i|
    head[k] = head_list[i]
  end
  if (head[:typeflag] == AREGTYPE) then
    head[:typeflag] = REGTYPE
  end
  if (head[:magic] != MAGIC) then
    raise MagicError, "unknown format: #{head[:magic].inspect}"
  end
  # why?
  #if (head[:version] != VERSION) then
  #  raise VersionError, "unknown version: #{head[:version].inspect}"
  #end
  [ :mode,
    :uid,
    :gid,
    :size,
    :mtime,
    :chksum
  ].each do |sym|
    head[sym] = head[sym].oct
  end
  if (head[:chksum] != chksum) then
    raise CheckSumError, 'broken tar'
  end
  head[:mtime] = Time.at(head[:mtime])
  if (skip_body) then
    skip_size = head[:size] + padding_size(head[:size])
    @io.read(skip_size)
  end
  head
end