Class: Archive::Tar::Format
- Inherits:
-
Object
- Object
- Archive::Tar::Format
- Defined in:
- lib/archive/tar/format.rb
Constant Summary collapse
- DEC_TYPES =
{ "\0" => :normal, "0" => :normal, "1" => :link, "2" => :symbolic, "3" => :character, "4" => :block, "5" => :directory, "6" => :fifo, "7" => :reserved, "I" => :index, "g" => :pax_global_header }
- ENC_TYPES =
DEC_TYPES.invert
Class Method Summary collapse
-
.blocks_for_bytes(bytes) ⇒ Object
Calculate quantity of blocks.
-
.calculate_checksum(header) ⇒ Object
Generate checksum with header.
-
.detect_type(header) ⇒ Object
Detect type of tar file by header.
-
.pack_header(header) ⇒ Object
Pack header from Stat.
-
.strip_nuls(string) ⇒ Object
Remove all NUL bytes at the end of a string.
-
.unpack_header(header) ⇒ Object
Transform tar header to Stat.
Class Method Details
.blocks_for_bytes(bytes) ⇒ Object
Calculate quantity of blocks
143 144 145 |
# File 'lib/archive/tar/format.rb', line 143 def blocks_for_bytes(bytes) bytes % 512 == 0 ? bytes / 512 : (bytes + 512 - bytes % 512) / 512 end |
.calculate_checksum(header) ⇒ Object
Generate checksum with header
91 92 93 94 95 96 97 98 99 |
# File 'lib/archive/tar/format.rb', line 91 def calculate_checksum(header) checksum = 0 header.each_byte do |byte| checksum += byte end checksum.to_s(8).rjust(6, " ") + "\0 " end |
.detect_type(header) ⇒ Object
Detect type of tar file by header
83 84 85 86 87 88 |
# File 'lib/archive/tar/format.rb', line 83 def detect_type(header) return :ustar if header[257, 6] == "ustar0" return :gnu if header[257, 6] == "ustar " :other end |
.pack_header(header) ⇒ Object
Pack header from Stat
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 |
# File 'lib/archive/tar/format.rb', line 102 def pack_header(header) blob = "" blob += header.path.ljust(100, "\0") blob += header.mode.to_s(8).rjust(8, "0") blob += header.uid.to_s(8).rjust(8, "0") blob += header.gid.to_s(8).rjust(8, "0") blob += header.size.to_s(8).rjust(12, "0") blob += header.mtime.to_i.to_s(8).rjust(12, "0") blob += " " * 8 blob += ENC_TYPES[header.type] blob += header.dest.ljust(100, "\0") case header.format when :ustar blob += "ustar\000" when :gnu blob += "ustar \0" end if header.gnu? || header.ustar? blob += header.user.ljust(32, "\0") blob += header.group.ljust(32, "\0") blob += header.major.to_s(8).rjust(8, "0") blob += header.minor.to_s(8).rjust(8, "0") if header.gnu? blob += header.atime.to_i.to_s(8).rjust(12, "0") blob += header.ctime.to_i.to_s(8).rjust(12, "0") end end pad_length = 512 - blob.bytesize blob += "\0" * pad_length blob[148, 8] = calculate_checksum(blob) blob end |
.strip_nuls(string) ⇒ Object
Remove all NUL bytes at the end of a string
44 45 46 47 48 49 50 |
# File 'lib/archive/tar/format.rb', line 44 def strip_nuls(string) until string[-1] != "\0" string = string[0..-2] end string end |
.unpack_header(header) ⇒ Object
Transform tar header to Stat
53 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 |
# File 'lib/archive/tar/format.rb', line 53 def unpack_header(header) new_obj = Archive::Tar::Stat.new new_obj.path = strip_nuls(header[0, 100]) new_obj.mode = header[100, 8].oct new_obj.uid = header[108, 8].oct new_obj.gid = header[116, 8].oct new_obj.size = header[124, 12].oct new_obj.mtime = Time.at(header[136, 12].oct) new_obj.checksum = header[148, 8].oct new_obj.type = DEC_TYPES[header[156]] new_obj.dest = strip_nuls(header[157, 100]) new_obj.format = header[257, 5] == "ustar" ? ( header[257, 6] == "ustar " ? :gnu : :ustar ) : :other new_obj.user = strip_nuls(header[265, 32]) new_obj.group = strip_nuls(header[297, 32]) new_obj.major = header[329, 8].oct new_obj.minor = header[337, 8].oct new_obj.path = header[345, 155].strip + new_obj.path if new_obj.ustar? if new_obj.gnu? new_obj.atime = Time.at(header[345, 12].oct) new_obj.ctime = Time.at(header[357, 12].oct) end new_obj end |