Class: Higgs::Tar::ArchiveWriter

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

Constant Summary collapse

FTYPE_TO_TAR =
{
  'file' => REGTYPE,
  'directory' => DIRTYPE,
  'characterSpecial' => CHRTYPE,
  'blockSpecial' => BLKTYPE,
  'fifo' => FIFOTYPE,
  'link' => SYMTYPE,
  'socket' => FIFOTYPE
}

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 included from Block

blocked_size, padding_size, tar?

Methods inherited from IOHandler

#initialize, #to_io

Constructor Details

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

Instance Method Details

#add(name, body, options = nil) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/higgs/tar.rb', line 333

def add(name, body, options=nil)
  head = {
    :name => name,
    :size => body.length
  }
  if (options) then
    head.update(options) 
  end
  if (block_given?) then
    yield(head)
  end
  write_header(head)
  body += "\0" * padding_size(body.length)
  @io.write(body)
  nil
end

#add_file(path) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/higgs/tar.rb', line 350

def add_file(path)
  stat = File.stat(path)
  unless (FTYPE_TO_TAR.key? stat.ftype) then
    raise FormatError, "unknown file type: #{stat.ftype}"
  end
  head = {
    :name => path,
    :mode => stat.mode,
    :uid => stat.uid,
    :gid => stat.gid,
    :size => (stat.file?) ? stat.size : 0,
    :mtime => stat.mtime,
    :typeflag => FTYPE_TO_TAR[stat.ftype]
  }
  if (block_given?) then
    yield(head)
  end
  write_header(head)
  if (stat.ftype == 'file') then
    File.open(path, 'rb') {|r_io|
      chunk_size = BLKSIZ * 128
      remaining_size = stat.size
      while (remaining_size > chunk_size)
        s = r_io.read(chunk_size) or raise IOError, 'unexpected EOF'
        @io.write(s)
        remaining_size -= chunk_size
      end
      s = r_io.read(chunk_size) or raise IOError, 'unexpected EOF'
      s += "\0" * padding_size(stat.size)
      @io.write(s)
    }
  end
  nil
end

#close(eoa = true) ⇒ Object



385
386
387
388
# File 'lib/higgs/tar.rb', line 385

def close(eoa=true)
  write_EOA if eoa
  super()
end

#write_EOAObject



318
319
320
321
# File 'lib/higgs/tar.rb', line 318

def write_EOA
  @io.write(EOA * 2)
  nil
end

#write_header(head) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/higgs/tar.rb', line 282

def write_header(head)
  name = head[:name] or raise FormatError, "required name: #{head.inspect}"
  if (name.length > MAX_LEN) then
    raise TooLongPathError, "too long path: #{name}"
  end
  mode     = format('%-8o', head[:mode] || 0100644) # 0100644 => -rw-r--r--
  uid      = format('%-8o', head[:uid] || Process.euid)
  gid      = format('%-8o', head[:gid] || Process.egid)
  size     = format('%-12o', head[:size])
  mtime    = format('%-12o', (head[:mtime] || Time.now).to_i)
  dummy_chksum = ' ' * 8
  typeflag = head[:typeflag] || REGTYPE
  linkname = head[:linkname] || ''
  magic    = head[:magic] || MAGIC
  version  = head[:version] || VERSION
  uname    = head[:uname] || ''
  gname    = head[:gname] || ''
  devmajor = head[:devmajor] || ''
  devminor = head[:devminor] || ''
  prefix   = ''
  header = [
    name, mode, uid, gid, size, mtime,
    dummy_chksum, typeflag, linkname, magic, version,
    uname, gname, devmajor, devminor, prefix
  ].pack(HEAD_FMT)
  header += "\0" * 12
  if (head.key? :chksum) then
    chksum = head[:chksum]
  else
    chksum = header.sum(20)
  end
  header[148, 8] = format('%-8o', chksum)
  @io.write(header)
  nil
end