Class: Archive::Tar::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/archive/tar/format.rb

Overview

:nodoc:

Constant Summary collapse

DEC_TYPES =
{
	'0'  => :file,
	"\0" => :file,
	'1'  => :link,
	'2'  => :symlink,
	'3'  => :character,
	'4'  => :block,
	'5'  => :directory,
	'6'  => :fifo,
	'7'  => :contiguous
}
ENC_TYPES =
{
	:file       => '0',
	:link       => '1',
	:symlink    => '2',
	:character  => '3',
	:block      => '4',
	:directory  => '5',
	:fifo       => '6',
	:contiguous => '7'
}

Class Method Summary collapse

Class Method Details

.next_block(io) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/archive/tar/format.rb', line 27

def next_block(io)
	block = ''
	loop do
		block = io.read(512)
		return block unless block == "\0" * 512
		return nil if io.eof?
	end
end

.pack_header(h) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/archive/tar/format.rb', line 58

def pack_header(h)
	packed = [
		h[:path],
		"%07o"  % h[:mode],
		"%07o"  % h[:uid],
		"%07o"  % h[:gid],
		"%011o" % h[:size],
		"%011o" % h[:mtime].to_i,
		'',
		ENC_TYPES[h[:type]],
		h[:dest],
		h[:ustar] ? "ustar" : '',
		"00",
		h[:user],
		h[:group],
		"%07o" % h[:major],
		"%07o" % h[:minor],
		h[:pre]
	].pack("a100 a8 a8 a8 a12 a12 A8 a a100 a6 a2 a32 a32 a8 a8 a155 x12")
	packed[148, 7] = "%06o\0" % packed.unpack("C*").inject { |total, byte| total + byte }
	packed
end

.stat(path, inodes) ⇒ Object



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
# File 'lib/archive/tar/format.rb', line 81

def stat(path, inodes)
	lstat = File.lstat(path)
	dest = lstat.symlink? ? File.readlink(path) : inodes[lstat.ino]

	inodes[lstat.ino] ||= path

	size = lstat.size
	size = 0 if dest || !lstat.file?

	{
		:path  => path[0,1] == '/' ? path[1, path.size - 1] : path,
		:mode  => lstat.mode,
		:uid   => lstat.uid,
		:gid   => lstat.gid,
		:size  => size,
		:mtime => lstat.mtime.to_i,
		:type  => (lstat.file? && dest ? :link : lstat.tar_type),
		:dest  => dest.to_s,

		:ustar => true,
		:uvers => "00",
		:user  => Etc.getpwuid(lstat.uid).name,
		:group => Etc.getgrgid(lstat.gid).name,
		:major => lstat.dev_major.to_i,
		:minor => lstat.dev_minor.to_i,
		:pre   => '', # FIXME: implement
	}
end

.unpack_header(block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/archive/tar/format.rb', line 36

def unpack_header(block)
	h = block.unpack("Z100 Z8 Z8 Z8 Z12 Z12 A8 a Z100 Z6 Z2 Z32 Z32 Z8 Z8 Z155")
	{
		:path  => h.shift,
		:mode  => h.shift.oct,
		:uid   => h.shift.oct,
		:gid   => h.shift.oct,
		:size  => h.shift.oct,
		:mtime => Time.at(h.shift.oct),
		:cksum => h.shift.oct,
		:type  => DEC_TYPES[h.shift],
		:dest  => h.shift,
		:ustar => h.shift.strip == 'ustar',
		:uvers => h.shift.strip,
		:user  => h.shift,
		:group => h.shift,
		:major => h.shift.oct,
		:minor => h.shift.oct,
		:pre   => h.shift
	}
end