Class: ReiserFS::FileData

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirEntry, superblock) ⇒ FileData

Initialization



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fs/ReiserFS/file_data.rb', line 8

def initialize(dirEntry, superblock)
  raise "Nil dirEntry object" if dirEntry.nil?
  raise "Nil superblock"      if superblock.nil?

  @sb   = superblock
  @de   = dirEntry
  @key  = @de.key
  @len  = @de.length
  @blocks = []

  # puts "Key=#{Utils.dumpKey(@key)} datalen=#{@len}"

  @sb.getLeafNodes(@key).each do |leaf|
    leaf.getItemHeaders(@key).each do |ih|
      next if Utils.typeIsStat?(leaf.getItemType(ih))
      raise "Directory Node when FileData expected" if Utils.typeIsDirectory?(leaf.getItemType(ih))

      if Utils.typeIsDirect?(leaf.getItemType(ih))
        @blocks << {:blockNum => leaf.blockNum, :offset => ih['location'], :length => ih['length'], :blockObj => leaf}
      end

      if Utils.typeIsIndirect?(leaf.getItemType(ih))
        leaf.getItem(ih).unpack("V*").each do |blockNum|
          @blocks << {:blockNum => blockNum, :offset => 0, :length => @sb.blockSize}
        end
      end
    end
  end

  @bpos  = []
  pos = 0
  @blocks.each do |b|
    @bpos << pos
    pos += b[:length]
  end

  @blen  = @blocks[0][:length]

  #      @blocks.each { |b| puts "Block(#{b['blockNum']}): len=#{b['length']} offset=#{b['offset']}"   }

  rewind
end

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos.



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

def pos
  @pos
end

Instance Method Details

#findIndex(pos) ⇒ Object



51
52
53
54
55
56
# File 'lib/fs/ReiserFS/file_data.rb', line 51

def findIndex(pos)
  index = pos / @blen
  index -= 1 while                    @bpos[index] > pos
  index += 1 while @bpos[index + 1] && (@bpos[index + 1] < pos)
  index
end

#read(bytes = @len) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fs/ReiserFS/file_data.rb', line 73

def read(bytes = @len)
  return nil if @pos >= @len

  index = findIndex(@pos)
  out = ""

  while out.length < bytes
    b      = @blocks[index]
    pos    = @bpos[index]
    index += 1

    blockLength = b[:length]
    blockLength = (@len - pos) if (pos + blockLength) > @len

    # Do we have to start reading from an offset?
    byteOffset = (pos < @pos) ? (@pos - pos) : 0

    # Get the block with data
    b[:blockObj] = @sb.readBlock(b[:blockNum])  unless b.key?(:blockObj)
    blockObj = b[:blockObj]

    # Extract the data we need out of it
    blockOffset = b[:offset]
    out << blockObj.data[blockOffset + byteOffset, blockLength - byteOffset]
  end

  bytes = out.length if bytes > out.length
  @pos += bytes

  out[0, bytes]
end

#rewindObject



58
59
60
# File 'lib/fs/ReiserFS/file_data.rb', line 58

def rewind
  @pos = 0
end

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



62
63
64
65
66
67
68
69
70
71
# File 'lib/fs/ReiserFS/file_data.rb', line 62

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 @len - offset
         end
  @pos = 0    if @pos < 0
  @pos = @len if @pos > @len
  @pos
end

#write(buf, _len = buf.length) ⇒ Object



105
106
107
# File 'lib/fs/ReiserFS/file_data.rb', line 105

def write(buf, _len = buf.length)
  raise "Write functionality is not yet supported on ReiserFS."
end