Class: Iso9660::FileData

Inherits:
Object
  • Object
show all
Defined in:
lib/fs/iso9660/file_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirEntry, bootSector) ⇒ FileData

Initialization



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fs/iso9660/file_data.rb', line 8

def initialize(dirEntry, bootSector)
  raise "Nil directory entry" if dirEntry.nil?
  raise "Nil boot sector" if bootSector.nil?

  @bs = bootSector
  @de = dirEntry
  @last_sect = @de.fileSize.divmod(@bs.sectorSize)
  @last_sect[0] += 1 if @last_sect[1] > 0
  @last_sect = @last_sect[0]
  rewind
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



5
6
7
# File 'lib/fs/iso9660/file_data.rb', line 5

def pos
  @pos
end

Instance Method Details

#getLSN(vsn) ⇒ Object



73
74
75
76
# File 'lib/fs/iso9660/file_data.rb', line 73

def getLSN(vsn)
  return -1 if vsn > @last_sect
  @de.fileStart + vsn
end

#getSector(vsn) ⇒ Object



67
68
69
70
71
# File 'lib/fs/iso9660/file_data.rb', line 67

def getSector(vsn)
  lsn = getLSN(vsn)
  raise "No logical sector for virtual sector #{vsn}." if lsn == -1
  @bs.getSectors(lsn, 1)
end

#read(bytes = @de.fileSize) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fs/iso9660/file_data.rb', line 35

def read(bytes = @de.fileSize)
  return nil if @pos >= @de.fileSize
  bytes = @de.fileSize - @pos if @pos + bytes > @de.fileSize

  # Get start & end locs.
  ss = @bs.sectorSize
  startSector, startOffset = @pos.divmod(ss)
  endSector, endOffset = (@pos + (bytes - 1)).divmod(ss)

  # Single sector.
  if startSector == endSector
    @pos += (endOffset - startOffset)
    return getSector(startSector)[startOffset..endOffset]
  end

  # Span sectors.
  out = MemoryBuffer.create(bytes)
  totalLen = 0
  (startSector..endSector).each do |sect|
    offset = 0; len = ss
    if sect == startSector
      offset = startOffset
      len -= offset
    end
    len -= (ss - (endOffset + 1)) if sect == endSector
    out[totalLen, len] = getSector(sect)[offset, len]
    totalLen += len
    @pos += len
  end
  out[0..totalLen]
end

#rewindObject



20
21
22
# File 'lib/fs/iso9660/file_data.rb', line 20

def rewind
  @pos = 0
end

#seek(offset, method = IO::SEEK_SET) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/fs/iso9660/file_data.rb', line 24

def seek(offset, method = IO::SEEK_SET)
  @pos = case method
         when IO::SEEK_SET then offset
         when IO::SEEK_CUR then @pos + offset
         when IO::SEEK_END then @de.length - offset
         end
  @pos = 0 if @pos < 0
  @pos = @de.fileSize if @pos > @de.fileSize
  @pos
end