Class: OdinFlex::AR

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/odinflex/ar.rb

Defined Under Namespace

Classes: Info

Constant Summary collapse

HEADER_LENGTH =

File Identifier

16 + # File Identifier
12 + # File modification timestamp
6  + # Owner ID
6  + # Group ID
8  + # File Mode
10 + # File size in bytes
2
AR_HEADER =

Ending characters

"!<arch>\n"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fd) ⇒ AR

Returns a new instance of AR.



23
24
25
26
# File 'lib/odinflex/ar.rb', line 23

def initialize fd
  @fd = fd
  @pos = fd.pos
end

Class Method Details

.is_ar?(io) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/odinflex/ar.rb', line 15

def self.is_ar? io
  pos = io.pos
  header = io.read(AR_HEADER.length)
  header == AR_HEADER
ensure
  io.seek pos, IO::SEEK_SET
end

Instance Method Details

#eachObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/odinflex/ar.rb', line 30

def each
  @fd.seek @pos, IO::SEEK_SET
  header = @fd.read(AR_HEADER.length)

  raise "Wrong Header" unless header == AR_HEADER

  loop do
    break if @fd.eof?

    identifier, timestamp, owner, group, mode, size, ending =
      @fd.read(HEADER_LENGTH).unpack('A16A12A6A6A8A10A2')

    raise "wrong ending #{ending}" unless ending.bytes == [0x60, 0x0A]
    pos = @fd.pos

    # Assume BSD for now
    fname_len = identifier[/\d+$/].to_i
    filename = @fd.read(fname_len).unpack1('A*')
    info = Info.new filename, timestamp, owner, group, mode, size.to_i - fname_len, @fd.pos

    yield info

    @fd.seek size.to_i + pos, IO::SEEK_SET
  end
end