class MiniTarball::HeaderFormatter

Constants

PERMISSION_BITMASK

Public Class Methods

format_number(value, length) click to toggle source

@param value [Integer] @param length [Integer]

# File lib/mini_tarball/header_formatter.rb, line 11
def self.format_number(value, length)
  return nil if value.nil?
  raise NotImplementedError.new("Negative numbers are not supported") if value.negative?

  octal_length = length - 1
  max_octal_value = ("0" + "7" * octal_length).to_i(8)

  if (value <= max_octal_value)
    to_octal(value, octal_length)
  else
    to_base256(value, length)
  end
end
format_permissions(value, length) click to toggle source

Removes file type bitfields and returns file permissions as formatted number @param value [Integer] @param length [Integer]

# File lib/mini_tarball/header_formatter.rb, line 47
def self.format_permissions(value, length)
  format_number(value & PERMISSION_BITMASK, length)
end
to_base256(value, length) click to toggle source
# File lib/mini_tarball/header_formatter.rb, line 29
def self.to_base256(value, length)
  encoded = Array.new(length, 0)
  encoded[0] = 0x80
  index = length - 1

  while value > 0
    raise ValueTooLargeError.new("Value is too large: #{value}") if index == 0
    encoded[index] = value % 256
    value /= 256
    index -= 1
  end

  encoded.pack("C#{length}")
end
to_octal(value, length) click to toggle source
# File lib/mini_tarball/header_formatter.rb, line 25
def self.to_octal(value, length)
  "%0#{length}o" % value
end