Class: ReiserFS::Superblock

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

Constant Summary collapse

DEF_LEAF_NODE_CACHE_SIZE =

Default cache sizes.

50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Superblock

Returns a new instance of Superblock.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fs/ReiserFS/superblock.rb', line 43

def initialize(stream)
  raise "Nil stream" if stream.nil?
  @stream = stream

  # Seek, read & decode the superblock structure
  @stream.seek(SUPERBLOCK_OFFSET)
  @sb = SUPERBLOCK.decode(@stream.read(SUPERBLOCK_SIZE))

  # Grab some quick facts & make sure there's nothing wrong. Tight qualification.
  raise "Invalid Magic String: #{@sb['magic']}" unless isMagic?(@sb['magic'])

  @totalBlocks  = @sb['num_blocks']
  @freeBlocks   = @sb['num_free_blocks']
  @rootBlock    = @sb['root_block']
  @treeHeight   = @sb['tree_height']
  @bitmapNumber = @sb['bitmap_number']
  @version      = @sb['version']
  @state        = @sb['state']
  @blockSize    = @sb['block_size']
  @nblocksInBitmap = 8 * @blockSize
  @oidMaxSize     = @sb['oid_max_size']
  @oidCurrentSize = @sb['oid_current_size']
  @superBlock   = SUPERBLOCK_OFFSET / @blockSize

  @leaf_nodes   = LruHash.new(DEF_LEAF_NODE_CACHE_SIZE)
end

Instance Attribute Details

#bitmapNumberObject (readonly)

Returns the value of attribute bitmapNumber.



41
42
43
# File 'lib/fs/ReiserFS/superblock.rb', line 41

def bitmapNumber
  @bitmapNumber
end

#blockSizeObject (readonly)

Returns the value of attribute blockSize.



41
42
43
# File 'lib/fs/ReiserFS/superblock.rb', line 41

def blockSize
  @blockSize
end

#nblocksInBitmapObject (readonly)

Returns the value of attribute nblocksInBitmap.



41
42
43
# File 'lib/fs/ReiserFS/superblock.rb', line 41

def nblocksInBitmap
  @nblocksInBitmap
end

#rootBlockObject (readonly)

Returns the value of attribute rootBlock.



41
42
43
# File 'lib/fs/ReiserFS/superblock.rb', line 41

def rootBlock
  @rootBlock
end

#treeHeightObject (readonly)

Returns the value of attribute treeHeight.



41
42
43
# File 'lib/fs/ReiserFS/superblock.rb', line 41

def treeHeight
  @treeHeight
end

Instance Method Details

#blockUsed?(blockNum) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fs/ReiserFS/superblock.rb', line 80

def blockUsed?(blockNum)
  bitmapNum = blockNum / @nblocksInBitmap
  raise "Block out of Range" if bitmapNum >= @bitmapNumber

  bitmapBlock      = getBitmapBlock(bitmapNum)
  bitmapOffset     = blockNum % @nblocksInBitmap
  bitmapByte       = bitmapOffset / 8
  bitmapByteOffset = bitmapOffset % 8
  bits = bitmapBlock[bitmapByte, 1].unpack('B8')[0]

  (bits[bitmapByteOffset, 1] == "1")
end

#freeBytesObject

Returns free space on file system in bytes.



130
131
132
# File 'lib/fs/ReiserFS/superblock.rb', line 130

def freeBytes
  @freeBlocks * @blockSize
end

#getBitmapBlock(bitmapNum) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/fs/ReiserFS/superblock.rb', line 70

def getBitmapBlock(bitmapNum)
  if bitmapNum == 0
    blockNum = @superBlock + 1
  else
    blockNum = bitmapNum * @nblocksInBitmap
  end

  readBlockRaw(blockNum)
end

#getLeafNodes(key) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fs/ReiserFS/superblock.rb', line 106

def getLeafNodes(key)
  key_s = Utils.dumpKey(key)
  return @leaf_nodes[key_s] if @leaf_nodes.key?(key_s)

  blocks = []
  leaves = []
  blocks << readBlock(@rootBlock)
  blocks.each do |block|
    next if block.nil?

    if block.isLeaf?
      leaves << block
      next
    end

    block.findPointers(key).each do |pointer|
      blocks << readBlock(pointer['block_number'])
    end
  end

  @leaf_nodes[key_s] = leaves
end

#readBlock(blockNum) ⇒ Object



100
101
102
103
104
# File 'lib/fs/ReiserFS/superblock.rb', line 100

def readBlock(blockNum)
  #     return nil if !blockUsed?(blockNum)
  data = readBlockRaw(blockNum)
  Block.new(data, blockNum)
end

#readBlockRaw(blockNum) ⇒ Object



93
94
95
96
97
98
# File 'lib/fs/ReiserFS/superblock.rb', line 93

def readBlockRaw(blockNum)
  raise "ReiserFS::SuperBlock >> blockNum is nil" if blockNum.nil?

  @stream.seek(blockNum * @blockSize)
  @stream.read(@blockSize)
end