Geminstaller C0 Coverage Information - RCov

spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems/package/tar_header.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems/package/tar_header.rb 266 171
78.95%
69.01%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 # -*- coding: utf-8 -*-
2 #--
3 # Copyright (C) 2004 Mauricio Julio Fernández Pradier
4 # See LICENSE.txt for additional licensing information.
5 #++
6 
7 ##
8 #--
9 # struct tarfile_entry_posix {
10 #   char name[100];     # ASCII + (Z unless filled)
11 #   char mode[8];       # 0 padded, octal, null
12 #   char uid[8];        # ditto
13 #   char gid[8];        # ditto
14 #   char size[12];      # 0 padded, octal, null
15 #   char mtime[12];     # 0 padded, octal, null
16 #   char checksum[8];   # 0 padded, octal, null, space
17 #   char typeflag[1];   # file: "0"  dir: "5"
18 #   char linkname[100]; # ASCII + (Z unless filled)
19 #   char magic[6];      # "ustar\0"
20 #   char version[2];    # "00"
21 #   char uname[32];     # ASCIIZ
22 #   char gname[32];     # ASCIIZ
23 #   char devmajor[8];   # 0 padded, octal, null
24 #   char devminor[8];   # o padded, octal, null
25 #   char prefix[155];   # ASCII + (Z unless filled)
26 # };
27 #++
28 # A header for a tar file
29 
30 class Gem::Package::TarHeader
31 
32   ##
33   # Fields in the tar header
34 
35   FIELDS = [
36     :checksum,
37     :devmajor,
38     :devminor,
39     :gid,
40     :gname,
41     :linkname,
42     :magic,
43     :mode,
44     :mtime,
45     :name,
46     :prefix,
47     :size,
48     :typeflag,
49     :uid,
50     :uname,
51     :version,
52   ]
53 
54   ##
55   # Pack format for a tar header
56 
57   PACK_FORMAT = 'a100' + # name
58                 'a8'   + # mode
59                 'a8'   + # uid
60                 'a8'   + # gid
61                 'a12'  + # size
62                 'a12'  + # mtime
63                 'a7a'  + # chksum
64                 'a'    + # typeflag
65                 'a100' + # linkname
66                 'a6'   + # magic
67                 'a2'   + # version
68                 'a32'  + # uname
69                 'a32'  + # gname
70                 'a8'   + # devmajor
71                 'a8'   + # devminor
72                 'a155'   # prefix
73 
74   ##
75   # Unpack format for a tar header
76 
77   UNPACK_FORMAT = 'A100' + # name
78                   'A8'   + # mode
79                   'A8'   + # uid
80                   'A8'   + # gid
81                   'A12'  + # size
82                   'A12'  + # mtime
83                   'A8'   + # checksum
84                   'A'    + # typeflag
85                   'A100' + # linkname
86                   'A6'   + # magic
87                   'A2'   + # version
88                   'A32'  + # uname
89                   'A32'  + # gname
90                   'A8'   + # devmajor
91                   'A8'   + # devminor
92                   'A155'   # prefix
93 
94   attr_reader(*FIELDS)
95 
96   ##
97   # Creates a tar header from IO +stream+
98 
99   def self.from(stream)
100     header = stream.read 512
101     empty = (header == "\0" * 512)
102 
103     fields = header.unpack UNPACK_FORMAT
104 
105     name     = fields.shift
106     mode     = fields.shift.oct
107     uid      = fields.shift.oct
108     gid      = fields.shift.oct
109     size     = fields.shift.oct
110     mtime    = fields.shift.oct
111     checksum = fields.shift.oct
112     typeflag = fields.shift
113     linkname = fields.shift
114     magic    = fields.shift
115     version  = fields.shift.oct
116     uname    = fields.shift
117     gname    = fields.shift
118     devmajor = fields.shift.oct
119     devminor = fields.shift.oct
120     prefix   = fields.shift
121 
122     new :name     => name,
123         :mode     => mode,
124         :uid      => uid,
125         :gid      => gid,
126         :size     => size,
127         :mtime    => mtime,
128         :checksum => checksum,
129         :typeflag => typeflag,
130         :linkname => linkname,
131         :magic    => magic,
132         :version  => version,
133         :uname    => uname,
134         :gname    => gname,
135         :devmajor => devmajor,
136         :devminor => devminor,
137         :prefix   => prefix,
138 
139         :empty    => empty
140 
141     # HACK unfactor for Rubinius
142     #new :name     => fields.shift,
143     #    :mode     => fields.shift.oct,
144     #    :uid      => fields.shift.oct,
145     #    :gid      => fields.shift.oct,
146     #    :size     => fields.shift.oct,
147     #    :mtime    => fields.shift.oct,
148     #    :checksum => fields.shift.oct,
149     #    :typeflag => fields.shift,
150     #    :linkname => fields.shift,
151     #    :magic    => fields.shift,
152     #    :version  => fields.shift.oct,
153     #    :uname    => fields.shift,
154     #    :gname    => fields.shift,
155     #    :devmajor => fields.shift.oct,
156     #    :devminor => fields.shift.oct,
157     #    :prefix   => fields.shift,
158 
159     #    :empty => empty
160   end
161 
162   ##
163   # Creates a new TarHeader using +vals+
164 
165   def initialize(vals)
166     unless vals[:name] && vals[:size] && vals[:prefix] && vals[:mode] then
167       raise ArgumentError, ":name, :size, :prefix and :mode required"
168     end
169 
170     vals[:uid] ||= 0
171     vals[:gid] ||= 0
172     vals[:mtime] ||= 0
173     vals[:checksum] ||= ""
174     vals[:typeflag] ||= "0"
175     vals[:magic] ||= "ustar"
176     vals[:version] ||= "00"
177     vals[:uname] ||= "wheel"
178     vals[:gname] ||= "wheel"
179     vals[:devmajor] ||= 0
180     vals[:devminor] ||= 0
181 
182     FIELDS.each do |name|
183       instance_variable_set "@#{name}", vals[name]
184     end
185 
186     @empty = vals[:empty]
187   end
188 
189   ##
190   # Is the tar entry empty?
191 
192   def empty?
193     @empty
194   end
195 
196   def ==(other) # :nodoc:
197     self.class === other and
198     @checksum == other.checksum and
199     @devmajor == other.devmajor and
200     @devminor == other.devminor and
201     @gid      == other.gid      and
202     @gname    == other.gname    and
203     @linkname == other.linkname and
204     @magic    == other.magic    and
205     @mode     == other.mode     and
206     @mtime    == other.mtime    and
207     @name     == other.name     and
208     @prefix   == other.prefix   and
209     @size     == other.size     and
210     @typeflag == other.typeflag and
211     @uid      == other.uid      and
212     @uname    == other.uname    and
213     @version  == other.version
214   end
215 
216   def to_s # :nodoc:
217     update_checksum
218     header
219   end
220 
221   ##
222   # Updates the TarHeader's checksum
223 
224   def update_checksum
225     header = header " " * 8
226     @checksum = oct calculate_checksum(header), 6
227   end
228 
229   private
230 
231   def calculate_checksum(header)
232     header.unpack("C*").inject { |a, b| a + b }
233   end
234 
235   def header(checksum = @checksum)
236     header = [
237       name,
238       oct(mode, 7),
239       oct(uid, 7),
240       oct(gid, 7),
241       oct(size, 11),
242       oct(mtime, 11),
243       checksum,
244       " ",
245       typeflag,
246       linkname,
247       magic,
248       oct(version, 2),
249       uname,
250       gname,
251       oct(devmajor, 7),
252       oct(devminor, 7),
253       prefix
254     ]
255 
256     header = header.pack PACK_FORMAT
257 
258     header << ("\0" * ((512 - header.size) % 512))
259   end
260 
261   def oct(num, len)
262     "%0#{len}o" % num
263   end
264 
265 end
266 

Generated on Mon May 10 23:40:28 -0700 2010 with rcov 0.9.8