scripts: Tweaked bd.read to behave like an actual bd_read callback

This better matches what you would expect from a function called
bd.read, at least in the context of littlefs, while also decreasing the
state (seek) we have to worry about.

Note that bd.readblock already behaved mostly like this, and is
preferred by every class except for Bptr.
This commit is contained in:
Christopher Haster
2025-04-07 03:06:11 -05:00
parent b2911fbbe7
commit 6ea18e6579
6 changed files with 15 additions and 36 deletions

View File

@@ -447,13 +447,10 @@ class Bd:
def repr(self):
return 'bd %sx%s' % (self.block_size, self.block_count)
def read(self, size=-1):
def read(self, block, off, size):
self.f.seek(block*self.block_size + off)
return self.f.read(size)
def seek(self, block, off=0, whence=0):
pos = self.f.seek(block*self.block_size + off, whence)
return pos // self.block_size, pos % self.block_size
def readblock(self, block):
self.f.seek(block*self.block_size)
return self.f.read(self.block_size)
@@ -2334,8 +2331,7 @@ class Bptr:
def fetch(cls, bd, rattr, block, off, size, cksize, cksum):
# seek/read cksize bytes from the block, the actual data should
# always be a subset of cksize
bd.seek(block)
ckdata = bd.read(cksize)
ckdata = bd.read(block, 0, cksize)
return cls(rattr, block, off, size, cksize, cksum, ckdata)