scripts: Synced dbgbmapd3.py Lfs class changes

- Added Lfs.traverse for full filesystem traversal
- Added Rbyd.shrub flag so we can tell if an Rbyd is a shrub
- Removed redundant leaves from paths in leaf iters
This commit is contained in:
Christopher Haster
2025-04-05 02:23:52 -05:00
parent 5f06558cbe
commit 97e2786545
5 changed files with 117 additions and 14 deletions
-4
View File
@@ -586,7 +586,6 @@ class Ralt:
return hash((self.tag, self.weight, self.jump)) return hash((self.tag, self.weight, self.jump))
# TODO sync
# our core rbyd type # our core rbyd type
class Rbyd: class Rbyd:
def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *, def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *,
@@ -1018,7 +1017,6 @@ class Rbyd:
return best return best
# TODO sync
# our rbyd btree type # our rbyd btree type
class Btree: class Btree:
def __init__(self, bd, rbyd): def __init__(self, bd, rbyd):
@@ -1648,7 +1646,6 @@ class Mdir:
return Mid(self.mid, rid), name_ return Mid(self.mid, rid), name_
# TODO sync
# the mtree, the skeletal structure of littlefs # the mtree, the skeletal structure of littlefs
class Mtree: class Mtree:
def __init__(self, bd, mrootchain, mtree, *, def __init__(self, bd, mrootchain, mtree, *,
@@ -2797,7 +2794,6 @@ class Gstate:
locals()[g.__name__.lower()] = ft.cached_property(_parser(g)) locals()[g.__name__.lower()] = ft.cached_property(_parser(g))
# TODO sync
# high-level littlefs representation # high-level littlefs representation
class Lfs: class Lfs:
def __init__(self, bd, mtree, config=None, gstate=None, cksum=None, *, def __init__(self, bd, mtree, config=None, gstate=None, cksum=None, *,
+10 -1
View File
@@ -471,6 +471,7 @@ class Ralt:
# our core rbyd type # our core rbyd type
class Rbyd: class Rbyd:
def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *, def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *,
shrub=False,
gcksumdelta=None, gcksumdelta=None,
corrupt=False): corrupt=False):
if isinstance(blocks, int): if isinstance(blocks, int):
@@ -484,6 +485,7 @@ class Rbyd:
self.cksum = cksum self.cksum = cksum
self.data = data self.data = data
self.shrub = shrub
self.gcksumdelta = gcksumdelta self.gcksumdelta = gcksumdelta
self.corrupt = corrupt self.corrupt = corrupt
@@ -672,6 +674,7 @@ class Rbyd:
# this helps avoid race conditions with cksums and stuff # this helps avoid race conditions with cksums and stuff
shrub = cls._fetch(rbyd.data, rbyd.block, trunk) shrub = cls._fetch(rbyd.data, rbyd.block, trunk)
shrub.blocks = rbyd.blocks shrub.blocks = rbyd.blocks
shrub.shrub = True
return shrub return shrub
def lookupnext(self, rid, tag=None, *, def lookupnext(self, rid, tag=None, *,
@@ -930,6 +933,10 @@ class Btree:
def cksum(self): def cksum(self):
return self.rbyd.cksum return self.rbyd.cksum
@property
def shrub(self):
return self.rbyd.shrub
def addr(self): def addr(self):
return self.rbyd.addr() return self.rbyd.addr()
@@ -1112,7 +1119,9 @@ class Btree:
break break
if path: if path:
yield bid-rid + (rbyd.weight-1), rbyd, path_[:-1] yield (bid-rid + (rbyd.weight-1), rbyd,
# path tail is usually redundant unless corrupt
path_[:-1] if rbyd else path_)
else: else:
yield bid-rid + (rbyd.weight-1), rbyd yield bid-rid + (rbyd.weight-1), rbyd
bid += rbyd.weight - rid + 1 bid += rbyd.weight - rid + 1
+87 -4
View File
@@ -520,6 +520,7 @@ class Ralt:
# our core rbyd type # our core rbyd type
class Rbyd: class Rbyd:
def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *, def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *,
shrub=False,
gcksumdelta=None, gcksumdelta=None,
corrupt=False): corrupt=False):
if isinstance(blocks, int): if isinstance(blocks, int):
@@ -533,6 +534,7 @@ class Rbyd:
self.cksum = cksum self.cksum = cksum
self.data = data self.data = data
self.shrub = shrub
self.gcksumdelta = gcksumdelta self.gcksumdelta = gcksumdelta
self.corrupt = corrupt self.corrupt = corrupt
@@ -721,6 +723,7 @@ class Rbyd:
# this helps avoid race conditions with cksums and stuff # this helps avoid race conditions with cksums and stuff
shrub = cls._fetch(rbyd.data, rbyd.block, trunk) shrub = cls._fetch(rbyd.data, rbyd.block, trunk)
shrub.blocks = rbyd.blocks shrub.blocks = rbyd.blocks
shrub.shrub = True
return shrub return shrub
def lookupnext(self, rid, tag=None, *, def lookupnext(self, rid, tag=None, *,
@@ -979,6 +982,10 @@ class Btree:
def cksum(self): def cksum(self):
return self.rbyd.cksum return self.rbyd.cksum
@property
def shrub(self):
return self.rbyd.shrub
def addr(self): def addr(self):
return self.rbyd.addr() return self.rbyd.addr()
@@ -1161,7 +1168,9 @@ class Btree:
break break
if path: if path:
yield bid-rid + (rbyd.weight-1), rbyd, path_[:-1] yield (bid-rid + (rbyd.weight-1), rbyd,
# path tail is usually redundant unless corrupt
path_[:-1] if rbyd else path_)
else: else:
yield bid-rid + (rbyd.weight-1), rbyd yield bid-rid + (rbyd.weight-1), rbyd
bid += rbyd.weight - rid + 1 bid += rbyd.weight - rid + 1
@@ -1926,7 +1935,9 @@ class Mtree:
else: else:
bid, rbyd, rid = mdir bid, rbyd, rid = mdir
if path: if path:
yield (bid-rid + (rbyd.weight-1), rbyd), path_[:-1] yield ((bid-rid + (rbyd.weight-1), rbyd),
# path tail is usually redundant unless corrupt
path_[:-1] if rbyd else path_)
else: else:
yield (bid-rid + (rbyd.weight-1), rbyd) yield (bid-rid + (rbyd.weight-1), rbyd)
mid = self.mid(bid-rid + (rbyd.weight-1) + 1) mid = self.mid(bid-rid + (rbyd.weight-1) + 1)
@@ -3111,6 +3122,70 @@ class Lfs:
file.orphaned = True file.orphaned = True
yield file yield file
# traverse the filesystem
def traverse(self, *,
mtree_only=False,
shrubs=False,
fragments=False,
path=False):
# traverse the mtree
for r in self.mtree.traverse(
path=path):
if path:
mdir, path_ = r
else:
mdir = r
# mdir?
if isinstance(mdir, Mdir):
if path:
yield mdir, path_
else:
yield mdir
# btree node? we only care about the rbyd for simplicity
else:
bid, rbyd = mdir
if path:
yield rbyd, path_
else:
yield rbyd
# traverse file bshrubs/btrees
if not mtree_only and isinstance(mdir, Mdir):
for mid, name in mdir.mids():
file = self._open(mid, mdir, name.tag, name)
for r in file.traverse(
path=path):
if path:
pos, data, path__ = r
path__ = [(mid, mdir, name)]+path__
else:
pos, data = r
# inlined data? we usually ignore these
if isinstance(data, Rattr):
if fragments:
if path:
yield data, path_+path__
else:
yield data
# block pointer?
elif isinstance(data, Bptr):
if path:
yield data, path_+path__
else:
yield data
# bshrub/btree node? we only care about the rbyd
# for simplicity, we also usually ignore shrubs
# since these live the the parent mdir
else:
if shrubs or not data.shrub:
if path:
yield data, path_+path__
else:
yield data
# common file operations, note Reg extends this for regular files # common file operations, note Reg extends this for regular files
class File: class File:
tag = None tag = None
@@ -3216,6 +3291,13 @@ class Lfs:
def _lookupleaf(self, pos, *, def _lookupleaf(self, pos, *,
path=False, path=False,
depth=None): depth=None):
# no bshrub?
if self.bshrub is None:
if path:
return None, None, []
else:
return None, None
# lookup data in our bshrub # lookup data in our bshrub
r = self.bshrub.lookupleaf(pos, r = self.bshrub.lookupleaf(pos,
path=path or depth, path=path or depth,
@@ -3228,7 +3310,7 @@ class Lfs:
if path: if path:
return None, None, path_ return None, None, path_
else: else:
return None, None, *path_ return None, None
# corrupt btree node? # corrupt btree node?
if not rbyd: if not rbyd:
@@ -3317,7 +3399,8 @@ class Lfs:
bid, rbyd, rid = data bid, rbyd, rid = data
if path: if path:
yield (pos, (bid-rid + (rbyd.weight-1), rbyd), yield (pos, (bid-rid + (rbyd.weight-1), rbyd),
path_[:-1]) # path tail is usually redundant unless corrupt
path_[:-1] if rbyd else path_)
else: else:
yield pos, (bid-rid + (rbyd.weight-1), rbyd) yield pos, (bid-rid + (rbyd.weight-1), rbyd)
pos += rbyd.weight pos += rbyd.weight
+17 -5
View File
@@ -486,6 +486,7 @@ class Ralt:
# our core rbyd type # our core rbyd type
class Rbyd: class Rbyd:
def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *, def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *,
shrub=False,
gcksumdelta=None, gcksumdelta=None,
corrupt=False): corrupt=False):
if isinstance(blocks, int): if isinstance(blocks, int):
@@ -499,6 +500,7 @@ class Rbyd:
self.cksum = cksum self.cksum = cksum
self.data = data self.data = data
self.shrub = shrub
self.gcksumdelta = gcksumdelta self.gcksumdelta = gcksumdelta
self.corrupt = corrupt self.corrupt = corrupt
@@ -687,6 +689,7 @@ class Rbyd:
# this helps avoid race conditions with cksums and stuff # this helps avoid race conditions with cksums and stuff
shrub = cls._fetch(rbyd.data, rbyd.block, trunk) shrub = cls._fetch(rbyd.data, rbyd.block, trunk)
shrub.blocks = rbyd.blocks shrub.blocks = rbyd.blocks
shrub.shrub = True
return shrub return shrub
def lookupnext(self, rid, tag=None, *, def lookupnext(self, rid, tag=None, *,
@@ -945,6 +948,10 @@ class Btree:
def cksum(self): def cksum(self):
return self.rbyd.cksum return self.rbyd.cksum
@property
def shrub(self):
return self.rbyd.shrub
def addr(self): def addr(self):
return self.rbyd.addr() return self.rbyd.addr()
@@ -1127,7 +1134,9 @@ class Btree:
break break
if path: if path:
yield bid-rid + (rbyd.weight-1), rbyd, path_[:-1] yield (bid-rid + (rbyd.weight-1), rbyd,
# path tail is usually redundant unless corrupt
path_[:-1] if rbyd else path_)
else: else:
yield bid-rid + (rbyd.weight-1), rbyd yield bid-rid + (rbyd.weight-1), rbyd
bid += rbyd.weight - rid + 1 bid += rbyd.weight - rid + 1
@@ -1892,7 +1901,9 @@ class Mtree:
else: else:
bid, rbyd, rid = mdir bid, rbyd, rid = mdir
if path: if path:
yield (bid-rid + (rbyd.weight-1), rbyd), path_[:-1] yield ((bid-rid + (rbyd.weight-1), rbyd),
# path tail is usually redundant unless corrupt
path_[:-1] if rbyd else path_)
else: else:
yield (bid-rid + (rbyd.weight-1), rbyd) yield (bid-rid + (rbyd.weight-1), rbyd)
mid = self.mid(bid-rid + (rbyd.weight-1) + 1) mid = self.mid(bid-rid + (rbyd.weight-1) + 1)
@@ -2107,9 +2118,10 @@ class Mtree:
else: else:
bid_, rbyd_, rid_, name_ = r bid_, rbyd_, rid_, name_ = r
if bid_ is None: if bid_ is None:
return None, path_ if path:
else: return None, path_
return None else:
return None
# corrupt btree node? # corrupt btree node?
if not rbyd_: if not rbyd_:
+3
View File
@@ -454,6 +454,7 @@ class Ralt:
# our core rbyd type # our core rbyd type
class Rbyd: class Rbyd:
def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *, def __init__(self, blocks, trunk, weight, rev, eoff, cksum, data, *,
shrub=False,
gcksumdelta=None, gcksumdelta=None,
corrupt=False): corrupt=False):
if isinstance(blocks, int): if isinstance(blocks, int):
@@ -467,6 +468,7 @@ class Rbyd:
self.cksum = cksum self.cksum = cksum
self.data = data self.data = data
self.shrub = shrub
self.gcksumdelta = gcksumdelta self.gcksumdelta = gcksumdelta
self.corrupt = corrupt self.corrupt = corrupt
@@ -655,6 +657,7 @@ class Rbyd:
# this helps avoid race conditions with cksums and stuff # this helps avoid race conditions with cksums and stuff
shrub = cls._fetch(rbyd.data, rbyd.block, trunk) shrub = cls._fetch(rbyd.data, rbyd.block, trunk)
shrub.blocks = rbyd.blocks shrub.blocks = rbyd.blocks
shrub.shrub = True
return shrub return shrub
def lookupnext(self, rid, tag=None, *, def lookupnext(self, rid, tag=None, *,