scripts: Dropped list/tuple distinction in Rbyd.fetch
Also tweaked how we fetch shrubs, adding Rbyd.fetchshrub and Btree.fetchshrub instead of overloading the bd argument. Oh, and also added --trunk to dbgmtree.py and dbglfs.py. Actually _using_ --trunk isn't advised, since it will probably just result in a corrupted filesystem, but these scripts are for accessing things that aren't normally allowed anyways. The reason for dropping the list/tuple distinction is because it was a big ugly hack, unpythonic, and likely to catch users (and myself) by surprise. Now, Rbyd.fetch and friends always require separate block/trunk arguments, and the exercise of deciding which trunk to use is left up to the caller.
This commit is contained in:
+73
-65
@@ -90,10 +90,10 @@ def bdgeom(s):
|
||||
return int(s, b)
|
||||
|
||||
# parse some rbyd addr encodings
|
||||
# 0xa -> [0xa]
|
||||
# 0xa.c -> [(0xa, 0xc)]
|
||||
# 0x{a,b} -> [0xa, 0xb]
|
||||
# 0x{a,b}.c -> [(0xa, 0xc), (0xb, 0xc)]
|
||||
# 0xa -> (0xa,)
|
||||
# 0xa.c -> ((0xa, 0xc),)
|
||||
# 0x{a,b} -> (0xa, 0xb)
|
||||
# 0x{a,b}.c -> ((0xa, 0xc), (0xb, 0xc))
|
||||
def rbydaddr(s):
|
||||
s = s.strip()
|
||||
b = 10
|
||||
@@ -124,7 +124,7 @@ def rbydaddr(s):
|
||||
else:
|
||||
addr.append(int(s, b))
|
||||
|
||||
return addr
|
||||
return tuple(addr)
|
||||
|
||||
def crc32c(data, crc=0):
|
||||
crc ^= 0xffffffff
|
||||
@@ -439,9 +439,9 @@ class Rattr:
|
||||
self.tag = tag
|
||||
self.weight = weight
|
||||
if isinstance(blocks, int):
|
||||
self.blocks = [blocks]
|
||||
self.blocks = (blocks,)
|
||||
else:
|
||||
self.blocks = list(blocks)
|
||||
self.blocks = blocks
|
||||
self.toff = toff
|
||||
self.tdata = tdata
|
||||
self.data = data
|
||||
@@ -515,9 +515,9 @@ class Ralt:
|
||||
self.tag = tag
|
||||
self.weight = weight
|
||||
if isinstance(blocks, int):
|
||||
self.blocks = [blocks]
|
||||
self.blocks = (blocks,)
|
||||
else:
|
||||
self.blocks = list(blocks)
|
||||
self.blocks = blocks
|
||||
self.toff = toff
|
||||
self.tdata = tdata
|
||||
self.jump = jump
|
||||
@@ -570,9 +570,9 @@ class Rbyd:
|
||||
gcksumdelta=None,
|
||||
corrupt=False):
|
||||
if isinstance(blocks, int):
|
||||
self.blocks = [blocks]
|
||||
self.blocks = (blocks,)
|
||||
else:
|
||||
self.blocks = list(blocks)
|
||||
self.blocks = blocks
|
||||
self.trunk = trunk
|
||||
self.weight = weight
|
||||
self.rev = rev
|
||||
@@ -615,51 +615,7 @@ class Rbyd:
|
||||
return hash((frozenset(self.blocks), self.trunk))
|
||||
|
||||
@classmethod
|
||||
def fetch(cls, bd, blocks, trunk=None):
|
||||
# multiple blocks? unfortunately this must be a list
|
||||
if isinstance(blocks, list):
|
||||
# fetch all blocks
|
||||
rbyds = [cls.fetch(bd, block, trunk) for block in blocks]
|
||||
# determine most recent revision
|
||||
i = 0
|
||||
for i_, rbyd in enumerate(rbyds):
|
||||
# compare with sequence arithmetic
|
||||
if rbyd and (
|
||||
not rbyds[i]
|
||||
or not ((rbyd.rev - rbyds[i].rev) & 0x80000000)
|
||||
or (rbyd.rev == rbyds[i].rev
|
||||
and rbyd.trunk > rbyds[i].trunk)):
|
||||
i = i_
|
||||
# keep track of the other blocks
|
||||
rbyd = rbyds[i]
|
||||
rbyd.blocks += tuple(
|
||||
rbyds[(i+1+j) % len(rbyds)].block
|
||||
for j in range(len(rbyds)-1))
|
||||
# and patch the gcksumdelta if we have one
|
||||
if rbyd.gcksumdelta is not None:
|
||||
rbyd.gcksumdelta.blocks = rbyd.blocks
|
||||
return rbyd
|
||||
|
||||
block = blocks
|
||||
|
||||
# blocks may also encode trunks
|
||||
block, trunk = (
|
||||
block[0] if isinstance(block, tuple)
|
||||
else block,
|
||||
trunk if trunk is not None
|
||||
else block[1] if isinstance(block, tuple)
|
||||
else None)
|
||||
|
||||
# bd can be either a bd reference or a preread block
|
||||
#
|
||||
# preread blocks can be useful for avoiding race conditions
|
||||
# with cksums and shrubs
|
||||
if isinstance(bd, Bd):
|
||||
# seek/read the block
|
||||
data = bd.readblock(block)
|
||||
else:
|
||||
data = bd
|
||||
|
||||
def _fetch(cls, data, block, trunk=None):
|
||||
# fetch the rbyd
|
||||
rev = fromle32(data[0:4])
|
||||
cksum = 0
|
||||
@@ -756,6 +712,39 @@ class Rbyd:
|
||||
gcksumdelta=gcksumdelta,
|
||||
corrupt=not trunk_)
|
||||
|
||||
@classmethod
|
||||
def fetch(cls, bd, blocks, trunk=None):
|
||||
# multiple blocks?
|
||||
if not isinstance(blocks, int):
|
||||
# fetch all blocks
|
||||
rbyds = [cls.fetch(bd, block, trunk) for block in blocks]
|
||||
# determine most recent revision
|
||||
i = 0
|
||||
for i_, rbyd in enumerate(rbyds):
|
||||
# compare with sequence arithmetic
|
||||
if rbyd and (
|
||||
not rbyds[i]
|
||||
or not ((rbyd.rev - rbyds[i].rev) & 0x80000000)
|
||||
or (rbyd.rev == rbyds[i].rev
|
||||
and rbyd.trunk > rbyds[i].trunk)):
|
||||
i = i_
|
||||
# keep track of the other blocks
|
||||
rbyd = rbyds[i]
|
||||
rbyd.blocks += tuple(
|
||||
rbyds[(i+1+j) % len(rbyds)].block
|
||||
for j in range(len(rbyds)-1))
|
||||
# and patch the gcksumdelta if we have one
|
||||
if rbyd.gcksumdelta is not None:
|
||||
rbyd.gcksumdelta.blocks = rbyd.blocks
|
||||
return rbyd
|
||||
|
||||
# seek/read the block
|
||||
block = blocks
|
||||
data = bd.readblock(block)
|
||||
|
||||
# fetch the rbyd
|
||||
return cls._fetch(data, block, trunk)
|
||||
|
||||
@classmethod
|
||||
def fetchck(cls, bd, blocks, trunk, weight, cksum):
|
||||
# try to fetch the rbyd normally
|
||||
@@ -772,6 +761,15 @@ class Rbyd:
|
||||
|
||||
return rbyd
|
||||
|
||||
@classmethod
|
||||
def fetchshrub(cls, rbyd, trunk):
|
||||
# steal the original rbyd's data
|
||||
#
|
||||
# this helps avoid race conditions with cksums and stuff
|
||||
shrub = cls._fetch(rbyd.data, rbyd.block, trunk)
|
||||
shrub.blocks = rbyd.blocks
|
||||
return shrub
|
||||
|
||||
def lookupnext(self, rid, tag=None, *,
|
||||
path=False):
|
||||
if not self or rid >= self.weight:
|
||||
@@ -1516,9 +1514,9 @@ def dbg_tree(rbyd, *,
|
||||
|
||||
|
||||
def main(disk, blocks=None, *,
|
||||
trunk=None,
|
||||
block_size=None,
|
||||
block_count=None,
|
||||
trunk=None,
|
||||
color='auto',
|
||||
**args):
|
||||
# figure out what color should be
|
||||
@@ -1536,9 +1534,19 @@ def main(disk, blocks=None, *,
|
||||
block_count = block_count_
|
||||
|
||||
# flatten blocks, default to block 0
|
||||
if not blocks:
|
||||
blocks = [(0,)]
|
||||
blocks = [block for blocks_ in blocks for block in blocks_]
|
||||
blocks = list(it.chain.from_iterable(blocks)) if blocks else [0]
|
||||
|
||||
# blocks may also encode trunks
|
||||
blocks, trunk = (
|
||||
[block[0] if isinstance(block, tuple)
|
||||
else block
|
||||
for block in blocks],
|
||||
trunk if trunk is not None
|
||||
else ft.reduce(
|
||||
lambda x, y: y,
|
||||
(block[1] for block in blocks
|
||||
if isinstance(block, tuple)),
|
||||
None))
|
||||
|
||||
with open(disk, 'rb') as f:
|
||||
# if block_size is omitted, assume the block device is one big block
|
||||
@@ -1548,7 +1556,7 @@ def main(disk, blocks=None, *,
|
||||
|
||||
# fetch the rbyd
|
||||
bd = Bd(f, block_size, block_count)
|
||||
rbyd = Rbyd.fetch(bd, blocks)
|
||||
rbyd = Rbyd.fetch(bd, blocks, trunk)
|
||||
|
||||
# print some information about the rbyd
|
||||
print('rbyd %s w%d, rev %08x, size %d, cksum %08x' % (
|
||||
@@ -1587,6 +1595,10 @@ if __name__ == "__main__":
|
||||
nargs='*',
|
||||
type=rbydaddr,
|
||||
help="Block address of metadata blocks.")
|
||||
parser.add_argument(
|
||||
'--trunk',
|
||||
type=lambda x: int(x, 0),
|
||||
help="Use this offset as the trunk of the tree.")
|
||||
parser.add_argument(
|
||||
'-b', '--block-size',
|
||||
type=bdgeom,
|
||||
@@ -1595,10 +1607,6 @@ if __name__ == "__main__":
|
||||
'--block-count',
|
||||
type=lambda x: int(x, 0),
|
||||
help="Block count in blocks.")
|
||||
parser.add_argument(
|
||||
'--trunk',
|
||||
type=lambda x: int(x, 0),
|
||||
help="Use this offset as the trunk of the tree.")
|
||||
parser.add_argument(
|
||||
'--color',
|
||||
choices=['never', 'always', 'auto'],
|
||||
|
||||
Reference in New Issue
Block a user